EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hepmc3writer.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file hepmc3writer.cpp
1 #include "HepMC3/GenVertex.h"
2 #include "HepMC3/GenVertex_fwd.h"
3 #include "HepMC3/FourVector.h"
4 #include "HepMC3/GenEvent.h"
5 #include "HepMC3/GenParticle.h"
6 #include "HepMC3/WriterAscii.h"
7 #include "HepMC3/Print.h"
8 
9 #include "inputParameters.h"
10 #include "eXevent.h"
11 #include "hepmc3writer.h"
12 
13 using HepMC3::FourVector;
14 using HepMC3::Print;
15 
17 {
18 }
19 
21 {
22 
23  std::string hepmc3_filename = param.baseFileName() + ".hepmc";
24 
26  initBeamHepMC3( param ); // Inits the beams
27  _hepmc3_output = new HepMC3::WriterAscii(hepmc3_filename);
28  return 0;
29 }
30 
32 {
33 
34  //Proton and Electron Mass
35  double p_mass = 0.93827;
36  double e_mass = 0.000511;
37 
38  //Calculate Four Vector
39  double electronBeam_E = e_mass*param.electronBeamLorentzGamma();
40  double targetBeam_E = p_mass*param.targetBeamLorentzGamma();
41  double electronBeam_pz = -1.0*sqrt(electronBeam_E*electronBeam_E - e_mass*e_mass);
42  double targetBeam_pz = sqrt(targetBeam_E*targetBeam_E - p_mass*p_mass);
43 
44  //Define Four Vector to fill HepMC3
45  hepmc3_electronBeam_four_vector_ = FourVector(0,0,electronBeam_pz,electronBeam_E);
46  hepmc3_targetBeam_four_vector_ = FourVector(0,0,targetBeam_pz,targetBeam_E);
48  int nuclear_pid = param.targetBeamA()*10 + param.targetBeamZ()*10000 + 1000000000;// Form 100ZZZAAAl; l=0
49  targetBeam_pdg_id_ = ( param.targetBeamA() > 1 ) ? nuclear_pid : 2212; //Everything is a proton right now
50 
51  return 0;
52 }
53 
54 int hepMC3Writer::writeEvent(const eXEvent &event, int eventnumber)
55 {
56 
58  HepMC3::GenEvent hepmc3_evt(HepMC3::Units::GEV , HepMC3::Units::MM);
59  hepmc3_evt.set_event_number( eventnumber );
60  //HepMC3::GenVertexPtr hepmc3_beam_vertex_to_write = std::make_shared<HepMC3::GenVertex>(FourVector(0,0,0,0));
61  HepMC3::GenVertexPtr hepmc3_gamma_vertex_to_write = std::make_shared<HepMC3::GenVertex>(FourVector(0,0,0,0));
62  HepMC3::GenVertexPtr hepmc3_ion_vertex_to_write = std::make_shared<HepMC3::GenVertex>(FourVector(0,0,0,0));
63 
64  HepMC3::GenParticlePtr hepmc3_electronBeam_particle = std::make_shared<HepMC3::GenParticle>( hepmc3_electronBeam_four_vector_, electronBeam_pdg_id_, 4 );
65  HepMC3::GenParticlePtr hepmc3_targetBeam_particle = std::make_shared<HepMC3::GenParticle>( hepmc3_targetBeam_four_vector_, targetBeam_pdg_id_, 4 );
66 
68  //hepmc3_beam_vertex_to_write->add_particle_out( hepmc3_electronBeam_particle );
69  //hepmc3_beam_vertex_to_write->add_particle_out( hepmc3_targetBeam_particle );
70 
71  hepmc3_gamma_vertex_to_write->add_particle_in( hepmc3_electronBeam_particle );
72  hepmc3_ion_vertex_to_write->add_particle_in( hepmc3_targetBeam_particle );
73  // hepmc3_gamma_vertex_to_write->add_particle_in( hepmc3_electronBeam_particle );
74  // hepmc3_gamma_vertex_to_write->add_particle_in( hepmc3_targetBeam_particle );
75  //hepmc3_vertex_to_write->add_particle_in( hepmc3_electronBeam_particle );
76  //hepmc3_vertex_to_write->add_particle_in( hepmc3_targetBeam_particle );
77 
78 
80  const std::vector<starlightParticle> * particle_vector = event.getParticles();
81 /*
82  lorentzVector beamElectron_lorentzVec = (*event.getSources())[0];
83  HepMC3::GenParticlePtr hepmc3_beamelectron = std::make_shared<HepMC3::GenParticle>( hepmc3_electronBeam_four_vector_, 11, 4 );
84  hepmc3_vertex_to_write->add_particle_in( hepmc3_beamelectron );
85 */
86 
87  lorentzVector gamma_lorentzVec = (*event.getGamma())[0];
88  FourVector hepmc3_gamma_four_vector = FourVector(gamma_lorentzVec.GetPx(),
89  gamma_lorentzVec.GetPy(),
90  gamma_lorentzVec.GetPz(),
91  gamma_lorentzVec.GetE());
92  HepMC3::GenParticlePtr hepmc3_gamma = std::make_shared<HepMC3::GenParticle>( hepmc3_gamma_four_vector, 22, 13 );
93  hepmc3_gamma_vertex_to_write->add_particle_out( hepmc3_gamma );
94  hepmc3_ion_vertex_to_write->add_particle_in( hepmc3_gamma );
95 
97  for ( std::vector<starlightParticle>::const_iterator particle_iter = (*particle_vector).begin();
98  particle_iter != (*particle_vector).end();
99  ++particle_iter)
100  {
101  int hepmc3_pid = (*particle_iter).getPdgCode();
103  FourVector hepmc3_four_vector = FourVector( (*particle_iter).GetPx(),
104  (*particle_iter).GetPy(),
105  (*particle_iter).GetPz(),
106  (*particle_iter).GetE());
107 
108  HepMC3::GenParticlePtr hepmc3_particle = std::make_shared<HepMC3::GenParticle>( hepmc3_four_vector, hepmc3_pid, 1 );
109  hepmc3_ion_vertex_to_write->add_particle_out( hepmc3_particle );
110  }
111 
112  lorentzVector electron_lorentzVec = (*event.getSources())[0];
113  FourVector hepmc3_electron_four_vector = FourVector(electron_lorentzVec.GetPx(),
114  electron_lorentzVec.GetPy(),
115  electron_lorentzVec.GetPz(),
116  electron_lorentzVec.GetE());
117 
118  HepMC3::GenParticlePtr hepmc3_electron = std::make_shared<HepMC3::GenParticle>( hepmc3_electron_four_vector, 11, 1 );
119 
120  hepmc3_gamma_vertex_to_write->add_particle_out( hepmc3_electron );
121 
122  // add code to output ion SRK August 8 2021
123  lorentzVector ion_lorentzVec = (*event.getTarget())[0];
124  FourVector hepmc3_ion_four_vector = FourVector(ion_lorentzVec.GetPx(),
125  ion_lorentzVec.GetPy(),
126  ion_lorentzVec.GetPz(),
127  ion_lorentzVec.GetE());
128 
129  // PDG code for protons is 2212; not clear what to do for ions, but use proton ID for now SRK August 8, 2021
130  HepMC3::GenParticlePtr hepmc3_ion = std::make_shared<HepMC3::GenParticle>( hepmc3_ion_four_vector, 2212, 1 );
131 
132  hepmc3_ion_vertex_to_write->add_particle_out( hepmc3_ion );
133 
134 
135 
137  //hepmc3_evt.add_vertex( hepmc3_beam_vertex_to_write );
138  hepmc3_evt.add_vertex( hepmc3_gamma_vertex_to_write );
139  hepmc3_evt.add_vertex( hepmc3_ion_vertex_to_write );
140  _hepmc3_output->write_event(hepmc3_evt);
141  // Print::listing(hepmc3_evt);
142  // Print::content(hepmc3_evt);
143  hepmc3_evt.clear();
144 
145  return eventnumber;
146 }
147