File indexing completed on 2025-01-30 10:30:44
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "HepMC3/GenEvent.h"
0011 #include "HepMC3/Data/GenEventData.h"
0012 #include "HepMC3/ReaderAscii.h"
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 using namespace HepMC3;
0026
0027 void example_hepmc_reader(const char* in_fname, int n_events)
0028 {
0029 ReaderAscii hepmc_input(in_fname);
0030
0031 int events_parsed = 0;
0032
0033 while(!hepmc_input.failed()) {
0034 GenEvent evt(Units::GEV,Units::MM);
0035
0036
0037
0038 hepmc_input.read_event(evt);
0039
0040
0041
0042 if( hepmc_input.failed() ) break;
0043
0044 {
0045 printf("%d\n", evt.particles_size());
0046
0047 GenEventData data;
0048 evt.write_data(data);
0049 for(auto const &part: data.particles) {
0050 printf(" %8d %8d -> %f %f %f\n", part.pid, part.status, part.momentum.x(), part.momentum.y(), part.momentum.z());
0051 }
0052
0053 }
0054
0055 ++events_parsed;
0056 if (events_parsed == n_events) break;
0057 }
0058
0059 hepmc_input.close();
0060
0061 #if _TODAY_
0062 auto *DatabasePDG = new TDatabasePDG();
0063 auto *pion = DatabasePDG->GetParticle(211), *kaon = DatabasePDG->GetParticle(321);
0064
0065 WriterAscii hepmc_output(out_fname);
0066 int events_parsed = 0;
0067 GenEvent evt(Units::GEV, Units::MM);
0068
0069
0070 TRandom *rdmn_gen = new TRandom(0x12345678);
0071
0072 for (events_parsed = 0; events_parsed < n_events; events_parsed++) {
0073
0074 GenParticlePtr p1 =
0075 std::make_shared<GenParticle>(FourVector(0.0, 0.0, 12.0, 12.0), 11, 4);
0076 GenParticlePtr p2 = std::make_shared<GenParticle>(
0077 FourVector(0.0, 0.0, 100.0, 100.004), 2212, 4);
0078
0079 GenVertexPtr v1 = std::make_shared<GenVertex>();
0080 v1->add_particle_in(p1);
0081 v1->add_particle_in(p2);
0082
0083 {
0084 Double_t eta = rdmn_gen->Uniform(2.50, 2.51);
0085 Double_t th = 2*std::atan(exp(-eta));
0086 Double_t p = rdmn_gen->Uniform(7.0, 7.1);
0087 Double_t phi = rdmn_gen->Uniform(0.0, 2*M_PI);
0088
0089 Double_t px = p * std::cos(phi) * std::sin(th);
0090 Double_t py = p * std::sin(phi) * std::sin(th);
0091 Double_t pz = p * std::cos(th);
0092
0093 auto particle = events_parsed%2 ? pion : kaon;
0094 GenParticlePtr pq = std::make_shared<GenParticle>(FourVector(
0095 px, py, pz,
0096 sqrt(p*p + pow(particle->Mass(), 2))),
0097 particle->PdgCode(), 1);
0098 v1->add_particle_out(pq);
0099 }
0100
0101 evt.add_vertex(v1);
0102
0103 if (events_parsed == 0) {
0104 std::cout << "First event: " << std::endl;
0105 Print::listing(evt);
0106 }
0107
0108 hepmc_output.write_event(evt);
0109 if (events_parsed % 10000 == 0) {
0110 std::cout << "Event: " << events_parsed << std::endl;
0111 }
0112 evt.clear();
0113 }
0114 hepmc_output.close();
0115 std::cout << "Events parsed and written: " << events_parsed << std::endl;
0116 #endif
0117 exit(0);
0118 }