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 { public: ExN01PrimaryGeneratorAction(); ~ExN01PrimaryGeneratorAction(); public: void generatePrimaries(G4Event* anEvent); private: G4ParticleGun* particleGun; }; #endif #include "ExN01PrimaryGeneratorAction.hh" #include "G4Event.hh" #include "G4ParticleGun.hh" #include "G4ThreeVector.hh" #include "G4Geantino.hh" #include "globals.hh" ExN01PrimaryGeneratorAction::ExN01PrimaryGeneratorAction() { G4int n_particle = 1; particleGun = new G4ParticleGun(n_particle); particleGun->SetParticleDefinition(G4Geantino::GeantinoDefinition()); particleGun->SetParticleEnergy(1.0*GeV); particleGun->SetParticlePosition(G4ThreeVector(-2.0*m,0.0*m,0.0*m)); } ExN01PrimaryGeneratorAction::~ExN01PrimaryGeneratorAction() { delete particleGun; } void ExN01PrimaryGeneratorAction::generatePrimaries(G4Event* anEvent) { G4int i = anEvent->get_eventID() % 3; switch(i) { case 0: particleGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0.0,0.0)); break; case 1: particleGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0.1,0.0)); break; case 2: particleGun->SetParticleMomentumDirection(G4ThreeVector(1.0,0.0,0.1)); break; } particleGun->generatePrimaryVertex(anEvent); } |
Source listing 2.6.1 An example of a G4VUserPrimaryGeneratorAction concrete class using G4ParticleGun. For the usage of G4ParticleGun refer to 2.6.2. |
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.
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.