File indexing completed on 2025-08-03 08:16:38
0001 #include <HepMC/GenEvent.h>
0002 #include <HepMC/GenParticle.h>
0003 #include <HepMC/GenVertex.h>
0004 #include <HepMC/IO_BaseClass.h> // for IO_BaseClass
0005 #include <HepMC/IO_GenEvent.h>
0006 #include <HepMC/SimpleVector.h> // for FourVector
0007 #include <HepMC/Units.h> // for GEV, MM
0008
0009 #include <CLHEP/Vector/LorentzVector.h>
0010
0011 #include <TDatabasePDG.h>
0012 #include <TParticlePDG.h> // for TParticlePDG
0013
0014 #include <cmath>
0015 #include <fstream>
0016 #include <iostream>
0017 #include <sstream>
0018 #include <string>
0019
0020 TDatabasePDG* PDGdb;
0021
0022
0023
0024 int fillEvent(HepMC::GenEvent* evt, std::ifstream& file)
0025 {
0026 std::string line;
0027 std::string label;
0028
0029 int found_event = 0;
0030 while ( getline( file, line ) )
0031 {
0032
0033 if ( line.starts_with("EVENT") )
0034 {
0035 found_event = 1;
0036 break;
0037 }
0038 }
0039
0040 if ( found_event == 1 )
0041 {
0042 int nevt, ntrk, nvtx;
0043
0044 std::stringstream evtline( line );
0045
0046 evtline >> label >> nevt >> ntrk >> nvtx;
0047
0048
0049
0050 if (label != "EVENT:")
0051 {
0052 std::cout << "label is " << label << std::endl;
0053 return -1;
0054 }
0055
0056 static int nprint = 100;
0057 if (nevt % nprint == 0)
0058 {
0059 std::cout << nevt << std::endl;
0060 if ( nevt>10000 )
0061 {
0062 nprint = 10000;
0063 }
0064 else if ( nevt>1000 )
0065 {
0066 nprint = 1000;
0067 }
0068 }
0069
0070 evt->set_event_number(nevt);
0071
0072
0073
0074
0075 double x, y, z, t;
0076 int nv, nproc, npar, ndau;
0077 file >> label >> x >> y >> z >> t >> nv >> nproc >> npar >> ndau;
0078 if (label != "VERTEX:")
0079 {
0080 return -1;
0081 }
0082 CLHEP::HepLorentzVector newVertex;
0083 newVertex = CLHEP::HepLorentzVector(x, y, z, t);
0084 HepMC::GenVertex* v0 = new HepMC::GenVertex(newVertex);
0085 evt->add_vertex(v0);
0086
0087
0088 double px, py, pz;
0089 int gpid, nev, ntr, stopv, pdgpid;
0090 for (int itrk = 0; itrk < ndau; itrk++)
0091 {
0092 file >> label >> gpid >> px >> py >> pz >> nev >> ntr >> stopv >> pdgpid;
0093 if (label != "TRACK:")
0094 {
0095 return -1;
0096 }
0097
0098
0099 TParticlePDG* partinfo = PDGdb->GetParticle(pdgpid);
0100 double mass = partinfo->Mass();
0101
0102
0103 double E = sqrt(mass * mass + px * px + py * py + pz * pz);
0104
0105 CLHEP::HepLorentzVector p4(px, py, pz, E);
0106
0107 const int particle_status = 1;
0108
0109
0110 v0->add_particle_out(new HepMC::GenParticle(p4, pdgpid, particle_status));
0111 }
0112 }
0113 else
0114 {
0115
0116 return -2;
0117 }
0118
0119 return 1;
0120 }
0121
0122
0123 int main(int argc, char* argv[])
0124 {
0125 if (argc != 3)
0126 {
0127 std::cerr << "Usage: " << argv[0] << "<starlight_file> <HepMC_output_file>" << std::endl;
0128 return 1;
0129 }
0130
0131
0132
0133 std::string starlightfname = argv[1];
0134 std::string hepmcfname = argv[2];
0135
0136
0137 std::ifstream starlightfile(starlightfname);
0138 if (!starlightfile.is_open())
0139 {
0140 std::cerr << " STARlight file not found: " << starlightfname << std::endl;
0141 return -1;
0142 }
0143
0144
0145 PDGdb = TDatabasePDG::Instance();
0146
0147
0148 std::ofstream outputFile(hepmcfname);
0149 HepMC::IO_GenEvent ascii_io(hepmcfname.c_str(), std::ios::out);
0150
0151
0152
0153 while (true)
0154 {
0155
0156
0157 HepMC::GenEvent* evt = new HepMC::GenEvent();
0158 evt->use_units(HepMC::Units::GEV, HepMC::Units::MM);
0159
0160 int status = fillEvent(evt, starlightfile);
0161 if (status < 0)
0162 {
0163 delete evt;
0164 break;
0165 }
0166
0167 ascii_io << evt;
0168
0169 delete evt;
0170 }
0171
0172 std::cout << "Conversion completed successfully. HEPMC file saved to " << hepmcfname << std::endl;
0173
0174 starlightfile.close();
0175
0176 return 0;
0177 }