6#ifndef ATLASHEPMC_GENEVENT_H
7#define ATLASHEPMC_GENEVENT_H
9#include "HepMC3/GenEvent.h"
10#include "HepMC3/GenHeavyIon.h"
11#include "HepMC3/GenPdfInfo.h"
12#include "HepMC3/PrintStreams.h"
18#include <unordered_map>
21inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent& e) {
return e.particles().begin(); }
22inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent& e) {
return e.particles().end(); }
23inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent& e) {
return e.particles().begin(); }
24inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent& e) {
return e.particles().end(); }
28using Print=HepMC3::Print;
29using GenHeavyIon=HepMC3::GenHeavyIon;
30using GenEvent=HepMC3::GenEvent;
32class ShortEventAttribute :
public HepMC3::Attribute {
34 ShortEventAttribute():HepMC3::Attribute(){}
35 ShortEventAttribute(
const HepMC3::GenEvent* e):HepMC3::Attribute(){ from_event(e); }
37 bool from_event(
const HepMC3::GenEvent* e){
38 NUP=
e->particles().size();
40 XWGTUP =
e->weights().size() ?
e->weights()[0] : 1.0;
41 auto A_signal_process_id=
e->attribute<HepMC3::IntAttribute>(
"signal_process_id");
42 IDPRUP = A_signal_process_id?A_signal_process_id->value() : 0;
43 auto A_event_scale=
e->attribute<HepMC3::DoubleAttribute>(
"event_scale");
44 SCALUP = A_event_scale? A_event_scale->value():0;
45 auto A_alphaQCD=
e->attribute<HepMC3::DoubleAttribute>(
"alphaQCD");
46 AQCDUP = A_alphaQCD? A_alphaQCD->value():0;
47 auto A_alphaQED=
e->attribute<HepMC3::DoubleAttribute>(
"alphaQED");
48 AQEDUP = A_alphaQED? A_alphaQED->value():0;
50 for (
int i = 0;
i < NUP; ++
i ){
51 PUP[
i][0] =
e->particles().at(i)->momentum().px();
52 PUP[
i][1] =
e->particles().at(i)->momentum().py();
53 PUP[
i][2] =
e->particles().at(i)->momentum().pz();
54 PUP[
i][3] =
e->particles().at(i)->momentum().e();
55 PUP[
i][4] =
e->particles().at(i)->momentum().m();
56 IDUP[
i] =
e->particles().at(i)->pdg_id();
57 auto pv =
e->particles().at(i)->production_vertex();
58 auto ev =
e->particles().at(i)->end_vertex();
59 if (pv &&
ev ) ISTUP[
i] = 2;
60 if (pv && !
ev ) ISTUP[
i] = 1;
61 if (
e->particles().at(i)->status() == 4 || !pv ) ISTUP[
i] = -1;
62 auto flow1 =
e->particles().at(i)->attribute<HepMC3::IntAttribute>(
"flow1");
63 auto flow2 =
e->particles().at(i)->attribute<HepMC3::IntAttribute>(
"flow2");
64 ICOLUP[
i].first = flow1 ? flow1->value() : 0;
65 ICOLUP[
i].second = flow2 ? flow2->value() : 0;
67 if (pv)
for (
auto p:
pv->particles_in()) {
l= std::min(l,
p->id());
h=std::max(h,
p->id());}
74 bool from_string(
const std::string &att)
override {
75 std::istringstream iss(att);
85 for (
int i = 0;
i < NUP; ++
i ){
88 iss >> MOTHUP[
i].first;
89 iss >> MOTHUP[
i].second;
90 iss >> ICOLUP[
i].first;
91 iss >> ICOLUP[
i].second;
104 bool to_string(std::string &fl)
const override {
105 std::ostringstream
file;
106 file <<
" " << std::setw(4) << NUP
107 <<
" " << std::setw(6) << IDPRUP
108 <<
" " << std::setw(14) << XWGTUP
109 <<
" " << std::setw(14) << SCALUP
110 <<
" " << std::setw(14) << AQEDUP
111 <<
" " << std::setw(14) << AQCDUP <<
"\n";
112 for (
int i = 0;
i < NUP; ++
i )
113 file <<
" " << std::setw(8) << IDUP[
i]
114 <<
" " << std::setw(2) << ISTUP[
i]
115 <<
" " << std::setw(4) << MOTHUP[
i].first
116 <<
" " << std::setw(4) << MOTHUP[
i].second
117 <<
" " << std::setw(4) << ICOLUP[
i].first
118 <<
" " << std::setw(4) << ICOLUP[
i].second
119 <<
" " << std::setw(14) << PUP[
i][0]
120 <<
" " << std::setw(14) << PUP[
i][1]
121 <<
" " << std::setw(14) << PUP[
i][2]
122 <<
" " << std::setw(14) << PUP[
i][3]
123 <<
" " << std::setw(14) << PUP[
i][4]
124 <<
" " << std::setw(1) << VTIMUP[
i]
125 <<
" " << std::setw(1) << SPINUP[
i] << std::endl;
135 PUP.resize(NUP, std::vector<double>(5));
146 std::vector<long> IDUP{};
147 std::vector<int> ISTUP{};
148 std::vector< std::pair<int,int> > MOTHUP{};
149 std::vector< std::pair<int,int> > ICOLUP{};
150 std::vector< std::vector<double> > PUP{};
151 std::vector<double> VTIMUP{};
152 std::vector<double> SPINUP{};
156class GenEventBarcodes :
public HepMC3::Attribute
159 virtual bool from_string(
const std::string & )
override {
return false; }
160 virtual bool to_string(std::string &att)
const override
162 att =
"GenEventBarcodes";
166 auto it = m_vertexBC.find (
id);
167 if (it != m_vertexBC.end())
return it->second;
171 auto it = m_vertexBC.find (
id);
172 if (it != m_vertexBC.end())
return it->second;
176 auto it = m_particleBC.find (
id);
177 if (it != m_particleBC.end())
return it->second;
181 auto it = m_particleBC.find (
id);
182 if (it != m_particleBC.end())
return it->second;
186 void add (GenVertexPtr p) {
188 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
190 m_vertexBC[
barcode->value()] = std::move(p);
194 void remove (GenVertexPtr p) {
196 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
198 auto it = m_vertexBC.find (
barcode->value());
199 if (it != m_vertexBC.end()) {
200 m_vertexBC.erase (it);
205 void add (GenParticlePtr p) {
207 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
209 m_particleBC[
barcode->value()] = std::move(p);
213 void remove (GenParticlePtr p) {
215 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
217 auto it = m_particleBC.find (
barcode->value());
218 if (it != m_particleBC.end()) {
219 m_particleBC.erase (it);
224 std::map<int, ConstGenVertexPtr> barcode_to_vertex_map()
const {
225 std::map<int, ConstGenVertexPtr> ret;
226 for (
const auto &bcvertpair: m_vertexBC)
227 ret.insert({bcvertpair.first,std::const_pointer_cast<const HepMC3::GenVertex>(bcvertpair.second)});
230 std::map<int, ConstGenParticlePtr> barcode_to_particle_map()
const {
231 std::map<int, ConstGenParticlePtr> ret;
232 for (
const auto &bcpartpair: m_particleBC)
233 ret.insert({bcpartpair.first,std::const_pointer_cast<const HepMC3::GenParticle>(bcpartpair.second)});
236 std::map<int,int> id_to_barcode_map()
const {
237 std::map<int, int> ret;
238 for (
const auto &bcvertpair: m_vertexBC) ret.insert({bcvertpair.second->id(),bcvertpair.first});
239 for (
const auto &bcpartpair: m_particleBC) ret.insert({bcpartpair.second->id(),bcpartpair.first});
244 void fillAttribute(GenEvent* e) {
245 const auto eventAttributes =
e->attributes();
246 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
247 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
251 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(i) > 0) {
252 const auto &
ptr = barcodeAttributeIt->second.at(i);
253 if (
ptr->is_parsed()) {
254 m_particleBC[
static_cast<HepMC3::IntAttribute*
>(
ptr.get())->value()] =
ptr->particle();
257 m_particleBC[std::atoi(
ptr->unparsed_string().c_str())] =
ptr->particle();
263 const auto &vertices =
e->vertices();
264 for (
size_t i = 1;
i <= vertices.size();
i++) {
265 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(-i) > 0) {
266 const auto &
ptr = barcodeAttributeIt->second.at(-i);
267 if (
ptr->is_parsed()) {
268 m_vertexBC[
static_cast<HepMC3::IntAttribute*
>(
ptr.get())->value()] =
ptr->vertex();
271 m_vertexBC[std::atoi(
ptr->unparsed_string().c_str())] =
ptr->vertex();
274 m_vertexBC[-
i] = vertices[
i-1];
281 std::unordered_map<int, GenVertexPtr> m_vertexBC;
282 std::unordered_map<int, GenParticlePtr> m_particleBC;
290class ShadowParticle :
public HepMC3::Attribute {
297 ShadowParticle(ConstGenParticlePtr p)
298 : Attribute(), m_shadow(
p) {}
301 virtual bool from_string(
const std::string &)
override {
return false; }
304 virtual bool to_string(std::string &att)
const override
306 att =
"ShadowParticle";
320 e->add_attribute(
"long_long_event_number", std::make_shared<HepMC3::LongLongAttribute>(num));
324 auto at =
e->attribute<HepMC3::LongLongAttribute>(
"long_long_event_number");
325 return at?at->value():
e->event_number();
328inline std::map<std::string, std::size_t> weights_map(
const HepMC3::GenEvent* e) {
329 std::map<std::string, std::size_t> ret;
330 auto run =
e->run_info();
331 if (!run)
return ret;
332 std::vector<std::string>
names =
run->weight_names();
333 for (
const auto& name: names) ret[
name] =
run->weight_index(name);
337inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent& e) {
return e.particles().begin(); }
338inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent& e) {
return e.particles().end(); }
339inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent& e) {
return e.particles().begin(); }
340inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent& e) {
return e.particles().end(); }
343 GenEvent*
e=
new GenEvent();
344 std::shared_ptr<HepMC3::IntAttribute> signal_process_id_A = std::make_shared<HepMC3::IntAttribute>(
signal_process_id);
345 e->add_attribute(
"signal_process_id",signal_process_id_A);
346 e->add_attribute(
"barcodes", std::make_shared<GenEventBarcodes>());
347 e->set_event_number(event_number);
352 GenEvent*
e=
new GenEvent();
353 e->set_event_number(inEvt->event_number());
354 e->weights()=inEvt->weights();
355 auto a_mpi = inEvt->attribute<HepMC3::IntAttribute>(
"mpi");
356 if (a_mpi)
e->add_attribute(
"mpi",std::make_shared<HepMC3::IntAttribute>(*a_mpi));
357 auto a_signal_process_id = inEvt->attribute<HepMC3::IntAttribute>(
"signal_process_id");
358 if (a_signal_process_id)
e->add_attribute(
"signal_process_id",std::make_shared<HepMC3::IntAttribute>(*a_signal_process_id));
359 auto a_event_scale = inEvt->attribute<HepMC3::DoubleAttribute>(
"event_scale");
360 if (a_event_scale)
e->add_attribute(
"event_scale",std::make_shared<HepMC3::DoubleAttribute>(*a_event_scale));
361 auto a_alphaQCD = inEvt->attribute<HepMC3::DoubleAttribute>(
"alphaQCD");
362 if (a_alphaQCD)
e->add_attribute(
"alphaQCD",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQCD));
363 auto a_alphaQED = inEvt->attribute<HepMC3::DoubleAttribute>(
"alphaQED");
364 if (a_alphaQED)
e->add_attribute(
"alphaQED",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQED));
365 auto a_pi = inEvt->pdf_info();
366 if (a_pi)
e->set_pdf_info(std::make_shared<HepMC3::GenPdfInfo>(*a_pi));
367 auto a_hi = inEvt->heavy_ion();
368 if (a_hi)
e->set_heavy_ion(std::make_shared<HepMC3::GenHeavyIon>(*a_hi));
369 auto a_random_states = inEvt->attribute<HepMC3::VectorLongIntAttribute>(
"random_states");
370 if (a_random_states)
e->add_attribute(
"random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(*a_random_states));
371 e->add_attribute(
"barcodes", std::make_shared<GenEventBarcodes>());
376 auto barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
378 barcodes = std::make_shared<GenEventBarcodes>();
379 e->add_attribute(
"barcodes", barcodes);
387 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
393 const auto eventAttributes =
e->attributes();
394 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
395 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
397 const auto &vertices =
e->vertices();
398 if (hasBarcodeAttribute) {
399 for (
size_t i = 1;
i <= vertices.size();
i++) {
400 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
401 if (ptrIt != barcodeAttributeIt->second.end()) {
402 const auto &
ptr = ptrIt->second;
403 if (
ptr->is_parsed()) {
404 if (
id ==
static_cast<HepMC3::IntAttribute*
>(
ptr.get())->value()) {
405 return ptr->vertex();
409 if (
id == std::atoi(
ptr->unparsed_string().c_str())) {
410 return ptr->vertex();
417 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
418 if (!vertices[-
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
419 return vertices[-
id-1];
422 return HepMC3::ConstGenVertexPtr();
427 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
433 const auto eventAttributes =
e->attributes();
434 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
435 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
438 if (hasBarcodeAttribute) {
440 const auto &ptrIt = barcodeAttributeIt->second.find(i);
441 if (ptrIt != barcodeAttributeIt->second.end()) {
442 const auto &
ptr = ptrIt->second;
443 if (
ptr->is_parsed()) {
444 if (
id ==
static_cast<HepMC3::IntAttribute*
>(
ptr.get())->value()) {
445 return ptr->particle();
449 if (
id == std::atoi(
ptr->unparsed_string().c_str())) {
450 return ptr->particle();
457 if (
id > 0 &&
id <=
static_cast<int>(
particles.size())) {
458 if (!particles[
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
462 return HepMC3::ConstGenParticlePtr();
467 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
473 const auto eventAttributes =
e->attributes();
474 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
475 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
477 const auto &vertices =
e->vertices();
478 if (hasBarcodeAttribute) {
479 for (
size_t i = 1;
i <= vertices.size();
i++) {
480 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
481 if (ptrIt != barcodeAttributeIt->second.end()) {
482 const auto &
ptr = ptrIt->second;
483 if (
ptr->is_parsed()) {
484 if (
id ==
static_cast<HepMC3::IntAttribute*
>(
ptr.get())->value()) {
485 return ptr->vertex();
489 if (
id == std::atoi(
ptr->unparsed_string().c_str())) {
490 return ptr->vertex();
497 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
498 if (!vertices[-
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
499 return vertices[-
id-1];
502 return HepMC3::GenVertexPtr();
507 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
513 const auto eventAttributes =
e->attributes();
514 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
515 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
518 if (hasBarcodeAttribute) {
520 const auto &ptrIt = barcodeAttributeIt->second.find(i);
521 if (ptrIt != barcodeAttributeIt->second.end()) {
522 const auto &
ptr = ptrIt->second;
523 if (
ptr->is_parsed()) {
524 if (
id ==
static_cast<HepMC3::IntAttribute*
>(
ptr.get())->value()) {
525 return ptr->particle();
529 if (
id == std::atoi(
ptr->unparsed_string().c_str())) {
530 return ptr->particle();
537 if (
id > 0 &&
id <=
static_cast<int>(
particles.size())) {
538 if (!particles[
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
542 return HepMC3::GenParticlePtr();
545inline int mpi(
const GenEvent& evt) {
546 std::shared_ptr<HepMC3::IntAttribute> A_mpi=
evt.attribute<HepMC3::IntAttribute>(
"mpi");
547 return A_mpi?(A_mpi->value()):0;
549inline int mpi(
const GenEvent* evt) {
550 std::shared_ptr<HepMC3::IntAttribute> A_mpi=
evt->attribute<HepMC3::IntAttribute>(
"mpi");
551 return A_mpi?(A_mpi->value()):0;
555 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=
evt.attribute<HepMC3::IntAttribute>(
"signal_process_id");
556 return A_signal_process_id?(A_signal_process_id->value()):0;
559 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=
evt->attribute<HepMC3::IntAttribute>(
"signal_process_id");
560 return A_signal_process_id?(A_signal_process_id->value()):0;
563 std::shared_ptr<HepMC3::IntAttribute>
signal_process_id = std::make_shared<HepMC3::IntAttribute>(i);
566inline void set_mpi(GenEvent* e,
const int i=0) {
567 std::shared_ptr<HepMC3::IntAttribute>
mpi = std::make_shared<HepMC3::IntAttribute>(i);
568 e->add_attribute(
"mpi",std::move(
mpi));
571 e->add_attribute(
"random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(
a));
574 if (!v || !e)
return;
577 v->add_attribute(
"signal_process_vertex",std::make_shared<HepMC3::IntAttribute>(1));
580inline GenVertexPtr signal_process_vertex(GenEvent* e) {
for (
auto v:
e->vertices())
if (
v->attribute<HepMC3::IntAttribute>(
"signal_process_vertex"))
return v;
return nullptr; }
582 if (!e)
return false;
584 for (
const auto& p :
e->beams()) {
if (
p->status() == 4) ++nBeams; }
585 if (nBeams != 2)
return false;
590 if (!
p->parent_event())
return false;
591 auto barcodes =
p->parent_event()->template attribute<GenEventBarcodes> (
"barcodes");
593 barcodes = std::make_shared<GenEventBarcodes>();
594 p->parent_event()->add_attribute(
"barcodes", barcodes);
597 bool ret =
p->add_attribute(
"barcode",std::make_shared<HepMC3::IntAttribute>(i));
613 if (num > std::numeric_limits<int>::max())
return false;
614 e->set_event_number((
int)num);
618 return e->event_number();
620inline GenEvent::particle_iterator
begin(HepMC::GenEvent& e) {
return e.particles_begin(); }
621inline GenEvent::particle_iterator
end(HepMC::GenEvent& e) {
return e.particles_end(); }
622inline GenEvent::particle_const_iterator
begin(
const HepMC::GenEvent& e) {
return e.particles_begin(); }
623inline GenEvent::particle_const_iterator
end(
const HepMC::GenEvent& e) {
return e.particles_end(); }
624inline GenEvent*
newGenEvent(
const int a,
const int b ) {
return new GenEvent(
a,b); }
627inline GenVertex*
barcode_to_vertex(
const GenEvent* e,
int id ) {
return e->barcode_to_vertex(
id);}
629inline int mpi(
const GenEvent& e) {
632inline int mpi(
const GenEvent* e) {
636 return e.signal_process_id();
639 return e->signal_process_id();
642 e->set_signal_process_id(i);
644inline void set_mpi(GenEvent* e,
const int i) {
648 e->set_random_states(
a);
651 e->set_signal_process_vertex(v);
654 HepMC::GenEvent* outEvt =
new HepMC::GenEvent( inEvt->signal_process_id(), inEvt->event_number() );
655 outEvt->set_mpi ( inEvt->mpi() );
656 outEvt->set_event_scale ( inEvt->event_scale() );
657 outEvt->set_alphaQCD ( inEvt->alphaQCD() );
658 outEvt->set_alphaQED ( inEvt->alphaQED() );
659 outEvt->weights() = inEvt->weights();
660 outEvt->set_random_states( inEvt->random_states() );
661 if (
nullptr != inEvt->heavy_ion() ) {
662 outEvt->set_heavy_ion ( *inEvt->heavy_ion() );
664 if (
nullptr != inEvt->pdf_info() ) {
665 outEvt->set_pdf_info ( *inEvt->pdf_info() );
676inline void line(std::ostream& os,
const GenEvent& e) {e.print(os);}
677inline void line(std::ostream& os,
const GenEvent* e) {e->print(os);}
678inline void content(std::ostream& os,
const GenEvent& e) {e.print(os);}
679inline void content(std::ostream& os,
const GenEvent* e) {e->print(os);}
bool add(const std::string &hname, TKey *tobj)
void line(std::ostream &os, const GenEvent &e)
void content(std::ostream &os, const GenEvent &e)
int signal_process_id(const GenEvent &e)
void set_mpi(GenEvent *e, const int i)
bool set_ll_event_number(HepMC::GenEvent *e, long long int num)
void set_signal_process_vertex(GenEvent *e, T v)
GenParticle * barcode_to_particle(const GenEvent *e, int id)
GenEvent::particle_iterator begin(HepMC::GenEvent &e)
GenEvent::particle_iterator end(HepMC::GenEvent &e)
void set_random_states(GenEvent *e, std::vector< T > a)
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
long long int get_ll_event_number(const HepMC::GenEvent *e)
HepMC::GenVertex * GenVertexPtr
bool suggest_barcode(T &p, int i)
void set_signal_process_id(GenEvent *e, const int i)
GenEvent * copyemptyGenEvent(const GenEvent *inEvt)
void fillBarcodesAttribute(GenEvent *)
GenParticle * GenParticlePtr
const GenParticle * ConstGenParticlePtr
GenVertex * signal_process_vertex(const GenEvent *e)
int mpi(const GenEvent &e)
bool valid_beam_particles(const GenEvent *e)
const HepMC::GenVertex * ConstGenVertexPtr
GenEvent * newGenEvent(const int a, const int b)
l
Printing final latex table to .tex output file.