EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
eventfilewriter.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file eventfilewriter.cpp
1 
2 //
3 // Copyright 2010
4 //
5 // This file is part of starlight.
6 //
7 // starlight is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // starlight is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with starlight. If not, see <http://www.gnu.org/licenses/>.
19 //
21 //
22 // File and Version Information:
23 // $Rev:: 228 $: revision of last commit
24 // $Author:: butter $: author of last commit
25 // $Date:: 2016-01-18 17:10:17 +0000 #$: date of last commit
26 //
27 // Description:
28 //
29 //
30 //
32 
33 #ifdef HEPMC3_ON
34 #include "hepmc3writer.h"
35 #endif
36 
37 #include <iostream>
38 #include <exception>
39 #include <cstdlib>
40 
41 #include "eventfilewriter.h"
42 #include "starlightparticlecodes.h"
43 
44 using namespace std;
45 
46 //______________________________________________________________________________
48 : fileWriter()
49 ,_writeFullPythia(false)
50 ,_writeFullHepMC3(false)
51 { }
52 
53 //______________________________________________________________________________
55 : fileWriter(filename)
56 { }
57 
58 //______________________________________________________________________________
60 {
61  _fileStream<<"CONFIG_OPT: "<<_p.productionMode()<<" "<<_p.prodParticleId()<<" "<<_p.nmbEvents()
62  <<" "<<_p.quantumGlauber()<<" "<<_p.impulseVM()<<" "<<_p.randomSeed()<<std::endl;
63  _fileStream<<"ELECTRON_BEAM: "<<" "<<_p.electronBeamLorentzGamma()<<std::endl;
64  _fileStream<<"TARGET_BEAM: "<<_p.targetBeamZ()<<" "<<_p.targetBeamA()<<" "<<_p.targetBeamLorentzGamma()<<std::endl;
65  _fileStream<<"PHOTON: "<<_p.nmbEnergyBins()<<" "<<_p.fixedQ2Range()<<" "<<_p.nmbGammaQ2Bins()
66  <<" "<<_p.minGammaQ2()<<" "<<_p.maxGammaQ2()<<std::endl;
67 
68  if ( _writeFullHepMC3 )
69  {
70 #ifdef HEPMC3_ON
71  _hepmc3writer = new hepMC3Writer();
72  _hepmc3writer->initWriter(_p);
73 #else
74  std::cout << "***HepMC3 file format not written --- eStarlight not compiled with HepMC3 Enabled***" << std::endl;
75  _writeFullHepMC3 = false;
76 #endif
77  }
78 
79  return 0;
80 }
81 
82 //______________________________________________________________________________
84 {
85  _fileStream<<"PYTHIA EVENT FILE"<<std::endl;
86  _fileStream<<"============================================"<<std::endl;
87  _fileStream<<"I, ievent, genevent, subprocess, nucleon, targetparton, xtargparton, beamparton, xbeamparton, thetabeamprtn, truey, trueQ2, truex, trueW2, trueNu, leptonphi, s_hat, t_hat, u_hat, pt2_hat, Q2_hat, F2, F1, R, sigma_rad, SigRadCor, EBrems, photonflux, t-diff, nrTracks"<<std::endl;
88  _fileStream<<"============================================"<<std::endl;
89  _fileStream<<"I K(I,1) K(I,2) K(I,3) K(I,4) K(I,5) P(I,1) P(I,2) P(I,3) P(I,4) P(I,5) V(I,1) V(I,2) V(I,3)"<<std::endl;
90  _fileStream<<"============================================"<<std::endl;
91 
92  //Proton and Electron Mass
93  double p_mass = 0.93827;
94  double e_mass = 0.000511;
95 
96  //Calculate Four Vector
97  double _electronBeam_E = e_mass*_p.electronBeamLorentzGamma();
98  double _targetBeam_E = p_mass*_p.targetBeamLorentzGamma();
99  double _electronBeam_pz = -1.0*sqrt(_electronBeam_E*_electronBeam_E - e_mass*e_mass);
100  double _targetBeam_pz = sqrt(_targetBeam_E*_targetBeam_E - p_mass*p_mass);
101 
102  //Define Four Vector
103  _electronBeam_four_vector_={0,0,_electronBeam_pz,_electronBeam_E};
104  _targetBeam_four_vector_ ={0,0,_targetBeam_pz,_targetBeam_E};
106  int nuclear_pid = _p.targetBeamA()*10 + _p.targetBeamZ()*10000 + 1000000000;// Form 100ZZZAAAl; l=0
107  _targetBeam_pdg_id_ = ( _p.targetBeamA() > 1 ) ? nuclear_pid : 2212; //Everything is a proton right now
108 
109  return 0;
110 }
111 
112 //______________________________________________________________________________
113 int eventFileWriter::writeEvent(eXEvent &event, int eventnumber)
114 {
115 
116  int numberoftracks = 0;
117  if(_writeFullPythia)
118  {
119  numberoftracks = event.getParticles()->size();
120  }
121  else
122  {
123  for(unsigned int i = 0; i<event.getParticles()->size(); ++i)
124  {
125  if(event.getParticles()->at(i).getStatus() >= 0) numberoftracks++;
126  }
127  }
128 
129 
130  // sometimes we don't have tracks due to wrongly picked W , check it
131  if(numberoftracks){
132  eventnumber++;
133 
134  _fileStream << "EVENT: " << eventnumber << " " << numberoftracks << " " << 1 << std::endl;
135 
136  _fileStream <<"VERTEX: "<<0.<<" "<<0.<<" "<<0.<<" "<<0.<<" "<<1<<" "<<0<<" "<<0<<" "<<numberoftracks<<std::endl;
137 
138  for( uint igam = 0 ; igam < event.getGammaEnergies()->size(); ++igam){
139  _fileStream <<"GAMMA: "<<event.getGammaEnergies()->at(igam)<<" "<<event.getGammaMasses()->at(igam)<<std::endl;
140  lorentzVector gam = event.getGamma()->at(igam);
141  // Write the photon 4-vector out to file. Might be used in later iterations, so I keep it here
142  //_fileStream <<"GAMMA VECTOR: "<<gam.GetPx()<<" "<<gam.GetPy()<<" "<<gam.GetPz()<<" "<<gam.GetE()<<" "<<-temp<<std::endl;
143  }
144  for( uint itarget = 0 ; itarget < event.getTarget()->size(); ++itarget){
145  lorentzVector target = event.getTarget()->at(itarget);
146  _fileStream <<"t: "<<event.getVertext()->at(itarget)<<std::endl;
147  _fileStream <<"TARGET: "<<target.GetPx()<<" "<<target.GetPy()<<" "<<target.GetPz()<<" "<<target.GetE()<<std::endl;
148  }
149  for( uint iel = 0 ; iel < event.getSources()->size(); ++iel){
150  lorentzVector el = event.getSources()->at(iel);
151  _fileStream <<"SOURCE: "<<el.GetPx()<<" "<<el.GetPy()<<" "<<el.GetPz()<<" "<<el.GetE()<<std::endl;
152  }
153 
154  int ipart = 0;
155  std::vector<starlightParticle>::const_iterator part = (event.getParticles())->begin();
156 
157  for (part = event.getParticles()->begin(); part != event.getParticles()->end(); part++, ipart++)
158  {
159  if(!_writeFullPythia)
160  {
161  if((*part).getStatus() < 0) continue;
162  }
163  _fileStream << "TRACK: " << " "<< starlightParticleCodes::jetsetToGeant((*part).getPdgCode()) <<" "<< (*part).GetPx() << " " << (*part).GetPy()
164  << " "<< (*part).GetPz() << " " << eventnumber << " " << ipart << " " << 0 << " "
165  << (*part).getPdgCode();
166 
167  if(_writeFullPythia)
168  {
169  lorentzVector vtx = (*part).getVertex();
170  _fileStream << " " << vtx.GetPx() << " " << vtx.GetPy() << " " << vtx.GetPz() << " " << vtx.GetE();
171  _fileStream << " " << (*part).getFirstParent() << " " << (*part).getLastParent() << " " << (*part).getFirstDaughter() << " " << (*part).getLastDaughter() << " " << (*part).getStatus();
172  }
173 
174  _fileStream <<std::endl;
175  }
176  }
177 
178 #ifdef HEPMC3_ON
179  if ( _writeFullHepMC3 ){
180  _hepmc3writer->writeEvent(event,eventnumber);
181  }
182 #endif
183 
184  return 0;
185 }
186 
187 
188 //______________________________________________________________________________
189 int eventFileWriter::writeEventLUND(eXEvent &event, int eventnumber)
190 {
191  _fileStream << "0 "<<eventnumber<<" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"<<std::endl;
192  _fileStream<<"============================================"<<std::endl;
193 
194  int particleIndex=1;
195  uint lineNumberOfFirstElectronDaughter=3;
196  uint lineNumberOfLastElectronDaughter=4;
197  uint lineNumberOfFirstIonDaughter=6;
198  uint lineNumberOfLastIonDaughter=lineNumberOfFirstIonDaughter+event.getParticles()->size();
199  uint numberOfElectrons = event.getSources()->size();
200  uint numberOfIons = event.getTarget()->size();
201  uint numberOfGammas = event.getGammaEnergies()->size();
202 
203  auto sp0 = std::setw(8);//column widths
204  auto sp1 = std::setw(14);
205 
207  _fileStream <<particleIndex << sp0 <<21 << sp0 <<_electronBeam_pdg_id_<<" "<<0<<" "<<lineNumberOfFirstElectronDaughter<<" "<<lineNumberOfLastElectronDaughter<<" " << std::setprecision(6) << sp1 <<_electronBeam_four_vector_[0]<<" " << sp1 <<_electronBeam_four_vector_[1]<<" " << sp1 <<_electronBeam_four_vector_[2]<<" " <<sp1<<_electronBeam_four_vector_[3]<<" "<<sp1<<electronmass<<" 0 0 0"<<std::endl;
208  particleIndex++;
209 
211  _fileStream <<particleIndex <<sp0<<21 << sp0 <<_targetBeam_pdg_id_<<" "<<0<<" "<<lineNumberOfFirstIonDaughter<<" "<<lineNumberOfLastIonDaughter<<" " << sp1 <<_targetBeam_four_vector_[0]<<" " <<sp1<<_targetBeam_four_vector_[1]<<" " <<sp1<<_targetBeam_four_vector_[2]<<" " <<sp1<<_targetBeam_four_vector_[3]<<" "<<sp1<<ionmass<<" 0 0 0"<<std::endl;
212  particleIndex++;
213 
214  //FOR ALEX
215  for( uint iel = 0 ; iel < numberOfElectrons; ++iel){
216  lorentzVector el = event.getSources()->at(iel);
217  _fileStream <<particleIndex<<sp0<<21 <<sp0<<11<<" "<<1<<" "<<0<<" "<<0<<" " <<sp1<<el.GetPx()<<" " <<sp1<<el.GetPy()<<" " <<sp1<<el.GetPz()<<" " <<sp1<<el.GetE()<<" "<<sp1<<electronmass<<" 0 0 0"<<std::endl;
218  particleIndex++;
219  }
220 
221  for( uint igam = 0 ; igam < numberOfGammas; ++igam){
222  lorentzVector gam = event.getGamma()->at(igam);
223  _fileStream <<particleIndex <<sp0<<21 <<sp0<<22<<" "<<1<<" "<<0<<" "<<0<<" " <<sp1<<gam.GetPx()<<" " <<sp1<<gam.GetPy()<<" " <<sp1<<gam.GetPz()<<" " <<sp1<<event.getGammaEnergies()->at(igam)<<" "<<sp1<<event.getGammaMasses()->at(igam)<<" 0 0 0"<<std::endl;
224  particleIndex++;
225  }
226 
227 
228 
229  for( uint iel = 0 ; iel < numberOfElectrons; ++iel){
230  lorentzVector el = event.getSources()->at(iel);
231  _fileStream <<particleIndex<<sp0<<1 <<sp0<<11<<" "<<3<<" "<<0<<" "<<0<<" " <<sp1<<el.GetPx()<<" " <<sp1<<el.GetPy()<<" " <<sp1<<el.GetPz()<<" " <<sp1<<el.GetE()<<" "<<sp1<<electronmass<<" 0 0 0"<<std::endl;
232  particleIndex++;
233  }
234 
235  //FIXME This is only set up for protons right now
236  for( uint itarget = 0 ; itarget < numberOfIons; ++itarget){
237  lorentzVector target = event.getTarget()->at(itarget);
238  _fileStream <<particleIndex <<sp0<<1 <<sp0<<2212<<" "<<2<<" "<<lineNumberOfFirstIonDaughter<<" "<<lineNumberOfLastIonDaughter<<" " <<sp1<<target.GetPx()<<" " <<sp1<<target.GetPy()<<" " <<sp1<<target.GetPz()<<" " <<sp1<<target.GetE()<<" "<<sp1<<ionmass<<" 0 0 0"<<std::endl;
239  particleIndex++;
240  }
241 
242 
243  int ipart = 0;
244  std::vector<starlightParticle>::const_iterator part = (event.getParticles())->begin();
245 
246  for (part = event.getParticles()->begin(); part != event.getParticles()->end(); part++, ipart++)
247  {
248  _fileStream <<particleIndex <<sp0<<1 <<sp0<<(*part).getPdgCode()<<" "<<2<<" "<<0<<" "<<0<<" " <<sp1<<(*part).GetPx()<<" " <<sp1<<(*part).GetPy()<<" " <<sp1<<(*part).GetPz()<<" " <<sp1<<(*part).GetE()<<" "<<sp1<<(*part).M()<<" 0 0 0"<<std::endl;
249  particleIndex++;
250  }
251 
252 
253  _fileStream<<"=============== Event finished ==============="<<std::endl;
254  return 0;
255 }
256 
257 
259 {
260 
261 #ifdef HEPMC3_ON
262  if ( _writeFullHepMC3 ){
263  _hepmc3writer->close();
264  }
265 #endif
266 
267  try
268  {
269  _fileStream.close();
270  }
271  catch (const ios::failure & error)
272  {
273  cerr << "I/O exception: " << error.what() << endl;
274  return EXIT_FAILURE;
275  }
276  return 0;
277 }