Code documentation of the user action part of the application

This section contains the code documentation of all the user actions of the application developed during the course.

The mandatory interface implementations

This is the mandatory action initialization (that includes the construction and registration of the primary generator action). An object form the action initialization implementation class must be registered in the run manager inside the main (e.g. main() in yourMainApplication) of each application.

class YourActionInitialization : public G4VUserActionInitialization

Implementation of the Geant4 G4VUserActionInitialization mandatory interface for this user application.

Author

M. Novak

Date

December 2019

All user actions (i.e. at least your implementation of the G4VUserPrimaryGeneratorAction ,that is YourPrimaryGeneratorAction in our case, but all optional actions) objects must be constructed and registered in the G4RunManager through a single user action initialization object from this class.

The construction and registration of all user action objects must be implemented in the Build() interface (pure virtual) method that is invoked by all worker threads before starting the run. The additional BuildForMaster() virtual method of the base class is invoked by only the master thread. No other user actions than implementation of the G4UserRunAction should be constructed here!

According to the above, we construct and register only YourRunAction object in the BuildForMaster() interface method while all the user actions are constructed and created in the Build() method. This is because, the master run action object YourRunAction::GenerateRun() method is invoked by the master at the beginning of the run to create its own YourRun object. The same is done for each workers. Hoever, unlike the individual worker YourRun objects, that are used during the simulatio to collect thread-local infomation, the single master YourRun object is untouched till the end of the run. It will be used only to collect/merge the individual thread-local run infomation into one global YourRun obejct after all worker finished the simulation.

Public Functions

void Build() const override

(Pure) Virtual method to be implemented by the user to instantiate User Action class objects.

Each application must implemnted this method. It is invoked by all workers at the beginning of the run to instantiate all (worker) thread-local user action objects. At least the construction and registration of the primary generation action must be implemented in this method.

void BuildForMaster() const override

Virtual method to be implemented by the user to instantiate User RunAction.

This method is invoked only by the master and only your implementation of the G4VUserRunAction should be constructed and registered here (see the class description why).

class YourPrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction

Implementation of the Geant4 G4VUserPrimaryGeneratorAction mandatory interface for this user application.

Author

M. Novak

Date

December 2019

Generation of primary particles of the application must be implemented in the GeneratePrimaries() interface. This interface method is invoked by the G4RunManager::GenerateEvent() whenever a new event is needed.

The construction and registration of the primary generator interface of the application is done in the implementation of the mandatory G4VUserActionInitialization (YourActionInitialization).

An instance of this primary generator object must be created and set in the implementation of the G4VUserActionInitialization::Build() interface method. This is done in YourActionInitialization::Build() in this application.

Note

each worker thread has their own object form this primary generator since YourActionInitialization::Build() interface method is invoked by each thread.

Note

we keep the pointer to the detector object in order to be able to set the primary generator gun position dynamically. This can be done anytime by invoking the UpdatePosition() method.

Public Functions

void GeneratePrimaries(G4Event *evt) override

It’s interface method to generate primary particles.

(Pure) virtual method to generata primary events. This method will be invoked from the G4RunManager whenever a new event is required.

Parameters

[in:out] – evt Pointer to the G4Event object to be filled with primaries.

void UpdatePosition()

Public method to set the position of the particle gun.

The detector might be resized by the user between its construction and the start of the run. This method can be invoked (form YourRunAction::BeginOfRunAction()) at the begin of the run in order to ensure the proper position of the gun.

Some of the optional interface implementations

class YourRun : public G4Run

Implementation of Geant4 G4Run for this user application.

Author

M. Novak

Date

December 2019

The run of your application usually encapsulates all infomation that you would like to collect during the simulation. Each thread will have their own object form this run class that is generated before the simulation by the individual threads itself simply calling the G4RunAction::GenerateRun() (i.e. YourRunAction::GenerateRun()) method of the G4RunAction (i.e. YourRunAction) object that was constructed and registered for the given thread (in YourActionInitialization::Build() or YourActionInitialization::BuildForMaster() methods for the workers or for the master respecively).

