Introduction to GEANT4

From UCL HEP PBT Wiki

(Difference between revisions)
Jump to: navigation, search
m
m
 
(23 intermediate revisions not shown)
Line 1: Line 1:
== <span style="color:#000080"> Introduction </span> ==
== <span style="color:#000080"> Introduction </span> ==
-
 
GEANT4 is a software toolkit based on C++. In your code you have to define:  
GEANT4 is a software toolkit based on C++. In your code you have to define:  
Line 6: Line 5:
* Your experimental setup - geometry, materials and primary particles.
* Your experimental setup - geometry, materials and primary particles.
* Which physics process you are interested in.  
* Which physics process you are interested in.  
-
* You may take actions during the simulation - inspect and store results.  
+
* Take actions during the simulation to inspect and store results.  
The interaction with GEANT4 is done via base classes.     
The interaction with GEANT4 is done via base classes.     
Line 26: Line 25:
; Manager class  
; Manager class  
-
* <span style="color:#ff0000"> G4RunManager </span>: Manages the simulation process;
+
* <span style="color:#ff0000"> G4RunManager </span>: Manages processing the run;
 +
 
 +
; Useful terminology
 +
 
 +
* <span style="color:#ff0000"> Step </span>: the smallest unit of Geant4 simulation, a particle is transported from one point to another
 +
* <span style="color:#ff0000"> Trajectory and TrajectoryPoint </span>: collection of steps and step points
 +
* <span style="color:#ff0000"> Process </span>: physics along the step
 +
* <span style="color:#ff0000"> Track </span>: a snapshot of a particle at some point along its path (not the same as trajectory)
 +
* <span style="color:#ff0000"> Event </span>: a collection of information from tracks and trajectories
 +
* <span style="color:#ff0000"> Run </span>: a collection of events
-
 
 
== <span style="color:#000080"> The function main() </span> ==
== <span style="color:#000080"> The function main() </span> ==
-
 
The function <span style="color:#ff0000"> main() </span> defines the skeleton of your simulation code. Inside the function you instantiate <span style="color:#ff0000"> G4RunManager </span> and notify it of your mandatory and optional classes. This is example <span style="color:#ff0000"> main() </span> function, where <span style="color:#ff0000"> MyDetectorConstruction </span>, <span style="color:#ff0000"> MyPhysicsList </span>, <span style="color:#ff0000"> MyPrimaryGeneratorAction </span>, <span style="color:#ff0000"> MyEventAction </span> and <span style="color:#ff0000"> MyRunAction </span> are derived classes from the GEANT4 base classes:
The function <span style="color:#ff0000"> main() </span> defines the skeleton of your simulation code. Inside the function you instantiate <span style="color:#ff0000"> G4RunManager </span> and notify it of your mandatory and optional classes. This is example <span style="color:#ff0000"> main() </span> function, where <span style="color:#ff0000"> MyDetectorConstruction </span>, <span style="color:#ff0000"> MyPhysicsList </span>, <span style="color:#ff0000"> MyPrimaryGeneratorAction </span>, <span style="color:#ff0000"> MyEventAction </span> and <span style="color:#ff0000"> MyRunAction </span> are derived classes from the GEANT4 base classes:
-
 
+
<pre style="color: #800000; background-color: #dcdcdc">
-
<span style="color:#800000"> int main() { </span>
+
int main() {  
    
    
-
<span style="color:#800000"> // Run manager construction </span>
+
// Run manager construction  
-
<span style="color:#800000"> G4RunManager* runManager = new G4RunManager; </span>
+
G4RunManager* runManager = new G4RunManager;  
-
<span style="color:#800000"> // mandatory user initialization classes </span>
+
// mandatory user initialization classes
-
<span style="color:#800000"> runManager->SetUserInitialization(new MyDetectorConstruction); </span>
+
runManager->SetUserInitialization(new MyDetectorConstruction);  
-
<span style="color:#800000"> runManager->SetUserInitialization(new MyPhysicsList); </span>
+
runManager->SetUserInitialization(new MyPhysicsList);
-
<span style="color:#800000"> // mandatory user action classes  </span>
+
// mandatory user action classes   
-
<span style="color:#800000"> runManager->SetUserAction(new MyPrimaryGeneratorAction); </span>
+
runManager->SetUserAction(new MyPrimaryGeneratorAction);  
-
<span style="color:#800000"> // optional user action classes </span> 
+
// optional user action classes  
-
<span style="color:#800000"> runManager->SetUserAction(new MyEventAction); </span>
+
runManager->SetUserAction(new MyEventAction);
-
 
+
-
<span style="color:#800000"> runManager->SetUserAction(new MyRunAction); ... } </span>
+
 +
runManager->SetUserAction(new MyRunAction); ... }
 +
</pre>
   
   
== <span style="color:#000080"> Experimental setup </span> ==
== <span style="color:#000080"> Experimental setup </span> ==
-
 
You derive your own class from <span style="color:#ff0000"> G4VUserDetectorConstruction </span> base class. In the derived class you will:  
You derive your own class from <span style="color:#ff0000"> G4VUserDetectorConstruction </span> base class. In the derived class you will:  
Line 70: Line 75:
* Place volumes of your detector geometry using <span style="color:#ff0000"> G4VPhysical Volume </span>  
* Place volumes of your detector geometry using <span style="color:#ff0000"> G4VPhysical Volume </span>  
 +
<u> Simple example of class <span style="color:#ff0000"> MyDetectorConstruction </span> </u>:
-
<u> Simple example of <span style="color:#ff0000"> MyDetectorConstruction </span> </u>:  
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
class MyDetectorConstruction:public G4VUserDetectorConstruction {
-
 
+
public:
-
<span style="color:#800000"> class MyDetectorConstruction:public G4VUserDetectorConstruction { </span>
+
-
 
+
-
<span style="color:#800000"> public: </span>
+
      
      
-
<span style="color:#800000"> MyDetectorConstruction(); </span>
+
MyDetectorConstruction();
-
<span style="color:#800000"> ~MyDetectorConstruction(); </span>
+
~MyDetectorConstruction();  
      
      
-
<span style="color:#800000"> virtual G4VPhysicalVolume* Construct(); </span>
+
virtual G4VPhysicalVolume* Construct();  
      
      
-
<span style="color:#800000"> private: </span>
+
private:  
    
    
-
<span style="color:#800000"> void  DefineMaterials(); }; </span>  
+
void  DefineMaterials(); };  
-
 
+
</pre>
Now construct the detector. Your detector is always placed in a mother volume called the world volume.   
Now construct the detector. Your detector is always placed in a mother volume called the world volume.   
 +
