ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FCALPrimaryGeneratorAction.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FCALPrimaryGeneratorAction.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 //
27 //
28 
29 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
30 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
31 
32 #include <fstream>
33 #include <cstdlib>
34 
36 
37 #include "G4SystemOfUnits.hh"
38 #include "G4Event.hh"
39 #include "G4ParticleGun.hh"
40 #include "G4ParticleTable.hh"
41 #include "G4ParticleDefinition.hh"
42 #include "Randomize.hh"
43 #include "G4DataVector.hh"
44 #include "G4AutoLock.hh"
45 
46 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
47 
48 
49 // Migration to MT: there is a single input file that is read by all threads.
50 // The idea is that the events are read by a single thread and processed
51 // by all threads. Threads ask for the next ID to be processed. When
52 // events are all processed we start over from the beginning of the file
53 namespace {
54  G4bool isFileRead = false;
55  G4Mutex mFileRead = G4MUTEX_INITIALIZER;
56  //Primary kinematics
57  G4DataVector fX;
58  G4DataVector fY;
59  G4DataVector fZ;
60  G4DataVector fCosX;
61  G4DataVector fCosY;
62  G4DataVector fCosZ;
63  size_t nextEventId = 0;
64  G4Mutex mNextEventId = G4MUTEX_INITIALIZER;
65 
66  size_t GetNextId() {
67  G4AutoLock l(&mNextEventId);
68  if ( nextEventId >= fX.size() ) //file data are over, restart file
69  {
70  G4Exception("FCALPrimaryGeneratorAction::GeneratePrimaries","lAr002",
71  JustWarning,"Data file with kinematics is over, restart it");
72  nextEventId=0;
73  }
74  return nextEventId++;
75  }
76 
77  void ReadKinematicFromFile(G4double energy) {
78  //Only one thread shoud read input file
79  G4AutoLock l(&mFileRead);
80  if ( isFileRead ) return;
81  // Read Kinematics from file
82  G4String file_name = "data-tracks/tracks-80GeV.dat";
83  if (energy < 30*GeV)
84  file_name = "data-tracks/tracks-20GeV.dat";
85  else if (energy < 50*GeV)
86  file_name = "data-tracks/tracks-40GeV.dat";
87  else if (energy < 70*GeV)
88  file_name = "data-tracks/tracks-60GeV.dat";
89  else if (energy < 90*GeV)
90  file_name = "data-tracks/tracks-80GeV.dat";
91  else if (energy < 150*GeV)
92  file_name = "data-tracks/tracks-120GeV.dat";
93  else
94  file_name = "data-tracks/tracks-200GeV.dat";
95  std::ifstream Traks_file(file_name);
96  if(!Traks_file)
97  {
99  ed << "Failed to open file " << file_name << G4endl;
100  G4Exception("FCALPrimaryGeneratorAction::FCALPrimaryGeneratorAction()",
101  "lAr001",FatalException,ed);
102  }
103  G4double xx=0,yy=0,zz=0,c1=0,c2=0,c3=0;
104  G4int iev = 0;
105  while(!(Traks_file.eof())) {
106  Traks_file >> iev >> xx >> yy >> zz >> c1 >> c2 >> c3;
107  fX.push_back(xx*cm);
108  fY.push_back(yy*cm);
109  fZ.push_back(zz*cm);
110  fCosX.push_back(c1);
111  fCosY.push_back(c2);
112  fCosZ.push_back(c3);
113  }
114  G4cout << "Read " << fX.size() << " events from file " << file_name << G4endl;
115  isFileRead= true;
116  Traks_file.close();
117  return;
118  }
119 }
120 
121 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
122 
124  fVerbosity(0)
125 {
126  particleGun = new G4ParticleGun();
127 
128  // default Particle
129  G4String particleName;
131  G4ParticleDefinition* particle = particleTable->FindParticle(particleName="e-");
133 
134  // default Energy
136 }
137 
138 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
139 
141 {
142  delete particleGun;
143 }
144 
145 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
146 
148 {
149  //this function is called at the begining of event
150  ReadKinematicFromFile(particleGun->GetParticleEnergy());
151 
152  //Get next event to be processed
153  size_t nEvent = GetNextId();
154  particleGun->SetParticlePosition(G4ThreeVector(fX[nEvent],fY[nEvent],fZ[nEvent]));
156  fCosY[nEvent],
157  -1.0*fCosZ[nEvent]));
158 
160 
161  if (fVerbosity)
162  {
163  G4cout<< " Event "<<anEvent->GetEventID()<< " Generated Vertex : "
164  <<anEvent->GetEventID() <<" (x,y,z)=(" << fX[nEvent] << ","
165  <<fY[nEvent] << "," << fZ[nEvent]<< ") (cosX,cosY,cosZ)=("
166  << -1.*fCosX[nEvent] << "," << fCosY[nEvent]
167  <<"," << -1.*fCosZ[nEvent] << ")"<<G4endl;
168  }
169 
170 }
171 
172 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
173 
174