26 m_file(filename), m_stream(0), m_isstream(false) {
28 ERROR(
"ReaderAsciiHepMC2: could not open input file: "<<filename )
35 : m_stream(&stream), m_isstream(true)
38 ERROR(
"ReaderAsciiHepMC2: could not open input stream " )
48 if ( (!
m_file.is_open()) && (!m_isstream) )
return false;
52 bool parsed_event_header =
false;
53 bool is_parsing_successful =
true;
54 int parsing_result = 0;
55 unsigned int vertices_count = 0;
56 unsigned int current_vertex_particles_count = 0;
57 unsigned int current_vertex_particles_parsed= 0;
73 m_isstream ? m_stream->getline(buf,512) :
m_file.getline(buf,512);
74 if( strlen(buf) == 0 )
continue;
76 if( strncmp(buf,
"HepMC",5) == 0 ) {
77 if( strncmp(buf,
"HepMC::Version",14) != 0 && strncmp(buf,
"HepMC::IO_GenEvent",18)!=0 )
79 WARNING(
"ReaderAsciiHepMC2: found unsupported expression in header. Will close the input." )
80 std::cout<<buf<<std::endl;
81 m_isstream ? m_stream->clear(ios::eofbit) :
m_file.clear(ios::eofbit);
83 if(parsed_event_header) {
84 is_parsing_successful =
true;
92 if(parsing_result<0) {
93 is_parsing_successful =
false;
94 ERROR(
"ReaderAsciiHepMC2: error parsing event information" )
97 vertices_count = parsing_result;
102 is_parsing_successful =
true;
104 parsed_event_header =
true;
111 if(current_vertex_particles_parsed < current_vertex_particles_count) {
112 is_parsing_successful =
false;
115 current_vertex_particles_parsed = 0;
119 if(parsing_result<0) {
120 is_parsing_successful =
false;
121 ERROR(
"ReaderAsciiHepMC2: error parsing vertex information" )
124 current_vertex_particles_count = parsing_result;
125 is_parsing_successful =
true;
132 if(parsing_result<0) {
133 is_parsing_successful =
false;
134 ERROR(
"ReaderAsciiHepMC2: error parsing particle information" )
137 ++current_vertex_particles_parsed;
138 is_parsing_successful =
true;
157 WARNING(
"ReaderAsciiHepMC2: skipping unrecognised prefix: " << buf[0] )
158 is_parsing_successful =
true;
162 if( !is_parsing_successful )
break;
165 m_isstream ? peek = m_stream->peek() : peek =
m_file.peek();
166 if( parsed_event_header && peek==
'E' )
break;
172 if( is_parsing_successful && current_vertex_particles_parsed < current_vertex_particles_count ) {
173 ERROR(
"ReaderAsciiHepMC2: not all particles parsed" )
174 is_parsing_successful =
false;
177 else if( is_parsing_successful &&
m_vertex_cache.size() != vertices_count ) {
178 ERROR(
"ReaderAsciiHepMC2: not all vertices parsed" )
179 is_parsing_successful =
false;
182 if( !is_parsing_successful ) {
183 ERROR(
"ReaderAsciiHepMC2: event parsing failed. Returning empty event" )
184 DEBUG( 1,
"Parsing failed at line:" << std::endl << buf )
186 m_isstream ? m_stream->clear(ios::badbit) :
m_file.clear(ios::badbit);
235 for (
int ii=0;ii<100;ii++)
239 m_vertex_cache[i]->add_attribute(
"weight"+to_string((
long long unsigned int)ii),rs);
249 const char *cursor = buf;
251 int vertices_count = 0;
252 int random_states_size = 0;
253 int weights_size = 0;
254 std::vector<long> random_states(0);
255 std::vector<double> weights(0);
258 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
259 event_no = atoi(cursor);
263 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
264 shared_ptr<IntAttribute> mpi = make_shared<IntAttribute>(atoi(cursor));
268 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
269 shared_ptr<DoubleAttribute> event_scale = make_shared<DoubleAttribute>(atof(cursor));
273 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
274 shared_ptr<DoubleAttribute> alphaQCD = make_shared<DoubleAttribute>(atof(cursor));
278 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
279 shared_ptr<DoubleAttribute> alphaQED = make_shared<DoubleAttribute>(atof(cursor));
283 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
284 shared_ptr<IntAttribute> signal_process_id = make_shared<IntAttribute>(atoi(cursor));
288 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
289 shared_ptr<IntAttribute> signal_process_vertex = make_shared<IntAttribute>(atoi(cursor));
290 evt.
add_attribute(
"signal_process_vertex",signal_process_vertex);
293 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
294 vertices_count = atoi(cursor);
297 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
300 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
303 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
304 random_states_size = atoi(cursor);
305 random_states.resize(random_states_size);
307 for (
int i = 0; i < random_states_size; ++i ) {
308 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
309 random_states[i] = atoi(cursor);
312 for (
int i = 0; i < random_states_size; ++i )
313 evt.
add_attribute(
"random_states"+to_string((
long long unsigned int)i),make_shared<IntAttribute>(random_states[i]));
316 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
317 weights_size = atoi(cursor);
318 weights.resize(weights_size);
320 for (
int i = 0; i < weights_size; ++i ) {
321 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
322 weights[i] = atof(cursor);
327 DEBUG( 10,
"ReaderAsciiHepMC2: E: "<<event_no<<
" ("<<vertices_count<<
"V, "<<weights_size<<
"W, "<<random_states_size<<
"RS)" )
329 return vertices_count;
333 const char *cursor = buf;
336 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
341 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
345 evt.
set_units(momentum_unit,length_unit);
353 GenVertexPtr data = make_shared<GenVertex>();
354 GenVertexPtr data_ghost = make_shared<GenVertex>();
356 const char *cursor = buf;
358 int num_particles_out = 0;
359 int weights_size = 0;
360 std::vector<double> weights(0);
362 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
363 barcode = atoi(cursor);
366 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
367 data->set_status( atoi(cursor) );
370 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
371 position.setX(atof(cursor));
374 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
375 position.setY(atof(cursor));
378 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
379 position.setZ(atof(cursor));
382 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
383 position.setT(atof(cursor));
384 data->set_position( position );
387 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
390 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
391 num_particles_out = atoi(cursor);
395 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
396 weights_size = atoi(cursor);
397 weights.resize(weights_size);
399 for (
int i = 0; i < weights_size; ++i ) {
400 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
401 weights[i] = atof(cursor);
411 for (
int i = 0; i < weights_size; ++i )
412 data_ghost->add_attribute(
"weight"+to_string((
long long unsigned int)i),make_shared<DoubleAttribute>(weights[i]));
415 DEBUG( 10,
"ReaderAsciiHepMC2: V: "<<-(
int)
m_vertex_cache.size()<<
" (old barcode"<<barcode<<
") "<<num_particles_out<<
" particles)" )
417 return num_particles_out;
421 GenParticlePtr data = make_shared<GenParticle>();
422 GenParticlePtr data_ghost = make_shared<GenParticle>();
425 const char *cursor = buf;
429 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
432 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
433 data->set_pid( atoi(cursor) );
436 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
437 momentum.
setPx(atof(cursor));
440 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
441 momentum.
setPy(atof(cursor));
444 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
445 momentum.
setPz(atof(cursor));
448 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
449 momentum.
setE(atof(cursor));
450 data->set_momentum(momentum);
453 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
454 data->set_generated_mass( atof(cursor) );
457 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
458 data->set_status( atoi(cursor) );
461 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
462 shared_ptr<DoubleAttribute> theta = make_shared<DoubleAttribute>(atof(cursor));
463 if (theta->value()!=0.0) data_ghost->add_attribute(
"theta",theta);
466 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
467 shared_ptr<DoubleAttribute> phi = make_shared<DoubleAttribute>(atof(cursor));
468 if (phi->value()!=0.0) data_ghost->add_attribute(
"phi",phi);
471 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
472 end_vtx = atoi(cursor);
475 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
476 int flowsize=atoi(cursor);
478 for (
int i=0;i<flowsize;i++)
480 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
481 int flowindex=atoi(cursor);
482 if( !(cursor = strchr(cursor+1,
' ')) )
return -1;
483 int flowvalue=atoi(cursor);
484 data_ghost->add_attribute(
"flow"+to_string((
long long int)flowindex),make_shared<IntAttribute>(flowvalue));
500 DEBUG( 10,
"ReaderAsciiHepMC2: P: "<<
m_particle_cache.size()<<
" ( pid: "<<data->pid()<<
") end vertex: "<<end_vtx )
506 const char *cursor = buf;
507 shared_ptr<GenCrossSection> xs = make_shared<GenCrossSection>();
509 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
510 double xs_val = atof(cursor);
512 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
513 double xs_err = atof(cursor);
515 xs->set_cross_section( xs_val , xs_err);
522 const char *cursor = buf;
523 const char *cursor2 = buf;
525 vector<string> w_names;
530 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
531 w_count = atoi(cursor);
533 if( w_count <= 0 )
return false;
535 w_names.resize(w_count);
537 for(
int i=0; i < w_count; ++i ) {
539 if( !(cursor = strchr(cursor+1,
'"')) )
return false;
540 if( !(cursor2 = strchr(cursor+1,
'"')) )
return false;
545 w_names[i].assign(cursor, cursor2-cursor);
550 run_info()->set_weight_names(w_names);
556 shared_ptr<GenHeavyIon> hi = make_shared<GenHeavyIon>();
557 const char *cursor = buf;
559 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
560 hi->Ncoll_hard = atoi(cursor);
562 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
563 hi->Npart_proj = atoi(cursor);
565 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
566 hi->Npart_targ = atoi(cursor);
568 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
569 hi->Ncoll = atoi(cursor);
571 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
572 hi->spectator_neutrons = atoi(cursor);
574 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
575 hi->spectator_protons = atoi(cursor);
577 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
578 hi->N_Nwounded_collisions = atoi(cursor);
580 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
581 hi->Nwounded_N_collisions = atoi(cursor);
583 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
584 hi->Nwounded_Nwounded_collisions = atoi(cursor);
586 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
587 hi->impact_parameter = atof(cursor);
589 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
590 hi->event_plane_angle = atof(cursor);
592 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
593 hi->eccentricity = atof(cursor);
595 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
596 hi->sigma_inel_NN = atof(cursor);
599 hi->centrality = 0.0;
607 shared_ptr<GenPdfInfo> pi = make_shared<GenPdfInfo>();
608 const char *cursor = buf;
610 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
611 pi->parton_id[0] = atoi(cursor);
613 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
614 pi->parton_id[1] = atoi(cursor);
616 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
617 pi->x[0] = atof(cursor);
619 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
620 pi->x[1] = atof(cursor);
622 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
623 pi->scale = atof(cursor);
625 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
626 pi->xf[0] = atof(cursor);
628 if( !(cursor = strchr(cursor+1,
' ')) )
return false;
629 pi->xf[1] = atof(cursor);
633 if( !(cursor = strchr(cursor+1,
' ')) ) pdfids=
false;
634 if(pdfids) pi->pdf_id[0] = atoi(cursor);
else pi->pdf_id[0] =0;
636 if(pdfids)
if( !(cursor = strchr(cursor+1,
' ')) ) pdfids=
false;
637 if(pdfids) pi->pdf_id[1] = atoi(cursor);
else pi->pdf_id[1] =0;
646 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.
vector< GenVertexPtr > m_vertex_cache
Vertex cache.
void add_vertex(GenVertexPtr v)
Add vertex.
static LengthUnit length_unit(const std::string &name)
Get length unit based on its name.
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition of class GenParticle.
bool failed()
Return status of the stream.
Definition of attribute class GenHeavyIon.
int parse_particle_information(const char *buf)
Parse particle.
~ReaderAsciiHepMC2()
Destructor.
Definition of class GenVertex.
const Units::LengthUnit & length_unit() const
Get length unit.
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 setPy(double pyy)
Set y-component of momentum.
ReaderAsciiHepMC2(const std::string &filename)
Default constructor.
#define DEBUG(LEVEL, MESSAGE)
Macro for printing debug messages with appropriate debug level.
void close()
Close file stream.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
LengthUnit
Position units.
Definition of class ReaderAsciiHepMC2.
MomentumUnit
Momentum units.
const Units::MomentumUnit & momentum_unit() const
Get momentum unit.
Stores event-related information.
Attribute that holds a real number as a double.
bool parse_weight_names(const char *buf)
Parse weight names.
bool read_event(GenEvent &evt)
Implementation of Reader::read_event.
void setPz(double pzz)
Set z-component of momentum.
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.
#define WARNING(MESSAGE)
Macro for printing warning messages.
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.
void setPx(double pxx)
Set x-component of momentum.
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.
const std::vector< double > & weights() const
Get event weight values as a vector.
Definition of class GenEvent.
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.
void setE(double ee)
Set energy component of momentum.
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.
void set_event_number(const int &num)
Set event number.