Home


Geant4 Basics


Geant4 Tutorials


Wisp GUI


About


Contact




Geant4 Quickstart Guide

Step 2: Defining the particles source

The previous step demonstrated how to define the experimental setup to simulate. This section will demonstrate how to create energetic particles which will interact with the setup.

In order to start off the simulation, the user must provide a particle source by creating a class that inherits from G4VUserPrimaryGeneratorAction and overrides the abstract method
void GeneratePrimaries(G4Event*).
 
 class BasicPrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction{
 public:
	void GeneratePrimaries(G4Event* anEvent){


The easiest way to generate primary particles in Geant4 is to use the helper class G4ParticleGun.
 void GeneratePrimaries(G4Event* anEvent){
	G4ParticleGun* myGun = new G4ParticleGun();
	
	//Tell the gun to emit protons
	myGun->SetParticleDefinition(G4Proton::ProtonDefinition());

	//Set the kinetic energy of the protons to 50 keV
	//and tell the gun to emit them along the x-axis
	myGun->SetParticleEnergy(50.*keV);
	myGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0,0));

	//Generate the primary particle
	myGun->GeneratePrimaryVertex(anEvent);
 }
Note that the proton definition has been obtained from a static constructor G4Proton::ProtonDefinition(). The reasonfor this is that particle definitions in Geant4 always exist only oncein memory. In this way, if a million protons are created, attributes like proton mass and charge only need to be held in memory once.

The class BasicPrimaryGeneretorAction defined above will be passed to the Geant4 kernel during initialization. At the beginning of the simulation the Geant4 kernel will call the void GeneratePrimaries(G4Event*) method in order to generate a 50 keV proton travelling in the direction of the x-axis.

Link to the complete BasicPrimaryGeneratorAction class.

Go to page 3: Writing the main-method

page 1 / page 2 / page 3 / page 4