Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4RunManager.hh
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//
27// $Id$
28//
29//
30
31// class description:
32//
33// This is a class for run control in GEANT4
34//
35// User must provide his own classes derived from the following
36// three abstract classes and register them to the RunManager.
37// G4VUserDetectorConstruction - Detector Geometry, Materials
38// G4VUserPhysicsList - Particle types and Processes
39// G4VUserPrimaryGeneratorAction - Event Generator selection
40//
41// In addition to the above mandatory classes, user can easily
42// customize of the default functionality of GEANT4 simulation
43// by making his own classes derived from the following 5 user
44// action classes.
45// G4UserRunAction - Actions for each Run
46// G4UserEventAction - Actions for each Event
47// G4UserStackingAction - Tracks Stacking selection
48// G4UserTrackingAction - Actions for each Track
49// G4UserSteppingAction - Actions for each Step
50//
51// G4RunManager is the only manager class in Geant4 kernel which
52// the user MUST construct an object by him/herself in the main().
53// Also, G4RunManager is the only manager class in Geant4 kernel
54// which the user CAN derive it to costomize the behavior of the
55// run control. For this case, user should use protected methods
56// provided in this class for procedures he/she does not want to
57// change.
58//
59// G4RunManager or the derived class of it MUST be a singleton.
60// The user MUST NOT construct more than one object even if there
61// are two different concrete implementations.
62//
63// G4RunManager controls all of state changes. See G4ApplicationState.hh
64// in intercoms category for the meanings of each state.
65//
66
67#ifndef G4RunManager_h
68#define G4RunManager_h 1
69
70// userAction classes
73class G4UserRunAction;
79
81class G4LogicalVolume;
82class G4Region;
83class G4Timer;
84class G4RunMessenger;
85class G4DCtable;
86class G4Run;
88
89#include "G4RunManagerKernel.hh"
90#include "G4Event.hh"
91#include "G4EventManager.hh"
92#include "globals.hh"
93#include <vector>
94#include <algorithm>
95
97{
98 public: // with description
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 public: // 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
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 InitializeEventLoop(G4int n_event,const char* macroFile=0,G4int n_select=-1);
184 virtual void ProcessOneEvent(G4int i_event);
185 virtual void TerminateOneEvent();
186 virtual void TerminateEventLoop();
187 // Granular virtual methods invoked from DoEventLoop() method.
188
189 ///////////////////////////////////////////////////////////virtual void BuildPhysicsTables();
190 // This method is invoked from RunInitialization() to create physics tables.
191
192 virtual G4Event* GenerateEvent(G4int i_event);
193 virtual void AnalyzeEvent(G4Event* anEvent);
194 // These two protected methods are invoked from DoEventLoop() method at the begining
195 // and the end of each event processing.
196 // GenerateEvent() method constructs a G4Event class object and invoke the user's
197 // G4VUserPrimaryGeneratorAction concrete class. If the user is using ODBMS and event
198 // objects have been created and stored in the data base, he/she must override this
199 // method.
200 // AnalyzeEvent() stores an event to a data base if a concrete G4VPersistentManager
201 // class is defined.
202
203 public: // with description
204 //////////////////////////////////////////////////////void UpdateRegion();
205 // Update region list.
206 // This method is mandatory before invoking following two dump methods.
207 // At RunInitialization(), this method is automatically invoked, and thus
208 // the user needs not invoke.
209
210 void DumpRegion(const G4String& rname) const;
211 // Dump information of a region.
212
213 void DumpRegion(G4Region* region=0) const;
214 // Dump information of a region.
215 // If the pointer is NULL, all regions are shown.
216
217 protected:
218 void StackPreviousEvent(G4Event* anEvent);
219
220 protected:
223
232
233 private:
234 G4RunMessenger* runMessenger;
235
236 protected:
242
247
250 std::vector<G4Event*>* previousEvents;
253
259
261
263
267
268 public:
269 virtual void rndmSaveThisRun();
270 virtual void rndmSaveThisEvent();
271 virtual void RestoreRandomNumberStatus(const G4String& fileN);
272
273 public: // with description
275 { userDetector = userInit; }
277 {
278 physicsList = userInit;
279 kernel->SetPhysics(userInit);
280 }
281 inline void SetUserAction(G4UserRunAction* userAction)
282 { userRunAction = userAction; }
284 { userPrimaryGeneratorAction = userAction; }
285 inline void SetUserAction(G4UserEventAction* userAction)
286 {
287 eventManager->SetUserAction(userAction);
288 userEventAction = userAction;
289 }
290 inline void SetUserAction(G4UserStackingAction* userAction)
291 {
292 eventManager->SetUserAction(userAction);
293 userStackingAction = userAction;
294 }
295 inline void SetUserAction(G4UserTrackingAction* userAction)
296 {
297 eventManager->SetUserAction(userAction);
298 userTrackingAction = userAction;
299 }
300 inline void SetUserAction(G4UserSteppingAction* userAction)
301 {
302 eventManager->SetUserAction(userAction);
303 userSteppingAction = userAction;
304 }
305 // These methods store respective user initialization and action classes.
307 { return userDetector; }
309 { return physicsList; }
310 inline const G4UserRunAction* GetUserRunAction() const
311 { return userRunAction; }
315 { return userEventAction; }
317 { return userStackingAction; }
319 { return userTrackingAction; }
321 { return userSteppingAction; }
322 // These methods returns respective user initialization and action classes.
323
326 // Set the number of additional (optional) waiting stacks.
327 // This method must be invoked at PreInit, Init or Idle states.
328 // Once the user set the number of additional waiting stacks,
329 // he/she can use the corresponding ENUM in G4ClassificationOfNewTrack.
330
331 inline const G4String& GetVersionString() const
332 { return kernel->GetVersionString(); }
333
336
338 // if vl = 1 : status before primary particle generation is stored
339 // if vl = 2 : status before event processing (after primary particle generation) is stored
340 // if vl = 3 : both are stored
341 // if vl = 0 : none is stored (default)
342 {
345 }
348
349 public:
350 inline void SetRandomNumberStore(G4bool flag)
351 { storeRandomNumberStatus = flag; }
353 { return storeRandomNumberStatus; }
354 inline void SetRandomNumberStoreDir(const G4String& dir)
355 {
356 G4String dirStr = dir;
357 if( dirStr(dirStr.length()-1) != '/' ) dirStr += "/";
358#ifndef WIN32
359 G4String shellCmd = "mkdir -p ";
360#else
361 std::replace(dirStr.begin(), dirStr.end(),'/','\\');
362 G4String shellCmd = "if not exist " + dirStr + " mkdir ";
363#endif
364 shellCmd += dirStr;
365 randomNumberStatusDir = dirStr;
366 G4int sysret = system(shellCmd);
367 if(sysret!=0)
368 {
369 G4String errmsg = "\"" + shellCmd + "\" returns non-zero value. Directory creation failed.";
370 G4Exception("GrRunManager::SetRandomNumberStoreDir","Run0071",JustWarning,errmsg);
371 G4cerr << " return value = " << sysret << G4endl;
372 }
373 }
374 inline const G4String& GetRandomNumberStoreDir() const
375 { return randomNumberStatusDir; }
379 {
381 { G4Exception("GrRunManager::SetRandomNumberStoreDir",
382 "Run0072",JustWarning,
383 "Random number status is not available for this event."); }
385 }
386
387 public: // with description
390 // This method must be invoked (or equivalent UI command can be used)
391 // in case the user changes his/her detector geometry after
392 // Initialize() metho has been invoked. Then, at the begining of the next BeamOn(),
393 // all necessary re-initialization will be done.
394
397 // This method must be invoked (or equivalent UI command can be used)
398 // in case the user changes his/her physics process(es), e.g. (in)activate
399 // some processes. Once this method is invoked, regardless of cuts are
400 // changed or not, BuildPhysicsTable() of PhysicsList is invoked for
401 // refreshing all physics tables.
402
404 {
405 G4cerr << "CutOffHasBeenModified becomes obsolete." << G4endl;
406 G4cerr << "It is safe to remove invoking this method." << G4endl;
407 }
408
409 public: // with description
411 // This method may be used if the orientation and/or size of this
412 // particular physical volume has been modified while rest of the
413 // geometries in the world has not been changed. This avoids the
414 // full re-optimization of the entire geometry tree which is forced
415 // if GeometryHasBeenModified() method is invoked.
416
418 // Same as above, but the mother logical volume is specified.
419
420 public:
421 inline void SetVerboseLevel(G4int vl)
422 { verboseLevel = vl;
423 kernel->SetVerboseLevel(vl); }
424 inline G4int GetVerboseLevel() const
425 { return verboseLevel; }
426
428 {
429 if(geometryToBeOptimized != vl)
430 {
434 }
435 }
437 { return geometryToBeOptimized; }
438
439 public: // with description
442 // Sets the number of events to be kept after processing. That is, "val" previous
443 // events can be used with the most recent event for digitizing pileup. "val"+1
444 // previous event is deleted.
445 // This method must be invoked before starting the event loop.
446 inline const G4Run* GetCurrentRun() const
447 { return currentRun; }
448 // Returns the pointer to the current run. This method is available for Geant4
449 // states of GeomClosed and EventProc.
450 inline const G4Event* GetCurrentEvent() const
451 { return currentEvent; }
452 // Returns the pointer to the current event. This method is available for EventProc
453 // state.
454 inline const G4Event* GetPreviousEvent(G4int i) const
455 {
456 if(i>=1 && i<=n_perviousEventsToBeStored)
457 { return (*previousEvents)[i-1]; }
458 return 0;
459 }
460 // Returns the pointer to the "i" previous event. This method is availavle for
461 // EventProc state. In case the event loop has not yet to reach to the requested
462 // event, null will be returned. To use this method, SetNumberOfEventsToBeStored()
463 // method mentioned above must be invoked previously to the event loop.
464 inline void SetRunIDCounter(G4int i)
465 { runIDCounter = i; }
466 // Set the run number counter. Initially, the counter is initialized to zero and
467 // incremented by one for every BeamOn().
468
469 public:
471 { return nParallelWorlds; }
474 inline void SetDCtable(G4DCtable* DCtbl)
475 { DCtable = DCtbl; }
476
477 public:
479 protected:
480 void UpdateScoring();
481};
482
483#endif
484
@ JustWarning
int G4int
Definition: G4Types.hh:66
bool G4bool
Definition: G4Types.hh:67
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cerr
void SetUserAction(G4UserEventAction *userAction)
void SetNumberOfAdditionalWaitingStacks(G4int iAdd)
void StoreRandomNumberStatusToG4Event(G4int vl)
void SetPhysics(G4VUserPhysicsList *uPhys)
void SetGeometryToBeOptimized(G4bool vl)
void SetPrimaryTransformer(G4PrimaryTransformer *pt)
const G4String & GetVersionString() const
void SetVerboseLevel(G4int vl)
const G4UserTrackingAction * GetUserTrackingAction() const
G4bool geometryInitialized
G4int storeRandomNumberStatusToG4Event
virtual void AbortRun(G4bool softAbort=false)
virtual void Initialize()
G4int nParallelWorlds
void SetUserAction(G4UserTrackingAction *userAction)
const G4VUserDetectorConstruction * GetUserDetectorConstruction() const
G4Timer * timer
void SetRunIDCounter(G4int i)
G4int n_select_msg
G4int numberOfEventProcessed
virtual void RestoreRandomNumberStatus(const G4String &fileN)
const G4VUserPrimaryGeneratorAction * GetUserPrimaryGeneratorAction() const
const G4UserEventAction * GetUserEventAction() const
virtual void BeamOn(G4int n_event, const char *macroFile=0, G4int n_select=-1)
G4UserEventAction * userEventAction
void SetRandomNumberStoreDir(const G4String &dir)
virtual void DoEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
void SetUserAction(G4UserEventAction *userAction)
virtual void rndmSaveThisEvent()
G4int runIDCounter
const G4Event * GetPreviousEvent(G4int i) const
G4RunManagerKernel * kernel
G4int verboseLevel
G4Run * currentRun
virtual G4bool ConfirmBeamOnCondition()
G4bool geometryToBeOptimized
void SetUserAction(G4UserStackingAction *userAction)
void SetUserAction(G4VUserPrimaryGeneratorAction *userAction)
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:62
virtual void AbortEvent()
virtual void rndmSaveThisRun()
virtual void InitializePhysics()
G4DCtable * DCtable
G4String randomNumberStatusForThisRun
G4bool runAborted
G4String msgText
G4UserRunAction * userRunAction
const G4String & GetRandomNumberStatusForThisEvent() const
virtual void DefineWorldVolume(G4VPhysicalVolume *worldVol, G4bool topologyIsChanged=true)
const G4UserStackingAction * GetUserStackingAction() const
const G4String & GetVersionString() const
virtual void InitializeGeometry()
virtual void RunTermination()
void SetNumberOfAdditionalWaitingStacks(G4int iAdd)
const G4UserSteppingAction * GetUserSteppingAction() const
void SetUserInitialization(G4VUserPhysicsList *userInit)
void SetDCtable(G4DCtable *DCtbl)
void SetNumberOfEventsToBeStored(G4int val)
G4bool physicsInitialized
void PhysicsHasBeenModified()
G4VPhysicalVolume * currentWorld
const G4Event * GetCurrentEvent() const
void StackPreviousEvent(G4Event *anEvent)
void ReOptimize(G4LogicalVolume *)
void GeometryHasBeenModified()
const G4String & GetRandomNumberStatusForThisRun() const
void SetNumberOfEventsToBeProcessed(G4int val)
void SetVerboseLevel(G4int vl)
G4bool GetRandomNumberStore() const
const G4VUserPhysicsList * GetUserPhysicsList() const
G4VUserDetectorConstruction * userDetector
const G4String & GetRandomNumberStoreDir() const
G4VUserPrimaryGeneratorAction * userPrimaryGeneratorAction
const G4Run * GetCurrentRun() const
G4int numberOfEventToBeProcessed
virtual ~G4RunManager()
G4int GetNumberOfParallelWorld() const
G4String randomNumberStatusDir
virtual void TerminateEventLoop()
void SetUserAction(G4UserRunAction *userAction)
G4String randomNumberStatusForThisEvent
const G4UserRunAction * GetUserRunAction() const
void SetGeometryToBeOptimized(G4bool vl)
void SetUserAction(G4UserSteppingAction *userAction)
G4UserTrackingAction * userTrackingAction
G4int n_perviousEventsToBeStored
void SetUserInitialization(G4VUserDetectorConstruction *userInit)
void ReOptimizeMotherOf(G4VPhysicalVolume *)
void StoreRandomNumberStatusToG4Event(G4int vl)
virtual void ProcessOneEvent(G4int i_event)
void CutOffHasBeenModified()
G4bool storeRandomNumberStatus
virtual G4Event * GenerateEvent(G4int i_event)
G4int GetFlagRandomNumberStatusToG4Event() const
void SetRandomNumberStore(G4bool flag)
G4bool initializedAtLeastOnce
G4EventManager * eventManager
virtual void TerminateOneEvent()
virtual void RunInitialization()
void DumpRegion(const G4String &rname) const
G4VUserPhysicsList * physicsList
virtual void AnalyzeEvent(G4Event *anEvent)
G4bool GetGeometryToBeOptimized()
G4UserStackingAction * userStackingAction
G4int GetVerboseLevel() const
void UpdateScoring()
G4Event * currentEvent
std::vector< G4Event * > * previousEvents
void ConstructScoringWorlds()
void SetPrimaryTransformer(G4PrimaryTransformer *pt)
virtual void InitializeEventLoop(G4int n_event, const char *macroFile=0, G4int n_select=-1)
G4UserSteppingAction * userSteppingAction
Definition: G4Run.hh:47
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41