Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrimaryGeneratorAction.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 
30 //---------------------------------------------------------------------------
31 //
32 // ClassName: PrimaryGeneratorAction
33 //
34 // Description: Generate primary beam
35 //
36 // Authors: V.Grichine, V.Ivanchenko
37 //
38 // Modified:
39 //
40 //----------------------------------------------------------------------------
41 //
42 
43 #include "PrimaryGeneratorAction.hh"
44 #include "DetectorConstruction.hh"
45 #include "PrimaryGeneratorMessenger.hh"
46 #include "Randomize.hh"
47 #include "G4ParticleGun.hh"
48 #include "G4ParticleTable.hh"
49 #include "G4ParticleDefinition.hh"
50 #include "G4PhysicalConstants.hh"
51 #include "G4SystemOfUnits.hh"
52 #include "Histo.hh"
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
55 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
56 
58  fDetector(pDet)
59 {
60  InitializeMe();
61 }
62 
63 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
64 
65 void PrimaryGeneratorAction::InitializeMe()
66 {
67  fMessenger = new PrimaryGeneratorMessenger(this);
68  fParticleGun = new G4ParticleGun();
69  fCounter = 0;
70  fVerbose = 0;
71  fX0 = 0.0;
72  fY0 = 0.0;
73  fZ0 = 0.0;
74  fSigmaX = 1.5*mm;
75  fSigmaY = 1.5*mm;
76  fSigmaZ = 0.0;
77  fSigmaE = 0.0;
78  fRMax2 = 2.5*2.5*mm*mm;
79  fSigmaTheta = 0.0;
80  // fSigmaTheta = 0.17*degree;
81  fMinCosTheta = 2.0;
82  SetBeamEnergy(50.0*MeV);
83  fPosition = G4ThreeVector(fX0,fY0,fZ0);
84  fDirection = G4ThreeVector(0.0,0.0,1.0);
85  fGauss = true;
86  if(fEnergy < (Histo::GetPointer())->GetMaxEnergy()) {
87  (Histo::GetPointer())->SetMaxEnergy(fEnergy);
88  }
89 }
90 
91 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
92 
94 {
95  delete fParticleGun;
96  delete fMessenger;
97 }
98 
99 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
100 
102 {
103  fCounter++ ;
104  fVerbose = (Histo::GetPointer())->GetVerbose();
105 
106  // Simulation of beam position
107  G4double x = fX0;
108  G4double y = fY0;
109  G4double z = fDetector->GetGeneratorPosZ();
110  do {
111  if(0.0 < fSigmaX) { x = G4RandGauss::shoot(fX0,fSigmaX); }
112  if(0.0 < fSigmaY) { y = G4RandGauss::shoot(fY0,fSigmaY); }
113  } while (x*x + y*y > fRMax2);
114 
115  fPosition = G4ThreeVector(x,y,z);
116  fParticleGun->SetParticlePosition(fPosition);
117 
118  // Simulation of beam direction
119  G4double ux = fDirection.x();
120  G4double uy = fDirection.y();
121  G4double uz = fDirection.z();
122 
123  // Beam particles are uniformly distributed over phi, cosTheta
124  if(1.0 > fMinCosTheta) {
125  uz = fMinCosTheta + (1.0 - fMinCosTheta)*G4UniformRand() ;
126  ux = std::sqrt((1.0 - uz)*(1.0 + uz)) ;
127  } else if (fSigmaTheta > 0.0) {
128  ux = G4RandGauss::shoot(0.0,fSigmaTheta);
129  uz = std::sqrt((1.0 - ux)*(1.0 + ux));
130  }
131 
132  G4double phi = twopi*G4UniformRand() ;
133  uy = ux;
134  ux *= std::cos(phi) ;
135  uy *= std::sin(phi) ;
136  fDirection = G4ThreeVector(ux,uy,uz) ;
137 
138  fParticleGun->SetParticleMomentumDirection(fDirection);
139 
140  // Simulation of beam kinetic energy
141  G4double kinEnergy = fEnergy;
142 
143  if(fGauss == "flatE") {
144  kinEnergy = fEnergy - fSigmaE + 2.*fSigmaE*G4UniformRand();
145  } else if(0.0 < fSigmaE) {
146  kinEnergy = fEnergy + G4RandGauss::shoot(0.0,fSigmaE);
147  }
148  fParticleGun->SetParticleEnergy(kinEnergy);
149 
150  if(fVerbose > 0) {
151  G4ParticleDefinition* particle = fParticleGun->GetParticleDefinition();
152  G4String particleName = particle->GetParticleName() ;
153  G4cout << "Event# " << fCounter
154  << " Beam particle is generated by PrimaryGeneratorAction "
155  << G4endl;
156  G4cout << "ParticleName= " << particleName
157  << " PDGcode= " << particle->GetPDGEncoding()
158  << std::setprecision(5)
159  << " KinEnergy(GeV)= "
160  << fEnergy/GeV
161  << " x(mm)= "
162  << x/mm
163  << " y(mm)= "
164  << y/mm
165  << " z(mm)= "
166  << z/mm
167  << " ux= "
168  << ux
169  << " uy= "
170  << uy
171  << " uz= "
172  << uz
173  << G4endl;
174  }
175 
176  fParticleGun->GeneratePrimaryVertex(anEvent);
177 }
178 
179 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
180 
182 {
183  fEnergy = val;
184  if(fEnergy < (Histo::GetPointer())->GetMaxEnergy()) {
185  (Histo::GetPointer())->SetMaxEnergy(fEnergy);
186  }
187 }
188 
189 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
190 
191 
192