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 | // |
---|
27 | // $Id: G4RunManager.hh,v 1.53 2010/06/11 09:02:14 gcosmo Exp $ |
---|
28 | // GEANT4 tag $Name: geant4-09-04-beta-01 $ |
---|
29 | // |
---|
30 | // |
---|
31 | |
---|
32 | // class description: |
---|
33 | // |
---|
34 | // This is a class for run control in GEANT4 |
---|
35 | // |
---|
36 | // User must provide his own classes derived from the following |
---|
37 | // three abstract classes and register them to the RunManager. |
---|
38 | // G4VUserDetectorConstruction - Detector Geometry, Materials |
---|
39 | // G4VUserPhysicsList - Particle types and Processes |
---|
40 | // G4VUserPrimaryGeneratorAction - Event Generator selection |
---|
41 | // |
---|
42 | // In addition to the above mandatory classes, user can easily |
---|
43 | // customize of the default functionality of GEANT4 simulation |
---|
44 | // by making his own classes derived from the following 5 user |
---|
45 | // action classes. |
---|
46 | // G4UserRunAction - Actions for each Run |
---|
47 | // G4UserEventAction - Actions for each Event |
---|
48 | // G4UserStackingAction - Tracks Stacking selection |
---|
49 | // G4UserTrackingAction - Actions for each Track |
---|
50 | // G4UserSteppingAction - Actions for each Step |
---|
51 | // |
---|
52 | // G4RunManager is the only manager class in Geant4 kernel which |
---|
53 | // the user MUST construct an object by him/herself in the main(). |
---|
54 | // Also, G4RunManager is the only manager class in Geant4 kernel |
---|
55 | // which the user CAN derive it to costomize the behavior of the |
---|
56 | // run control. For this case, user should use protected methods |
---|
57 | // provided in this class for procedures he/she does not want to |
---|
58 | // change. |
---|
59 | // |
---|
60 | // G4RunManager or the derived class of it MUST be a singleton. |
---|
61 | // The user MUST NOT construct more than one object even if there |
---|
62 | // are two different concrete implementations. |
---|
63 | // |
---|
64 | // G4RunManager controls all of state changes. See G4ApplicationState.hh |
---|
65 | // in intercoms category for the meanings of each state. |
---|
66 | // |
---|
67 | |
---|
68 | #ifndef G4RunManager_h |
---|
69 | #define G4RunManager_h 1 |
---|
70 | |
---|
71 | // userAction classes |
---|
72 | class G4VUserDetectorConstruction; |
---|
73 | class G4VUserPhysicsList; |
---|
74 | class G4UserRunAction; |
---|
75 | class G4VUserPrimaryGeneratorAction; |
---|
76 | class G4UserEventAction; |
---|
77 | class G4UserStackingAction; |
---|
78 | class G4UserTrackingAction; |
---|
79 | class G4UserSteppingAction; |
---|
80 | |
---|
81 | class G4VPhysicalVolume; |
---|
82 | class G4Region; |
---|
83 | class G4Timer; |
---|
84 | class G4RunMessenger; |
---|
85 | class G4DCtable; |
---|
86 | class G4Run; |
---|
87 | class G4PrimaryTransformer; |
---|
88 | |
---|
89 | #include "G4RunManagerKernel.hh" |
---|
90 | #include "G4Event.hh" |
---|
91 | #include "G4EventManager.hh" |
---|
92 | #include "globals.hh" |
---|
93 | #include <vector> |
---|
94 | #include <algorithm> |
---|
95 | |
---|
96 | class G4RunManager |
---|
97 | { |
---|
98 | public: // with description |
---|
99 | static G4RunManager* GetRunManager(); |
---|
100 | // Static method which returns the singleton pointer of G4RunManager or |
---|
101 | // its derived class. |
---|
102 | |
---|
103 | private: |
---|
104 | static G4RunManager* fRunManager; |
---|
105 | |
---|
106 | public: // with description |
---|
107 | G4RunManager(); |
---|
108 | virtual ~G4RunManager(); |
---|
109 | // The constructor and the destructor. The user must construct this class |
---|
110 | // object at the beginning of his/her main() and must delete it at the |
---|
111 | // bottom of the main(). |
---|
112 | |
---|
113 | public: // with description |
---|
114 | virtual void BeamOn(G4int n_event,const char* macroFile=0,G4int n_select=-1); |
---|
115 | // This method starts an event loof of "n_event" events. The condition of Geant4 |
---|
116 | // is examined before starting the event loop. This method must be invoked at |
---|
117 | // Idle state. The state will be changed to GeomClosed during the event loop and |
---|
118 | // will go back to Idle when the loop is over or aborted. |
---|
119 | // In case a string "macroFile" which represents the name of a macro file is given, |
---|
120 | // this macro file will be executed AT THE END of each event processing. In case |
---|
121 | // "n_select" is greater than zero, at the ond of first "n_select" events the macro |
---|
122 | // file is executed. |
---|
123 | virtual void Initialize(); |
---|
124 | // This method invokes all the necessary initialization procedures for an event |
---|
125 | // loop. This method must be invoked at the Geant4 state of PreInit or Idle. The |
---|
126 | // state will be changed to Init during the initialization procedures and then |
---|
127 | // changed to Idle. |
---|
128 | // This method invokes two protected methods, InitializeGeometry() and |
---|
129 | // InitializePhysics(). |
---|
130 | // After some event loops, the user can invoke this method once again. It is |
---|
131 | // required if the user changes geometry, physics process, and/or cut off value. |
---|
132 | // If the user forget the second invokation, G4RunManager will invoke BeamOn() |
---|
133 | // method will invoke this method. (Note that this feature is not valid for the |
---|
134 | // first initialization.) |
---|
135 | virtual void DefineWorldVolume(G4VPhysicalVolume * worldVol, |
---|
136 | G4bool topologyIsChanged=true); |
---|
137 | // This method must be invoked if the geometry setup has been changed between |
---|
138 | // runs. The flag 'topologyIsChanged' will specify if the geometry topology is |
---|
139 | // different from the original one used in the previous run; if not, it must be |
---|
140 | // set to false, so that the original optimisation and navigation history is |
---|
141 | // preserved. This method is invoked also at initialisation. |
---|
142 | //////////////////////////////////////////////////////virtual void ResetNavigator() const; |
---|
143 | // Resets the state of the navigator for tracking; needed for geometry updates. |
---|
144 | // It forces the optimisation and navigation history to be reset. |
---|
145 | virtual void AbortRun(G4bool softAbort=false); |
---|
146 | // This method safely aborts the current event loop even if an event is in progress. |
---|
147 | // This method is available for Geant4 states of GeomClosed and EventProc. The state |
---|
148 | // will be changed to Idle, so that another event loop can be done. |
---|
149 | // If softAbort is true, the event loop is aborted after processing the current |
---|
150 | // event, while the current event is aborted if it is false. |
---|
151 | virtual void AbortEvent(); |
---|
152 | // This method aborts the currently processing event, remaining events in the |
---|
153 | // current event loop will be processed. This method is available only for |
---|
154 | // EventProc state. |
---|
155 | |
---|
156 | protected: // with description |
---|
157 | |
---|
158 | virtual void InitializeGeometry(); |
---|
159 | virtual void InitializePhysics(); |
---|
160 | // These protected methods are invoked from Initialize() method for the |
---|
161 | // initializations of geometry and physics processes. The user's concrete |
---|
162 | // G4VUserDetectorConstruction class will be accessed from InitializeGeometry() and |
---|
163 | // G4VUserPhysicsList class will be accessed from InitializePhysics(). |
---|
164 | |
---|
165 | virtual G4bool ConfirmBeamOnCondition(); |
---|
166 | virtual void RunInitialization(); |
---|
167 | virtual void DoEventLoop(G4int n_event,const char* macroFile=0,G4int n_select=-1); |
---|
168 | virtual void RunTermination(); |
---|
169 | // These four protected methods are invoked from BeamOn() method. These four methods |
---|
170 | // are invoked in this order. |
---|
171 | // ConfirmBeamOnCondition() method checks if all the necessary initializations have |
---|
172 | // already done. If the condition is not satisfied, false is returned and the follwing |
---|
173 | // three methods will be skipped. |
---|
174 | // RunInitialization() method initializes a run. For example, a G4Run class object |
---|
175 | // is constructed in this method. |
---|
176 | // DoEventLoop() method control an event loop. Arguments are same as BeamOn() method. |
---|
177 | // Inide the event loop, two following protected methods are invoked at the begining |
---|
178 | // and the end of each event. |
---|
179 | // RunTermination() method terminates a run processing. For example, a G4Run class |
---|
180 | // object is deleted in this class. If the user uses ODBMS and wants to store the |
---|
181 | // G4Run class object, he/she must override this method. |
---|
182 | |
---|
183 | ///////////////////////////////////////////////////////////virtual void BuildPhysicsTables(); |
---|
184 | // This method is invoked from RunInitialization() to create physics tables. |
---|
185 | |
---|
186 | virtual G4Event* GenerateEvent(G4int i_event); |
---|
187 | virtual void AnalyzeEvent(G4Event* anEvent); |
---|
188 | // These two protected methods are invoked from DoEventLoop() method at the begining |
---|
189 | // and the end of each event processing. |
---|
190 | // GenerateEvent() method constructs a G4Event class object and invoke the user's |
---|
191 | // G4VUserPrimaryGeneratorAction concrete class. If the user is using ODBMS and event |
---|
192 | // objects have been created and stored in the data base, he/she must override this |
---|
193 | // method. |
---|
194 | // AnalyzeEvent() stores an event to a data base if a concrete G4VPersistentManager |
---|
195 | // class is defined. |
---|
196 | |
---|
197 | public: // with description |
---|
198 | //////////////////////////////////////////////////////void UpdateRegion(); |
---|
199 | // Update region list. |
---|
200 | // This method is mandatory before invoking following two dump methods. |
---|
201 | // At RunInitialization(), this method is automatically invoked, and thus |
---|
202 | // the user needs not invoke. |
---|
203 | |
---|
204 | void DumpRegion(const G4String& rname) const; |
---|
205 | // Dump information of a region. |
---|
206 | |
---|
207 | void DumpRegion(G4Region* region=0) const; |
---|
208 | // Dump information of a region. |
---|
209 | // If the pointer is NULL, all regions are shown. |
---|
210 | |
---|
211 | protected: |
---|
212 | void StackPreviousEvent(G4Event* anEvent); |
---|
213 | |
---|
214 | protected: |
---|
215 | G4RunManagerKernel * kernel; |
---|
216 | G4EventManager * eventManager; |
---|
217 | |
---|
218 | G4VUserDetectorConstruction * userDetector; |
---|
219 | G4VUserPhysicsList * physicsList; |
---|
220 | G4UserRunAction * userRunAction; |
---|
221 | G4VUserPrimaryGeneratorAction * userPrimaryGeneratorAction; |
---|
222 | G4UserEventAction * userEventAction; |
---|
223 | G4UserStackingAction * userStackingAction; |
---|
224 | G4UserTrackingAction * userTrackingAction; |
---|
225 | G4UserSteppingAction * userSteppingAction; |
---|
226 | |
---|
227 | private: |
---|
228 | G4RunMessenger* runMessenger; |
---|
229 | |
---|
230 | protected: |
---|
231 | G4bool geometryInitialized; |
---|
232 | G4bool physicsInitialized; |
---|
233 | G4bool runAborted; |
---|
234 | G4bool initializedAtLeastOnce; |
---|
235 | G4bool geometryToBeOptimized; |
---|
236 | |
---|
237 | G4int runIDCounter; |
---|
238 | G4int verboseLevel; |
---|
239 | G4Timer * timer; |
---|
240 | G4DCtable* DCtable; |
---|
241 | |
---|
242 | G4Run* currentRun; |
---|
243 | G4Event* currentEvent; |
---|
244 | std::vector<G4Event*>* previousEvents; |
---|
245 | G4int n_perviousEventsToBeStored; |
---|
246 | G4int numberOfEventToBeProcessed; |
---|
247 | |
---|
248 | G4bool storeRandomNumberStatus; |
---|
249 | G4int storeRandomNumberStatusToG4Event; |
---|
250 | G4String randomNumberStatusDir; |
---|
251 | G4String randomNumberStatusForThisRun; |
---|
252 | G4String randomNumberStatusForThisEvent; |
---|
253 | |
---|
254 | G4VPhysicalVolume* currentWorld; |
---|
255 | |
---|
256 | G4int nParallelWorlds; |
---|
257 | |
---|
258 | |
---|
259 | public: |
---|
260 | virtual void rndmSaveThisRun(); |
---|
261 | virtual void rndmSaveThisEvent(); |
---|
262 | virtual void RestoreRandomNumberStatus(const G4String& fileN); |
---|
263 | |
---|
264 | public: // with description |
---|
265 | inline void SetUserInitialization(G4VUserDetectorConstruction* userInit) |
---|
266 | { userDetector = userInit; } |
---|
267 | inline void SetUserInitialization(G4VUserPhysicsList* userInit) |
---|
268 | { |
---|
269 | physicsList = userInit; |
---|
270 | kernel->SetPhysics(userInit); |
---|
271 | } |
---|
272 | inline void SetUserAction(G4UserRunAction* userAction) |
---|
273 | { userRunAction = userAction; } |
---|
274 | inline void SetUserAction(G4VUserPrimaryGeneratorAction* userAction) |
---|
275 | { userPrimaryGeneratorAction = userAction; } |
---|
276 | inline void SetUserAction(G4UserEventAction* userAction) |
---|
277 | { |
---|
278 | eventManager->SetUserAction(userAction); |
---|
279 | userEventAction = userAction; |
---|
280 | } |
---|
281 | inline void SetUserAction(G4UserStackingAction* userAction) |
---|
282 | { |
---|
283 | eventManager->SetUserAction(userAction); |
---|
284 | userStackingAction = userAction; |
---|
285 | } |
---|
286 | inline void SetUserAction(G4UserTrackingAction* userAction) |
---|
287 | { |
---|
288 | eventManager->SetUserAction(userAction); |
---|
289 | userTrackingAction = userAction; |
---|
290 | } |
---|
291 | inline void SetUserAction(G4UserSteppingAction* userAction) |
---|
292 | { |
---|
293 | eventManager->SetUserAction(userAction); |
---|
294 | userSteppingAction = userAction; |
---|
295 | } |
---|
296 | // These methods store respective user initialization and action classes. |
---|
297 | inline const G4VUserDetectorConstruction* GetUserDetectorConstruction() const |
---|
298 | { return userDetector; } |
---|
299 | inline const G4VUserPhysicsList* GetUserPhysicsList() const |
---|
300 | { return physicsList; } |
---|
301 | inline const G4UserRunAction* GetUserRunAction() const |
---|
302 | { return userRunAction; } |
---|
303 | inline const G4VUserPrimaryGeneratorAction* GetUserPrimaryGeneratorAction() const |
---|
304 | { return userPrimaryGeneratorAction; } |
---|
305 | inline const G4UserEventAction* GetUserEventAction() const |
---|
306 | { return userEventAction; } |
---|
307 | inline const G4UserStackingAction* GetUserStackingAction() const |
---|
308 | { return userStackingAction; } |
---|
309 | inline const G4UserTrackingAction* GetUserTrackingAction() const |
---|
310 | { return userTrackingAction; } |
---|
311 | inline const G4UserSteppingAction* GetUserSteppingAction() const |
---|
312 | { return userSteppingAction; } |
---|
313 | // These methods returns respective user initialization and action classes. |
---|
314 | |
---|
315 | inline void SetNumberOfAdditionalWaitingStacks(G4int iAdd) |
---|
316 | { eventManager->SetNumberOfAdditionalWaitingStacks(iAdd); } |
---|
317 | // Set the number of additional (optional) waiting stacks. |
---|
318 | // This method must be invoked at PreInit, Init or Idle states. |
---|
319 | // Once the user set the number of additional waiting stacks, |
---|
320 | // he/she can use the corresponding ENUM in G4ClassificationOfNewTrack. |
---|
321 | |
---|
322 | inline const G4String& GetVersionString() const |
---|
323 | { return kernel->GetVersionString(); } |
---|
324 | |
---|
325 | inline void SetPrimaryTransformer(G4PrimaryTransformer* pt) |
---|
326 | { kernel->SetPrimaryTransformer(pt); } |
---|
327 | |
---|
328 | inline void StoreRandomNumberStatusToG4Event(G4int vl) |
---|
329 | // if vl = 1 : status before primary particle generation is stored |
---|
330 | // if vl = 2 : status before event processing (after primary particle generation) is stored |
---|
331 | // if vl = 3 : both are stored |
---|
332 | // if vl = 0 : none is stored (default) |
---|
333 | { |
---|
334 | storeRandomNumberStatusToG4Event = vl; |
---|
335 | eventManager->StoreRandomNumberStatusToG4Event(vl); |
---|
336 | } |
---|
337 | inline G4int GetFlagRandomNumberStatusToG4Event() const |
---|
338 | { return storeRandomNumberStatusToG4Event; } |
---|
339 | |
---|
340 | public: |
---|
341 | inline void SetRandomNumberStore(G4bool flag) |
---|
342 | { storeRandomNumberStatus = flag; } |
---|
343 | inline G4bool GetRandomNumberStore() const |
---|
344 | { return storeRandomNumberStatus; } |
---|
345 | inline void SetRandomNumberStoreDir(const G4String& dir) |
---|
346 | { |
---|
347 | G4String dirStr = dir; |
---|
348 | if( dirStr(dirStr.length()-1) != '/' ) dirStr += "/"; |
---|
349 | #ifndef WIN32 |
---|
350 | G4String shellCmd = "mkdir -p "; |
---|
351 | #else |
---|
352 | std::replace(dirStr.begin(), dirStr.end(),'/','\\'); |
---|
353 | G4String shellCmd = "mkdir "; |
---|
354 | #endif |
---|
355 | shellCmd += dirStr; |
---|
356 | randomNumberStatusDir = dirStr; |
---|
357 | system(shellCmd); |
---|
358 | } |
---|
359 | inline const G4String& GetRandomNumberStoreDir() const |
---|
360 | { return randomNumberStatusDir; } |
---|
361 | inline const G4String& GetRandomNumberStatusForThisRun() const |
---|
362 | { return randomNumberStatusForThisRun; } |
---|
363 | inline const G4String& GetRandomNumberStatusForThisEvent() const |
---|
364 | { |
---|
365 | if(storeRandomNumberStatusToG4Event==0 || storeRandomNumberStatusToG4Event==2) |
---|
366 | { G4Exception("Random number status is not available for this event."); } |
---|
367 | return randomNumberStatusForThisEvent; |
---|
368 | } |
---|
369 | |
---|
370 | public: // with description |
---|
371 | inline void GeometryHasBeenModified() |
---|
372 | { kernel->GeometryHasBeenModified(); } |
---|
373 | // This method must be invoked (or equivalent UI command can be used) |
---|
374 | // in case the user changes his/her detector geometry after |
---|
375 | // Initialize() metho has been invoked. Then, at the begining of the next BeamOn(), |
---|
376 | // all necessary re-initialization will be done. |
---|
377 | |
---|
378 | inline void PhysicsHasBeenModified() |
---|
379 | { kernel->PhysicsHasBeenModified(); } |
---|
380 | // This method must be invoked (or equivalent UI command can be used) |
---|
381 | // in case the user changes his/her physics process(es), e.g. (in)activate |
---|
382 | // some processes. Once this method is invoked, regardless of cuts are |
---|
383 | // changed or not, BuildPhysicsTable() of PhysicsList is invoked for |
---|
384 | // refreshing all physics tables. |
---|
385 | |
---|
386 | inline void CutOffHasBeenModified() |
---|
387 | { |
---|
388 | G4cerr << "CutOffHasBeenModified becomes obsolete." << G4endl; |
---|
389 | G4cerr << "It is safe to remove invoking this method." << G4endl; |
---|
390 | } |
---|
391 | |
---|
392 | public: |
---|
393 | inline void SetVerboseLevel(G4int vl) |
---|
394 | { verboseLevel = vl; |
---|
395 | kernel->SetVerboseLevel(vl); } |
---|
396 | inline G4int GetVerboseLevel() const |
---|
397 | { return verboseLevel; } |
---|
398 | |
---|
399 | inline void SetGeometryToBeOptimized(G4bool vl) |
---|
400 | { |
---|
401 | if(geometryToBeOptimized != vl) |
---|
402 | { |
---|
403 | geometryToBeOptimized = vl; |
---|
404 | kernel->GeometryHasBeenModified(); |
---|
405 | kernel->SetGeometryToBeOptimized(vl); |
---|
406 | } |
---|
407 | } |
---|
408 | inline G4bool GetGeometryToBeOptimized() |
---|
409 | { return geometryToBeOptimized; } |
---|
410 | |
---|
411 | public: // with description |
---|
412 | inline void SetNumberOfEventsToBeStored(G4int val) |
---|
413 | { n_perviousEventsToBeStored = val; } |
---|
414 | // Sets the number of events to be kept after processing. That is, "val" previous |
---|
415 | // events can be used with the most recent event for digitizing pileup. "val"+1 |
---|
416 | // previous event is deleted. |
---|
417 | // This method must be invoked before starting the event loop. |
---|
418 | inline const G4Run* GetCurrentRun() const |
---|
419 | { return currentRun; } |
---|
420 | // Returns the pointer to the current run. This method is available for Geant4 |
---|
421 | // states of GeomClosed and EventProc. |
---|
422 | inline const G4Event* GetCurrentEvent() const |
---|
423 | { return currentEvent; } |
---|
424 | // Returns the pointer to the current event. This method is available for EventProc |
---|
425 | // state. |
---|
426 | inline const G4Event* GetPreviousEvent(G4int i) const |
---|
427 | { |
---|
428 | if(i>=1 && i<=n_perviousEventsToBeStored) |
---|
429 | { return (*previousEvents)[i-1]; } |
---|
430 | return 0; |
---|
431 | } |
---|
432 | // Returns the pointer to the "i" previous event. This method is availavle for |
---|
433 | // EventProc state. In case the event loop has not yet to reach to the requested |
---|
434 | // event, null will be returned. To use this method, SetNumberOfEventsToBeStored() |
---|
435 | // method mentioned above must be invoked previously to the event loop. |
---|
436 | inline void SetRunIDCounter(G4int i) |
---|
437 | { runIDCounter = i; } |
---|
438 | // Set the run number counter. Initially, the counter is initialized to zero and |
---|
439 | // incremented by one for every BeamOn(). |
---|
440 | inline G4int GetNumberOfParallelWorld() const |
---|
441 | { return nParallelWorlds; } |
---|
442 | public: |
---|
443 | inline void SetDCtable(G4DCtable* DCtbl) |
---|
444 | { DCtable = DCtbl; } |
---|
445 | |
---|
446 | public: |
---|
447 | void ConstructScoringWorlds(); |
---|
448 | protected: |
---|
449 | void UpdateScoring(); |
---|
450 | }; |
---|
451 | |
---|
452 | #endif |
---|
453 | |
---|