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;
51 IDPRUP = A_signal_process_id ? A_signal_process_id->value() : 0;
53 SCALUP = A_event_scale ? A_event_scale->value() : 0;
55 AQCDUP = A_alphaQCD ? A_alphaQCD->value() : 0;
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;
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});
263 const auto eventAttributes = e->attributes();
265 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
267 const auto &particles = e->particles();
268 for (
size_t i = 1; i <= particles.size(); i++) {
269 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(i) > 0) {
270 const auto &ptr = barcodeAttributeIt->second.at(i);
271 if (ptr->is_parsed()) {
272 m_particleBC[
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()] = ptr->particle();
275 m_particleBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->particle();
281 const auto &vertices = e->vertices();
282 for (
size_t i = 1; i <= vertices.size(); i++) {
283 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(-i) > 0) {
284 const auto &ptr = barcodeAttributeIt->second.at(-i);
285 if (ptr->is_parsed()) {
286 m_vertexBC[
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()] = ptr->vertex();
289 m_vertexBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->vertex();
319 virtual bool from_string(
const std::string &)
override {
return false; }
324 att =
"ShadowParticle";
343 return at ? at->value() : e->event_number();
346inline std::map<std::string, std::size_t>
weights_map(
const HepMC3::GenEvent* e) {
347 std::map<std::string, std::size_t> ret;
348 auto run = e->run_info();
349 if (!
run)
return ret;
350 const std::vector<std::string> names =
run->weight_names();
351 for (
const auto& name: names) ret[name] =
run->weight_index(name);
355inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent& e) {
return e.particles().begin(); }
356inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent& e) {
return e.particles().end(); }
357inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent& e) {
return e.particles().begin(); }
358inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent& e) {
return e.particles().end(); }
362 std::shared_ptr<HepMC3::IntAttribute> signal_process_id_A = std::make_shared<HepMC3::IntAttribute>(
signal_process_id);
365 e->set_event_number(event_number);
371 e->set_event_number(inEvt->event_number());
372 e->weights() = inEvt->weights();
373 auto a_mpi = inEvt->attribute<HepMC3::IntAttribute>(
HepMCStr::mpi);
374 if (a_mpi) e->add_attribute(
HepMCStr::mpi, std::make_shared<HepMC3::IntAttribute>(*a_mpi));
376 if (a_signal_process_id) e->add_attribute(
HepMCStr::signal_process_id, std::make_shared<HepMC3::IntAttribute>(*a_signal_process_id));
378 if (a_event_scale) e->add_attribute(
HepMCStr::event_scale, std::make_shared<HepMC3::DoubleAttribute>(*a_event_scale));
380 if (a_alphaQCD) e->add_attribute(
HepMCStr::alphaQCD, std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQCD));
382 if (a_alphaQED) e->add_attribute(
HepMCStr::alphaQED, std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQED));
383 auto a_pi = inEvt->pdf_info();
384 if (a_pi) e->set_pdf_info(std::make_shared<HepMC3::GenPdfInfo>(*a_pi));
385 auto a_hi = inEvt->heavy_ion();
386 if (a_hi) e->set_heavy_ion(std::make_shared<HepMC3::GenHeavyIon>(*a_hi));
388 if (a_random_states) e->add_attribute(
HepMCStr::random_states, std::make_shared<HepMC3::VectorLongIntAttribute>(*a_random_states));
396 barcodes = std::make_shared<GenEventBarcodes>();
400 barcodes->fillAttribute(e);
411 const auto eventAttributes = e->attributes();
413 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
415 const auto &vertices = e->vertices();
416 if (hasBarcodeAttribute) {
417 for (
size_t i = 1; i <= vertices.size(); i++) {
418 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
419 if (ptrIt != barcodeAttributeIt->second.end()) {
420 const auto &ptr = ptrIt->second;
421 if (ptr->is_parsed()) {
422 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
423 return ptr->vertex();
427 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
428 return ptr->vertex();
435 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
437 return vertices[-
id-1];
440 return HepMC3::ConstGenVertexPtr();
451 const auto eventAttributes = e->attributes();
453 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
455 const auto &particles = e->particles();
456 if (hasBarcodeAttribute) {
457 for (
size_t i = 1; i <= particles.size(); i++) {
458 const auto &ptrIt = barcodeAttributeIt->second.find(i);
459 if (ptrIt != barcodeAttributeIt->second.end()) {
460 const auto &ptr = ptrIt->second;
461 if (ptr->is_parsed()) {
462 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
463 return ptr->particle();
467 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
468 return ptr->particle();
475 if (
id > 0 &&
id <=
static_cast<int>(particles.size())) {
477 return particles[
id-1];
480 return HepMC3::ConstGenParticlePtr();
491 const auto eventAttributes = e->attributes();
493 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
495 const auto &vertices = e->vertices();
496 if (hasBarcodeAttribute) {
497 for (
size_t i = 1; i <= vertices.size(); i++) {
498 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
499 if (ptrIt != barcodeAttributeIt->second.end()) {
500 const auto &ptr = ptrIt->second;
501 if (ptr->is_parsed()) {
502 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
503 return ptr->vertex();
507 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
508 return ptr->vertex();
515 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
517 return vertices[-
id-1];
520 return HepMC3::GenVertexPtr();
531 const auto eventAttributes = e->attributes();
533 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
535 const auto &particles = e->particles();
536 if (hasBarcodeAttribute) {
537 for (
size_t i = 1; i <= particles.size(); i++) {
538 const auto &ptrIt = barcodeAttributeIt->second.find(i);
539 if (ptrIt != barcodeAttributeIt->second.end()) {
540 const auto &ptr = ptrIt->second;
541 if (ptr->is_parsed()) {
542 if (
id ==
static_cast<HepMC3::IntAttribute*
>(ptr.get())->value()) {
543 return ptr->particle();
547 if (
id == std::atoi(ptr->unparsed_string().c_str())) {
548 return ptr->particle();
555 if (
id > 0 &&
id <=
static_cast<int>(particles.size())) {
557 return particles[
id-1];
560 return HepMC3::GenParticlePtr();
564 std::shared_ptr<HepMC3::IntAttribute> A_mpi = evt.attribute<HepMC3::IntAttribute>(
HepMCStr::mpi);
565 return A_mpi ? (A_mpi->value()) : 0;
568 std::shared_ptr<HepMC3::IntAttribute> A_mpi = evt->attribute<HepMC3::IntAttribute>(
HepMCStr::mpi);
569 return A_mpi ? (A_mpi->value()) : 0;
574 return A_signal_process_id ? (A_signal_process_id->value()) : 0;
578 return A_signal_process_id ? (A_signal_process_id->value()) : 0;
581 std::shared_ptr<HepMC3::IntAttribute>
signal_process_id = std::make_shared<HepMC3::IntAttribute>(i);
585 std::shared_ptr<HepMC3::IntAttribute>
mpi = std::make_shared<HepMC3::IntAttribute>(i);
592 if (!v || !e)
return;
600 if (!e)
return false;
602 for (
const auto& p : e->beams()) {
if (p->status() == 4) ++nBeams; }
603 if (nBeams != 2)
return false;
608 if (!p->parent_event())
return false;
609 auto barcodes = p->parent_event()->template attribute<GenEventBarcodes> (
HepMCStr::barcodes);
611 barcodes = std::make_shared<GenEventBarcodes>();
615 const bool ret = p->add_attribute(
HepMCStr::barcode, std::make_shared<HepMC3::IntAttribute>(i));
616 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)
const std::string event_scale
const std::string barcode
const std::string long_long_event_number
const std::string signal_process_vertex
const std::string signal_process_id
const std::string random_states
const std::string alphaQED
const std::string alphaQCD
const std::string barcodes
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[])