<pre style="color: #800000; background-color: #dcdcdc">
 +
G4PhysicalVolume* MyDetectorConstruction::Construct() { 
-
<span style="color:#800000"> G4PhysicalVolume* MyDetectorConstruction::Construct() { </span>
+
...
-
 
+
-
<span style="color:#800000"> ... </span>
+
-
<span style="color:#800000"> // World volume </span>
+
// World volume  
-
<span style="color:#800000"> G4VSolid* pWorld = new G4Box("World",5*m,5*m,5*m); </span>
+
G4VSolid* pWorld = new G4Box("World",5*m,5*m,5*m);  
                                          
                                          
-
<span style="color:#800000"> G4LogicalVolume* pWorldLog = new G4LogicalVolume(pWorld,vacuum, "World"); </span> 
+
G4LogicalVolume* pWorldLog = new G4LogicalVolume(pWorld,vacuum, "World");  
                                                                            
                                                                            
-
<span style="color:#800000"> G4VPhysicalVolume* pWorldPhys = new G4PVPlacement(0,G4ThreeVector(),pWorldLog,"World",0,false,0); </span>
+
G4VPhysicalVolume* pWorldPhys = new G4PVPlacement(0,G4ThreeVector(),pWorldLog,"World",0,false,0);
 +
// Water box
-
<span style="color:#800000"> // Water box </span>
+
G4VSolid* pBoxSolid = new G4Box(“WaterBox”, 1.*m, 2.*m, 3.*m); 
-
<span style="color:#800000"> G4VSolid* pBoxSolid = new G4Box(“WaterBox”, 1.*m, 2.*m, 3.*m); </span>
+
G4LogicalVolume* pBoxLog = new G4LogicalVolume( pBoxSolid, water, “WaterBox”);  
-
<span style="color:#800000"> G4LogicalVolume* pBoxLog = new G4LogicalVolume( pBoxSolid, water, “WaterBox”); </span>
+
G4VPhysicalVolume* aBoxPhys = new G4PVPlacement( pRotation, G4ThreeVector(posX, posY, posZ), pBoxLog,  
-
 
+
-
<span style="color:#800000"> G4VPhysicalVolume* aBoxPhys = new G4PVPlacement( pRotation, G4ThreeVector(posX, posY, posZ), pBoxLog, “WaterBox”, pWorldLog, false, copyNo); ... } </span>
+
 +
“WaterBox”, pWorldLog, false, copyNo); ... }
 +
</pre>
The elements and materials are defined using classes <span style="color:#ff0000"> G4Element </span> and <span style="color:#ff0000"> G4Material </span>. For example water, hydrogen and oxygen are defined as:
The elements and materials are defined using classes <span style="color:#ff0000"> G4Element </span> and <span style="color:#ff0000"> G4Material </span>. For example water, hydrogen and oxygen are defined as:
 +
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyDetectorConstruction::DefineMaterials() {
-
<span style="color:#800000"> void MyDetectorConstruction::DefineMaterials() { </span>
+
...
-
<span style="color:#800000"> ... </span>
+
G4Element* H = new G4Element("Hydrogen","H",z=1.,a= 1.01*g/mole);  
-
 
+
-
<span style="color:#800000"> G4Element* H = new G4Element("Hydrogen","H",z=1.,a= 1.01*g/mole); </span>
+
   
   
-
<span style="color:#800000"> G4Element* O = new G4Element("Oxygen","O",z=8.,a=16.00*g/mole); </span>
+
G4Element* O = new G4Element("Oxygen","O",z=8.,a=16.00*g/mole);  
-
<span style="color:#800000"> density = 1.000*g/cm3; </span>
+
density = 1.000*g/cm3;  
-
<span style="color:#800000"> G4Material* H2O = new G4Material("Water",density,ncomp=2); </span>
+
G4Material* water = new G4Material("Water",density,ncomp=2);
-
<span style="color:#800000"> H2O->AddElement(H, natoms=2); </span>
+
water->AddElement(H, natoms=2);
-
 
+
-
<span style="color:#800000"> H2O->AddElement(O, natoms=1); ...} </span>
+
 +
water->AddElement(O, natoms=1); ...} 
 +
</pre>
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__detectorConstruction.html Here] you can find more examples of DetectorConstruction classes.
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__detectorConstruction.html Here] you can find more examples of DetectorConstruction classes.
-
 
== <span style="color:#000080"> Physics processes </span> ==  
== <span style="color:#000080"> Physics processes </span> ==  
-
 
You can build your own physics list or chose from already built physics lists. To build your own physics lists, you can use two base physics list classes: <span style="color:#ff0000"> G4VUserPhysicsList </span> and <span style="color:#ff0000"> G4ModularPhysicsList </span>. The class <span style="color:#ff0000"> G4VUserPhysicsList </span> is used for simple physics lists while <span style="color:#ff0000"> G4ModularPhysicsList </span> is used to build more complex physics lists. There exist also already built pre-packaged physics lists.
You can build your own physics list or chose from already built physics lists. To build your own physics lists, you can use two base physics list classes: <span style="color:#ff0000"> G4VUserPhysicsList </span> and <span style="color:#ff0000"> G4ModularPhysicsList </span>. The class <span style="color:#ff0000"> G4VUserPhysicsList </span> is used for simple physics lists while <span style="color:#ff0000"> G4ModularPhysicsList </span> is used to build more complex physics lists. There exist also already built pre-packaged physics lists.
-
 
=== <span style="color:#000080"> Simple physics lists </span> ===   
=== <span style="color:#000080"> Simple physics lists </span> ===   
-
 
If the particles in your simulation undergo a descrete number of physics processes you can use the class <span style="color:#ff0000"> G4VUserPhysicsList </span> to define them. This class has three methods:
If the particles in your simulation undergo a descrete number of physics processes you can use the class <span style="color:#ff0000"> G4VUserPhysicsList </span> to define them. This class has three methods:
* ConstructParticles() : Define all necessary particles;   
* ConstructParticles() : Define all necessary particles;   
-
* ConstructProcesses() : Define all necessary processes and assign them to proper particles;
+
* ConstructProcesses() : Define all necessary processes and assign them to corresponding particles;
* SetCuts() : Define production thresholds in terms of range;  
* SetCuts() : Define production thresholds in terms of range;  
 +
