26 m_file(filename), m_stream(0), m_isstream(false) {
28 HEPMC3_ERROR(
"ReaderAsciiHepMC2: could not open input file: " << filename )
35 : m_stream(&stream), m_isstream(true)
38 HEPMC3_ERROR(
"ReaderAsciiHepMC2: could not open input stream ")
48 const size_t max_buffer_size = 512*512;
49 char buf[max_buffer_size];
55 if ( peek ==
'E' ) nn--;
56 if (nn < 0)
return true;
66 const size_t max_buffer_size = 512*512;
67 char buf[max_buffer_size];
68 bool parsed_event_header =
false;
69 bool is_parsing_successful =
true;
70 int parsing_result = 0;
71 unsigned int vertices_count = 0;
72 unsigned int current_vertex_particles_count = 0;
73 unsigned int current_vertex_particles_parsed = 0;
91 if ( strlen(buf) == 0 )
continue;
93 if ( strncmp(buf,
"HepMC", 5) == 0 ) {
94 if ( strncmp(buf,
"HepMC::Version", 14) != 0 && strncmp(buf,
"HepMC::IO_GenEvent", 18) != 0 )
96 HEPMC3_WARNING(
"ReaderAsciiHepMC2: found unsupported expression in header. Will close the input.")
97 std::cout <<buf << std::endl;
100 if (parsed_event_header) {
101 is_parsing_successful =
true;
109 if (parsing_result < 0) {
110 is_parsing_successful =
false;
111 HEPMC3_ERROR(
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing event information")
114 vertices_count = parsing_result;
121 evt.
reserve(vertices_count,vertices_count*3);
122 is_parsing_successful =
true;
124 parsed_event_header =
true;
131 if (current_vertex_particles_parsed < current_vertex_particles_count) {
132 is_parsing_successful =
false;
135 current_vertex_particles_parsed = 0;
139 if (parsing_result < 0) {
140 is_parsing_successful =
false;
141 HEPMC3_ERROR(
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing vertex information")
144 current_vertex_particles_count = parsing_result;
145 is_parsing_successful =
true;
152 if (parsing_result < 0) {
153 is_parsing_successful =
false;
154 HEPMC3_ERROR(
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing particle information")
157 ++current_vertex_particles_parsed;
158 is_parsing_successful =
true;
177 HEPMC3_WARNING(
"ReaderAsciiHepMC2: skipping unrecognised prefix: " << buf[0])
178 is_parsing_successful =
true;
182 if ( !is_parsing_successful )
break;
186 if ( parsed_event_header && peek ==
'E' )
break;
192 if (is_parsing_successful && current_vertex_particles_parsed < current_vertex_particles_count) {
193 HEPMC3_ERROR(
"ReaderAsciiHepMC2: not all particles parsed")
194 is_parsing_successful =
false;
197 else if (is_parsing_successful &&
m_vertex_cache.size() != vertices_count) {
198 HEPMC3_ERROR(
"ReaderAsciiHepMC2: not all vertices parsed")
199 is_parsing_successful =
false;
202 if ( !is_parsing_successful ) {
203 HEPMC3_ERROR(
"ReaderAsciiHepMC2: event parsing failed. Returning empty event")
204 HEPMC3_DEBUG(1,
"Parsing failed at line:" << std::endl << buf)
227 std::vector<GenParticlePtr> beams;
228 for (
auto p:
m_vertex_cache[i]->particles_out())
if (p->status() == 4 && !(p->end_vertex())) beams.push_back(p);
233 HEPMC3_DEBUG(30,
"ReaderAsciiHepMC2::read_event - moved particle with status=4 from the outgoing to the incoming particles of vertex: " <<
m_vertex_cache[i]->
id());
252 if (random_states_a) {
253 std::vector<long int> random_states_v = random_states_a->
value();
254 for (
size_t i = 0; i < random_states_v.size(); ++i )
255 evt.
add_attribute(
"random_states" + std::to_string((
long long unsigned int)i), std::make_shared<IntAttribute>(random_states_v[i]));
261 std::map< std::string, std::map<int, std::shared_ptr<Attribute> > > cached_attributes =
m_event_ghost->
attributes();
262 if (cached_attributes.find(
"flows") != cached_attributes.end()) {
263 std::map<int, std::shared_ptr<Attribute> > flows = cached_attributes.at(
"flows");
267 for (
auto f: flows)
if (f.first > 0 && f.first <= (
int)
m_particle_cache.size()) {
268 std::shared_ptr<VectorIntAttribute> casted = std::dynamic_pointer_cast<
VectorIntAttribute>(f.second);
269 if (!casted)
continue;
270 std::vector<int> this_p_flow = casted->
value();
271 for (
size_t i = 0; i<this_p_flow.size(); i++)
m_particle_cache[f.first-1]->add_attribute(
"flow" + std::to_string(i + 1), std::make_shared<IntAttribute>(this_p_flow[i]));
276 if (cached_attributes.find(
"phi") != cached_attributes.end()) {
277 std::map<int, std::shared_ptr<Attribute> > phi = cached_attributes.at(
"phi");
281 if (cached_attributes.find(
"theta") != cached_attributes.end()) {
282 std::map<int, std::shared_ptr<Attribute> > theta = cached_attributes.at(
"theta");
286 if (cached_attributes.find(
"weights") != cached_attributes.end()) {
287 std::map<int, std::shared_ptr<Attribute> > weights = cached_attributes.at(
"weights");
289 for (
auto f: weights)
if (f.first < 0 && f.first >= -(
int)
m_vertex_cache.size())
m_vertex_cache[-f.first-1]->add_attribute(
"weights", f.second);
291 for (
auto f: weights)
if (f.first < 0 && f.first >= -(
int)
m_vertex_cache.size()) {
292 std::shared_ptr<VectorDoubleAttribute> casted = std::dynamic_pointer_cast<
VectorDoubleAttribute>(f.second);
293 if (!casted)
continue;
294 std::vector<double> this_v_weight = casted->
value();
295 for (
size_t i = 0; i < this_v_weight.size(); i++)
m_particle_cache[-f.first-1]->add_attribute(
"weight"+std::to_string(i), std::make_shared<DoubleAttribute>(this_v_weight[i]));
300 std::shared_ptr<IntAttribute> signal_process_vertex_barcode = evt.
attribute<
IntAttribute>(
"signal_process_vertex");
301 if (signal_process_vertex_barcode) {
302 int signal_process_vertex_barcode_value = signal_process_vertex_barcode->
value();
307 std::shared_ptr<IntAttribute> signal_process_vertex = std::make_shared<IntAttribute>(
m_vertex_cache.at(i)->id());
308 evt.
add_attribute(
"signal_process_vertex", signal_process_vertex);
319 const char *cursor = buf;
320 int vertices_count = 0;
321 int random_states_size = 0;
322 int weights_size = 0;
323 std::vector<long> random_states(0);
324 std::vector<double> weights(0);
327 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
331 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
332 evt.
add_attribute(
"mpi", std::make_shared<IntAttribute>(atoi(cursor)));
335 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
336 evt.
add_attribute(
"event_scale", std::make_shared<DoubleAttribute>(atof(cursor)));
339 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
340 evt.
add_attribute(
"alphaQCD", std::make_shared<DoubleAttribute>(atof(cursor)));
343 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
344 evt.
add_attribute(
"alphaQED", std::make_shared<DoubleAttribute>(atof(cursor)));
347 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
348 evt.
add_attribute(
"signal_process_id", std::make_shared<IntAttribute>(atoi(cursor)));
351 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
352 evt.
add_attribute(
"signal_process_vertex", std::make_shared<IntAttribute>(atoi(cursor)));
355 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
356 vertices_count = atoi(cursor);
359 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
362 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
365 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
366 random_states_size = atoi(cursor);
367 random_states.resize(random_states_size);
369 for (
int i = 0; i < random_states_size; ++i ) {
370 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
371 random_states[i] = atoi(cursor);
374 if (random_states.size()) evt.
add_attribute(
"random_states", std::make_shared<VectorLongIntAttribute>(random_states));
377 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
378 weights_size = atoi(cursor);
379 weights.resize(weights_size);
381 for (
int i = 0; i < weights_size; ++i ) {
382 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
383 weights[i] = atof(cursor);
388 HEPMC3_DEBUG(10,
"ReaderAsciiHepMC2: E: " << evt.
event_number() <<
" (" << vertices_count <<
"V, " << weights_size <<
"W, " << random_states_size <<
"RS)")
390 return vertices_count;
394 const char *cursor = buf;
397 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
402 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
406 evt.
set_units(momentum_unit, length_unit);
414 GenVertexPtr data = std::make_shared<GenVertex>();
415 GenVertexPtr data_ghost = std::make_shared<GenVertex>();
416 const char *cursor = buf;
418 int num_particles_out = 0;
419 int weights_size = 0;
420 std::vector<double> weights(0);
422 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
423 barcode = atoi(cursor);
426 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
427 data->set_status(atoi(cursor));
430 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
431 double X(atof(cursor));
434 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
435 double Y(atof(cursor));
438 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
439 double Z(atof(cursor));
442 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
443 double T(atof(cursor));
447 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
450 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
451 num_particles_out = atoi(cursor);
454 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
455 weights_size = atoi(cursor);
456 weights.resize(weights_size);
458 for (
int i = 0; i < weights_size; ++i ) {
459 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
460 weights[i] = atof(cursor);
471 if (weights.size()) data_ghost->add_attribute(
"weights", std::make_shared<VectorDoubleAttribute>(weights));
475 HEPMC3_DEBUG(10,
"ReaderAsciiHepMC2: V: " << -(
int)
m_vertex_cache.size() <<
" (old barcode" << barcode <<
") " << num_particles_out <<
" particles)")
477 return num_particles_out;
481 GenParticlePtr data = std::make_shared<GenParticle>();
482 GenParticlePtr data_ghost = std::make_shared<GenParticle>();
484 const char *cursor = buf;
488 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
491 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
492 data->set_pid(atoi(cursor));
495 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
496 double Px(atof(cursor));
499 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
500 double Py(atof(cursor));
503 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
504 double Pz(atof(cursor));
507 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
508 double E(atof(cursor));
512 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
513 data->set_generated_mass(atof(cursor));
516 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
517 data->set_status(atoi(cursor));
520 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
521 double theta_v = atof(cursor);
522 if (theta_v != 0.0) data_ghost->add_attribute(
"theta", std::make_shared<DoubleAttribute>(theta_v));
525 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
526 double phi_v = atof(cursor);
527 if (phi_v != 0.0) data_ghost->add_attribute(
"phi", std::make_shared<DoubleAttribute>(phi_v));
530 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
531 end_vtx = atoi(cursor);
534 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
535 int flowsize = atoi(cursor);
537 std::map<int, int> flows;
538 for (
int i = 0; i < flowsize; i++)
540 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
541 int flowindex = atoi(cursor);
542 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
543 int flowvalue = atoi(cursor);
544 flows[flowindex] = flowvalue;
548 std::vector<int> vectorflows;
549 for (
auto f: flows) vectorflows.push_back(f.second);
550 data_ghost->add_attribute(
"flows", std::make_shared<VectorIntAttribute>(vectorflows));
571 const char *cursor = buf;
572 std::shared_ptr<GenCrossSection> xs = std::make_shared<GenCrossSection>();
574 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
575 double xs_val = atof(cursor);
577 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
578 double xs_err = atof(cursor);
580 xs->set_cross_section(xs_val, xs_err);
587 const char *cursor = buf;
588 const char *cursor2 = buf;
590 std::vector<std::string> w_names;
595 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
596 w_count = atoi(cursor);
598 if ( w_count <= 0 )
return false;
600 w_names.resize(w_count);
602 for (
int i=0; i < w_count; ++i ) {
604 if ( !(cursor = strchr(cursor+1,
'"')) )
return false;
605 if ( !(cursor2 = strchr(cursor+1,
'"')) )
return false;
610 w_names[i].assign(cursor, cursor2-cursor);
615 run_info()->set_weight_names(w_names);
621 std::shared_ptr<GenHeavyIon> hi = std::make_shared<GenHeavyIon>();
622 const char *cursor = buf;
624 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
625 hi->Ncoll_hard = atoi(cursor);
627 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
628 hi->Npart_proj = atoi(cursor);
630 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
631 hi->Npart_targ = atoi(cursor);
633 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
634 hi->Ncoll = atoi(cursor);
636 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
637 hi->spectator_neutrons = atoi(cursor);
639 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
640 hi->spectator_protons = atoi(cursor);
642 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
643 hi->N_Nwounded_collisions = atoi(cursor);
645 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
646 hi->Nwounded_N_collisions = atoi(cursor);
648 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
649 hi->Nwounded_Nwounded_collisions = atoi(cursor);
651 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
652 hi->impact_parameter = atof(cursor);
654 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
655 hi->event_plane_angle = atof(cursor);
657 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
658 hi->eccentricity = atof(cursor);
660 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
661 hi->sigma_inel_NN = atof(cursor);
664 hi->centrality = 0.0;
672 std::shared_ptr<GenPdfInfo> pi = std::make_shared<GenPdfInfo>();
673 const char *cursor = buf;
675 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
676 pi->parton_id[0] = atoi(cursor);
678 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
679 pi->parton_id[1] = atoi(cursor);
681 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
682 pi->x[0] = atof(cursor);
684 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
685 pi->x[1] = atof(cursor);
687 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
688 pi->scale = atof(cursor);
690 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
691 pi->xf[0] = atof(cursor);
693 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
694 pi->xf[1] = atof(cursor);
698 if ( !(cursor = strchr(cursor+1,
' ')) ) pdfids =
false;
699 if (pdfids) pi->pdf_id[0] = atoi(cursor);
700 else pi->pdf_id[0] = 0;
702 if (pdfids)
if ( !(cursor = strchr(cursor+1,
' ')) ) pdfids =
false;
703 if (pdfids) pi->pdf_id[1] = atoi(cursor);
704 else pi->pdf_id[1] = 0;
714 if ( !
m_file.is_open() )
return;
int event_number() const
Get event number.
std::ifstream m_file
Input file.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
std::map< std::string, std::string > m_options
options
const Units::LengthUnit & length_unit() const
Get length unit.
#define HEPMC3_WARNING(MESSAGE)
Macro for printing HEPMC3_HEPMC3_WARNING messages.
void add_vertex(GenVertexPtr v)
Add vertex.
Attribute that holds a vector of FPs of type double.
bool m_isstream
toggles usage of m_file or m_stream
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.
Definition of class GenParticle.
std::vector< GenParticlePtr > m_particle_cache_ghost
Particle cache for attributes.
std::vector< double > value() const
get the value associated to this Attribute.
Definition of attribute class GenHeavyIon.
int parse_particle_information(const char *buf)
Parse particle.
~ReaderAsciiHepMC2()
Destructor.
Definition of class GenVertex.
bool failed() override
Return status of the stream.
Attribute that holds a vector of integers of type int.
#define HEPMC3_DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
GenEvent * m_event_ghost
To save particle and verstex attributes.
bool parse_xs_info(GenEvent &evt, const char *buf)
Parse pdf information.
void add_particle(GenParticlePtr p)
Add particle.
int value() const
get the value associated to this Attribute.
static std::string name(MomentumUnit u)
Get name of momentum unit.
void add_attribute(const std::string &name, const std::shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
void close() override
Close file stream.
ReaderAsciiHepMC2(const std::string &filename)
Default constructor.
const std::vector< double > & weights() const
Get event weight values as a vector.
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
LengthUnit
Position units.
Definition of class ReaderAsciiHepMC2.
MomentumUnit
Momentum units.
Stores event-related information.
bool parse_weight_names(const char *buf)
Parse weight names.
std::vector< int > m_end_vertex_barcodes
Old end vertex barcodes.
Definition of class Setup.
void add_tree(const std::vector< GenParticlePtr > &particles)
Add whole tree in topological order.
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
void remove_attribute(const std::string &name, const int &id=0)
Remove attribute.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
void reserve(const size_t &particles, const size_t &vertices=0)
Reserve memory for particles and vertices.
std::istream * m_stream
For ctor when reading from stdin.
void set_units(Units::MomentumUnit new_momentum_unit, Units::LengthUnit new_length_unit)
Change event units Converts event from current units to new ones.
Definition of event attribute class GenPdfInfo.
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Attribute that holds a vector of integers of type int.
std::vector< GenVertexPtr > m_vertex_cache_ghost
Vertex cache for attributes.
Definition of class GenEvent.
bool read_event(GenEvent &evt) override
Implementation of Reader::read_event.
std::vector< long int > value() const
get the value associated to this Attribute.
std::map< std::string, std::map< int, std::shared_ptr< Attribute > > > attributes() const
Get a copy of the list of attributes.
std::vector< int > m_vertex_barcodes
Old vertex barcodes.
std::vector< GenVertexPtr > m_vertex_cache
Vertex cache.
std::vector< GenParticlePtr > m_particle_cache
Particle cache.
int parse_event_information(GenEvent &evt, const char *buf)
Parse event.
std::vector< int > value() const
get the value associated to this Attribute.
void clear()
Remove contents of this event.
bool parse_units(GenEvent &evt, const char *buf)
Parse units.
bool parse_heavy_ion(GenEvent &evt, const char *buf)
Parse heavy ion information.
bool parse_pdf_info(GenEvent &evt, const char *buf)
Parse pdf information.
int parse_vertex_information(const char *buf)
Parse vertex.
Attribute that holds an Integer implemented as an int.
bool skip(const int) override
skip events
void set_event_number(const int &num)
Set event number.