Geant4 Quickstart Guide
Step 3: Writing the main-method
The previous two sections demonstrated how to create the experiment setup and how to create the particles
to be simulated within the setup. This section will demonstrate how to put everything together to create
a Geant4 application.
A Geant4 simulation is handled by the Geant4 runmanager. The user initializes the runmanager by passing the
experimental setup and particle source objects to the runmanager, as well as specifying the list of
physical processes to be simulated. These are the three mandatory user initialization classes. The creation
of a robust physics list can be complicated and Geant4 provides a number of reference physics lists which
can be used straight out of the box.
At first an instance of the Geant4 run manager is obtained:
int main(int argc,char** argv)
{
//Get instance of runmanager
G4RunManager * runManager = new G4RunManager;
Next, the user initialization classes must be passed to runManager.
The BasicPrimaryGeneratorAction classes are those discussed in steps 1 and 2
of this quickstart guide. The physics list is a reference physics list provided by Geant4. Note that the
physics list must always be registered before the primary generator action can be instantiated since otherwise
the relevant particle definitions may not be available.
// Physics list
G4VModularPhysicsList* physicsList = new QBBC;
runManager->SetUserInitialization(physicsList);
// Primary generator action
runManager->SetUserAction(new BasicPrimaryGeneratorAction());
Now all the required components have been registered with runManager: we have
told it about the experimental setup in BasicDetectorConstruction, the physics to be simulated is given by the QBBC reference physics list and the method for generating the particle to be simulated is defined in BasicPrimaryGeneratorAction.
What remains is to initialize the Geant4 kernel, and tell it to run a single event as described by BasicPrimaryGeneratorAction.
runManager->Initialize();
//Cause the run manager to generate a single event using the
//primary generator action registered above.
runManager->BeamOn(1);
//After the run is complete, free up the memory used by run
//manager and return
delete runManager;
return 0;
This completes the main-method of the application. The complete main method is given below.
int main(int argc,char** argv){
G4RunManager * runManager = new G4RunManager;
// Detector construction
runManager->SetUserInitialization(new BasicDetectorConstruction());
// Physics list
G4VModularPhysicsList* physicsList = new QBBC;
runManager->SetUserInitialization(physicsList);
// Primary generator action
runManager->SetUserAction(new BasicPrimaryGeneratorAction());
// Initialize G4 kernel
runManager->Initialize();
//Cause the run manager to generate a single event using the
//primary generator action registered above.
runManager->BeamOn(1);
//After the run is complete, free up the memory used by run
//manager and return
delete runManager;
return 0;
}
Go to page 4: Compiling and running the application.
page 1 / page 2 / page 3 / page 4
|