3.4.  Run

3.4.1.  Basic concept of Run

In Geant4, Run is the largest unit of simulation. A run consists of a sequence of events. Within a run, the detector geometry, the set up of sensitive detectors, and the physics processes used in the simulation should be kept unchanged. A run is represented by a G4Run class object. A run starts with BeamOn() method of G4RunManager.

3.4.1.1.  Representation of a run

G4Run represents a run. It has a run identification number, which should be set by the user, and the number of events simulated during the run. Please note that the run identification number is not used by the Geant4 kernel, and thus can be arbitrarily assigned at the user's convenience.

G4Run has pointers to the tables G4VHitsCollection and G4VDigiCollection. These tables are associated in case sensitive detectors and digitizer modules are simulated, respectively. The usage of these tables will be mentioned in Section 4.4 and Section 4.5.

3.4.1.2.  Manage the run procedures

G4RunManager manages the procedures of a run. In the constructor of G4RunManager, all of the manager classes in Geant4 kernel, except for some static managers, are constructed. These managers are deleted in the destructor of G4RunManager. G4RunManager must be a singleton, and the pointer to this singleton object can be obtained by the getRunManager() static method.

As already mentioned in Section 2.1, all of the user initialization classes and user action classes defined by the user should be assigned to G4RunManager before starting initialization of the Geant4 kernel. The assignments of these user classes are done by SetUserInitialization() and SetUserAction() methods. All user classes defined by the Geant4 kernel will be summarized in Chapter 6.

G4RunManager has several public methods, which are listed below.

Initialize()
All initializations required by the Geant4 kernel are triggered by this method. Initializations are:
  • construction of the detector geometry and set up of sensitive detectors and/or digitizer modules,

  • construction of particles and physics processes,

  • calculation of cross-section tables.

This method is thus mandatory before proceeding to the first run. This method will be invoked automatically for the second and later runs in case some of the initialized quantities need to be updated.
BeamOn(G4int numberOfEvent)
This method triggers the actual simulation of a run, that is, an event loop. It takes an integer argument which represents the number of events to be simulated.
GetRunManager()
This static method returns the pointer to the G4RunManager singleton object.
GetCurrentEvent()
This method returns the pointer to the G4Event object which is currently being simulated. This method is available only when an event is being processed. At this moment, the application state of Geant4, which is explained in the following sub-section, is "EventProc". When Geant4 is in a state other than "EventProc", this method returns null. Please note that the return value of this method is const G4Event * and thus you cannot modify the contents of the object.
SetNumberOfEventsToBeStored(G4int nPrevious)
When simulating the "pile up" of more than one event, it is essential to access more than one event at the same moment. By invoking this method, G4RunManager keeps nPrevious G4Event objects. This method must be invoked before proceeding to BeamOn().
GetPreviousEvent(G4int i_thPrevious)
The pointer to the i_thPrevious G4Event object can be obtained through this method. A pointer to a const object is returned. It is inevitable that i_thPrevious events must have already been simulated in the same run for getting the i_thPrevious event. Otherwise, this method returns null.
AbortRun()
This method should be invoked whenever the processing of a run must be stopped. It is valid for GeomClosed and EventProc states. Run processing will be safely aborted even in the midst of processing an event. However, the last event of the aborted run will be incomplete and should not be used for further analysis.

3.4.1.3.  G4UserRunAction

G4UserRunAction is one of the user action classes from which you can derive your own concrete class. This base class has two virtual methods, as follows:

BeginOfRunAction()
This method is invoked at the beginning of the BeamOn() method but after confirmation of the conditions of the Geant4 kernel. Likely uses of this method include:
  • setting a run identification number,

  • booking histograms,

  • setting run specific conditions of the sensitive detectors and/or digitizer modules (e.g., dead channels).

EndOfRunAction()
This method is invoked at the very end of the BeamOn() method. Typical use cases of this method are
  • store/print histograms,

  • manipulate run summaries.

3.4.2.  Geant4 as a state machine

