6#ifndef ATLASHEPMC_GENEVENT_H
7#define ATLASHEPMC_GENEVENT_H
8#include "HepMC3/GenEvent.h"
9#include "HepMC3/GenHeavyIon.h"
10#include "HepMC3/GenPdfInfo.h"
11#include "HepMC3/PrintStreams.h"
27#include <unordered_map>
30inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent& e) {
return e.particles().begin(); }
31inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent& e) {
return e.particles().end(); }
32inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent& e) {
return e.particles().begin(); }
33inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent& e) {
return e.particles().end(); }
47 NUP=e->particles().size();
49 XWGTUP = e->weights().size() ? e->weights()[0] : 1.0;
50 auto A_signal_process_id = e->attribute<HepMC3::IntAttribute>(HepMCStr::signal_process_id);
51 IDPRUP = A_signal_process_id ? A_signal_process_id->value() : 0;
52 auto A_event_scale = e->attribute<HepMC3::DoubleAttribute>(HepMCStr::event_scale);
53 SCALUP = A_event_scale ? A_event_scale->value() : 0;
54 auto A_alphaQCD = e->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQCD);
55 AQCDUP = A_alphaQCD ? A_alphaQCD->value() : 0;
56 auto A_alphaQED = e->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQED);
57 AQEDUP = A_alphaQED ? A_alphaQED->value() : 0;
58 for (
int i = 0; i <
NUP; ++i ){
59 const auto & thisParticle = e->particles().at(i);
60 PUP[i][0] = thisParticle->momentum().px();
61 PUP[i][1] = thisParticle->momentum().py();
62 PUP[i][2] = thisParticle->momentum().pz();
63 PUP[i][3] = thisParticle->momentum().e();
64 PUP[i][4] = thisParticle->momentum().m();
65 IDUP[i] = thisParticle->pdg_id();
66 const auto pv = thisParticle->production_vertex();
67 const auto ev = thisParticle->end_vertex();
70 if (thisParticle->status() == 4 || !pv )
ISTUP[i] = -1;
71 const auto flow1 = thisParticle->attribute<HepMC3::IntAttribute>(HepMCStr::flow1);
72 const auto flow2 = thisParticle->attribute<HepMC3::IntAttribute>(HepMCStr::flow2);
73 ICOLUP[i].first = flow1 ? flow1->value() : 0;
74 ICOLUP[i].second = flow2 ? flow2->value() : 0;
77 if (pv && !pv->particles_in().empty()) {
78 l = pv->particles_in().front()->id();
80 for (
const auto& p : pv->particles_in()) {
81 const int id = p->id();
93 std::istringstream iss(att);
103 for (
int i = 0; i <
NUP; ++i ){
123 std::ostringstream
file;
124 file <<
" " << std::setw(4) <<
NUP
125 <<
" " << std::setw(6) <<
IDPRUP
126 <<
" " << std::setw(14) <<
XWGTUP
127 <<
" " << std::setw(14) <<
SCALUP
128 <<
" " << std::setw(14) <<
AQEDUP
129 <<
" " << std::setw(14) <<
AQCDUP <<
"\n";
130 for (
int i = 0; i <
NUP; ++i )
131 file <<
" " << std::setw(8) <<
IDUP[i]
132 <<
" " << std::setw(2) <<
ISTUP[i]
133 <<
" " << std::setw(4) <<
MOTHUP[i].first
134 <<
" " << std::setw(4) <<
MOTHUP[i].second
135 <<
" " << std::setw(4) <<
ICOLUP[i].first
136 <<
" " << std::setw(4) <<
ICOLUP[i].second
137 <<
" " << std::setw(14) <<
PUP[i][0]
138 <<
" " << std::setw(14) <<
PUP[i][1]
139 <<
" " << std::setw(14) <<
PUP[i][2]
140 <<
" " << std::setw(14) <<
PUP[i][3]
141 <<
" " << std::setw(14) <<
PUP[i][4]
142 <<
" " << std::setw(1) <<
VTIMUP[i]
143 <<
" " << std::setw(1) <<
SPINUP[i] << std::endl;
153 PUP.resize(
NUP, std::vector<double>(5));
166 std::vector< std::pair<int,int> >
MOTHUP{};
167 std::vector< std::pair<int,int> >
ICOLUP{};
168 std::vector< std::vector<double> >
PUP{};
177 virtual bool from_string(
const std::string & )
override {
return false; }
180 att =
"GenEventBarcodes";
185 if (it !=
m_vertexBC.end())
return it->second;
190 if (it !=
m_vertexBC.end())
return it->second;
206 auto barcode = p->attribute<HepMC3::IntAttribute>(HepMCStr::barcode);
214 auto barcode = p->attribute<HepMC3::IntAttribute>(HepMCStr::barcode);
225 auto barcode = p->attribute<HepMC3::IntAttribute>(HepMCStr::barcode);
233 auto barcode = p->attribute<HepMC3::IntAttribute>(HepMCStr::barcode);
243 std::map<int, ConstGenVertexPtr> ret;
245 ret.insert({bcvertpair.first, std::const_pointer_cast<const HepMC3::GenVertex>(bcvertpair.second)});
249 std::map<int, ConstGenParticlePtr> ret;
251 ret.insert({bcpartpair.first, std::const_pointer_cast<const HepMC3::GenParticle>(bcpartpair.second)});
255 std::map<int, int> ret;
256 for (
const auto &bcvertpair:
m_vertexBC) ret.insert({bcvertpair.second->id(), bcvertpair.first});
257 for (
const auto &bcpartpair:
m_particleBC) ret.insert({bcpartpair.second->id(), bcpartpair.first});
266 const auto eventAttributes = e->attributes();
267 const auto barcodeAttributeIt = eventAttributes.find(HepMCStr::barcode);
268 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
270 const auto &particles = e->particles();
271 for (
size_t i = 1; i <= particles.size(); i++) {
272 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(i) > 0) {
273 const auto &ptr = barcodeAttributeIt->second.at(i);
274 if (ptr->is_parsed()) {
275 m_particleBC[
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()] = ptr->particle();
278 m_particleBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->particle();
284 const auto &vertices = e->vertices();
285 for (
size_t i = 1; i <= vertices.size(); i++) {
286 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(-i) > 0) {
287 const auto &ptr = barcodeAttributeIt->second.at(-i);
288 if (ptr->is_parsed()) {
289 m_vertexBC[
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()] = ptr->vertex();
292 m_vertexBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->vertex();
322 virtual bool from_string(
const std::string &)
override {
return false; }
327 att =
"ShadowParticle";
341 e->add_attribute(HepMCStr::long_long_event_number, std::make_shared<HepMC3::LongLongAttribute>(num));
345 auto at = e->attribute<HepMC3::LongLongAttribute>(HepMCStr::long_long_event_number);
346 return at ? at->value() : e->event_number();
349inline std::map<std::string, std::size_t>
weights_map(
const HepMC3::GenEvent* e) {
350 std::map<std::string, std::size_t> ret;
351 auto run = e->run_info();
352 if (!
run)
return ret;
353 const std::vector<std::string> names =
run->weight_names();
354 for (
const auto& name: names) ret[name] =
run->weight_index(name);
358inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent& e) {
return e.particles().begin(); }
359inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent& e) {
return e.particles().end(); }
360inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent& e) {
return e.particles().begin(); }
361inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent& e) {
return e.particles().end(); }
365 std::shared_ptr<HepMC3::IntAttribute> signal_process_id_A = std::make_shared<HepMC3::IntAttribute>(
signal_process_id);
366 e->add_attribute(HepMCStr::signal_process_id, signal_process_id_A);
367 e->add_attribute(HepMCStr::barcodes, std::make_shared<GenEventBarcodes>());
368 e->set_event_number(event_number);
374 e->set_event_number(inEvt->event_number());
375 e->weights() = inEvt->weights();
376 auto a_mpi = inEvt->attribute<HepMC3::IntAttribute>(HepMCStr::mpi);
377 if (a_mpi) e->add_attribute(HepMCStr::mpi, std::make_shared<HepMC3::IntAttribute>(*a_mpi));
378 auto a_signal_process_id = inEvt->attribute<HepMC3::IntAttribute>(HepMCStr::signal_process_id);
379 if (a_signal_process_id) e->add_attribute(HepMCStr::signal_process_id, std::make_shared<HepMC3::IntAttribute>(*a_signal_process_id));
380 auto a_event_scale = inEvt->attribute<HepMC3::DoubleAttribute>(HepMCStr::event_scale);
381 if (a_event_scale) e->add_attribute(HepMCStr::event_scale, std::make_shared<HepMC3::DoubleAttribute>(*a_event_scale));
382 auto a_alphaQCD = inEvt->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQCD);
383 if (a_alphaQCD) e->add_attribute(HepMCStr::alphaQCD, std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQCD));
384 auto a_alphaQED = inEvt->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQED);
385 if (a_alphaQED) e->add_attribute(HepMCStr::alphaQED, std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQED));
386 auto a_pi = inEvt->pdf_info();
387 if (a_pi) e->set_pdf_info(std::make_shared<HepMC3::GenPdfInfo>(*a_pi));
388 auto a_hi = inEvt->heavy_ion();
389 if (a_hi) e->set_heavy_ion(std::make_shared<HepMC3::GenHeavyIon>(*a_hi));
390 auto a_random_states = inEvt->attribute<HepMC3::VectorLongIntAttribute>(HepMCStr::random_states);
391 if (a_random_states) e->add_attribute(HepMCStr::random_states, std::make_shared<HepMC3::VectorLongIntAttribute>(*a_random_states));
392 e->add_attribute(HepMCStr::barcodes, std::make_shared<GenEventBarcodes>());
399 barcodes = std::make_shared<GenEventBarcodes>();
400 e->add_attribute(HepMCStr::barcodes, barcodes);
403 barcodes->fillAttribute(e);
414 const auto eventAttributes = e->attributes();
415 const auto barcodeAttributeIt = eventAttributes.find(HepMCStr::barcode);
416 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
418 const auto &vertices = e->vertices();
419 if (hasBarcodeAttribute) {
420 for (
size_t i = 1; i <= vertices.size(); i++) {
421 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
422 if (ptrIt != barcodeAttributeIt->second.end()) {
423 const auto &ptr = ptrIt->second;
424 if (ptr->is_parsed()) {
425 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
426 return ptr->vertex();
430 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
431 return ptr->vertex();
438 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
439 if (!vertices[-
id-1]->attribute<HepMC3::IntAttribute>(HepMCStr::barcode)) {
440 return vertices[-
id-1];
443 return HepMC3::ConstGenVertexPtr();
454 const auto eventAttributes = e->attributes();
455 const auto barcodeAttributeIt = eventAttributes.find(HepMCStr::barcode);
456 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
458 const auto &particles = e->particles();
459 if (hasBarcodeAttribute) {
460 for (
size_t i = 1; i <= particles.size(); i++) {
461 const auto &ptrIt = barcodeAttributeIt->second.find(i);
462 if (ptrIt != barcodeAttributeIt->second.end()) {
463 const auto &ptr = ptrIt->second;
464 if (ptr->is_parsed()) {
465 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
466 return ptr->particle();
470 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
471 return ptr->particle();
478 if (
id > 0 &&
id <=
static_cast<int>(particles.size())) {
479 if (!particles[
id-1]->attribute<HepMC3::IntAttribute>(HepMCStr::barcode)) {
480 return particles[
id-1];
483 return HepMC3::ConstGenParticlePtr();
494 const auto eventAttributes = e->attributes();
495 const auto barcodeAttributeIt = eventAttributes.find(HepMCStr::barcode);
496 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
498 const auto &vertices = e->vertices();
499 if (hasBarcodeAttribute) {
500 for (
size_t i = 1; i <= vertices.size(); i++) {
501 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
502 if (ptrIt != barcodeAttributeIt->second.end()) {
503 const auto &ptr = ptrIt->second;
504 if (ptr->is_parsed()) {
505 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
506 return ptr->vertex();
510 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
511 return ptr->vertex();
518 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
519 if (!vertices[-
id-1]->attribute<HepMC3::IntAttribute>(HepMCStr::barcode)) {
520 return vertices[-
id-1];
523 return HepMC3::GenVertexPtr();
534 const auto eventAttributes = e->attributes();
535 const auto barcodeAttributeIt = eventAttributes.find(HepMCStr::barcode);
536 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
538 const auto &particles = e->particles();
539 if (hasBarcodeAttribute) {
540 for (
size_t i = 1; i <= particles.size(); i++) {
541 const auto &ptrIt = barcodeAttributeIt->second.find(i);
542 if (ptrIt != barcodeAttributeIt->second.end()) {
543 const auto &ptr = ptrIt->second;
544 if (ptr->is_parsed()) {
545 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
546 return ptr->particle();
550 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
551 return ptr->particle();
558 if (
id > 0 &&
id <=
static_cast<int>(particles.size())) {
559 if (!particles[
id-1]->attribute<HepMC3::IntAttribute>(HepMCStr::barcode)) {
560 return particles[
id-1];
563 return HepMC3::GenParticlePtr();
567 std::shared_ptr<HepMC3::IntAttribute> A_mpi = evt.attribute<HepMC3::IntAttribute>(HepMCStr::mpi);
568 return A_mpi ? (A_mpi->value()) : 0;
571 std::shared_ptr<HepMC3::IntAttribute> A_mpi = evt->attribute<HepMC3::IntAttribute>(HepMCStr::mpi);
572 return A_mpi ? (A_mpi->value()) : 0;
576 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id = evt.attribute<HepMC3::IntAttribute>(HepMCStr::signal_process_id);
577 return A_signal_process_id ? (A_signal_process_id->value()) : 0;
580 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id = evt->attribute<HepMC3::IntAttribute>(HepMCStr::signal_process_id);
581 return A_signal_process_id ? (A_signal_process_id->value()) : 0;
584 std::shared_ptr<HepMC3::IntAttribute>
signal_process_id = std::make_shared<HepMC3::IntAttribute>(i);
588 std::shared_ptr<HepMC3::IntAttribute>
mpi = std::make_shared<HepMC3::IntAttribute>(i);
589 e->add_attribute(HepMCStr::mpi, std::move(
mpi));
592 e->add_attribute(HepMCStr::random_states, std::make_shared<HepMC3::VectorLongIntAttribute>(
a));
595 if (!v || !e)
return;
598 v->add_attribute(HepMCStr::signal_process_vertex, std::make_shared<HepMC3::IntAttribute>(1));
603 if (!e)
return false;
605 for (
const auto& p : e->beams()) {
if (p->status() == 4) ++nBeams; }
606 if (nBeams != 2)
return false;
611 if (!p->parent_event())
return false;
612 auto barcodes = p->parent_event()->template attribute<GenEventBarcodes> (HepMCStr::barcodes);
614 barcodes = std::make_shared<GenEventBarcodes>();
615 p->parent_event()->add_attribute(HepMCStr::barcodes, barcodes);
618 const bool ret = p->add_attribute(HepMCStr::barcode, std::make_shared<HepMC3::IntAttribute>(i));
619 if (ret) barcodes->add(p);
Header file for AthHistogramAlgorithm.
virtual bool to_string(std::string &att) const override
std::map< int, ConstGenVertexPtr > barcode_to_vertex_map() const
std::unordered_map< int, GenVertexPtr > m_vertexBC
GenVertexPtr barcode_to_vertex(int id)
void remove(GenParticlePtr p)
ConstGenParticlePtr barcode_to_particle(int id) const
void add(GenParticlePtr p)
std::map< int, ConstGenParticlePtr > barcode_to_particle_map() const
void remove(GenVertexPtr p)
std::unordered_map< int, GenParticlePtr > m_particleBC
GenParticlePtr barcode_to_particle(int id)
void fillAttribute(GenEvent *e)
virtual bool from_string(const std::string &) override
ConstGenVertexPtr barcode_to_vertex(int id) const
std::map< int, int > id_to_barcode_map() const
ShadowParticle(ConstGenParticlePtr p)
Constructor initializing attribute value.
virtual bool to_string(std::string &att) const override
Implementation of Attribute::to_string.
virtual bool from_string(const std::string &) override
Implementation of Attribute::from_string.
ShadowParticle()
Default constructor.
ConstGenParticlePtr value() const
get a pointer to the shadow particle.
ConstGenParticlePtr m_shadow
The shadow particle.
bool from_event(const HepMC3::GenEvent *e)
ShortEventAttribute(const HepMC3::GenEvent *e)
std::vector< std::vector< double > > PUP
bool to_string(std::string &fl) const override
std::vector< std::pair< int, int > > ICOLUP
std::vector< std::pair< int, int > > MOTHUP
std::vector< double > VTIMUP
std::vector< double > SPINUP
bool from_string(const std::string &att) override
std::vector< HepMC3::GenParticlePtr >::const_iterator begin(HepMC3::GenEvent &e)
std::vector< HepMC3::GenParticlePtr >::const_iterator end(HepMC3::GenEvent &e)
void set_mpi(GenEvent *e, const int i=0)
int signal_process_id(const GenEvent &evt)
ConstGenVertexPtr barcode_to_vertex(const GenEvent *e, int id)
std::map< std::string, std::size_t > weights_map(const HepMC3::GenEvent *e)
ConstGenParticlePtr barcode_to_particle(const GenEvent *e, int id)
std::vector< HepMC3::GenParticlePtr >::const_iterator begin(HepMC3::GenEvent &e)
void set_signal_process_vertex(GenEvent *e, T &v)
void set_signal_process_id(GenEvent *e, const int i=0)
int mpi(const GenEvent &evt)
ConstGenVertexPtr signal_process_vertex(const GenEvent *e)
void set_random_states(GenEvent *e, std::vector< long int > &a)
std::vector< HepMC3::GenParticlePtr >::const_iterator end(HepMC3::GenEvent &e)
void fillBarcodesAttribute(GenEvent *e)
bool suggest_barcode(T &p, int i)
HepMC3::GenParticlePtr GenParticlePtr
HepMC3::GenVertexPtr GenVertexPtr
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
GenEvent * newGenEvent(const int signal_process_id, const int event_number)
GenEvent * copyemptyGenEvent(const GenEvent *inEvt)
long long int get_ll_event_number(const HepMC3::GenEvent *e)
HepMC3::GenHeavyIon GenHeavyIon
bool set_ll_event_number(HepMC3::GenEvent *e, long long int num)
HepMC3::ConstGenVertexPtr ConstGenVertexPtr
bool valid_beam_particles(const GenEvent *e)
HepMC3::GenEvent GenEvent
int run(int argc, char *argv[])