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