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 GenEventBarcodes :
public HepMC3::Attribute
37 virtual bool from_string(
const std::string & )
override {
return false; }
38 virtual bool to_string(std::string &att)
const override
40 att =
"GenEventBarcodes";
44 auto it = m_vertexBC.find (
id);
45 if (
it != m_vertexBC.end())
return it->second;
49 auto it = m_vertexBC.find (
id);
50 if (
it != m_vertexBC.end())
return it->second;
54 auto it = m_particleBC.find (
id);
55 if (
it != m_particleBC.end())
return it->second;
59 auto it = m_particleBC.find (
id);
60 if (
it != m_particleBC.end())
return it->second;
66 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
74 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
76 auto it = m_vertexBC.find (
barcode->value());
77 if (
it != m_vertexBC.end()) {
78 m_vertexBC.erase (
it);
85 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
93 auto barcode =
p->attribute<HepMC3::IntAttribute>(
"barcode");
95 auto it = m_particleBC.find (
barcode->value());
96 if (
it != m_particleBC.end()) {
97 m_particleBC.erase (
it);
102 std::map<int, ConstGenVertexPtr> barcode_to_vertex_map()
const {
103 std::map<int, ConstGenVertexPtr>
ret;
104 for (
const auto &bcvertpair: m_vertexBC)
105 ret.insert({bcvertpair.first,std::const_pointer_cast<const HepMC3::GenVertex>(bcvertpair.second)});
108 std::map<int, ConstGenParticlePtr> barcode_to_particle_map()
const {
109 std::map<int, ConstGenParticlePtr>
ret;
110 for (
const auto &bcpartpair: m_particleBC)
111 ret.insert({bcpartpair.first,std::const_pointer_cast<const HepMC3::GenParticle>(bcpartpair.second)});
114 std::map<int,int> id_to_barcode_map()
const {
115 std::map<int, int>
ret;
116 for (
const auto &bcvertpair: m_vertexBC)
ret.insert({bcvertpair.second->id(),bcvertpair.first});
117 for (
const auto &bcpartpair: m_particleBC)
ret.insert({bcpartpair.second->id(),bcpartpair.first});
122 void fillAttribute(GenEvent*
e) {
123 const auto eventAttributes =
e->attributes();
124 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
125 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
129 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(
i) > 0) {
130 const auto &
ptr = barcodeAttributeIt->second.at(
i);
131 if (
ptr->is_parsed()) {
132 m_particleBC[std::dynamic_pointer_cast<HepMC3::IntAttribute>(
ptr)->value()] =
ptr->particle();
135 m_particleBC[
std::atoi(
ptr->unparsed_string().c_str())] =
ptr->particle();
141 const auto &vertices =
e->vertices();
142 for (
size_t i = 1;
i <= vertices.size();
i++) {
143 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(-
i) > 0) {
144 const auto &
ptr = barcodeAttributeIt->second.at(-
i);
145 if (
ptr->is_parsed()) {
146 m_vertexBC[std::dynamic_pointer_cast<HepMC3::IntAttribute>(
ptr)->value()] =
ptr->vertex();
152 m_vertexBC[-
i] = vertices[
i-1];
159 std::unordered_map<int, GenVertexPtr> m_vertexBC;
160 std::unordered_map<int, GenParticlePtr> m_particleBC;
168 class ShadowParticle :
public HepMC3::Attribute {
176 : Attribute(), m_shadow(
p) {}
179 virtual bool from_string(
const std::string &)
override {
return false; }
182 virtual bool to_string(std::string &att)
const override
184 att =
"ShadowParticle";
198 e->add_attribute(
"long_long_event_number", std::make_shared<HepMC3::LongLongAttribute>(
num));
202 auto at =
e->attribute<HepMC3::LongLongAttribute>(
"long_long_event_number");
203 return at?at->value():
e->event_number();
206 inline std::map<std::string, std::size_t> weights_map(
const HepMC3::GenEvent*
e) {
207 std::map<std::string, std::size_t>
ret;
208 auto run =
e->run_info();
210 std::vector<std::string>
names =
run->weight_names();
215 inline std::vector<HepMC3::GenParticlePtr>::const_iterator
begin(HepMC3::GenEvent&
e) {
return e.particles().begin(); }
216 inline std::vector<HepMC3::GenParticlePtr>::const_iterator
end(HepMC3::GenEvent&
e) {
return e.particles().end(); }
217 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
begin(
const HepMC3::GenEvent&
e) {
return e.particles().begin(); }
218 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator
end(
const HepMC3::GenEvent&
e) {
return e.particles().end(); }
221 GenEvent*
e=
new GenEvent();
222 std::shared_ptr<HepMC3::IntAttribute> signal_process_id_A = std::make_shared<HepMC3::IntAttribute>(
signal_process_id);
223 e->add_attribute(
"signal_process_id",signal_process_id_A);
224 e->add_attribute(
"barcodes", std::make_shared<GenEventBarcodes>());
225 e->set_event_number(event_number);
230 GenEvent*
e=
new GenEvent();
231 e->set_event_number(inEvt->event_number());
232 e->weights()=inEvt->weights();
233 auto a_mpi = inEvt->attribute<HepMC3::IntAttribute>(
"mpi");
234 if (a_mpi)
e->add_attribute(
"mpi",std::make_shared<HepMC3::IntAttribute>(*a_mpi));
235 auto a_signal_process_id = inEvt->attribute<HepMC3::IntAttribute>(
"signal_process_id");
236 if (a_signal_process_id)
e->add_attribute(
"signal_process_id",std::make_shared<HepMC3::IntAttribute>(*a_signal_process_id));
237 auto a_event_scale = inEvt->attribute<HepMC3::DoubleAttribute>(
"event_scale");
238 if (a_event_scale)
e->add_attribute(
"event_scale",std::make_shared<HepMC3::DoubleAttribute>(*a_event_scale));
239 auto a_alphaQCD = inEvt->attribute<HepMC3::DoubleAttribute>(
"alphaQCD");
240 if (a_alphaQCD)
e->add_attribute(
"alphaQCD",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQCD));
241 auto a_alphaQED = inEvt->attribute<HepMC3::DoubleAttribute>(
"alphaQED");
242 if (a_alphaQED)
e->add_attribute(
"alphaQED",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQED));
243 auto a_pi = inEvt->pdf_info();
244 if (a_pi)
e->set_pdf_info(std::make_shared<HepMC3::GenPdfInfo>(*a_pi));
245 auto a_hi = inEvt->heavy_ion();
246 if (a_hi)
e->set_heavy_ion(std::make_shared<HepMC3::GenHeavyIon>(*a_hi));
247 auto a_random_states = inEvt->attribute<HepMC3::VectorLongIntAttribute>(
"random_states");
248 if (a_random_states)
e->add_attribute(
"random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(*a_random_states));
249 e->add_attribute(
"barcodes", std::make_shared<GenEventBarcodes>());
254 auto barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
256 barcodes = std::make_shared<GenEventBarcodes>();
265 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
271 const auto eventAttributes =
e->attributes();
272 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
273 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
275 const auto &vertices =
e->vertices();
276 if (hasBarcodeAttribute) {
277 for (
size_t i = 1;
i <= vertices.size();
i++) {
278 const auto &ptrIt = barcodeAttributeIt->second.find(-
i);
279 if (ptrIt != barcodeAttributeIt->second.end()) {
280 const auto &
ptr = ptrIt->second;
281 if (
ptr->is_parsed()) {
282 if (
id == std::dynamic_pointer_cast<HepMC3::IntAttribute>(
ptr)->value()) {
283 return ptr->vertex();
288 return ptr->vertex();
295 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
296 if (!vertices[-
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
297 return vertices[-
id-1];
305 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
311 const auto eventAttributes =
e->attributes();
312 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
313 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
316 if (hasBarcodeAttribute) {
318 const auto &ptrIt = barcodeAttributeIt->second.find(
i);
319 if (ptrIt != barcodeAttributeIt->second.end()) {
320 const auto &
ptr = ptrIt->second;
321 if (
ptr->is_parsed()) {
322 if (
id == std::dynamic_pointer_cast<HepMC3::IntAttribute>(
ptr)->value()) {
323 return ptr->particle();
328 return ptr->particle();
335 if (
id > 0 &&
id <=
static_cast<int>(
particles.size())) {
336 if (!
particles[
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
345 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
351 const auto eventAttributes =
e->attributes();
352 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
353 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
355 const auto &vertices =
e->vertices();
356 if (hasBarcodeAttribute) {
357 for (
size_t i = 1;
i <= vertices.size();
i++) {
358 const auto &ptrIt = barcodeAttributeIt->second.find(-
i);
359 if (ptrIt != barcodeAttributeIt->second.end()) {
360 const auto &
ptr = ptrIt->second;
361 if (
ptr->is_parsed()) {
362 if (
id == std::dynamic_pointer_cast<HepMC3::IntAttribute>(
ptr)->value()) {
363 return ptr->vertex();
368 return ptr->vertex();
375 if (-
id > 0 && -
id <=
static_cast<int>(vertices.size())) {
376 if (!vertices[-
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
377 return vertices[-
id-1];
385 const auto &
barcodes =
e->attribute<GenEventBarcodes> (
"barcodes");
391 const auto eventAttributes =
e->attributes();
392 const auto barcodeAttributeIt = eventAttributes.find(
"barcode");
393 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
396 if (hasBarcodeAttribute) {
398 const auto &ptrIt = barcodeAttributeIt->second.find(
i);
399 if (ptrIt != barcodeAttributeIt->second.end()) {
400 const auto &
ptr = ptrIt->second;
401 if (
ptr->is_parsed()) {
402 if (
id == std::dynamic_pointer_cast<HepMC3::IntAttribute>(
ptr)->value()) {
403 return ptr->particle();
408 return ptr->particle();
415 if (
id > 0 &&
id <=
static_cast<int>(
particles.size())) {
416 if (!
particles[
id-1]->attribute<HepMC3::IntAttribute>(
"barcode")) {
423 inline int mpi(
const GenEvent&
evt) {
424 std::shared_ptr<HepMC3::IntAttribute> A_mpi=
evt.attribute<HepMC3::IntAttribute>(
"mpi");
425 return A_mpi?(A_mpi->value()):0;
427 inline int mpi(
const GenEvent*
evt) {
428 std::shared_ptr<HepMC3::IntAttribute> A_mpi=
evt->attribute<HepMC3::IntAttribute>(
"mpi");
429 return A_mpi?(A_mpi->value()):0;
433 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=
evt.attribute<HepMC3::IntAttribute>(
"signal_process_id");
434 return A_signal_process_id?(A_signal_process_id->value()):0;
437 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=
evt->attribute<HepMC3::IntAttribute>(
"signal_process_id");
438 return A_signal_process_id?(A_signal_process_id->value()):0;
441 std::shared_ptr<HepMC3::IntAttribute>
signal_process_id = std::make_shared<HepMC3::IntAttribute>(
i);
444 inline void set_mpi(GenEvent*
e,
const int i=0) {
445 std::shared_ptr<HepMC3::IntAttribute>
mpi = std::make_shared<HepMC3::IntAttribute>(
i);
446 e->add_attribute(
"mpi",
mpi);
449 e->add_attribute(
"random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(
a));
452 if (!
v || !
e)
return;
455 v->add_attribute(
"signal_process_vertex",std::make_shared<HepMC3::IntAttribute>(1));
460 if (!
e)
return false;
462 for (
const auto&
p :
e->beams()) {
if (
p->status() == 4) ++nBeams; }
463 if (nBeams != 2)
return false;
468 if (!
p->parent_event())
return false;
469 auto barcodes =
p->parent_event()->template attribute<GenEventBarcodes> (
"barcodes");
471 barcodes = std::make_shared<GenEventBarcodes>();
472 p->parent_event()->add_attribute(
"barcodes",
barcodes);
475 bool ret =
p->add_attribute(
"barcode",std::make_shared<HepMC3::IntAttribute>(
i));
492 e->set_event_number((
int)
num);
496 return e->event_number();
498 inline GenEvent::particle_iterator
begin(HepMC::GenEvent&
e) {
return e.particles_begin(); }
499 inline GenEvent::particle_iterator
end(HepMC::GenEvent&
e) {
return e.particles_end(); }
500 inline GenEvent::particle_const_iterator
begin(
const HepMC::GenEvent&
e) {
return e.particles_begin(); }
501 inline GenEvent::particle_const_iterator
end(
const HepMC::GenEvent&
e) {
return e.particles_end(); }
507 inline int mpi(
const GenEvent&
e) {
510 inline int mpi(
const GenEvent*
e) {
514 return e.signal_process_id();
517 return e->signal_process_id();
520 e->set_signal_process_id(
i);
526 e->set_random_states(
a);
529 e->set_signal_process_vertex(
v);
532 HepMC::GenEvent* outEvt =
new HepMC::GenEvent( inEvt->signal_process_id(), inEvt->event_number() );
533 outEvt->set_mpi ( inEvt->mpi() );
534 outEvt->set_event_scale ( inEvt->event_scale() );
535 outEvt->set_alphaQCD ( inEvt->alphaQCD() );
536 outEvt->set_alphaQED ( inEvt->alphaQED() );
537 outEvt->weights() = inEvt->weights();
538 outEvt->set_random_states( inEvt->random_states() );
539 if (
nullptr != inEvt->heavy_ion() ) {
540 outEvt->set_heavy_ion ( *inEvt->heavy_ion() );
542 if (
nullptr != inEvt->pdf_info() ) {
543 outEvt->set_pdf_info ( *inEvt->pdf_info() );
551 template <>
inline bool suggest_barcode<std::unique_ptr<HepMC::GenParticle> >(std::unique_ptr<HepMC::GenParticle>&
p,
int i) {
return p->suggest_barcode(
i);}
554 inline void line(std::ostream&
os,
const GenEvent&
e) {
e.print(
os);}
555 inline void line(std::ostream&
os,
const GenEvent*
e) {
e->print(
os);}