source: trunk/examples/advanced/xray_fluorescence/src/XrayFluoPrimaryGeneratorAction.cc @ 807

Last change on this file since 807 was 807, checked in by garnier, 16 years ago

update

File size: 8.7 KB
Line 
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
38#include "XrayFluoPrimaryGeneratorAction.hh"
39//#include "G4DataVector.hh"
40#include "XrayFluoDetectorConstruction.hh"
41#include "XrayFluoPrimaryGeneratorMessenger.hh"
42#include "XrayFluoRunAction.hh"
43#include "G4Event.hh"
44#include "G4ParticleGun.hh"
45#include "G4ParticleTable.hh"
46#include "G4ParticleDefinition.hh"
47#include "Randomize.hh"
48#include "XrayFluoAnalysisManager.hh"
49#include "XrayFluoDataSet.hh"
50#ifdef G4ANALYSIS_USE 
51#include "AIDA/AIDA.h"
52#endif
53//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
54
55XrayFluoPrimaryGeneratorAction::XrayFluoPrimaryGeneratorAction(XrayFluoDetectorConstruction* XrayFluoDC)
56  :rndmFlag("off"),beam("off"),spectrum("off"),isoVert("off"),phaseSpaceGunFlag(false), 
57   rayleighFlag(true), particleEnergies(0),  particleTypes(0), detectorPosition(0) 
58{
59
60  XrayFluoDetector = XrayFluoDC;
61
62  G4int n_particle = 1;
63  particleGun  = new G4ParticleGun(n_particle);
64 
65  //create a messenger for this class
66  gunMessenger = new XrayFluoPrimaryGeneratorMessenger(this);
67  runManager = new XrayFluoRunAction();
68 
69  // default particle kinematic
70 
71  G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
72  G4String particleName;
73  G4ParticleDefinition* particle
74    = particleTable->FindParticle(particleName="gamma");
75  particleGun->SetParticleDefinition(particle);
76  particleGun->SetParticleMomentumDirection(G4ThreeVector(0.,0.,1.));
77  particleGun->SetParticleEnergy(10.*keV);
78
79  G4double position = -0.5*(XrayFluoDetector->GetWorldSizeZ());
80  particleGun->SetParticlePosition(G4ThreeVector(0.*cm,0.*cm,position));
81
82
83 
84  G4cout << "XrayFluoPrimaryGeneratorAction created" << G4endl;
85 
86}
87
88
89void XrayFluoPrimaryGeneratorAction::ActivatePhaseSpace(G4String fileName) {
90
91  // load phase-space
92#ifdef G4ANALYSIS_USE     
93  phaseSpaceGunFlag = true;
94
95  // reads the data stored on disk form previous runs
96  // and get these data to data members
97
98  XrayFluoAnalysisManager* analysis =  XrayFluoAnalysisManager::getInstance();
99  analysis->LoadGunData(fileName, rayleighFlag);
100  particleEnergies = analysis->GetEmittedParticleEnergies();
101  particleTypes = analysis->GetEmittedParticleTypes();
102  detectorPosition = XrayFluoDetector->GetDetectorPosition();
103  detectorPosition.setR(detectorPosition.r()-(5.*cm)); // 5 cm dietro
104#endif
105
106}
107
108
109void XrayFluoPrimaryGeneratorAction::SetRayleighFlag (G4bool value)
110{
111
112  G4cout <<  "value: " << value << G4endl; 
113  rayleighFlag = value;
114 
115}
116
117
118//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
119
120XrayFluoPrimaryGeneratorAction::~XrayFluoPrimaryGeneratorAction()
121{
122  delete particleGun;
123  delete gunMessenger;
124  delete runManager;
125
126  G4cout << "XrayFluoPrimaryGeneratorAction deleted" << G4endl;
127
128}
129
130//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
131
132void XrayFluoPrimaryGeneratorAction::GeneratePrimaries(G4Event* anEvent)
133{
134  //this function is called at the begining of event
135  //
136  G4double z0 = -0.5*(XrayFluoDetector->GetWorldSizeZ());
137  G4double y0 = 0.*cm, x0 = 0.*cm;
138  if (rndmFlag == "on")
139    {
140      y0 = (XrayFluoDetector->GetDia3SizeXY())/std::sqrt(2.)*(G4UniformRand()-0.5); // it was GetSampleSizeXY(),
141      x0 = (XrayFluoDetector->GetDia3SizeXY())/std::sqrt(2.)*(G4UniformRand()-0.5); // not divided by std::sqrt(2.)
142    } 
143  particleGun->SetParticlePosition(G4ThreeVector(x0,y0,z0));
144 
145  //randomize starting point
146  if (beam == "on")
147    {
148      G4double radius = 0.5 * mm;
149      G4double rho = radius*std::sqrt(G4UniformRand());
150      G4double theta = 2*pi*G4UniformRand()*rad;
151      G4double position = -0.5*(XrayFluoDetector->GetWorldSizeZ());
152     
153      G4double y = rho * std::sin(theta);
154      G4double x = rho * std::cos(theta);
155     
156      particleGun->SetParticlePosition(G4ThreeVector(x,y,position));
157    }
158  //shoot particles according to a certain spectrum
159  if (spectrum =="on")
160    {
161      G4String particle =  particleGun->GetParticleDefinition()
162        ->GetParticleName();
163      if(particle == "proton"|| particle == "alpha")
164        {
165          G4DataVector* energies =  runManager->GetEnergies();
166          G4DataVector* data =  runManager->GetData();
167         
168          G4double sum = runManager->GetDataSum();
169          G4double partSum = 0;
170          G4int j = 0;
171          G4double random= sum*G4UniformRand();
172          while (partSum<random)
173            {
174              partSum += (*data)[j];
175              j++;
176            }
177         
178          particleGun->SetParticleEnergy((*energies)[j]);
179       
180        }
181      else if (particle == "gamma")
182        {
183          const XrayFluoDataSet* dataSet = runManager->GetGammaSet();
184         
185          G4int i = 0;
186          G4int id = 0;
187          G4double minEnergy = 0. * keV;
188          G4double particleEnergy= 0.;
189          G4double maxEnergy = 10. * keV;
190          G4double energyRange = maxEnergy - minEnergy;
191
192           while ( i == 0)
193            {
194              G4double random = G4UniformRand();
195             
196              G4double randomNum = G4UniformRand(); //*5.0E6;
197             
198              particleEnergy = (random*energyRange) + minEnergy;
199             
200              if ((dataSet->FindValue(particleEnergy,id)) > randomNum)
201                {
202                  i = 1;
203                 
204                }
205            }
206           particleGun->SetParticleEnergy(particleEnergy);
207        }
208    }
209
210  // Randomize starting point and direction
211 
212  if (isoVert == "on")
213    {
214      G4double rho = 1. *m;
215      //theta in [0;pi/2]
216      G4double theta = (pi/2)*G4UniformRand();
217      //phi in [-pi;pi]
218      G4double phi = (G4UniformRand()*2*pi)- pi;
219      G4double x = rho*std::sin(theta)*std::sin(phi);
220      G4double y = rho*std::sin(theta)*std::cos(phi);
221      G4double z = -(rho*std::cos(theta));
222      particleGun->SetParticlePosition(G4ThreeVector(x,y,z));
223     
224      G4double Xdim = XrayFluoDetector->GetSampleSizeXY();
225      G4double Ydim = XrayFluoDetector->GetSampleSizeXY();
226     
227      G4double Dx = Xdim*(G4UniformRand()-0.5);
228     
229      G4double Dy = Ydim*(G4UniformRand()-0.5);
230     
231      particleGun->SetParticleMomentumDirection(G4ThreeVector(-x+Dx,-y+Dy,-z));
232     
233    }
234
235
236  if (phaseSpaceGunFlag){
237
238    size_t i = anEvent->GetEventID();
239
240    particleGun->SetParticlePosition(detectorPosition); 
241    particleGun->SetParticleMomentumDirection(detectorPosition);
242    G4double energy;
243    G4ParticleTable* particleTable = G4ParticleTable::GetParticleTable();
244    G4ParticleDefinition* particle;
245    if (i < particleEnergies->size()) {     
246      energy = (*particleEnergies)[i];
247      particle = particleTable->FindParticle((*particleTypes)[i]);
248    }
249
250    else {
251      energy = 0.;
252      particle = particleTable->FindParticle("gamma");
253    }
254
255    particleGun->SetParticleEnergy(energy);
256
257
258
259    particleGun->SetParticleDefinition(particle);
260  }
261
262#ifdef G4ANALYSIS_USE
263
264  G4double partEnergy = particleGun->GetParticleEnergy();
265  XrayFluoAnalysisManager* analysis =  XrayFluoAnalysisManager::getInstance();
266  analysis->analysePrimaryGenerator(partEnergy/keV);
267
268#endif
269 
270  particleGun->GeneratePrimaryVertex(anEvent);
271}
272
273//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
274
275
276
277
278
279
280
281
282
Note: See TracBrowser for help on using the repository browser.