76 std::array<char, 262144> buf{};
77 bool parsed_event_header =
false;
78 bool is_parsing_successful =
true;
79 int parsing_result = 0;
80 unsigned int vertices_count = 0;
81 unsigned int current_vertex_particles_count = 0;
82 unsigned int current_vertex_particles_parsed = 0;
100 if ( strlen(buf.data()) == 0 )
continue;
102 if ( strncmp(buf.data(),
"HepMC", 5) == 0 ) {
103 if ( strncmp(buf.data(),
"HepMC::Version", 14) != 0 && strncmp(buf.data(),
"HepMC::IO_GenEvent", 18) != 0 )
105 HEPMC3_WARNING_LEVEL(500,
"ReaderAsciiHepMC2: found unsupported expression in header. Will close the input.")
106 std::cout <<buf.data() << std::endl;
109 if (parsed_event_header) {
110 is_parsing_successful =
true;
118 if (parsing_result < 0) {
119 is_parsing_successful =
false;
123 vertices_count = parsing_result;
132 is_parsing_successful =
true;
134 parsed_event_header =
true;
141 if (current_vertex_particles_parsed < current_vertex_particles_count) {
142 is_parsing_successful =
false;
145 current_vertex_particles_parsed = 0;
149 if (parsing_result < 0) {
150 is_parsing_successful =
false;
154 current_vertex_particles_count = parsing_result;
155 is_parsing_successful =
true;
162 if (parsing_result < 0) {
163 is_parsing_successful =
false;
164 HEPMC3_ERROR_LEVEL(600,
"ReaderAsciiHepMC2: HEPMC3_ERROR parsing particle information")
167 ++current_vertex_particles_parsed;
168 is_parsing_successful =
true;
172 is_parsing_successful =
parse_units(evt, buf.data());
188 is_parsing_successful =
true;
192 if ( !is_parsing_successful )
break;
196 if ( parsed_event_header && peek ==
'E' )
break;
202 if (is_parsing_successful && current_vertex_particles_parsed < current_vertex_particles_count) {
204 is_parsing_successful =
false;
207 else if (is_parsing_successful &&
m_vertex_cache.size() != vertices_count) {
209 is_parsing_successful =
false;
212 if ( !is_parsing_successful ) {
213 HEPMC3_ERROR_LEVEL(600,
"ReaderAsciiHepMC2: event parsing failed. Returning empty event")
214 HEPMC3_DEBUG(1,
"Parsing failed at line:" << std::endl << buf.data())
220 run_info()->set_weight_names(std::vector<std::string> {
"Default"});
223 HEPMC3_WARNING_LEVEL(600,
"ReaderAsciiHepMC2: weights are empty, an event weight 1.0 will be added.")
244 std::vector<GenParticlePtr> beams;
246 for (
const auto& p:
m_vertex_cache[i]->particles_out())
if (p->status() == 4 && !(p->end_vertex())) beams.emplace_back(p);
251 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());
270 if (
m_options.count(
"event_random_states_are_separated") != 0)
273 if (random_states_a) {
274 std::vector<long int> random_states_v = random_states_a->value();
275 for (
size_t i = 0; i < random_states_v.size(); ++i ) {
276 evt.
add_attribute(
"random_states" + std::to_string(
static_cast<long long unsigned int>(i)), std::make_shared<IntAttribute>(random_states_v[i]));
283 std::map< std::string, std::map<int, std::shared_ptr<Attribute> > > cached_attributes =
m_event_ghost->attributes();
284 if (cached_attributes.count(
"flows") != 0) {
285 const std::map<int, std::shared_ptr<Attribute> >& flows = cached_attributes.at(
"flows");
286 if (
m_options.count(
"particle_flows_are_separated") == 0) {
287 for (
const auto& f: flows)
if (f.first > 0 && f.first <=
static_cast<int>(
m_particle_cache.size())) {
m_particle_cache[f.first-1]->add_attribute(
"flows", f.second);}
289 for (
const auto& f: flows) {
291 std::shared_ptr<VectorIntAttribute> casted = std::dynamic_pointer_cast<VectorIntAttribute>(f.second);
292 if (!casted)
continue;
293 std::vector<int> this_p_flow = casted->value();
294 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]));
300 if (cached_attributes.count(
"phi") != 0) {
301 const std::map<int, std::shared_ptr<Attribute> >& phi = cached_attributes.at(
"phi");
302 for (
const auto& f: phi)
if (f.first > 0 &&f.first <=
static_cast<int>(
m_particle_cache.size()))
m_particle_cache[f.first-1]->add_attribute(
"phi", f.second);
305 if (cached_attributes.count(
"theta") != 0) {
306 const std::map<int, std::shared_ptr<Attribute> >& theta = cached_attributes.at(
"theta");
307 for (
const auto& f: theta)
if (f.first > 0 && f.first <=
static_cast<int>(
m_particle_cache.size()))
m_particle_cache[f.first-1]->add_attribute(
"theta", f.second);
310 if (cached_attributes.count(
"weights") != 0) {
311 const std::map<int, std::shared_ptr<Attribute> >& weights = cached_attributes.at(
"weights");
312 if (
m_options.count(
"vertex_weights_are_separated") == 0) {
313 for (
const auto& f: weights) {
if (f.first < 0 && f.first >= -
static_cast<int>(
m_vertex_cache.size()))
m_vertex_cache[-f.first-1]->add_attribute(
"weights", f.second);}
315 for (
const auto& f: weights) {
316 if (f.first < 0 && f.first >= -
static_cast<int>(
m_vertex_cache.size())) {
317 std::shared_ptr<VectorDoubleAttribute> casted = std::dynamic_pointer_cast<VectorDoubleAttribute>(f.second);
318 if (!casted)
continue;
319 std::vector<double> this_v_weight = casted->value();
320 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]));
325 std::shared_ptr<IntAttribute> signal_process_vertex_barcode = evt.
attribute<
IntAttribute>(
"signal_process_vertex");
326 if (signal_process_vertex_barcode) {
327 int signal_process_vertex_barcode_value = signal_process_vertex_barcode->value();
332 std::shared_ptr<IntAttribute> signal_process_vertex = std::make_shared<IntAttribute>(
m_vertex_cache.at(i)->id());
333 evt.
add_attribute(
"signal_process_vertex", signal_process_vertex);
344 const char *cursor = buf;
345 size_t vertices_count = 0;
346 int random_states_size = 0;
347 int weights_size = 0;
348 std::vector<long> random_states(0);
349 std::vector<double> weights(0);
352 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
356 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
357 evt.
add_attribute(
"mpi", std::make_shared<IntAttribute>(atoi(cursor)));
360 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
361 evt.
add_attribute(
"event_scale", std::make_shared<DoubleAttribute>(atof(cursor)));
364 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
365 evt.
add_attribute(
"alphaQCD", std::make_shared<DoubleAttribute>(atof(cursor)));
368 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
369 evt.
add_attribute(
"alphaQED", std::make_shared<DoubleAttribute>(atof(cursor)));
372 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
373 evt.
add_attribute(
"signal_process_id", std::make_shared<IntAttribute>(atoi(cursor)));
376 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
377 evt.
add_attribute(
"signal_process_vertex", std::make_shared<IntAttribute>(atoi(cursor)));
380 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
381 vertices_count = atoi(cursor);
384 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
387 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
390 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
391 random_states_size = atoi(cursor);
392 if (random_states_size >= 0 ) {
393 random_states.resize(random_states_size);
395 HEPMC3_DEBUG(0,
"ReaderAsciiHepMC2: E: " << evt.
event_number() <<
" (" << vertices_count <<
"V, " << random_states_size <<
"RS)")
397 for (
int i = 0; i < random_states_size; ++i ) {
398 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
399 random_states[i] = atoi(cursor);
402 if (!random_states.empty()) evt.
add_attribute(
"random_states", std::make_shared<VectorLongIntAttribute>(random_states));
405 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
406 weights_size = atoi(cursor);
407 if (weights_size >= 0 ) {
408 weights.resize(weights_size);
410 HEPMC3_DEBUG(0,
"ReaderAsciiHepMC2: E: " << evt.
event_number() <<
" (" << vertices_count <<
"V, " << weights_size <<
"WS)")
412 for (
int i = 0; i < weights_size; ++i ) {
413 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
414 weights[i] = atof(cursor);
419 HEPMC3_DEBUG(10,
"ReaderAsciiHepMC2: E: " << evt.
event_number() <<
" (" << vertices_count <<
"V, " << weights_size <<
"W, " << random_states_size <<
"RS)")
421 return vertices_count;
445 GenVertexPtr data = std::make_shared<GenVertex>();
446 GenVertexPtr data_ghost = std::make_shared<GenVertex>();
447 const char *cursor = buf;
449 int num_particles_out = 0;
450 int weights_size = 0;
451 std::vector<double> weights(0);
453 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
454 barcode = atoi(cursor);
457 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
458 data->set_status(atoi(cursor));
461 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
462 double X(atof(cursor));
465 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
466 double Y(atof(cursor));
469 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
470 double Z(atof(cursor));
473 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
474 double T(atof(cursor));
478 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
481 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
482 num_particles_out = atoi(cursor);
485 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
486 weights_size = atoi(cursor);
487 weights.resize(weights_size);
489 for (
int i = 0; i < weights_size; ++i ) {
490 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
491 weights[i] = atof(cursor);
502 if (!weights.empty()) data_ghost->add_attribute(
"weights", std::make_shared<VectorDoubleAttribute>(weights));
506 HEPMC3_DEBUG(10,
"ReaderAsciiHepMC2: V: " << -
static_cast<int>(
m_vertex_cache.size()) <<
" (old barcode " << barcode <<
") " << num_particles_out <<
" particles)")
508 return num_particles_out;
512 GenParticlePtr data = std::make_shared<GenParticle>();
513 GenParticlePtr data_ghost = std::make_shared<GenParticle>();
515 const char *cursor = buf;
519 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
522 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
523 data->set_pid(atoi(cursor));
526 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
527 double Px(atof(cursor));
530 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
531 double Py(atof(cursor));
534 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
535 double Pz(atof(cursor));
538 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
539 double E(atof(cursor));
543 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
544 data->set_generated_mass(atof(cursor));
547 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
548 data->set_status(atoi(cursor));
551 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
552 double theta_v = atof(cursor);
553 if (theta_v != 0.0) data_ghost->add_attribute(
"theta", std::make_shared<DoubleAttribute>(theta_v));
556 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
557 double phi_v = atof(cursor);
558 if (phi_v != 0.0) data_ghost->add_attribute(
"phi", std::make_shared<DoubleAttribute>(phi_v));
561 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
562 end_vtx = atoi(cursor);
565 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
566 int flowsize = atoi(cursor);
568 std::map<int, int> flows;
569 for (
int i = 0; i < flowsize; i++)
571 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
572 int flowindex = atoi(cursor);
573 if ( !(cursor = strchr(cursor+1,
' ')) )
return -1;
574 int flowvalue = atoi(cursor);
575 flows[flowindex] = flowvalue;
579 std::vector<int> vectorflows;
580 vectorflows.reserve(flows.size());
581 for (
const auto& f: flows) { vectorflows.emplace_back(f.second); }
582 data_ghost->add_attribute(
"flows", std::make_shared<VectorIntAttribute>(vectorflows));
658 std::shared_ptr<GenHeavyIon> hi = std::make_shared<GenHeavyIon>();
659 const char *cursor = buf;
661 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
662 hi->Ncoll_hard = atoi(cursor);
664 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
665 hi->Npart_proj = atoi(cursor);
667 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
668 hi->Npart_targ = atoi(cursor);
670 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
671 hi->Ncoll = atoi(cursor);
673 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
674 hi->spectator_neutrons = atoi(cursor);
676 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
677 hi->spectator_protons = atoi(cursor);
679 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
680 hi->N_Nwounded_collisions = atoi(cursor);
682 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
683 hi->Nwounded_N_collisions = atoi(cursor);
685 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
686 hi->Nwounded_Nwounded_collisions = atoi(cursor);
688 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
689 hi->impact_parameter = atof(cursor);
691 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
692 hi->event_plane_angle = atof(cursor);
694 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
695 hi->eccentricity = atof(cursor);
697 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
698 hi->sigma_inel_NN = atof(cursor);
701 hi->centrality = 0.0;
709 std::shared_ptr<GenPdfInfo> pi = std::make_shared<GenPdfInfo>();
710 const char *cursor = buf;
712 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
713 pi->parton_id[0] = atoi(cursor);
715 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
716 pi->parton_id[1] = atoi(cursor);
718 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
719 pi->x[0] = atof(cursor);
721 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
722 pi->x[1] = atof(cursor);
724 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
725 pi->scale = atof(cursor);
727 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
728 pi->xf[0] = atof(cursor);
730 if ( !(cursor = strchr(cursor+1,
' ')) )
return false;
731 pi->xf[1] = atof(cursor);
735 if ( !(cursor = strchr(cursor+1,
' ')) ) pdfids =
false;
736 if (pdfids) {pi->pdf_id[0] = atoi(cursor);}
737 else {pi->pdf_id[0] = 0;}
739 if (pdfids)
if ( !(cursor = strchr(cursor+1,
' ')) ) pdfids =
false;
740 if (pdfids) { pi->pdf_id[1] = atoi(cursor);}
741 else {pi->pdf_id[1] = 0;}
Definition of class GenParticle.