ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ML2PrimaryGenerationAction.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ML2PrimaryGenerationAction.cc
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 // The code was written by :
27 // ^Claudio Andenna claudio.andenna@ispesl.it, claudio.andenna@iss.infn.it
28 // *Barbara Caccia barbara.caccia@iss.it
29 // with the support of Pablo Cirrone (LNS, INFN Catania Italy)
30 // with the contribute of Alessandro Occhigrossi*
31 //
32 // ^INAIL DIPIA - ex ISPESL and INFN Roma, gruppo collegato Sanità, Italy
33 // *Istituto Superiore di Sanità and INFN Roma, gruppo collegato Sanità, Italy
34 // Viale Regina Elena 299, 00161 Roma (Italy)
35 // tel (39) 06 49902246
36 // fax (39) 06 49387075
37 //
38 // more information:
39 // http://g4advancedexamples.lngs.infn.it/Examples/medical-linac
40 //
41 //*******************************************************//
42 
43 
45 
46 using namespace CLHEP;
47 
49 :particleGun(0),gamma(0),electron(0),positron(0),primaryParticleData(0),particles(0)
50 {
51 }
53 
55 {
56  if (instance == 0)
57  {
59  }
60  return instance;
61 }
63 {
64  rm = new G4RotationMatrix();
66  particle = new Sparticle;
68 
70 
71  gamma = particleTable->FindParticle("gamma");
72  electron = particleTable->FindParticle("e-");
73  positron = particleTable->FindParticle("e+");
74  particleGun = new G4ParticleGun();
75 
76  primaryParticleData = pData;
77  primaryParticleData -> nPrimaryParticle = 0;
78  primaryParticleData -> partPDGE = 0;
79 }
80 
82 {
84  switch (idParticleSource)
85  {
86  case id_randomTarget:
87  setGunRandom();
88  break;
89  case id_phaseSpace:
91  break;
92  }
93 }
94 
96 {
100 }
101 
103 {
107 }
108 
110 {
111  delete particleGun;
112  delete [] particles;
113  delete particles;
114 }
116 {
117  static int currentRecycle=nRecycling;
118  static G4ThreeVector pos0, dir0;
119  if (currentRecycle==nRecycling)
120  {
121  currentRecycle=0;
122  switch (idCurrentParticleSource)
123  {
124  case id_randomTarget:
126  break;
127  case id_phaseSpace:
129  break;
130  }
131  pos0 = pos;
132  dir0 = dir;
133  }
134  currentRecycle++;
135  pos = pos0;
136  dir = dir0;
137  applySourceRotation(); // to follow the accelerator rotation
138 
141 
146 }
148 {
149  sinTheta = RandGauss::shoot(0., 0.003);
150  cosTheta = std::sqrt(1 - sinTheta*sinTheta);
151  phi = twopi*G4UniformRand();
152  dir.set(sinTheta*std::cos(phi), sinTheta*std::sin(phi), cosTheta);
153 
156 
157  pos.setX(rho*std::sin(alpha));
158  pos.setY(rho*std::cos(alpha));
159  pos.setZ(-(accTargetZPosition +5.)*mm); // the primary electrons are generated 5 mm before the target
162 }
164 {
165  static bool bFirstTime=true;
166  if (bFirstTime)
167  {bFirstTime=false;fillParticlesContainer();}
168  if (nParticle==nMaxParticlesInRamPhaseSpace) // once all the particles stored in RAM hae been processed a new set is loaded
169  {
171  nParticle=0;
172  }
173 
177  switch (particles[nParticle].partPDGE)
178  {
179  case -11:
181  break;
182  case 11:
184  break;
185  case 22:
187  break;
188  }
189  nPhSpParticles++;
190  nParticle++;
191 }
193 {
194  pos=*rm*pos;
195  dir=*rm*dir;
196 }
198 {
199  static int currentFilePosition=0;
200  static int currentFileSize=0;
201  int startDataFilePosition;
202  std::ifstream in;
204  if (in)
205  {
206  G4cout << "ERROR phase space file: " << calculatedPhaseSpaceFileIN << " NOT found. Run abort " << G4endl;
208  }
209 
210  static bool bFirstTime=true;
211  if (bFirstTime)
212  {
213  in.seekg(-1,std::ios::end);
214  currentFileSize=in.tellg();
215  in.seekg(0,std::ios::beg);
216  bFirstTime=false;
217  }
218 
219  char a[1000];
220  in.getline(a,1000,'\n');
221  in.getline(a,1000,'\n');
222  startDataFilePosition=in.tellg();
223  if (currentFilePosition>0)
224  {in.seekg(currentFilePosition, std::ios::beg);}
225  int i;
226  G4double x,y,z;
227  G4int d;
228 
229  static bool checkFileRewind=false;
230  static bool bRewindTheFile=false;
231  static int nPhSpFileRewind=0;
232 
233  for (i=0;i<nMaxParticlesInRamPhaseSpace;i++)
234  {
235  if (bRewindTheFile) // to read the phase space file again to fill the container
236  {
237  in.close();
239  in.seekg(startDataFilePosition, std::ios::beg);
240  checkFileRewind=true;
241  bRewindTheFile=false;
242  G4cout<<"\n################\nI have reached the end of the phase space file "<<++nPhSpFileRewind <<" times, I rewind the file\n" << G4endl;
243  G4cout <<"loaded " <<i <<"/"<< nMaxParticlesInRamPhaseSpace<<" particles" << G4endl;
244  }
245  in >> d;
246  in >> x; in >>y; in >> z;
247 /* G4cout <<"x:" <<x << G4endl;
248  G4cout <<"y:" <<y << G4endl;
249  G4cout <<"z:" <<z << G4endl;*/
251  in >> x; in >>y; in >> z;
252  particles[i].dir.set(x,y,z);
253  in >> x;
254  particles[i].kinEnergy=x;
255  in >> d;
256  particles[i].partPDGE=d;
257  in >> d; in >> d;
258  if (in.eof()) {bRewindTheFile=true;}
259  if (checkFileRewind) {checkFileRewind=false;}
260  }
261  G4cout <<"loaded " <<i <<"/"<< nMaxParticlesInRamPhaseSpace<<" particles" << G4endl;
262  currentFilePosition=in.tellg(); // to remind the actual position in the phase space file
263  if (currentFilePosition>=currentFileSize) // to read the phase space file again
264  {currentFilePosition=startDataFilePosition;}
265  in.close();
266 }
267