Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XrayFluoPrimaryGeneratorAction.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 //
27 // $Id: XrayFluoPrimaryGeneratorAction.cc
28 // GEANT4 tag $Name: xray_fluo-V03-02-00
29 //
30 // Author: Elena Guardincerri (Elena.Guardincerri@ge.infn.it)
31 //
32 // History:
33 // -----------
34 // 28 Nov 2001 Elena Guardincerri Created
35 //
36 // -------------------------------------------------------------------
37 
41 #include "XrayFluoRunAction.hh"
42 #include "G4Event.hh"
43 #include "G4Gamma.hh"
44 #include "G4ParticleGun.hh"
45 #include "G4ParticleTable.hh"
46 #include "G4ParticleDefinition.hh"
47 #include "G4MTRunManager.hh"
48 #include "Randomize.hh"
50 #include "XrayFluoDataSet.hh"
51 #include "G4PhysicalConstants.hh"
52 #include "G4SystemOfUnits.hh"
53 
54 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
55 
57  XrayFluoDetectorConstruction* XrayFluoDC)
58  :rndmFlag("off"),beam("off"),spectrum("off"),isoVert("off"),phaseSpaceGunFlag(false),
59  rayleighFlag(true), detectorPosition(0)
60 {
61  runAction = 0;
62  XrayFluoDetector = XrayFluoDC;
63 
64  G4int n_particle = 1;
65  particleGun = new G4ParticleGun(n_particle);
66 
67  //create a messenger for this class
68  gunMessenger = new XrayFluoPrimaryGeneratorMessenger(this);
69 
70  // default particle kinematic
71  G4ParticleDefinition* particle
73  particleGun->SetParticleDefinition(particle);
74  particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
75  particleGun->SetParticleEnergy(10. * keV);
76 
77  G4double position = -0.5*(XrayFluoDetector->GetWorldSizeZ());
78  particleGun->SetParticlePosition(G4ThreeVector(0.*cm,0.*cm,position));
79 
80  G4cout << "XrayFluoPrimaryGeneratorAction created" << G4endl;
81 
82 }
83 
84 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
85 
87 
88  // load phase-space
89  phaseSpaceGunFlag = true;
90 
91  // reads the data stored on disk form previous runs
92  // and get these data to data members
93 
95  analysis->LoadGunData(fileName, rayleighFlag);
96  detectorPosition = XrayFluoDetector->GetDetectorPosition();
97  detectorPosition.setR(detectorPosition.r()-(5.*cm)); // 5 cm before the detector, so in front of it.
98 
99 }
100 
101 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
102 
104 {
105  rayleighFlag = value;
106 }
107 
108 
109 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
110 
112 {
113  delete particleGun;
114  delete gunMessenger;
115 }
116 
117 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
118 
120 {
121  //retrieve runAction, if not done
122  if (!runAction)
123  {
124  //Sequential runaction
125  if (G4RunManager::GetRunManager()->GetRunManagerType() ==
127  runAction = static_cast<const XrayFluoRunAction*>
129  else //MT master runaction
130  runAction = static_cast<const XrayFluoRunAction*>
132  if (!runAction)
133  G4cout << "Something wrong here!" << G4endl;
134  }
135 
136  //this function is called at the begining of event
137  //
138  G4double z0 = -0.5*(XrayFluoDetector->GetWorldSizeZ());
139  G4double y0 = 0.*cm, x0 = 0.*cm;
140  if (rndmFlag == "on")
141  {
142  y0 = (XrayFluoDetector->GetDia3SizeXY())/std::sqrt(2.)*(G4UniformRand()-0.5); // it was GetSampleSizeXY(),
143  x0 = (XrayFluoDetector->GetDia3SizeXY())/std::sqrt(2.)*(G4UniformRand()-0.5); // not divided by std::sqrt(2.)
144  }
145  particleGun->SetParticlePosition(G4ThreeVector(x0,y0,z0));
146 
147  //randomize starting point
148  if (beam == "on")
149  {
150  G4double radius = 0.5 * mm;
151  G4double rho = radius*std::sqrt(G4UniformRand());
152  G4double theta = 2*pi*G4UniformRand()*rad;
153  G4double position = -0.5*(XrayFluoDetector->GetWorldSizeZ());
154 
155  G4double y = rho * std::sin(theta);
156  G4double x = rho * std::cos(theta);
157 
158  particleGun->SetParticlePosition(G4ThreeVector(x,y,position));
159  }
160  //shoot particles according to a certain spectrum
161  if (spectrum =="on")
162  {
163  G4String particle = particleGun->GetParticleDefinition()
164  ->GetParticleName();
165  if(particle == "proton"|| particle == "alpha")
166  {
167  G4DataVector* energies = runAction->GetEnergies();
168  G4DataVector* data = runAction->GetData();
169 
170  G4double sum = runAction->GetDataSum();
171  G4double partSum = 0;
172  G4int j = 0;
173  G4double random= sum*G4UniformRand();
174  while (partSum<random)
175  {
176  partSum += (*data)[j];
177  j++;
178  }
179 
180  particleGun->SetParticleEnergy((*energies)[j]);
181 
182  }
183  else if (particle == "gamma")
184  {
185  const XrayFluoDataSet* dataSet = runAction->GetGammaSet();
186 
187  G4int i = 0;
188  G4int id = 0;
189  G4double minEnergy = 0. * keV;
190  G4double particleEnergy= 0.;
191  G4double maxEnergy = 10. * keV;
192  G4double energyRange = maxEnergy - minEnergy;
193 
194  while ( i == 0)
195  {
196  G4double random = G4UniformRand();
197 
198  G4double randomNum = G4UniformRand(); //*5.0E6;
199 
200  particleEnergy = (random*energyRange) + minEnergy;
201 
202  if ((dataSet->FindValue(particleEnergy,id)) > randomNum)
203  {
204  i = 1;
205 
206  }
207  }
208  particleGun->SetParticleEnergy(particleEnergy);
209  }
210  }
211 
212  // Randomize starting point and direction
213 
214  if (isoVert == "on")
215  {
216  G4double rho = 1. *m;
217  //theta in [0;pi/2]
218  G4double theta = (pi/2)*G4UniformRand();
219  //phi in [-pi;pi]
220  G4double phi = (G4UniformRand()*2*pi)- pi;
221  G4double x = rho*std::sin(theta)*std::sin(phi);
222  G4double y = rho*std::sin(theta)*std::cos(phi);
223  G4double z = -(rho*std::cos(theta));
224  particleGun->SetParticlePosition(G4ThreeVector(x,y,z));
225 
226  G4double Xdim = XrayFluoDetector->GetSampleSizeXY();
227  G4double Ydim = XrayFluoDetector->GetSampleSizeXY();
228 
229  G4double Dx = Xdim*(G4UniformRand()-0.5);
230 
231  G4double Dy = Ydim*(G4UniformRand()-0.5);
232 
233  particleGun->SetParticleMomentumDirection(G4ThreeVector(-x+Dx,-y+Dy,-z));
234 
235  }
236 
237  // using prevoiously genereated emissions from sample.....
238 
239  if (phaseSpaceGunFlag){
240 
241  particleGun->SetParticlePosition(detectorPosition);
242  particleGun->SetParticleMomentumDirection(detectorPosition);
243 
245 
246  const std::pair<G4double,G4String> kine =
248 
249  G4double energy = kine.first;
250  G4ParticleDefinition* particle = particleTable->FindParticle(kine.second);
251 
252  particleGun->SetParticleEnergy(energy);
253  particleGun->SetParticleDefinition(particle);
254 
255 
256  }
257 
258  G4double partEnergy = particleGun->GetParticleEnergy();
260  analysis->analysePrimaryGenerator(partEnergy/keV);
261 
262 
263  particleGun->GeneratePrimaryVertex(anEvent);
264 }
265 
266 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
267 
268 
269 
270 
271 
272 
273 
274 
275 
XrayFluoPrimaryGeneratorAction(const XrayFluoDetectorConstruction *)
G4ParticleDefinition * FindParticle(G4int PDGEncoding)
static constexpr double mm
Definition: G4SIunits.hh:115
CLHEP::Hep3Vector G4ThreeVector
void setR(double s)
tuple x
Definition: test.py:50
const std::pair< G4double, G4String > GetEmittedParticleEnergyAndType()
static constexpr double rad
Definition: G4SIunits.hh:149
void SetParticleMomentumDirection(G4ParticleMomentum aMomentumDirection)
int G4int
Definition: G4Types.hh:78
virtual void GeneratePrimaryVertex(G4Event *evt)
const G4String & GetParticleName() const
void LoadGunData(G4String, G4bool)
const XML_Char const XML_Char * data
Definition: expat.h:268
void analysePrimaryGenerator(G4double energy)
void SetParticlePosition(G4ThreeVector aPosition)
G4DataVector * GetEnergies() const
#define G4UniformRand()
Definition: Randomize.hh:97
G4GLOB_DLL std::ostream G4cout
static constexpr double m
Definition: G4SIunits.hh:129
const XML_Char int const XML_Char * value
Definition: expat.h:331
static XrayFluoAnalysisManager * getInstance()
bool G4bool
Definition: G4Types.hh:79
static G4MTRunManager * GetMasterRunManager()
static constexpr double cm
Definition: G4SIunits.hh:119
const XrayFluoDataSet * GetGammaSet() const
G4DataVector * GetData() const
void SetParticleEnergy(G4double aKineticEnergy)
static G4RunManager * GetRunManager()
Definition: G4RunManager.cc:79
static G4ParticleTable * GetParticleTable()
G4double energy(const ThreeVector &p, const G4double m)
G4double FindValue(G4double e, G4int) const
G4ParticleDefinition * GetParticleDefinition() const
tuple z
Definition: test.py:28
G4double GetDataSum() const
#define G4endl
Definition: G4ios.hh:61
static constexpr double pi
Definition: G4SIunits.hh:75
const G4UserRunAction * GetUserRunAction() const
double G4double
Definition: G4Types.hh:76
double r() const
static constexpr double keV
Definition: G4SIunits.hh:216
void SetParticleDefinition(G4ParticleDefinition *aParticleDefinition)
G4double GetParticleEnergy() const
static G4Gamma * Definition()
Definition: G4Gamma.cc:49