<u> Simple example of class <span style="color:#ff0000"> MyPhysicsList </span> </u>:
-
<u> Simple example of <span style="color:#ff0000"> MyPhysicsList </span> </u>:  
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
class MyPhysicsList:public G4VUserPhysicsList() {
 +
public:
-
<span style="color:#800000"> class MyPhysicsList:public G4VUserPhysicsList() { </span>
+
MyPhysicsList();
-
<span style="color:#800000"> public: </span>
+
~MyPhysicsList();
-
<span style="color:#800000"> MyPhysicsList(); </span>
+
void ConstructParticle();  
-
<span style="color:#800000"> ~MyPhysicsList(); </span>
+
void ConstructProcess();  
-
<span style="color:#800000"> void ConstructParticle(); </span>
+
void SetCuts(); }  
-
 
+
</pre>
-
<span style="color:#800000"> void ConstructProcess(); </span>
+
-
 
+
-
<span style="color:#800000"> void SetCuts(); } </span>
+
   
   
-
 
Now implement the methods ConstructParticle(), ConstructProcess() and SetCuts():
Now implement the methods ConstructParticle(), ConstructProcess() and SetCuts():
-
 
   
   
-
<span style="color:#800000"> void MyPhysicsList::ConstructParticle() { </span>
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyPhysicsList::ConstructParticle() {  
-
<span style="color:#800000"> // Define the particles </span>
+
// Define the particles  
-
<span style="color:#800000"> G4Electron::ElectronDefinition(); </span>
+
G4Electron::ElectronDefinition();  
-
<span style="color:#800000"> G4Positron::PositronDefinition(); </span>
+
G4Positron::PositronDefinition();  
-
<span style="color:#800000"> G4Proton::ProtonDefinition(); </span>
+
G4Proton::ProtonDefinition();  
-
<span style="color:#800000"> G4Neutron::NeutronDefinition(); </span>
+
G4Neutron::NeutronDefinition();  
-
<span style="color:#800000"> G4Gamma::GammaDefinition(); ... } </span>
+
G4Gamma::GammaDefinition(); ... }  
 +
</pre>
-
 