Geant4 is designed as a state machine. Some methods in Geant4 are available for only a certain state(s). G4RunManager controls the state changes of the Geant4 application. States of Geant4 are represented by the enumeration G4ApplicationState. It has six states through the life cycle of a Geant4 application.

G4State_PreInit state
A Geant4 application starts with this state. The application needs to be initialized when it is in this state. The application occasionally comes back to this state if geometry, physics processes, and/or cut-off have been changed after processing a run.
G4State_Init state
The application is in this state while the Initialize() method of G4RunManager is being invoked. Methods defined in any user initialization classes are invoked during this state.
G4State_Idle state
The application is ready for starting a run.
G4State_GeomClosed state
When BeamOn() is invoked, the application proceeds to this state to process a run. Geometry, physics processes, and cut-off cannot be changed during run processing.
G4State_EventProc state
A Geant4 application is in this state when a particular event is being processed. GetCurrentEvent() and GetPreviousEvent() methods of G4RunManager are available only at this state.
G4State_Quit state
When the destructor of G4RunManager is invoked, the application comes to this "dead end" state. Managers of the Geant4 kernel are being deleted and thus the application cannot come back to any other state.
G4State_Abort state
When a G4Exception occurs, the application comes to this "dead end" state and causes a core dump. The user still has a hook to do some "safe" opperations, e.g. storing histograms, by implementing a user concrete class of G4VStateDependent. The user also has a choice to suppress the occurence of G4Exception by a UI command /control/suppressAbortion. When abortion is suppressed, you will still get error messages issued by G4Exception, and there is NO guarantee of a correct result after the G4Exception error message.

G4StateManager belongs to the intercoms category.

3.4.3.  User's hook for state change

In case the user wants to do something at the moment of state change of Geant4, the user can create a concrete class of the G4VStateDependent base class. For example, the user can store histograms when G4Exception occurs and Geant4 comes to the Abort state, but before the actual core dump.

The following is an example user code which stores histograms when Geant4 becomes to the Abort state. This class object should be mabe in, for example main(), by the user code. This object will be automatically registered to G4StateManager at its construction.

Example 3.1.  Header file of UserHookForAbortState

#ifndef UserHookForAbortState_H
#define UserHookForAbortState_H 1

#include "G4VStateDependent.hh"

class UserHookForAbortState : public G4VStateDependent
{
 public:
  UserHookForAbortState();   // constructor
  ~UserHookForAbortState();  // destructor

  virtual G4bool Notify(G4ApplicationState requiredState);
};

Example 3.2.  Source file of UserHookForAbortState

#include "UserHookForAbortState.hh"

UserHookForAbortState::UserHookForAbortState() {;}
UserHookForAbortState::~UserHookForAbortState() {;}

G4bool UserHookForAbortState::Notify(G4ApplicationState requiredState)
{
  if(requiredState!=Abort) return true;
  
  // Do book keeping here

  return true;
}

3.4.4.  Customizing the Run Manager

3.4.4.1.  Virtual Methods in the Run Manager

G4RunManager is a concrete class with a complete set of functionalities for managing the Geant4 kernel. It is the only manager class in the Geant4 kernel which must be constructed in the main() method of the user's application. Thus, instead of constructing the G4RunManager provided by Geant4, you are free to construct your own RunManager. It is recommended, however, that your RunManager inherit G4RunManager. For this purpose, G4RunManager has various virtual methods which provide all the functionalities required to handle the Geant4 kernel. Hence, your customized run manager need only override the methods particular to your needs; the remaining methods in G4RunManager base class can still be used. A summary of the available methods is presented here:

public: virtual void Initialize();
main entry point of Geant4 kernel initialization
protected: virtual void InitializeGeometry();
geometry construction
protected: virtual void InitializePhysics();
physics processes construction
public: virtual void BeamOn(G4int n_event);
main entry point of the event loop
protected: virtual G4bool ConfirmBeamOnCondition();
check the kernel conditions for the event loop
protected: virtual void RunInitialization();
prepare a run
protected: virtual void DoEventLoop(G4int n_events);
manage an event loop
protected: virtual G4Event* GenerateEvent(G4int i_event);
generation of G4Event object
protected: virtual void AnalyzeEvent(G4Event* anEvent);
storage/analysis of an event
protected: virtual void RunTermination();
terminate a run
public: virtual void DefineWorldVolume(G4VPhysicalVolume * worldVol);
set the world volume to G4Navigator
public: virtual void AbortRun();
abort the run

