24 m_file(filename), m_stream(0), m_isstream(false) {
26 HEPMC3_ERROR(
"ReaderAsciiHepMC2: could not open input file: "<<filename )
33 : m_stream(&stream), m_isstream(true)
36 HEPMC3_ERROR(
"ReaderAsciiHepMC2: could not open input stream " )
46 const size_t max_buffer_size=512*512;
47 char buf[max_buffer_size];
54 if (nn<0)
return true;
64 const size_t max_buffer_size=512*512;
65 const size_t max_weights_size=256;
66 char buf[max_buffer_size];
67 bool parsed_event_header =
false;
68 bool is_parsing_successful =
true;
69 int parsing_result = 0;
70 unsigned int vertices_count = 0;
71 unsigned int current_vertex_particles_count = 0;
72 unsigned int current_vertex_particles_parsed= 0;
89 if( strlen(buf) == 0 )
continue;
91 if( strncmp(buf,
"HepMC",5) == 0 ) {
92 if( strncmp(buf,
"HepMC::Version",14) != 0 && strncmp(buf,
"HepMC::IO_GenEvent",18)!=0 )
94 HEPMC3_WARNING(
"ReaderAsciiHepMC2: found unsupported expression in header. Will close the input." )
95 std::cout<<buf<<std::endl;
98 if(parsed_event_header) {
99 is_parsing_successful =
true;
107 if(parsing_result<0) {
108 is_parsing_successful =
false;
109 HEPMC3_ERROR(
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing event information" )
112 vertices_count = parsing_result;
117 is_parsing_successful =
true;
119 parsed_event_header =
true;
126 if(current_vertex_particles_parsed < current_vertex_particles_count) {
127 is_parsing_successful =
false;
130 current_vertex_particles_parsed = 0;
134 if(parsing_result<0) {
135 is_parsing_successful =
false;
136 HEPMC3_ERROR(
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing vertex information" )
139 current_vertex_particles_count = parsing_result;
140 is_parsing_successful =
true;
147 if(parsing_result<0) {
148 is_parsing_successful =
false;
149 HEPMC3_ERROR(
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing particle information" )
152 ++current_vertex_particles_parsed;
153 is_parsing_successful =
true;
172 HEPMC3_WARNING(
"ReaderAsciiHepMC2: skipping unrecognised prefix: " << buf[0] )
173 is_parsing_successful =
true;
177 if( !is_parsing_successful )
break;
181 if( parsed_event_header && peek==
'E' )
break;
187 if( is_parsing_successful && current_vertex_particles_parsed < current_vertex_particles_count ) {
188 HEPMC3_ERROR(
"ReaderAsciiHepMC2: not all particles parsed" )
189 is_parsing_successful =
false;
192 else if( is_parsing_successful &&
m_vertex_cache.size() != vertices_count ) {
193 HEPMC3_ERROR(
"ReaderAsciiHepMC2: not all vertices parsed" )
194 is_parsing_successful =
false;
197 if( !is_parsing_successful ) {
198 HEPMC3_ERROR(
"ReaderAsciiHepMC2: event parsing failed. Returning empty event" )
199 HEPMC3_DEBUG( 1,
"Parsing failed at line:" << std::endl << buf )
260 for (
size_t ii=0; ii<max_weights_size; ii++)
264 m_vertex_cache[i]->add_attribute(
"weight"+to_string((
long long unsigned int)ii),rs);
274 const char *cursor = buf;
276 int vertices_count = 0;
277 int random_states_size = 0;
278 int weights_size = 0;
279 std::vector<long> random_states(0);
280 std::vector<double> weights(0);
283 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
284 event_no = atoi(cursor);
288 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
289 shared_ptr<IntAttribute> mpi = make_shared<IntAttribute>(atoi(cursor));
293 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
294 shared_ptr<DoubleAttribute> event_scale = make_shared<DoubleAttribute>(atof(cursor));
298 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
299 shared_ptr<DoubleAttribute> alphaQCD = make_shared<DoubleAttribute>(atof(cursor));
303 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
304 shared_ptr<DoubleAttribute> alphaQED = make_shared<DoubleAttribute>(atof(cursor));
308 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
309 shared_ptr<IntAttribute> signal_process_id = make_shared<IntAttribute>(atoi(cursor));
313 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
314 shared_ptr<IntAttribute> signal_process_vertex = make_shared<IntAttribute>(atoi(cursor));
315 evt.
add_attribute(
"signal_process_vertex",signal_process_vertex);
318 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
319 vertices_count = atoi(cursor);
322 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
325 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
328 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
329 random_states_size = atoi(cursor);
330 random_states.resize(random_states_size);
332 for (
int i = 0; i < random_states_size; ++i ) {
333 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
334 random_states[i] = atoi(cursor);
338 evt.
add_attribute(
"random_states",make_shared<VectorLongIntAttribute>(random_states));
342 for (
int i = 0; i < random_states_size; ++i )
343 evt.
add_attribute(
"random_states"+to_string((
long long unsigned int)i),make_shared<IntAttribute>(random_states[i]));
346 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
347 weights_size = atoi(cursor);
348 weights.resize(weights_size);
350 for (
int i = 0; i < weights_size; ++i ) {
351 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
352 weights[i] = atof(cursor);
357 HEPMC3_DEBUG( 10,
"ReaderAsciiHepMC2: E: "<<event_no<<
" ("<<vertices_count<<
"V, "<<weights_size<<
"W, "<<random_states_size<<
"RS)" )
359 return vertices_count;
363 const char *cursor = buf;
366 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
371 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
375 evt.
set_units(momentum_unit,length_unit);
383 GenVertexPtr data = make_shared<GenVertex>();
384 GenVertexPtr data_ghost = make_shared<GenVertex>();
386 const char *cursor = buf;
388 int num_particles_out = 0;
389 int weights_size = 0;
390 std::vector<double> weights(0);
392 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
393 barcode = atoi(cursor);
396 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
397 data->set_status( atoi(cursor) );
400 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
401 position.setX(atof(cursor));
404 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
405 position.setY(atof(cursor));
408 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
409 position.setZ(atof(cursor));
412 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
413 position.setT(atof(cursor));
414 data->set_position( position );
417 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
420 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
421 num_particles_out = atoi(cursor);
425 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
426 weights_size = atoi(cursor);
427 weights.resize(weights_size);
429 for (
int i = 0; i < weights_size; ++i ) {
430 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
431 weights[i] = atof(cursor);
443 for (
int i = 0; i < weights_size; ++i )
444 data_ghost->add_attribute(
"weight"+to_string((
long long unsigned int)i),make_shared<DoubleAttribute>(weights[i]));
448 data_ghost->add_attribute(
"weights",make_shared<VectorDoubleAttribute>(weights));
452 HEPMC3_DEBUG( 10,
"ReaderAsciiHepMC2: V: "<<-(
int)
m_vertex_cache.size()<<
" (old barcode"<<barcode<<
") "<<num_particles_out<<
" particles)" )
454 return num_particles_out;
458 GenParticlePtr data = make_shared<GenParticle>();
459 GenParticlePtr data_ghost = make_shared<GenParticle>();
462 const char *cursor = buf;
466 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
469 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
470 data->set_pid( atoi(cursor) );
473 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
474 momentum.
setPx(atof(cursor));
477 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
478 momentum.
setPy(atof(cursor));
481 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
482 momentum.
setPz(atof(cursor));
485 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
486 momentum.
setE(atof(cursor));
487 data->set_momentum(momentum);
490 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
491 data->set_generated_mass( atof(cursor) );
494 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
495 data->set_status( atoi(cursor) );
498 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
499 shared_ptr<DoubleAttribute> theta = make_shared<DoubleAttribute>(atof(cursor));
500 if (theta->value()!=0.0) data_ghost->add_attribute(
"theta",theta);
503 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
504 shared_ptr<DoubleAttribute> phi = make_shared<DoubleAttribute>(atof(cursor));
505 if (phi->value()!=0.0) data_ghost->add_attribute(
"phi",phi);
508 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
509 end_vtx = atoi(cursor);
512 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
513 int flowsize=atoi(cursor);
515 std::map<int,int> flows;
516 for (
int i=0; i<flowsize; i++)
518 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
519 int flowindex=atoi(cursor);
520 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
521 int flowvalue=atoi(cursor);
522 flows[flowindex]=flowvalue;
526 std::vector<int> vectorflows;
527 for (
auto f: flows) vectorflows.push_back(f.second);
528 data_ghost->add_attribute(
"flows",make_shared<VectorIntAttribute>(vectorflows));
532 for (
auto f: flows) data_ghost->add_attribute(
"flow"+to_string((
long long int)f.first),make_shared<IntAttribute>(f.second));
553 const char *cursor = buf;
554 shared_ptr<GenCrossSection> xs = make_shared<GenCrossSection>();
556 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
557 double xs_val = atof(cursor);
559 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
560 double xs_err = atof(cursor);
562 xs->set_cross_section( xs_val , xs_err);
569 const char *cursor = buf;
570 const char *cursor2 = buf;
572 vector<string> w_names;
577 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
578 w_count = atoi(cursor);
580 if( w_count <= 0 )
return false;
582 w_names.resize(w_count);
584 for(
int i=0; i < w_count; ++i ) {
586 if( !(cursor = strchr(cursor+1,
'"')) )
return false;
587 if( !(cursor2 = strchr(cursor+1,
'"')) )
return false;
592 w_names[i].assign(cursor, cursor2-cursor);
597 run_info()->set_weight_names(w_names);
603 shared_ptr<GenHeavyIon> hi = make_shared<GenHeavyIon>();
604 const char *cursor = buf;
606 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
607 hi->Ncoll_hard = atoi(cursor);
609 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
610 hi->Npart_proj = atoi(cursor);
612 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
613 hi->Npart_targ = atoi(cursor);
615 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
616 hi->Ncoll = atoi(cursor);
618 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
619 hi->spectator_neutrons = atoi(cursor);
621 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
622 hi->spectator_protons = atoi(cursor);
624 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
625 hi->N_Nwounded_collisions = atoi(cursor);
627 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
628 hi->Nwounded_N_collisions = atoi(cursor);
630 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
631 hi->Nwounded_Nwounded_collisions = atoi(cursor);
633 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
634 hi->impact_parameter = atof(cursor);
636 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
637 hi->event_plane_angle = atof(cursor);
639 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
640 hi->eccentricity = atof(cursor);
642 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
643 hi->sigma_inel_NN = atof(cursor);
646 hi->centrality = 0.0;
654 shared_ptr<GenPdfInfo> pi = make_shared<GenPdfInfo>();
655 const char *cursor = buf;
657 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
658 pi->parton_id[0] = atoi(cursor);
660 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
661 pi->parton_id[1] = atoi(cursor);
663 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
664 pi->x[0] = atof(cursor);
666 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
667 pi->x[1] = atof(cursor);
669 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
670 pi->scale = atof(cursor);
672 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
673 pi->xf[0] = atof(cursor);
675 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
676 pi->xf[1] = atof(cursor);
680 if( !(cursor = strchr(cursor+1,
' ')) ) pdfids=
false;
681 if(pdfids) pi->pdf_id[0] = atoi(cursor);
682 else pi->pdf_id[0] =0;
684 if(pdfids)
if( !(cursor = strchr(cursor+1,
' ')) ) pdfids=
false;
685 if(pdfids) pi->pdf_id[1] = atoi(cursor);
686 else pi->pdf_id[1] =0;
696 if( !
m_file.is_open() )
return;
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
std::ifstream m_file
Input file.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
std::map< std::string, std::string > m_options
options
vector< GenVertexPtr > m_vertex_cache
Vertex cache.
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.
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.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
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.
static std::string name(MomentumUnit u)
Get name of momentum unit.
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.
LengthUnit
Position units.
Definition of class ReaderAsciiHepMC2.
MomentumUnit
Momentum units.
Stores event-related information.
Attribute that holds a real number as a double.
bool parse_weight_names(const char *buf)
Parse weight names.
vector< int > m_vertex_barcodes
Old vertex barcodes.
vector< GenParticlePtr > m_particle_cache
Particle cache.
vector< GenVertexPtr > m_vertex_cache_ghost
Vertex cache for attributes.
Definition of class Setup.
void add_tree(const std::vector< GenParticlePtr > &particles)
Add whole tree in topological order.
static MomentumUnit momentum_unit(const std::string &name)
Get momentum unit based on its name.
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.
void add_attribute(const string &name, const shared_ptr< Attribute > &att, const int &id=0)
Add event attribute to event.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition of event attribute class GenPdfInfo.
vector< GenParticlePtr > m_particle_cache_ghost
Particle cache for attributes.
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition of class GenEvent.
bool read_event(GenEvent &evt) override
Implementation of Reader::read_event.
int parse_event_information(GenEvent &evt, const char *buf)
Parse event.
void clear()
Remove contents of this event.
vector< int > m_end_vertex_barcodes
Old end vertex barcodes.
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.