Loading...
Searching...
No Matches
G4BlineTracer.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26/// \file field/BlineTracer/src/G4BlineTracer.cc
27/// \brief Implementation of the G4BlineTracer class
28//
29//
30//
31//
32// --------------------------------------------------------------------
33//
34// G4BlineTracer implementation
35//
36// --------------------------------------------------------------------
37// Author: Laurent Desorgher (desorgher@phim.unibe.ch)
38// Created - 2003-10-06
39// --------------------------------------------------------------------
40
41#include "G4BlineTracer.hh"
44#include "G4BlineEventAction.hh"
46#include "G4BlineEquation.hh"
47
48#include "G4RunManager.hh"
49#include "G4TransportationManager.hh"
50#include "G4FieldManager.hh"
51#include "G4PropagatorInField.hh"
52#include "G4MagIntegratorDriver.hh"
53#include "G4CashKarpRKF45.hh"
54#include "G4LogicalVolumeStore.hh"
55#include "G4ChordFinder.hh"
56#include "G4SystemOfUnits.hh"
57
58//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
59
67
68//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
69
71{
72 delete fMessenger;
73 delete fSteppingAction;
74 delete fEventAction;
76 for (size_t i=0; i< fVecEquationOfMotion.size();i++)
77 {
79 if (fVecChordFinders[i]) delete fVecChordFinders[i];
80 }
81}
82
83//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
84
88
89//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
90
94
95//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
96
97void G4BlineTracer::ComputeBlines(G4int n_of_lines)
98{
99 //the first time ResetChordFinders should be called
100 //
102 {
105 }
106
107 // Replace the user action by the ad-hoc actions for Blines
108
109 G4RunManager* theRunManager = G4RunManager::GetRunManager();
110 auto user_run_action =
111 (G4UserRunAction*)theRunManager->GetUserRunAction();
112 theRunManager->SetUserAction(this);
113
114 auto user_stepping_action =
115 (G4UserSteppingAction*)theRunManager->GetUserSteppingAction();
116 theRunManager->SetUserAction(fSteppingAction);
117
118 auto userPrimaryAction =
119 (G4VUserPrimaryGeneratorAction*)theRunManager->GetUserPrimaryGeneratorAction();
120 if (userPrimaryAction)
122 theRunManager->SetUserAction(fPrimaryGeneratorAction);
123
124 auto user_event_action =
125 (G4UserEventAction*)theRunManager->GetUserEventAction();
126 theRunManager->SetUserAction(fEventAction);
127
128 auto user_tracking_action =
129 (G4UserTrackingAction*)theRunManager->GetUserTrackingAction();
130 G4UserTrackingAction* aNullTrackingAction = nullptr;
131 theRunManager->SetUserAction(aNullTrackingAction);
132
133 auto user_stacking_action =
134 (G4UserStackingAction*)theRunManager->GetUserStackingAction();
135 G4UserStackingAction* aNullStackingAction = nullptr;
136 theRunManager->SetUserAction(aNullStackingAction);
137
138 // replace the user defined chordfinder by the element of fVecChordFinders
139
140 std::vector<G4ChordFinder*> user_chord_finders;
141 std::vector<G4double> user_largest_acceptable_step;
142 for (size_t i=0;i<fVecChordFinders.size();i++)
143 {
144 user_largest_acceptable_step.push_back(-1.);
145 if (fVecChordFinders[i])
146 {
147 user_chord_finders.push_back(fVecFieldManagers[i]->GetChordFinder());
148 fVecChordFinders[i]->SetDeltaChord(user_chord_finders[i]->GetDeltaChord());
149 fVecFieldManagers[i]->SetChordFinder(fVecChordFinders[i]);
150 }
151 else user_chord_finders.push_back(nullptr);
152 }
153
154 // I have tried to use the smooth line filter ability but I could not obtain
155 // a smooth trajectory in the G4TrajectoryContainer after an event
156 // Another solution for obtaining a smooth trajectory is to limit
157 // the LargestAcceptableStep in the G4PropagatorInField object.
158 // This is the solution I used.
159
160 // Old solution:
161 // G4TransportationManager::GetTransportationManager()
162 // ->GetPropagatorInField()->SetTrajectoryFilter(fTrajectoryFilter);
163
164 // New solution:
165 // set the largest_acceptable_step to max_step:length
166
167 G4TransportationManager* tmanager =
168 G4TransportationManager::GetTransportationManager();
169 G4double previous_largest_acceptable_step =
170 tmanager->GetPropagatorInField()->GetLargestAcceptableStep();
171
172 tmanager->GetPropagatorInField()
173 ->SetLargestAcceptableStep(fMaxTrackingStep);
174
175 // Start the integration of n_of_lines different magnetic field lines
176
177 for (G4int il=0; il<n_of_lines;il++)
178 {
179 // for each magnetic field line we integrate once backward and once
180 // forward from the same starting point
181
182 // backward integration
183
184 for (size_t i=0; i< fVecEquationOfMotion.size();i++)
185 {
187 fVecEquationOfMotion[i]->SetBackwardDirectionOfIntegration(true);
188 }
189 theRunManager->BeamOn(1);
190
191 // forward integration
192
193 for (size_t i=0; i < fVecEquationOfMotion.size();i++)
194 {
196 fVecEquationOfMotion[i]->SetBackwardDirectionOfIntegration(false);
197 }
198 theRunManager->BeamOn(1);
199 }
200
201 // Remove trajectory filter to PropagatorInField
202 // It was for old solution when using smooth trajectory filter
203
204 // tmanager->GetPropagatorInField()->SetTrajectoryFilter(0);
205
206 // back to User defined actions and other parameters
207 // -------------------------------------------------
208
209 tmanager->GetPropagatorInField()
210 ->SetLargestAcceptableStep(previous_largest_acceptable_step);
211
212 // return to User actions
213
214 theRunManager->SetUserAction(user_run_action);
215 theRunManager->SetUserAction(user_event_action);
216 theRunManager->SetUserAction(userPrimaryAction);
217 theRunManager->SetUserAction(user_stepping_action);
218 theRunManager->SetUserAction(user_tracking_action);
219 theRunManager->SetUserAction(user_stacking_action);
220
221 // set user defined chord finders and largest acceptable step
222
223 for (size_t i=0;i<fVecFieldManagers.size();i++)
224 {
225 if (user_chord_finders[i])
226 fVecFieldManagers[i]->SetChordFinder(user_chord_finders[i]);
227 }
228}
229
230//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
231////////////////////////////////////////////////////////////////
232
233/*
234G4bool G4BlineTracer::CheckMagneticFields()
235{
236 // Check FieldManagers
237
238 G4TransportationManager* tmanager =
239 G4TransportationManager::GetTransportationManager();
240
241 if (fVecFieldManagers[0] != tmanager->GetFieldManager())
242 return false;
243 if (fVecMagneticFields[0] != tmanager->GetFieldManager()->GetDetectorField())
244 return false;
245 G4LogicalVolumeStore* theVolumeStore = G4LogicalVolumeStore::GetInstance();
246
247 std::vector<G4FieldManagers*> LogicalVolumeFields;
248 size_t j=0;
249 for (size_t i=0; i<theVolumeStore.size();i++)
250 {
251 if (theVolumeStore[i]->GetFieldManager())
252 {
253 j++;
254 if (j >= fVecFieldManagers.size()) return false;
255 if (fVecFieldManagers[j] != theVolumeStore[i]->GetFieldManager())
256 return false;
257 if (fVecMagneticFields[j] !=
258 theVolumeStore[i]->GetFieldManager()->GetDetectorField())
259 return false;
260 }
261 }
262 if (j<fVecFieldManagers.size()) return false;
263
264 return true;
265}
266*/
267
268//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
269
271{
272 for (size_t i=0; i<fVecEquationOfMotion.size();i++)
273 {
274 delete fVecEquationOfMotion[i];
275 delete fVecChordFinders[i];
276 }
277
278 fVecChordFinders.clear();
279 fVecFieldManagers.clear();
280 fVecMagneticFields.clear();
281 fVecEquationOfMotion.clear();
282
283 // global field
284
285 fVecChordFinders.push_back(nullptr);
286 fVecMagneticFields.push_back(nullptr);
287 fVecEquationOfMotion.push_back(nullptr);
288 fVecFieldManagers.push_back(G4TransportationManager::GetTransportationManager()
289 ->GetFieldManager());
290 if (fVecFieldManagers[0])
291 {
293 (G4MagneticField*) fVecFieldManagers[0]->GetDetectorField();
294 if (fVecMagneticFields[0])
295 {
297 auto pStepper = new G4CashKarpRKF45(fVecEquationOfMotion[0]);
298 auto pIntgrDriver =
299 new G4MagInt_Driver(0.01*mm,pStepper,pStepper->GetNumberOfVariables());
300 fVecChordFinders[0] = new G4ChordFinder(pIntgrDriver);
301 }
302 }
303
304 // local fields
305
306 G4LogicalVolumeStore* theVolumeStore = G4LogicalVolumeStore::GetInstance();
307
308 size_t j=0;
309 for (size_t i=0; i<theVolumeStore->size();i++)
310 {
311 if ((*theVolumeStore)[i]->GetFieldManager())
312 {
313 j++;
314 fVecFieldManagers.push_back(((*theVolumeStore)[i])->GetFieldManager());
316 fVecFieldManagers[j]->GetDetectorField());
317 fVecEquationOfMotion.push_back(nullptr);
318 fVecChordFinders.push_back(nullptr);
319 if (fVecMagneticFields[j])
320 {
322 auto pStepper = new G4CashKarpRKF45(fVecEquationOfMotion[j]);
323 auto pIntgrDriver =
324 new G4MagInt_Driver(.01*mm,pStepper,pStepper->GetNumberOfVariables());
325 fVecChordFinders[j] = new G4ChordFinder(pIntgrDriver);
326 }
327 }
328 }
329}
Definition of the G4BlineEquation class.
Definition of the G4BlineEventAction class.
Definition of the G4BlinePrimaryGeneratorAction class.
Definition of the G4BlineSteppingAction class.
Definition of the G4BlineTracerMessenger class.
Definition of the G4BlineTracer class.
void SetUserPrimaryAction(G4VUserPrimaryGeneratorAction *anAction)
std::vector< G4BlineEquation * > fVecEquationOfMotion
void ComputeBlines(G4int nlines)
void ResetChordFinders()
std::vector< G4MagneticField * > fVecMagneticFields
G4BlineSteppingAction * fSteppingAction
~G4BlineTracer() override
std::vector< G4FieldManager * > fVecFieldManagers
void BeginOfRunAction(const G4Run *aRun) override
G4bool fWas_ResetChordFinders_already_called
G4double fMaxTrackingStep
std::vector< G4ChordFinder * > fVecChordFinders
void EndOfRunAction(const G4Run *aRun) override
G4BlineTracerMessenger * fMessenger
G4BlineEventAction * fEventAction
G4BlinePrimaryGeneratorAction * fPrimaryGeneratorAction

Applications | User Support | Publications | Collaboration