3.4.4.2.  Customizing the Event Loop

In G4RunManager the event loop is handled by the virtual method DoEventLoop(). This method is implemented by a for loop consisting of the following steps:

  1. construct a G4Event object and assign to it primary vertex(es) and primary particles. This is done by the virtual GeneratePrimaryEvent() method.

  2. send the G4Event object to G4EventManager for the detector simulation. Hits and trajectories will be associated with the G4Event object as a consequence.

  3. perform bookkeeping for the current G4Event object. This is done by the virtual AnalyzeEvent() method.

DoEventLoop() performs the entire simulation of an event. However, it is often useful to split the above three steps into isolated application programs. If, for example, you wish to examine the effects of changing discriminator thresholds, ADC gate widths and/or trigger conditions on simulated events, much time can be saved by performing steps 1 and 2 in one program and step 3 in another. The first program need only generate the hit/trajectory information once and store it, perhaps in a database. The second program could then retrieve the stored G4Event objects and perform the digitization (analysis) using the above threshold, gate and trigger settings. These settings could then be changed and the digitization program re-run without re-generating the G4Events.

3.4.4.3.  Changing the Detector Geometry

The detector geometry defined in your G4VUserDetectorConstruction concrete class can be changed during a run break (between two runs). Two different cases are considered.

The first is the case in which you want to delete the entire structure of your old geometry and build up a completely new set of volumes. For this case, you need to set the new world physical volume pointer to the RunManager. Thus, you should proceed in the following way.

  G4RunManager* runManager = G4RunManager::GetRunManager();
  runManager->DefineWorldVolume( newWorldPhys );

Presumably this case is rather rare. The second case is more frequent for the user.

The second case is the following. Suppose you want to move and/or rotate a particular piece of your detector component. This case can easily happen for a beam test of your detector. It is obvious for this case that you need not change the world volume. Rather, it should be said that your world volume (experimental hall for your beam test) should be big enough for moving/rotating your test detector. For this case, you can still use all of your detector geometries, and just use a Set method of a particular physical volume to update the transformation vector as you want. Thus, you don't need to re-set your world volume pointer to RunManager.

If you want to change your geometry for every run, you can implement it in the BeginOfRunAction() method of G4UserRunAction class, which will be invoked at the beginning of each run, or, derive the RunInitialization() method. Please note that, for both of the above mentioned cases, you need to let RunManager know "the geometry needs to be closed again". Thus, you need to invoke

  runManager->GeometryHasBeenModified();

before proceeding to the next run. An example of changing geometry is given in a Geant4 tutorial in Geant4 Training kit #2.

3.4.4.4.  Switch physics processes

In the InitializePhysics() method, G4VUserPhysicsList::Construct is invoked in order to define particles and physics processes in your application. Basically, you can not add nor remove any particles during execution, because particles are static objects in Geant4 (see Section 2.4 and Section 5.3 for details). In addition, it is very difficult to add and/or remove physics processes during execution, because registration procedures are very complex, except for experts (see Section 2.5 and Section 5.2). This is why the initializePhysics() method is assumed to be invoked at once in Geant4 kernel initialization.

However, you can switch on/off physics processes defined in your G4VUserPhysicsList concrete class and also change parameters in physics processes during the run break.

You can use ActivateProcess() and InActivateProcess() methods of G4ProcessManager anywhere outside the event loop to switch on/off some process. You should be very careful to switch on/off processes inside the event loop, though it is not prohibited to use these methods even in the EventProc state.

It is a likely case to change cut-off values in a run. You can change defaultCutValue in G4VUserPhysicsList during the Idle state. In this case, all cross section tables need to be recalculated before the event loop. You should use the CutOffHasBeenModified() method when you change cut-off values so that the SetCuts method of your PhysicsList concrete class will be invoked.