While the run objects of the worker threads are used during the simulation to collect thread-local data, the run object generated by the master, is not used during the simulation. This latter is used only at the end of the run and only to collect all thread-local run data into one gloabl run object. This is done, by calling the Merge() method of this class that should contain the implementation how run data can be added/merged.

Note

for this application we will implement a simple histogram class. The only reason why we do this is to demonstrait the good practice regarding the implementation of objects, that are used inside the run object, shoudl follow (e.g. easily "mergable").

Public Functions

void Merge(const G4Run *run) override

Virtual method to be implemented to define the way of merging the underlying (thread local) Run global data structures into one global instance.

This method will be invoked by the master on its own run object (YourRun) on each of the worker, i.e. thread local, run (YourRun) objects passing by their pointer as the input argument.

Parameters

run[in] Pointer to a run (YourRun) obejct that needs to be merged to this run object.

class YourRunAction : public G4UserRunAction

Implementation of the Geant4 G4UserRunAction optional interface for this user application.

Author

M. Novak

Date

December 2019

This interface gives the possibility for interacting with the simulation before the simulation of the first (BeginOfRunAction()) and after the last (EndOfRunAction()) event. The additional GenerateRun() virtual method can be used to generate YourRun object, i.e. your implementation of G4Run, both for the master and for the individual worker threads.

Note

this is the only user action that can be constructed and registered in YourActionInitialization::BuildForMaster().

Public Functions

G4Run *GenerateRun() override

Virtual method to generate YourRun for each threads.

void BeginOfRunAction(const G4Run *run) override

Virtial method that is called by the RunManager before the first event starts.

This is the point where we usually perfrom some final actions needed to be done before the simulation, constructing some run global objects (e.g. for analysis) or we will ensure here that the primary generator gun position is correct.

Parameters

run[in] Pointer to the G4Run (YourRun) object that has been generated for this thread.

void EndOfRunAction(const G4Run *run) override

Virtial method that is called by the RunManager after the last event simulation is completed.

This is the point where we usually perfrom some actions needed to be done when the simulation is completed, performing final computation and writing the simulation results usually happens here (we do this here by calling the corresponding method of YourRun).

Parameters

run[in] Pointer to the G4Run (YourRun) object that has been generated for this thread.

class YourEventAction : public G4UserEventAction

Implementation of the Geant4 G4UserEventAction optional interface for this user application.

Author

M. Novak

Date

December 2019

This interface gives the possibility for interacting with the simulation before (BeginOfEventAction()) and after (EndOfEventAction()) the simulation of each event.

Public Functions

void BeginOfEventAction(const G4Event *evt) override

Virtial method that is called by the RunManager before the start of each event.

This is the point where we usually reset some local variables that are used to accumulate information during the simnulation of a given event.

Parameters

evt[in] Pointer to the G4Event object that will be simulated now.

void EndOfEventAction(const G4Event *evt) override

Virtial method that is called by the RunManager after the end of each event.

This is the point where we usually propagate to upper level (i.e. usualy to the run (YourRun) obejct) local infomation, that has been collected during the simnulation of a given event.

Parameters

evt[in] Pointer to the G4Event object that simulated has just been completed.

class YourSteppingAction : public G4UserSteppingAction

Implementation of the Geant4 G4UserSteppingAction optional interface for this user application.

Author

M. Novak

Date

December 2019

This interface gives the possibility for interacting with the simulation after the complition of the individual simulation steps in its UserSteppingAction() method.

Public Functions

void UserSteppingAction(const G4Step *step) override

Interface metho dthat is called after each simulation steps.

We do all the fine grane infomation collection in this method that infomation are usually propagated to an auuper elevel, e.g. YourEventAction.

Warning

You need to be careful here! Since this method is invoked after each individual simulation step, this is a very perfomance critical point.

Parameters

step[in] Pointer to the G4Step object that has just been completed.