6 #ifndef ATLASHEPMC_GENEVENT_H
7 #define ATLASHEPMC_GENEVENT_H
11 #include "HepMC3/GenEvent.h"
12 #include "HepMC3/GenHeavyIon.h"
13 #include "HepMC3/GenPdfInfo.h"
14 #include "HepMC3/PrintStreams.h"
20 #include <unordered_map>
23 inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent&
e) {
return e.particles().begin(); }
24 inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent&
e) {
return e.particles().end(); }
25 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent&
e) {
return e.particles().begin(); }
26 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent&
e) {
return e.particles().end(); }
31 using GenHeavyIon=HepMC3::GenHeavyIon;
32 using GenEvent=HepMC3::GenEvent;
34 class ShortEventAttribute :
public HepMC3::Attribute {
36 ShortEventAttribute():HepMC3::Attribute(){}
37 ShortEventAttribute(
const HepMC3::GenEvent*
e):HepMC3::Attribute(){ from_event(
e); }
39 bool from_event(
const HepMC3::GenEvent*
e){
40 NUP=
e->particles().size();
42 XWGTUP =
e->weights().size() ?
e->weights()[0] : 1.0;
43 auto A_signal_process_id=
e->attribute<HepMC3::IntAttribute>(
"signal_process_id");
44 IDPRUP = A_signal_process_id?A_signal_process_id->value() : 0;
45 auto A_event_scale=
e->attribute<HepMC3::DoubleAttribute>(
"event_scale");
46 SCALUP = A_event_scale? A_event_scale->value():0;
47 auto A_alphaQCD=
e->attribute<HepMC3::DoubleAttribute>(
"alphaQCD");
48 AQCDUP = A_alphaQCD? A_alphaQCD->value():0;
49 auto A_alphaQED=
e->attribute<HepMC3::DoubleAttribute>(
"alphaQED");
50 AQEDUP = A_alphaQED? A_alphaQED->value():0;
52 for (
int i = 0;
i < NUP; ++
i ){
53 PUP[
i][0] =
e->particles().at(
i)->momentum().px();
54 PUP[
i][1] =
e->particles().at(
i)->momentum().py();
55 PUP[
i][2] =
e->particles().at(
i)->momentum().pz();
56 PUP[
i][3] =
e->particles().at(
i)->momentum().e();
57 PUP[
i][4] =
e->particles().at(
i)->momentum().m();
58 IDUP[
i] =
e->particles().at(
i)->pdg_id();
59 auto pv =
e->particles().at(
i)->production_vertex();
60 auto ev =
e->particles().at(
i)->end_vertex();
61 if (
pv &&
ev ) ISTUP[
i] = 2;
62 if (
pv && !
ev ) ISTUP[
i] = 1;
63 if (
e->particles().at(
i)->status() == 4 || !
pv ) ISTUP[
i] = -1;
64 auto flow1 =
e->particles().at(
i)->attribute<HepMC3::IntAttribute>(
"flow1");
65 auto flow2 =
e->particles().at(
i)->attribute<HepMC3::IntAttribute>(
"flow2");
66 ICOLUP[
i].first = flow1 ? flow1->value() : 0;
67 ICOLUP[
i].second = flow2 ? flow2->value() : 0;
76 bool from_string(
const std::string &att)
override {
77 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{};
156 class 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;
188 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
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);
207 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
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();
274 m_vertexBC[-
i] = vertices[
i-1];
281 std::unordered_map<int, GenVertexPtr> m_vertexBC;
282 std::unordered_map<int, GenParticlePtr> m_particleBC;
290 class ShadowParticle :
public HepMC3::Attribute {
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();
328 inline 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();
337 inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent&
e) {
return e.particles().begin(); }
338 inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent&
e) {
return e.particles().end(); }
339 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent&
e) {
return e.particles().begin(); }
340 inline 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>();
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();
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];
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();
450 return ptr->particle();
457 if (
id > 0 &&
id <=
static_cast<int>(
particles.size())) {
458 if (!
particles[
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
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();
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];
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();
530 return ptr->particle();
537 if (
id > 0 &&
id <=
static_cast<int>(
particles.size())) {
538 if (!
particles[
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
545 inline 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;
549 inline 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);
566 inline 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));
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));
614 e->set_event_number((
int)
num);
618 return e->event_number();
620 inline GenEvent::particle_iterator
begin(HepMC::GenEvent&
e) {
return e.particles_begin(); }
621 inline GenEvent::particle_iterator
end(HepMC::GenEvent&
e) {
return e.particles_end(); }
622 inline GenEvent::particle_const_iterator
begin(
const HepMC::GenEvent&
e) {
return e.particles_begin(); }
623 inline GenEvent::particle_const_iterator
end(
const HepMC::GenEvent&
e) {
return e.particles_end(); }
629 inline int mpi(
const GenEvent&
e) {
632 inline int mpi(
const GenEvent*
e) {
636 return e.signal_process_id();
639 return e->signal_process_id();
642 e->set_signal_process_id(
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() );
673 template <>
inline bool suggest_barcode<std::unique_ptr<HepMC::GenParticle> >(std::unique_ptr<HepMC::GenParticle>&
p,
int i) {
return p->suggest_barcode(
i);}
676 inline void line(std::ostream&
os,
const GenEvent&
e) {
e.print(
os);}
677 inline void line(std::ostream&
os,
const GenEvent*
e) {
e->print(
os);}