Overview Contents Previous Next Geant4 User's Guide
For Application Developers
Getting Started with Geant4

2.6 How to Generate a Primary Event

2.6.1 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.

#ifndef ExN01PrimaryGeneratorAction_h
#define ExN01PrimaryGeneratorAction_h 1

#include "G4VUserPrimaryGeneratorAction.hh"

class G4ParticleGun;
class G4Event;

class ExN01PrimaryGeneratorAction : public G4VUserPrimaryGeneratorAction

    void generatePrimaries(G4Event* anEvent);

    G4ParticleGun* particleGun;


#include "ExN01PrimaryGeneratorAction.hh"
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ThreeVector.hh"
#include "G4Geantino.hh"
#include "globals.hh"

  G4int n_particle = 1;
  particleGun = new G4ParticleGun(n_particle);


  delete particleGun;

void ExN01PrimaryGeneratorAction::generatePrimaries(G4Event* anEvent)
  G4int i = anEvent->get_eventID() % 3;
    case 0:
    case 1:
    case 2:

Source listing 2.6.1
An example of a G4VUserPrimaryGeneratorAction concrete class using G4ParticleGun. For the usage of G4ParticleGun refer to 2.6.2. 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 Source listing 2.6.1, 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.

2.6.2 G4VPrimaryGenerator

Geant4 provides two G4VPrimaryGenerator concrete classes. One is G4ParticleGun, which will be discussed here, and the other is G4HEPEvtInterface, which will be discussed in Section 3.6. 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 Section 3.2). 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.

About the authors