How to Generate a Primary Event

Generating Primary Events

G4VuserPrimaryGeneratorAction is one of the mandatory classes available for deriving your own concrete class. In your concrete class, you have to specify how a primary event should be generated. Actual generation of primary particles will be done by concrete classes of G4VPrimaryGenerator, explained in the following sub-section. Your G4VUserPrimaryGeneratorAction concrete class just arranges the way primary particles are generated.

Listing 18 PrimaryGeneratorAction: An example of a G4VUserPrimaryGeneratorAction concrete class using G4ParticleGun. For the usage of G4Particle Gun refer to the next subsection.
//////////////////////////////////
// PrimaryGeneratorAction.hh
//////////////////////////////////

#ifndef PrimaryGeneratorAction_h
#define PrimaryGeneratorAction_h 1

#include "G4VUserPrimaryGeneratorAction.hh"
#include "G4ThreeVector.hh"
#include "globals.hh"

class G4ParticleGun;
class G4Event;

class PrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction
{
  public:
    PrimaryGeneratorAction(
      const G4String& particleName = "geantino",
      G4double energy = 1.*MeV,
      G4ThreeVector position= G4ThreeVector(0,0,0),
      G4ThreeVector momentumDirection = G4ThreeVector(0,0,1));
    ~PrimaryGeneratorAction();

    // methods
    virtual void GeneratePrimaries(G4Event*);

  private:
    // data members
    G4ParticleGun*  fParticleGun; //pointer a to G4 service class
};

#endif

//////////////////////////////////
// PrimaryGeneratorAction.cc
//////////////////////////////////

#include "PrimaryGeneratorAction.hh"

#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

PrimaryGeneratorAction::PrimaryGeneratorAction(
                                const G4String& particleName,
                                G4double energy,
                                G4ThreeVector position,
                                G4ThreeVector momentumDirection)
  : G4VUserPrimaryGeneratorAction(),
    fParticleGun(0)
{
  G4int nofParticles = 1;
  fParticleGun  = new G4ParticleGun(nofParticles);

  // default particle kinematic
  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
  G4ParticleDefinition* particle
    = particleTable->FindParticle(particleName);
  fParticleGun->SetParticleDefinition(particle);
  fParticleGun->SetParticleEnergy(energy);
  fParticleGun->SetParticlePosition(position);
  fParticleGun->SetParticleMomentumDirection(momentumDirection);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

PrimaryGeneratorAction::~PrimaryGeneratorAction()
{
  delete fParticleGun;
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void PrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
{
  // this function is called at the beginning of event

  fParticleGun->GeneratePrimaryVertex(anEvent);
}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

Selection of the generator

In the constructor of your G4VUserPrimaryGeneratorAction, you should instantiate the primary generator(s). If necessary, you need to set some initial conditions for the generator(s).

In PrimaryGeneratorAction, G4ParticleGun is constructed to use as the actual primary particle generator. Methods of G4ParticleGun are described in the following section. Please note that the primary generator object(s) you construct in your G4VUserPrimaryGeneratorAction concrete class must be deleted in your destructor.

Generation of an event

G4VUserPrimaryGeneratorAction has a pure virtual method named generatePrimaries(). This method is invoked at the beginning of each event. In this method, you have to invoke the G4VPrimaryGenerator concrete class you instantiated via the generatePrimaryVertex() method.

You can invoke more than one generator and/or invoke one generator more than once. Mixing up several generators can produce a more complicated primary event.

G4VPrimaryGenerator

Geant4 provides three G4VPrimaryGenerator concrete classes. Among these G4ParticleGun and G4GeneralParticleSource will be discussed here. The third one is G4HEPEvtInterface, which will be discussed in Event Generator Interface.

G4ParticleGun

G4ParticleGun is a generator provided by Geant4. This class generates primary particle(s) with a given momentum and position. It does not provide any sort of randomizing. The constructor of G4ParticleGun takes an integer which causes the generation of one or more primaries of exactly same kinematics. It is a rather frequent user requirement to generate a primary with randomized energy, momentum, and/or position. Such randomization can be achieved by invoking various set methods provided by G4ParticleGun. The invocation of these methods should be implemented in the generatePrimaries() method of your concrete G4VUserPrimaryGeneratorAction class before invoking generatePrimaryVertex() of G4ParticleGun. Geant4 provides various random number generation methods with various distributions (see Global Usage Classes).

Public methods of G4ParticleGun

The following methods are provided by G4ParticleGun, and all of them can be invoked from the generatePrimaries() method in your concrete G4VUserPrimaryGeneratorAction class.

  • void SetParticleDefinition(G4ParticleDefinition*)

  • void SetParticleMomentum(G4ParticleMomentum)

  • void SetParticleMomentumDirection(G4ThreeVector)

  • void SetParticleEnergy(G4double)

  • void SetParticleTime(G4double)

  • void SetParticlePosition(G4ThreeVector)

  • void SetParticlePolarization(G4ThreeVector)

  • void SetNumberOfParticles(G4int)

G4GeneralParticleSource

For many applications G4ParticleGun is a suitable particle generator. However if you want to generate primary particles in more sophisticated manner, you can utilize G4GeneralParticleSource, the Geant4 General Particle Source module (GPS), discussed in the next section ( General Particle Source).