Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReadSynRadFiles.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ReadSynRadFiles.cc
1 #include "ReadSynRadFiles.h"
2 
4 
5 #include <HepMC/GenEvent.h>
6 #include <HepMC/GenParticle.h> // for GenParticle
7 #include <HepMC/GenVertex.h>
8 #include <HepMC/PdfInfo.h> // for PdfInfo
9 #include <HepMC/SimpleVector.h> // for FourVector
10 #include <HepMC/Units.h> // for GEV, MM
11 
12 // eicsmear classes
13 #include <eicsmear/erhic/EventMC.h>
14 #include <eicsmear/erhic/ParticleMC.h> // for ParticleMC
15 #include <eicsmear/erhic/Pid.h> // for Pid
16 
17 // General Root and C++ classes
18 #include <TBranch.h> // for TBranch
19 #include <TChain.h>
20 #include <TVector3.h> // for TVector3
21 
22 #include <algorithm> // copy
23 #include <cassert>
24 #include <iostream> // for operator<<, endl, basic_ostream
25 #include <iterator> // ostream_operator
26 #include <string>
27 #include <vector> // for vector
28 #include <vector>
29 
30 #include <boost/tokenizer.hpp>
31 
32 class PHCompositeNode;
33 class PHHepMCGenEvent;
34 
35 using namespace std;
36 
38 
40  : SubsysReco(name)
41  , filename("")
42  , nEntries(1)
43  , entry(1)
44  , GenEvent(nullptr)
45  , _node_name("PHHepMCGenEvent")
46 {
47  hepmc_helper.set_embedding_id(1); // default embedding ID to 1
48  return;
49 }
50 
52 
54 {
55 }
56 
58 
60 {
61  filename = name;
62  m_csv_input.open(filename);
63  assert(m_csv_input.is_open());
64 
65  // skip header
66  string line;
67  std::getline(m_csv_input, line);
68 
69  return true;
70 }
71 
73 {
74  /* Create node tree */
75  CreateNodeTree(topNode);
76  return 0;
77 }
78 
80 {
81  /* Create GenEvent */
82  HepMC::GenEvent *evt = new HepMC::GenEvent();
83 
84  /* define the units (Pythia uses GeV and mm) */
85  evt->use_units(HepMC::Units::GEV, HepMC::Units::CM);
86 
87  /* Loop over all particles for this event in input file and fill
88  * vector with HepMC particles */
89  vector<HepMC::GenParticle *> hepmc_particles;
90  vector<unsigned> origin_index;
91  // /* add HepMC particles to Hep MC vertices; skip first two particles
92  // * in loop, assuming that they are the beam particles */
93  vector<HepMC::GenVertex *> hepmc_vertices;
94 
95  typedef boost::tokenizer<boost::escaped_list_separator<char> > Tokenizer;
96 
97  vector<string> vec;
98  string line;
99 
100  int SumPhoton(0);
101  double SumFlux(0);
102 
103  if (Verbosity())
104  {
105  cout << "ReadSynRadFiles::process_event - reading " << nEntries << " lines from " << filename << endl;
106  }
107  for (int ii = 1; ii <= nEntries; ii++)
108  {
109  if (not std::getline(m_csv_input, line))
110  {
111  cout << "ReadSynRadFiles::process_event - "
112  << "input file end reached" << endl;
113 
115  }
116 
117  Tokenizer tok(line);
118  vec.assign(tok.begin(), tok.end());
119 
120  if (vec.size() != 14 and vec.size() != 15)
121  {
122  cout << "ReadSynRadFiles::process_event - "
123  << "invalid input :" << line << endl;
124 
126  }
127 
128  // Pos_X_[cm] Pos_Y_[cm] Pos_Z_[cm] Pos_u Pos_v Dir_X Dir_Y Dir_Z Dir_theta_[rad] Dir_phi_[rad] LowFluxRatio Energy_[eV] Flux_[photon/s] Power_[W]
129 
130  int id = 0;
131  const double Pos_X_cm = stod(vec[id++]);
132  const double Pos_Y_cm = stod(vec[id++]);
133  const double Pos_Z_cm = stod(vec[id++]);
134  const double Pos_u = stod(vec[id++]);
135  const double Pos_v = stod(vec[id++]);
136  const double Dir_X = stod(vec[id++]);
137  const double Dir_Y = stod(vec[id++]);
138  const double Dir_Z = stod(vec[id++]);
139  const double Dir_theta_rad = stod(vec[id++]);
140  const double Dir_phi_rad = stod(vec[id++]);
141  const double LowFluxRatio = stod(vec[id++]);
142  const double Energy_eV = stod(vec[id++]);
143  const double Flux_photon_s = stod(vec[id++]);
144  const double Power_W = stod(vec[id++]);
145  assert(id == 14);
146 
147  if (Verbosity())
148  {
149  cout << "ReadSynRadFiles::process_event - " << line << " -> " << endl
150  << Pos_X_cm << ", "
151  << Pos_Y_cm << ", "
152  << Pos_Z_cm << ", "
153  << Pos_u << ", "
154  << Pos_v << ", "
155  << Dir_X << ", "
156  << Dir_Y << ", "
157  << Dir_Z << ", "
158  << Dir_theta_rad << ", "
159  << Dir_phi_rad << ", "
160  << LowFluxRatio << ", "
161  << Energy_eV << ", "
162  << Flux_photon_s << ", "
163  << Power_W << ". "
164  << endl;
165  }
166 
167  double xz_sign = +1;
168  //assert(m_reverseXZ);
169  if (m_reverseXZ)
170  {
171  xz_sign = -1;
172 
173  static bool once = true;
174 
175  if (once)
176  {
177  cout << "ReadSynRadFiles::process_event - reverse x z axis direction for input photons" << endl;
178  once = false;
179  }
180  }
181 
182  const double E_GeV = Energy_eV / 1e9;
183  // const double E_GeV = Energy_eV / 1e3;
184  const double px = E_GeV * Dir_X;
185  const double py = E_GeV * Dir_Y;
186  const double pz = E_GeV * Dir_Z;
187 
188  /* Create HepMC particle record */
189  HepMC::GenParticle *hepmcpart = new HepMC::GenParticle(HepMC::FourVector(px * xz_sign, py, pz * xz_sign, E_GeV), 22);
190 
191  hepmcpart->set_status(1);
192 
193  /* add particle information */
194  hepmcpart->setGeneratedMass(0);
195 
196  /* append particle to vector */
197  hepmc_particles.push_back(hepmcpart);
198  origin_index.push_back(ii);
199 
200  HepMC::GenVertex *hepmcvtx = new HepMC::GenVertex(HepMC::FourVector(Pos_X_cm * xz_sign,
201  Pos_Y_cm,
202  Pos_Z_cm * xz_sign,
203  0));
204  hepmc_vertices.push_back(hepmcvtx);
205  hepmcvtx->add_particle_out(hepmcpart);
206 
207  ++SumPhoton;
208  SumFlux += Flux_photon_s;
209  }
210 
211  /* Check if hepmc_particles and origin_index vectors are the same size */
212  if (hepmc_particles.size() != origin_index.size())
213  {
214  cout << "ReadSynRadFiles::process_event - Lengths of HepMC particles and Origin index vectors do not match!" << endl;
215 
216  delete evt;
218  }
219 
220  /* Add HepMC vertices to event */
221  for (unsigned v = 0; v < hepmc_vertices.size(); v++)
222  evt->add_vertex(hepmc_vertices.at(v));
223 
224  // save weights
225  auto &weightcontainer = evt->weights();
226  weightcontainer.push_back(SumFlux);
227  weightcontainer.push_back(SumPhoton);
228 
229  /* pass HepMC to PHNode*/
230  PHHepMCGenEvent *success = hepmc_helper.insert_event(evt);
231  if (!success)
232  {
233  cout << "ReadSynRadFiles::process_event - Failed to add event to HepMC record!" << endl;
235  }
236 
237  entry++;
238  /* Done */
239  return 0;
240 }
241 
243 {
245 
247 }