+
GEANT4 provides a variety of physics processes. These processes are decoupled from one another and you can select those which are relevant to your simulation. The processes are grouped in seven categories  
-
GEANT4 provides a variety of physics processes. These processes are decoupled from one another and you can select those which are relevant to your simulation. The processes are grouped in seven categories and their list is available [http://geant4.cern.ch/support/proc_mod_catalog/processes/ here]:
+
<div style="column-count:2;-moz-column-count:2;-webkit-column-count:2">
<div style="column-count:2;-moz-column-count:2;-webkit-column-count:2">
Line 203: Line 203:
</div>
</div>
-
 
+
and their list is available [http://geant4.cern.ch/support/proc_mod_catalog/processes/ here]. For each particle in ConstructParticle() assign all the physics processes you want to consider in your simulation:  
-
For each particle defined in ConstructParticle() assign all the physics processes that you want to
+
-
consider in your simulation:  
+
    
    
 +
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyPhysicsList::ConstructProcess() {
-
<span style="color:#800000"> void MyPhysicsList::ConstructProcess() { </span>
+
AddTransportation(); // Assign transportation process to all particles  
-
 
+
-
<span style="color:#800000"> AddTransportation(); // Assign transportation process to all particles </span>
+
-
 
+
-
<span style="color:#800000"> ConstructEM(); // Electromagnetic processes </span>
+
-
<span style="color:#800000"> ConstructGeneral(); // Other processes } </span>
+
ConstructEM(); // Electromagnetic processes  
 +
ConstructGeneral(); // Other processes }
 +
</pre>
In methods ConstructEM() and ConstructGeneral() assign the physics processes to the corresponding particles:  
In methods ConstructEM() and ConstructGeneral() assign the physics processes to the corresponding particles:  
 +
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyPhysicsList::ConstructEM() {
-
<span style="color:#800000"> void MyPhysicsList::ConstructEM() { </span>
+
aParticleIterator->reset();
-
<span style="color:#800000"> aParticleIterator->reset(); </span>
+
while((*aParticleIterator)()){
-
<span style="color:#800000"> while((*aParticleIterator)()){ </span>
+
G4ParticleDefinition* particle = aParticleIterator->value();  
-
 
+
-
<span style="color:#800000"> G4ParticleDefinition* particle = aParticleIterator->value(); </span>
+
   
   
-
<span style="color:#800000"> G4ProcessManager* pmanager = particle->GetProcessManager(); </span>
+
G4ProcessManager* pmanager = particle->GetProcessManager();  
-
<span style="color:#800000"> G4String particleName = particle->GetParticleName(); </span>
+
G4String particleName = particle->GetParticleName();  
      
      
-
<span style="color:#800000"> if (particleName == "gamma") { </span>
+
if (particleName == "gamma") {  
      
      
-
<span style="color:#800000"> pmanager->AddDiscreteProcess(new G4GammaConversion); ...} </span>  
+
pmanager->AddDiscreteProcess(new G4GammaConversion); ...}  
-
   
+
</pre>
 +
 
 +
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyPhysicsList::ConstructGeneral() {
-
<span style="color:#800000"> void MyPhysicsList::ConstructGeneral() { </span>
+
G4Decay* theDecayProcess = new G4Decay()  
-
<span style="color:#800000"> G4Decay* theDecayProcess = new G4Decay() </span>
+
aParticleIterator->reset();
-
<span style="color:#800000"> aParticleIterator->reset(); </span>
+
while((*aParticleIterator)()) {
-
<span style="color:#800000"> while((*aParticleIterator)()) { </span>
+
G4ParticleDefinition* particle = aParticleIterator->value();  
-
 
+
-
<span style="color:#800000"> G4ParticleDefinition* particle = aParticleIterator->value(); </span>
+
   
   
-
<span style="color:#800000"> G4ProcessManager* pmanager = particle->GetProcessManager(); </span>
+
G4ProcessManager* pmanager = particle->GetProcessManager();  
-
<span style="color:#800000"> if theDecayProcess->IsApplicable(*particle)) { </span>
+
if theDecayProcess->IsApplicable(*particle)) {  
      
      
-
<span style="color:#800000"> pmanager->AddProcess(theDecayProcess); </span>
+
pmanager->AddProcess(theDecayProcess);  
      
      
-
<span style="color:#800000"> pmanager->SetProcessOrdering(theDecayProcess,idxPostStep); </span>
+
pmanager->SetProcessOrdering(theDecayProcess,idxPostStep);
      
      
-
<span style="color:#800000"> pmanager->SetProcessOrdering(theDecayProcess,idxAtRest); }}} </span>
+
pmanager->SetProcessOrdering(theDecayProcess,idxAtRest); }}}  
 +
</pre>
 +
This is the full [http://geant4.cern.ch/support/proc_mod_catalog/particles/ list] of physics processes available for every  particle. Finally, in method SetCuts() you can define cuts on the particles:
-
This is the full [http://geant4.cern.ch/support/proc_mod_catalog/particles/ list] of physics processes every  particle can participate in. Finally, method SetCuts() is defined as:
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyPhysicsList::SetCuts() { </span>
 +
defaultCutValue = 1.0*mm;
-
<span style="color:#800000"> void MyPhysicsList::SetCuts() { </span>
+
SetCutValue(defaultCutValue, "gamma");
-
<span style="color:#800000"> defaultCutValue = 1.0*mm; </span>
+
SetCutValue(defaultCutValue, "e+");  
-
<span style="color:#800000"> SetCutValue(defaultCutValue, "gamma"); </span>
+
SetCutValue(defaultCutValue, "e-");  
-
 
+
</pre>
-
<span style="color:#800000"> SetCutValue(defaultCutValue, "e+"); </span>
+
-
 
+
-
<span style="color:#800000"> SetCutValue(defaultCutValue, "e-"); </span>
+
 +
See the [[Computed tomography]] tutorial to learn more about simple physics lists .
=== <span style="color:#000080"> Detailed physics lists </span> ===  
=== <span style="color:#000080"> Detailed physics lists </span> ===  
 +
If you want to build more realistic physics list you have to use the class <span style="color:#ff0000"> G4VModularPhysicsList </span>. In <span style="color:#ff0000"> G4VModularPhysicsList </span> you can group the physics processes into separate modules which are already pre-build physics list and later chose one of those modules. 
-
If you want to build more realistic physics list you have to use the class <span style="color:#ff0000"> G4VModularPhysicsList </span>. For example, the photon from the example above can undergo compton scattering apart from conversion. In <span style="color:#ff0000"> G4VModularPhysicsList </span> you can group the physics processes into separate modules: EM physics, hadronic physics, decay physics etc.
+
<u> Simple example of class <span style="color:#ff0000"> MyPhysicsList </span> </u>:
-
 
+
-
 
+
-
<u> Simple example of <span style="color:#ff0000"> MyPhysicsList </span> </u>:
+
-
 
+
-
<span style="color:#800000"> class MyPhysicsList:public G4VModularPhysicsList { </span>
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
class MyPhysicsList:public G4VModularPhysicsList {  
-
<span style="color:#800000"> public: </span>
+
public:  
-
<span style="color:#800000"> MyPhysicsList(); </span>
+
MyPhysicsList();  
-
<span style="color:#800000"> ~MyPhysicsList(); </span>
+
~MyPhysicsList();  
-
<span style="color:#800000"> virtual void ConstructParticle(); </span>
+
virtual void ConstructParticle();  
      
      
-
<span style="color:#800000"> virtual void SetCuts(); </span>
+
virtual void SetCuts();  
          
          
-
<span style="color:#800000"> void AddPhysicsList(const G4String& name); </span>
+
void AddPhysicsList(const G4String& name);  
-
<span style="color:#800000"> virtual void ConstructProcess(); </span>
+
virtual void ConstructProcess();  
        
        
-
<span style="color:#800000"> private: </span>
+
private:  
      
      
-
<span style="color:#800000"> G4String                            fEmName; </span>
+
G4String                            fEmName;  
-
<span style="color:#800000"> G4VPhysicsConstructor*              fEmPhysicsList; </span>
+
G4VPhysicsConstructor*              fEmPhysicsList;  
-
<span style="color:#800000"> G4VPhysicsConstructor*              fDecPhysicsList; </span>
+
G4VPhysicsConstructor*              fDecPhysicsList;  
-
<span style="color:#800000"> std::vector<G4VPhysicsConstructor*>  fHadronPhysicsList; }; </span>
+
std::vector<G4VPhysicsConstructor*>  fHadronPhysicsList; };
 +
</pre>
 +
Now, build the physics lists:
-
Now we can build the physics lists:
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
void MyPhysicsList::AddPhysicsList(const G4String& name) {
 +
...
-
<span style="color:#800000"> void MyPhysicsList::AddPhysicsList(const G4String& name) { </span>
+
if (name == "emstandard_opt3") {  
-
 
+
-
<span style="color:#800000"> ... </span>
+
-
 
+
-
<span style="color:#800000"> if (name == "emstandard_opt3") { </span>
+
      
      
-
<span style="color:#800000"> fEmName = name; </span> 
+
fEmName = name;  
      
      
-
<span style="color:#800000"> delete fEmPhysicsList; </span>
+
delete fEmPhysicsList;  
      
      
-
<span style="color:#800000"> fEmPhysicsList = new G4EmStandardPhysics_option3(); </span> 
+
fEmPhysicsList = new G4EmStandardPhysics_option3();  
-
<span style="color:#800000"> } else if (name == "emlivermore") { </span>
+
} else if (name == "emlivermore") {  
    
    
-
<span style="color:#800000"> fEmName = name; </span>
+
fEmName = name;  
      
      
-
<span style="color:#800000"> delete fEmPhysicsList; </span>
+
delete fEmPhysicsList;  
      
      
-
<span style="color:#800000"> fEmPhysicsList = new G4EmLivermorePhysics(); </span>
+
fEmPhysicsList = new G4EmLivermorePhysics();  
-
<span style="color:#800000"> } else if (name == "empenelope") { </span>
+
} else if (name == "empenelope") {  
      
      
-
<span style="color:#800000"> fEmName = name; </span>
+
fEmName = name;  
      
      
-
<span style="color:#800000"> delete fEmPhysicsList; </span>
+
delete fEmPhysicsList;  
      
      
-
<span style="color:#800000"> fEmPhysicsList = new G4EmPenelopePhysics(); </span>
+
fEmPhysicsList = new G4EmPenelopePhysics();  
-
<span style="color:#800000"> } else if (name == "HElastic") { </span>
+
} else if (name == "HElastic") {  
      
      
-
<span style="color:#800000"> fHadronPhysicsList.push_back( new G4HadronHElasticPhysics()); </span>
+
fHadronPhysicsList.push_back( new G4HadronHElasticPhysics());  
-
<span style="color:#800000"> } else if (name == "HInelastic") { </span>
+
} else if (name == "HInelastic") {  
    
    
-
<span style="color:#800000"> fHadronPhysicsList.push_back(new G4HadronInelasticQBBC()); </span>
+
fHadronPhysicsList.push_back(new G4HadronInelasticQBBC());  
      
      
-
<span style="color:#800000"> } ... } </span>
+
} ... }  
-
 
+
</pre>
and  
and  
-
 
+
<pre style="color: #800000; background-color: #dcdcdc">
-
<span style="color:#800000"> void MyPhysicsList::ConstructProcess() { </span>
+
void MyPhysicsList::ConstructProcess() {  
   
   
-
<span style="color:#800000"> AddTransportation(); // transportation </span>
+
AddTransportation(); // transportation  
    
    
-
<span style="color:#800000"> fEmPhysicsList->ConstructProcess(); // electromagnetic physics list </span>
+
fEmPhysicsList->ConstructProcess(); // electromagnetic physics list  
-
<span style="color:#800000"> fDecPhysicsList->ConstructProcess(); // decay physics list </span>
+
fDecPhysicsList->ConstructProcess(); // decay physics list  
    
    
-
<span style="color:#800000"> for(size_t i=0; i<fHadronPhys.size(); i++) { // hadronic physics lists </span>
+
for(size_t i=0; i<fHadronPhys.size(); i++) { // hadronic physics lists  
    
    
-
<span style="color:#800000"> fHadronPhys[i]->ConstructProcess(); } } </span>
+
fHadronPhys[i]->ConstructProcess(); } }  
 +
</pre>
    
    
 +
See the [[Monoenergetic proton pencil beam]] tutorial to learn more about detailed physics lists.
=== <span style="color:#000080"> Pre-packaged physics lists </span> ===
=== <span style="color:#000080"> Pre-packaged physics lists </span> ===
Some built-in pre-packaged physics lists are available [http://geant4.web.cern.ch/geant4/support/proc_mod_catalog/physics_lists/referencePL.shtml here]. You can use them as a starting point of your simulation.     
Some built-in pre-packaged physics lists are available [http://geant4.web.cern.ch/geant4/support/proc_mod_catalog/physics_lists/referencePL.shtml here]. You can use them as a starting point of your simulation.     
-
 
<u> Simple example of <span style="color:#ff0000"> pre-packaged physics list </span> </u>:
<u> Simple example of <span style="color:#ff0000"> pre-packaged physics list </span> </u>:
-
 
In function <span style="color:#ff0000"> main() </span>:  
In function <span style="color:#ff0000"> main() </span>:  
-
<span style="color:#800000"> G4PhysListFactory factory* physListFactory = new G4PhysListFactory(); </span>
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
G4PhysListFactory factory* physListFactory = new G4PhysListFactory();  
-
<span style="color:#800000"> G4VUserPhysicsList* physicsList = physListFactory->GetReferencePhysList(“FTFP_BERT”); </span>
+
G4VUserPhysicsList* physicsList = physListFactory->GetReferencePhysList(“FTFP_BERT”);  
-
 
+
-
<span style="color:#800000"> runManager->SetUserInitialization(physicsList); </span>
+
-
 
+
-
 
+
-
Most of the pre-packaged physics lists use "standard" electromagnetic physics processes. The "standard" EM processes are defined with classes: G4EmStandardPhysics, G4EmStandardPhysics_option1, G4EmStandardPhysics_option2 and G4EmStandardPhysics_option3. If you want to use "low energy" electromagnetic physics processes (defined with classes G4EmLivermorePhysics, G4EmLivermorePolarizedPhysics, G4EmPenelopePhysics and G4EmDNAPhysics) in the pre-packaged physics list you have to define them in your physics list class (see example ProtonPencilBeam).
+
 +
runManager->SetUserInitialization(physicsList);
 +
</pre>
For example, if you want to simulate clinical proton beam of energy 150 MeV you can use pre-packaged physics list e.g. QGSP_BIC, QGSP_BERT and FTFP_BERT. If you are interested in Bragg curve physics, use a physics list ending in "EMV" or "EMX" e.g. QGSP_BERT_EMV.  
For example, if you want to simulate clinical proton beam of energy 150 MeV you can use pre-packaged physics list e.g. QGSP_BIC, QGSP_BERT and FTFP_BERT. If you are interested in Bragg curve physics, use a physics list ending in "EMV" or "EMX" e.g. QGSP_BERT_EMV.  
   
   
-
 
== <span style="color:#000080"> Generate primary particles </span> ==  
== <span style="color:#000080"> Generate primary particles </span> ==  
-
 
You derive your own class from <span style="color:#ff0000"> G4VUserPrimaryGeneratorAction </span> base class.
You derive your own class from <span style="color:#ff0000"> G4VUserPrimaryGeneratorAction </span> base class.
Line 403: Line 398:
* <span style="color:#ff0000"> G4GeneralParticleSource </span> simulates a beam of particles and the primary vertex is randomly chosen on surface of a given volume with pre-defined energy spectra, spatial and angular distribution.  
* <span style="color:#ff0000"> G4GeneralParticleSource </span> simulates a beam of particles and the primary vertex is randomly chosen on surface of a given volume with pre-defined energy spectra, spatial and angular distribution.  
 +
<u> Simple example of class <span style="color:#ff0000"> MyPrimaryGeneratorAction </span> using particle gun </u>:
-
<u> Simple example of <span style="color:#ff0000"> MyPrimaryGeneratorAction </span> using particle gun </u>:
+
<pre style="color: #800000; background-color: #dcdcdc">
-
 
+
class MyPrimaryGeneratorAction:public G4VUserPrimaryGeneratorAction {  
-
 
+
-
<span style="color:#800000"> class MyPrimaryGeneratorAction:public G4VUserPrimaryGeneratorAction { </span>
+
-
 
+
-
<span style="color:#800000"> public: </span>
+
public:  
-
<span style="color:#800000"> MyPrimaryGeneratorAction( </span>
+
MyPrimaryGeneratorAction(  
-
<span style="color:#800000"> const G4String& particleName = "proton", </span>
+
const G4String& particleName = "proton",  
        
        
-
<span style="color:#800000"> G4double energy = 1.*CLHEP::MeV, </span>
+
G4double energy = 1.*CLHEP::MeV,  
        
        
-
<span style="color:#800000"> G4ThreeVector position= G4ThreeVector(0,0,0), </span>
+
G4ThreeVector position= G4ThreeVector(0,0,0),  
        
        
-
<span style="color:#800000"> G4ThreeVector momentumDirection = G4ThreeVector(0,0,1)); </span>   
+
G4ThreeVector momentumDirection = G4ThreeVector(0,0,1));    
      
      
-
<span style="color:#800000"> ~MyPrimaryGeneratorAction(); </span>
+
~MyPrimaryGeneratorAction();  
-
<span style="color:#800000"> virtual void GeneratePrimaries(G4Event*); </span>
+
virtual void GeneratePrimaries(G4Event*);  
-
<span style="color:#800000"> private: </span>
+
private:  
      
      
-
<span style="color:#800000"> G4ParticleGun*  fParticleGun; }; </span>
+
G4ParticleGun*  fParticleGun; };  
 +
</pre>
 +
<u> Simple example of class <span style="color:#ff0000"> MyPrimaryGeneratorAction </span> using general particle source </u>:
-
<u> Simple example of <span style="color:#ff0000"> MyPrimaryGeneratorAction </span> using general particle source </u>:
+
<pre style="color: #800000; background-color: #dcdcdc">
 +
class MyPrimaryGeneratorAction:public G4VUserPrimaryGeneratorAction {
 +
public:
-
<span style="color:#800000"> class MyPrimaryGeneratorAction:public G4VUserPrimaryGeneratorAction { </span>
+
MyPrimaryGeneratorAction();   
-
<span style="color:#800000"> public: </span>
+
~MyPrimaryGeneratorAction();
-
<span style="color:#800000"> MyPrimaryGeneratorAction(); </span>   
+
virtual void GeneratePrimaries(G4Event*);  
-
<span style="color:#800000"> ~MyPrimaryGeneratorAction(); </span>
 
-
<span style="color:#800000"> virtual void GeneratePrimaries(G4Event*); </span>
+
private:  
 +
static const G4String ParticleName;
-
<span style="color:#800000"> private: </span>
+
static const G4double ParticleEnergy;
-
<span style="color:#800000"> static const G4String ParticleName; </span>
+
G4GeneralParticleSource*  fGeneralParticleSource; };  
 +
</pre>
-
<span style="color:#800000"> static const G4double ParticleEnergy; </span>
+
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__primaryGenerator.html Here] you can find how to implement MyPrimaryGeneratorAction class in your code.
-
<span style="color:#800000"> G4GeneralParticleSource*  fGeneralParticleSource; }; </span>
+
The [[Monoenergetic photon pencil beam]] example uses the '''G4ParticleGun''' class. The [[Proton beam with realistic geometry]] tutorial uses '''G4GeneralParticleSource'''. 
 +
 
 +
== <span style="color:#000080"> Optional user classes </span> ==
 +
=== <span style="color:#000080"> Run </span> ===
-
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__primaryGenerator.html Here] you can find how to implement MyPrimaryGeneratorAction class defined above in your code.
+
You can derive your own class from <span style="color:#ff0000"> G4UserRunAction </span> base class where
-
 
+
you may book results of the run. The run starts with "Beam On" and within a run you can not change the detector geometry and physics processes. The run is represented by class <span style="color:#ff0000"> G4Run </span>. 
 +
 
 +
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__userActions.html Here] you can find examples of user defined RunAction classes.
-
== <span style="color:#000080"> Optional user classes </span> ==
+
=== <span style="color:#000080"> Event </span> ===
 +
 
 +
Event is the basic GEANT4 unit. At the beginning of an event are generated primary tracks which are pushed into a stack. The tracks from the stack are analyzed one by one. The primary tracks might lead to secondary tracks which are also pushed into the stack. When the stack becomes empty the processing of the event is over. The event is represented by class <span style="color:#ff0000"> G4Event </span> which gives hits and trajectory collections as output.   
 +
 
 +
You can derive your own class from <span style="color:#ff0000"> G4UserEventAction </span> base class where
 +
you can select and analize data.
 +
 
 +
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__userActions.html Here] you can find examples of user defined EventAction classes.
 +
 
 +
=== <span style="color:#000080"> Track </span> ===
 +
 
 +
Track is a snapshot of a particle and it is represented by <span style="color:#ff0000"> G4Track </span> class. At the end of the event the track objects do not exist. Tracks are pushed step by step. Moving by one step is called "stepping" and this can be controlled by the <span style="color:#ff0000"> G4UserSteppingAction </span> class (see below). At the end of each step the state of a track can be changed (killed, suspended, postponed).   
 +
 
 +
=== <span style="color:#000080"> Step </span> ===
 +
 
 +
Step is defined by two points, it contains also information about the particle e.g. energy loss on the step. A step is represented by <span style="color:#ff0000"> G4Step </span> and <span style="color:#ff0000"> G4StepPoint </span> classes. <span style="color:#ff0000"> G4UserSteppingAction </span> is optional class where you can kill, suspend, postpone a track.
 +
 
 +
Status is attached to each <span style="color:#ff0000"> G4StepPoint </span> to show how step was determined. You can use ''PostStepPoint'' to get status of current step and ''PreStepPoint'' to get status of previous step. For example to get the "x" coordinate of a step you do the following:
 +
 
 +
<pre style="color: #800000; background-color: #dcdcdc">
 +
G4StepPoint* prePoint  = step->GetPreStepPoint();
 +
 
 +
G4double x1 = prePoint->GetPosition().x();
 +
</pre>
 +
=== <span style="color:#000080"> Trajectory </span> ===
-
[http://geant4.web.cern.ch/geant4/UserDocumentation/Doxygen/examples_doc/html/group__extended__common__userActions.html Here] you can find examples of user defined RunAction and EventAction classes.
+
Trajectories are represented by classes <span style="color:#ff0000"> G4Trajectory </span> and <span style="color:#ff0000"> G4TrajectoryPoint </span>. <span style="color:#ff0000"> G4Trajectory </span> class copies some of the <span style="color:#ff0000"> G4Track </span> class information. <span style="color:#ff0000"> G4TrajectoryPoint </span> is the class which copies some of the <span style="color:#ff0000"> G4Step </span> information.

Latest revision as of 17:00, 4 September 2014

Contents

Introduction

GEANT4 is a software toolkit based on C++. In your code you have to define:

  • Your experimental setup - geometry, materials and primary particles.
  • Which physics process you are interested in.
  • Take actions during the simulation to inspect and store results.

The interaction with GEANT4 is done via base classes.

Mandatory classes
  • G4VUserDetectorConstruction : Describe the experimental setup, geometry and materials;
  • G4VUserPhysicsList : Define particles, physics processes and range cuts;
  • G4VUserPrimaryGeneratorAction : Describe particle source, source dimensions, initial position, energy spectrum, angular distributions;
Optional classes
  • G4UserRunAction : Define and store histograms;
  • G4UserEventAction : Event selection and analysis of simulation data;
  • G4UserStackingAction : Customize priority of tracks;
  • G4UserTrackingAction : Decide whether a trajectory should be stored or not;
  • G4UserSteppingAction : Kill, suspend, postpone a track;
Manager class
  • G4RunManager : Manages processing the run;
Useful terminology
  • Step : the smallest unit of Geant4 simulation, a particle is transported from one point to another
  • Trajectory and TrajectoryPoint : collection of steps and step points
  • Process : physics along the step
  • Track : a snapshot of a particle at some point along its path (not the same as trajectory)
  • Event : a collection of information from tracks and trajectories
  • Run : a collection of events

The function main()

The function main() defines the skeleton of your simulation code. Inside the function you instantiate G4RunManager and notify it of your mandatory and optional classes. This is example main() function, where MyDetectorConstruction , MyPhysicsList , MyPrimaryGeneratorAction , MyEventAction and MyRunAction are derived classes from the GEANT4 base classes:

int main() { 
  
// Run manager construction 

G4RunManager* runManager = new G4RunManager; 


// mandatory user initialization classes

runManager->SetUserInitialization(new MyDetectorConstruction); 

runManager->SetUserInitialization(new MyPhysicsList);


// mandatory user action classes  

runManager->SetUserAction(new MyPrimaryGeneratorAction); 


// optional user action classes 

runManager->SetUserAction(new MyEventAction);  

runManager->SetUserAction(new MyRunAction); ... } 

Experimental setup

You derive your own class from G4VUserDetectorConstruction base class. In the derived class you will:

  • Describe the shape and the size of your detector using G4VSolid
  • Construct materials and electromagnetic fields using G4Logical Volume
  • Place volumes of your detector geometry using G4VPhysical Volume

Simple example of class MyDetectorConstruction :

class MyDetectorConstruction:public G4VUserDetectorConstruction { 

public:  
    
MyDetectorConstruction();

~MyDetectorConstruction(); 
    
virtual G4VPhysicalVolume* Construct(); 

     
private: 
   
void  DefineMaterials(); };    

Now construct the detector. Your detector is always placed in a mother volume called the world volume.

G4PhysicalVolume* MyDetectorConstruction::Construct() {  

...  

// World volume 

G4VSolid* pWorld = new G4Box("World",5*m,5*m,5*m); 
                                         
G4LogicalVolume* pWorldLog = new G4LogicalVolume(pWorld,vacuum, "World");    
                                                                           
G4VPhysicalVolume* pWorldPhys = new G4PVPlacement(0,G4ThreeVector(),pWorldLog,"World",0,false,0);  

// Water box 

G4VSolid* pBoxSolid = new G4Box(“WaterBox”, 1.*m, 2.*m, 3.*m);  

G4LogicalVolume* pBoxLog = new G4LogicalVolume( pBoxSolid, water, “WaterBox”); 

G4VPhysicalVolume* aBoxPhys = new G4PVPlacement( pRotation, G4ThreeVector(posX, posY, posZ), pBoxLog, 

“WaterBox”, pWorldLog, false, copyNo); ... } 

The elements and materials are defined using classes G4Element and G4Material . For example water, hydrogen and oxygen are defined as:

void MyDetectorConstruction::DefineMaterials() { 

... 

G4Element* H = new G4Element("Hydrogen","H",z=1.,a= 1.01*g/mole); 
 
G4Element* O = new G4Element("Oxygen","O",z=8.,a=16.00*g/mole); 


density = 1.000*g/cm3; 

G4Material* water = new G4Material("Water",density,ncomp=2);  

water->AddElement(H, natoms=2);  

water->AddElement(O, natoms=1); ...}  

Here you can find more examples of DetectorConstruction classes.

Physics processes

You can build your own physics list or chose from already built physics lists. To build your own physics lists, you can use two base physics list classes: G4VUserPhysicsList and G4ModularPhysicsList . The class G4VUserPhysicsList is used for simple physics lists while G4ModularPhysicsList is used to build more complex physics lists. There exist also already built pre-packaged physics lists.

Simple physics lists

If the particles in your simulation undergo a descrete number of physics processes you can use the class G4VUserPhysicsList to define them. This class has three methods:

  • ConstructParticles() : Define all necessary particles;
  • ConstructProcesses() : Define all necessary processes and assign them to corresponding particles;
  • SetCuts() : Define production thresholds in terms of range;

Simple example of class MyPhysicsList :

class MyPhysicsList:public G4VUserPhysicsList() { 

public: 

MyPhysicsList(); 

~MyPhysicsList(); 

void ConstructParticle(); 

void ConstructProcess(); 

void SetCuts(); } 

Now implement the methods ConstructParticle(), ConstructProcess() and SetCuts():

void MyPhysicsList::ConstructParticle() { 

// Define the particles 

G4Electron::ElectronDefinition(); 

G4Positron::PositronDefinition(); 

G4Proton::ProtonDefinition(); 

G4Neutron::NeutronDefinition(); 

G4Gamma::GammaDefinition(); ... } 

GEANT4 provides a variety of physics processes. These processes are decoupled from one another and you can select those which are relevant to your simulation. The processes are grouped in seven categories

  • electromagnetic
  • hadronic
  • decay
  • photolepton-hadron
  • optical
  • parameterization
  • transportation

and their list is available here. For each particle in ConstructParticle() assign all the physics processes you want to consider in your simulation:

void MyPhysicsList::ConstructProcess() { 

AddTransportation(); // Assign transportation process to all particles 

ConstructEM(); // Electromagnetic processes 

ConstructGeneral(); // Other processes }

In methods ConstructEM() and ConstructGeneral() assign the physics processes to the corresponding particles:

void MyPhysicsList::ConstructEM() { 

aParticleIterator->reset(); 

while((*aParticleIterator)()){ 

G4ParticleDefinition* particle = aParticleIterator->value(); 
 
G4ProcessManager* pmanager = particle->GetProcessManager(); 

G4String particleName = particle->GetParticleName(); 
     
if (particleName == "gamma") { 
     
pmanager->AddDiscreteProcess(new G4GammaConversion); ...} 
void MyPhysicsList::ConstructGeneral() { 

G4Decay* theDecayProcess = new G4Decay() 

aParticleIterator->reset(); 

while((*aParticleIterator)()) { 

G4ParticleDefinition* particle = aParticleIterator->value(); 
 
G4ProcessManager* pmanager = particle->GetProcessManager(); 

if theDecayProcess->IsApplicable(*particle)) { 
     
pmanager->AddProcess(theDecayProcess); 
     
pmanager->SetProcessOrdering(theDecayProcess,idxPostStep);  
    
pmanager->SetProcessOrdering(theDecayProcess,idxAtRest); }}} 

This is the full list of physics processes available for every particle. Finally, in method SetCuts() you can define cuts on the particles:

void MyPhysicsList::SetCuts() { </span>

defaultCutValue = 1.0*mm; 

SetCutValue(defaultCutValue, "gamma"); 

SetCutValue(defaultCutValue, "e+"); 

SetCutValue(defaultCutValue, "e-"); 

See the Computed tomography tutorial to learn more about simple physics lists .

Detailed physics lists

If you want to build more realistic physics list you have to use the class G4VModularPhysicsList . In G4VModularPhysicsList you can group the physics processes into separate modules which are already pre-build physics list and later chose one of those modules.

Simple example of class MyPhysicsList :

class MyPhysicsList:public G4VModularPhysicsList { 

public: 

MyPhysicsList(); 

~MyPhysicsList(); 

virtual void ConstructParticle(); 
    
virtual void SetCuts(); 
        
void AddPhysicsList(const G4String& name); 

virtual void ConstructProcess(); 

      
private: 
    
G4String                             fEmName; 

G4VPhysicsConstructor*               fEmPhysicsList; 

G4VPhysicsConstructor*               fDecPhysicsList; 

std::vector<G4VPhysicsConstructor*>  fHadronPhysicsList; };

Now, build the physics lists:

void MyPhysicsList::AddPhysicsList(const G4String& name) { 

... 

if (name == "emstandard_opt3") { 
    
fEmName = name;   
    
delete fEmPhysicsList; 
    
fEmPhysicsList = new G4EmStandardPhysics_option3(); 


} else if (name == "emlivermore") { 
   
fEmName = name; 
    
delete fEmPhysicsList; 
    
fEmPhysicsList = new G4EmLivermorePhysics(); 


} else if (name == "empenelope") { 
    
fEmName = name; 
    
delete fEmPhysicsList; 
    
fEmPhysicsList = new G4EmPenelopePhysics(); 


} else if (name == "HElastic") { 
    
fHadronPhysicsList.push_back( new G4HadronHElasticPhysics()); 


} else if (name == "HInelastic") { 
   
fHadronPhysicsList.push_back(new G4HadronInelasticQBBC()); 
    
} ... } 

and

void MyPhysicsList::ConstructProcess() { 
 
AddTransportation(); // transportation 
  
fEmPhysicsList->ConstructProcess(); // electromagnetic physics list 

fDecPhysicsList->ConstructProcess(); // decay physics list 
  
for(size_t i=0; i<fHadronPhys.size(); i++) { // hadronic physics lists 
  
fHadronPhys[i]->ConstructProcess(); } } 

See the Monoenergetic proton pencil beam tutorial to learn more about detailed physics lists.

Pre-packaged physics lists

Some built-in pre-packaged physics lists are available here. You can use them as a starting point of your simulation.

Simple example of pre-packaged physics list :

In function main() :

G4PhysListFactory factory* physListFactory = new G4PhysListFactory(); 

G4VUserPhysicsList* physicsList = physListFactory->GetReferencePhysList(“FTFP_BERT”); 

runManager->SetUserInitialization(physicsList); 

For example, if you want to simulate clinical proton beam of energy 150 MeV you can use pre-packaged physics list e.g. QGSP_BIC, QGSP_BERT and FTFP_BERT. If you are interested in Bragg curve physics, use a physics list ending in "EMV" or "EMX" e.g. QGSP_BERT_EMV.

Generate primary particles

You derive your own class from G4VUserPrimaryGeneratorAction base class. Actual generation of primary particles is done via classes G4ParticleGun and G4GeneralParticleSource .

  • G4ParticleGun is used to simulate a beam of particles. It shoots a primary particle of a certain energy and direction from a given point at a given time.
  • G4GeneralParticleSource simulates a beam of particles and the primary vertex is randomly chosen on surface of a given volume with pre-defined energy spectra, spatial and angular distribution.

Simple example of class MyPrimaryGeneratorAction using particle gun :

class MyPrimaryGeneratorAction:public G4VUserPrimaryGeneratorAction { 

public: 

MyPrimaryGeneratorAction( 

const G4String& particleName = "proton", 
      
G4double energy = 1.*CLHEP::MeV, 
      
G4ThreeVector position= G4ThreeVector(0,0,0), 
      
G4ThreeVector momentumDirection = G4ThreeVector(0,0,1));     
    
~MyPrimaryGeneratorAction(); 

virtual void GeneratePrimaries(G4Event*); 


private: 
    
G4ParticleGun*  fParticleGun; }; 

Simple example of class MyPrimaryGeneratorAction using general particle source :

class MyPrimaryGeneratorAction:public G4VUserPrimaryGeneratorAction { 

public: 

MyPrimaryGeneratorAction();     

~MyPrimaryGeneratorAction(); 

virtual void GeneratePrimaries(G4Event*); 


private: 

static const G4String ParticleName; 

static const G4double ParticleEnergy; 

G4GeneralParticleSource*  fGeneralParticleSource; }; 

Here you can find how to implement MyPrimaryGeneratorAction class in your code.

The Monoenergetic photon pencil beam example uses the G4ParticleGun class. The Proton beam with realistic geometry tutorial uses G4GeneralParticleSource.

Optional user classes

Run

You can derive your own class from G4UserRunAction base class where you may book results of the run. The run starts with "Beam On" and within a run you can not change the detector geometry and physics processes. The run is represented by class G4Run .

Here you can find examples of user defined RunAction classes.

Event

Event is the basic GEANT4 unit. At the beginning of an event are generated primary tracks which are pushed into a stack. The tracks from the stack are analyzed one by one. The primary tracks might lead to secondary tracks which are also pushed into the stack. When the stack becomes empty the processing of the event is over. The event is represented by class G4Event which gives hits and trajectory collections as output.

You can derive your own class from G4UserEventAction base class where you can select and analize data.

Here you can find examples of user defined EventAction classes.

Track

Track is a snapshot of a particle and it is represented by G4Track class. At the end of the event the track objects do not exist. Tracks are pushed step by step. Moving by one step is called "stepping" and this can be controlled by the G4UserSteppingAction class (see below). At the end of each step the state of a track can be changed (killed, suspended, postponed).

Step

Step is defined by two points, it contains also information about the particle e.g. energy loss on the step. A step is represented by G4Step and G4StepPoint classes. G4UserSteppingAction is optional class where you can kill, suspend, postpone a track.

Status is attached to each G4StepPoint to show how step was determined. You can use PostStepPoint to get status of current step and PreStepPoint to get status of previous step. For example to get the "x" coordinate of a step you do the following:

G4StepPoint* prePoint  = step->GetPreStepPoint();

G4double x1 = prePoint->GetPosition().x(); 

Trajectory

Trajectories are represented by classes G4Trajectory and G4TrajectoryPoint . G4Trajectory class copies some of the G4Track class information. G4TrajectoryPoint is the class which copies some of the G4Step information.

Personal tools