Download Geant4 User`s Guide for Application Developers

Transcript
Chapter 2.
Getting Started with Geant4
- Running a Simple Example
2.1. How to Define the main() Program
2.1.1. A Sample main() Method
The contents of main() will vary according to the needs of a given simulation application and therefore must
be supplied by the user. The Geant4 toolkit does not provide a main() method, but a sample is provided here
as a guide to the beginning user. Example 2.1 is the simplest example of main() required to build a simulation
program.
Example 2.1. Simplest example of main()
#include "G4RunManager.hh"
#include "G4UImanager.hh"
#include "ExN01DetectorConstruction.hh"
#include "ExN01PhysicsList.hh"
#include "ExN01PrimaryGeneratorAction.hh"
int main()
{
// construct the default run manager
G4RunManager* runManager = new G4RunManager;
// set mandatory initialization classes
runManager->SetUserInitialization(new ExN01DetectorConstruction);
runManager->SetUserInitialization(new ExN01PhysicsList);
// set mandatory user action class
runManager->SetUserAction(new ExN01PrimaryGeneratorAction);
// initialize G4 kernel
runManager->initialize();
// get the pointer to the UI manager and set verbosities
G4UImanager* UI = G4UImanager::GetUIpointer();
UI->ApplyCommand("/run/verbose 1");
UI->ApplyCommand("/event/verbose 1");
UI->ApplyCommand("/tracking/verbose 1");
// start a run
int numberOfEvent = 3;
runManager->BeamOn(numberOfEvent);
// job termination
delete runManager;
return 0;
}
The main() method is implemented by two toolkit classes, G4RunManager and G4UImanager, and three classes, ExN01DetectorConstruction, ExN01PhysicsList and ExN01PrimaryGeneratorAction, which are derived from
toolkit classes. Each of these are explained in the following sections.
2.1.2. G4RunManager
The first thing main() must do is create an instance of the G4RunManager class. This is the only manager class
in the Geant4 kernel which should be explicitly constructed in the user's main(). It controls the flow of the
program and manages the event loop(s) within a run. When G4RunManager is created, the other major manager
classes are also created. They are deleted automatically when G4RunManager is deleted. The run manager is also
responsible for managing initialization procedures, including methods in the user initialization classes. Through
these the run manager must be given all the information necessary to build and run the simulation, including
2