Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
A01PrimaryGeneratorAction.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
28 //
29 // $Id$
30 // --------------------------------------------------------------
31 //
32 
35 
36 #include "G4Event.hh"
37 #include "G4ParticleGun.hh"
38 #include "G4ParticleTable.hh"
39 #include "G4ParticleDefinition.hh"
40 #include "G4SystemOfUnits.hh"
41 #include "Randomize.hh"
42 
44 {
45  fMomentum = 1000.*MeV;
46  fSigmaMomentum = 50.*MeV;
47  fSigmaAngle = 2.*deg;
48  fRandomizePrimary = true;
49 
50  G4int n_particle = 1;
51  fParticleGun = new G4ParticleGun(n_particle);
52 
53  //create a messenger for this class
54  fGunMessenger = new A01PrimaryGeneratorMessenger(this);
55 
57  G4String particleName;
58  fPositron = particleTable->FindParticle(particleName="e+");
59  fMuon = particleTable->FindParticle(particleName="mu+");
60  fPion = particleTable->FindParticle(particleName="pi+");
61  fKaon = particleTable->FindParticle(particleName="kaon+");
62  fProton = particleTable->FindParticle(particleName="proton");
63 
64  // default particle kinematics
65  fParticleGun->SetParticlePosition(G4ThreeVector(0.,0.,-8.*m));
66  fParticleGun->SetParticleDefinition(fPositron);
67 }
68 
70 {
71  delete fParticleGun;
72  delete fGunMessenger;
73 }
74 
76 {
77  G4ParticleDefinition* particle;
78 
79  if(fRandomizePrimary)
80  {
82  G4int i = (int)(2.*G4UniformRand());
83  switch(i)
84  {
85  case 0:
86  particle = fPositron;
87  break;
88  case 1:
89  particle = fMuon;
90  break;
91  case 2:
92  particle = fPion;
93  break;
94  case 3:
95  particle = fKaon;
96  break;
97  default:
98  particle = fProton;
99  break;
100  }
101  fParticleGun->SetParticleDefinition(particle);
102  }
103  else
104  {
105  particle = fParticleGun->GetParticleDefinition();
106  }
107 
108  G4double pp = fMomentum + (G4UniformRand()-0.5)*fSigmaMomentum;
109  G4double mass = particle->GetPDGMass();
110  G4double Ekin = std::sqrt(pp*pp+mass*mass)-mass;
111  fParticleGun->SetParticleEnergy(Ekin);
112 
113  G4double angle = (G4UniformRand()-0.5)*fSigmaAngle;
114  fParticleGun->SetParticleMomentumDirection(G4ThreeVector(std::sin(angle),0.,std::cos(angle)));
115 
116  fParticleGun->GeneratePrimaryVertex(anEvent);
117 }
118