9#include <generators/hepmc/HepMCReader.h>
10#include <framework/gearbox/Unit.h>
11#include <framework/gearbox/Const.h>
12#include <framework/logging/Logger.h>
14#include <HepMC/IO_GenEvent.h>
16#include <Math/Vector4D.h>
24 B2INFO(
"Reading HEPMC inputfile at " << filename);
26 if (!
m_input) {
throw (HepMCCouldNotOpenFileError() << filename); }
32 int eventID = evt.event_number();
37 eventID = evt.event_number();
51 if (eventID == -999) {
throw HepMCInvalidEventError(); }
54 const int nparticles = evt.particles_size();
56 B2DEBUG(20,
"Found eventID " << eventID <<
" with " << nparticles <<
" particles.");
58 if (nparticles <= 0) {
59 throw (HepMCInvalidEventError());
61 const int event_offset = graph.
size();
63 for (
int i = 0; i < nparticles; i++) {
67 std::unordered_map<int, int> hash_index_map;
68 HepMC::GenEvent::particle_iterator tmp_particle = evt.particles_begin();
69 for (
int i = 0; i < nparticles; ++i) {
71 const int hash = (*tmp_particle)->barcode();
72 hash_index_map[hash] = i;
75 const double len_conv = HepMC::Units::conversion_factor(evt.length_unit(), HepMC::Units::CM);
76 const double mom_conv = HepMC::Units::conversion_factor(evt.momentum_unit(), HepMC::Units::GEV);
77 auto read_particle = evt.particles_begin();
79 for (
int i = 0; i < nparticles; ++i) {
80 auto* decay_vertex = (*read_particle)->end_vertex();
81 auto* production_vertex = (*read_particle)->production_vertex();
85 const int status = (*read_particle)->status();
86 const bool isFinalstate = !decay_vertex && status == 1;
87 const bool isVirtual = (status == 4) || (status == 21) || (status == 22) || (status == 23) || (status == 51) || (status == 52)
89 const int pdg_code = (*read_particle)->pdg_id() ;
90 const double mass = (*read_particle)->generated_mass() * mom_conv;
91 auto const mom_tmp = (*read_particle)->momentum();
93 const HepMC::FourVector momentum(
100 B2DEBUG(20,
"Read particle: status " << status <<
" isFinal " << isFinalstate <<
" isVirtual " << isVirtual <<
" pdg " << pdg_code
101 <<
" mass " << mass <<
" px " << momentum.x() <<
" py " << momentum.y() <<
" px " << momentum.z() <<
" E " << momentum.t());
104 p.setMomentum(ROOT::Math::XYZVector(momentum.x(), momentum.y(), momentum.z()));
105 p.setEnergy(momentum.t());
107 if (production_vertex) {
108 const auto pos = production_vertex->position();
109 p.setProductionVertex(ROOT::Math::XYZVector(pos.x(), pos.y(), pos.z()) * len_conv *
Unit::cm);
111 p.setValidVertex(
true);
120 if (production_vertex) {
121 if (production_vertex->particles_in_const_begin() != production_vertex->particles_in_const_end()) {
122 auto parent = production_vertex->particles_begin(HepMC::parents);
123 const int parent_index_in_graph = hash_index_map[(*parent)->barcode()];
124 p.comesFrom(graph[event_offset + parent_index_in_graph]);
129 ROOT::Math::PxPyPzEVector p4 = p.get4Vector();
130 p4.SetPz(-1.0 * p4.Pz());
142 B2DEBUG(20,
"Returning event id " << eventID);
151 if (evt.is_valid()) {
152 const int nparticles = evt.particles_size();
153 B2DEBUG(20,
"Found valid event.i N particles " << nparticles);
156 B2DEBUG(20,
"The next event was invalid. Will stop reading now.");
165 HepMC::IO_GenEvent ascii_in(filename.c_str(), std::ios::in);
167 HepMC::GenEvent* evt = ascii_in.read_next_event();
169 evt = ascii_in.read_next_event();
172 B2INFO(
"Counted " << count <<
" events in " << filename <<
".");
static const double speedOfLight
[cm/ns]
void readNextEvent(HepMC::GenEvent &evt)
read the next event from the IO stream and write into evt
int countEvents(const std::string &filename)
Count events in file.
void open(const std::string &filename)
Opens an ascii file and prepares it for reading.
std::ifstream m_input
The input stream of the ascii file.
const int m_maxEvent
max events to process
int getEvent(MCParticleGraph &graph, double &weight)
Reads the next event and stores the result in the given MCParticle graph.
bool m_wrongSignPz
Bool to indicate that HER and LER were swapped.
int nextValidEvent(HepMC::GenEvent &evt)
Got to next event in bounds.
const int m_minEvent
min event nr to process
int m_nVirtual
The number of particles in each event with a set Virtual flag.
Class to represent Particle data in graph.
Class to build, validate and sort a particle decay chain.
size_t size() const
Return the number of particles in the graph.
@ c_PrimaryParticle
bit 0: Particle is primary particle.
static const double cm
Standard units with the value = 1.
static const double GeV
Standard of [energy, momentum, mass].
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.