ATLAS Offline Software
Loading...
Searching...
No Matches
GenEvent.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4/* Author: Andrii Verbytskyi andrii.verbytskyi@mpp.mpg.de */
5
6#ifndef ATLASHEPMC_GENEVENT_H
7#define ATLASHEPMC_GENEVENT_H
8#ifdef HEPMC3
9#include "HepMC3/GenEvent.h"
10#include "HepMC3/GenHeavyIon.h"
11#include "HepMC3/GenPdfInfo.h"
12#include "HepMC3/PrintStreams.h"
13#include "AtlasHepMC/Barcode.h"
17
18#include <algorithm>
19#include <cstdlib>
20#include <iomanip>
21#include <map>
22#include <memory>
23#include <sstream>
24#include <string>
25#include <vector>
26
27#include <unordered_map>
28
29namespace HepMC3 {
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(); }
34}
35
36namespace HepMC {
37inline const std::string s_barcodeStr{"barcode"};
38inline const std::string s_barcodePluralStr{"barcodes"};
39using Print=HepMC3::Print;
40using GenHeavyIon=HepMC3::GenHeavyIon;
41using GenEvent=HepMC3::GenEvent;
42
43class ShortEventAttribute : public HepMC3::Attribute {
44public:
45 ShortEventAttribute():HepMC3::Attribute(){}
46 ShortEventAttribute(const HepMC3::GenEvent* e):HepMC3::Attribute(){ from_event(e); }
47
48 bool from_event(const HepMC3::GenEvent* e){
49 NUP=e->particles().size();
50 resize();
51 XWGTUP = e->weights().size() ? e->weights()[0] : 1.0;
52 auto A_signal_process_id=e->attribute<HepMC3::IntAttribute>("signal_process_id");
53 IDPRUP = A_signal_process_id?A_signal_process_id->value() : 0;
54 auto A_event_scale=e->attribute<HepMC3::DoubleAttribute>("event_scale");
55 SCALUP = A_event_scale? A_event_scale->value():0;
56 auto A_alphaQCD=e->attribute<HepMC3::DoubleAttribute>("alphaQCD");
57 AQCDUP = A_alphaQCD? A_alphaQCD->value():0;
58 auto A_alphaQED=e->attribute<HepMC3::DoubleAttribute>("alphaQED");
59 AQEDUP = A_alphaQED? A_alphaQED->value():0;
60 for ( int i = 0; i < NUP; ++i ){
61 const auto & thisParticle = e->particles().at(i);
62 PUP[i][0] = thisParticle->momentum().px();
63 PUP[i][1] = thisParticle->momentum().py();
64 PUP[i][2] = thisParticle->momentum().pz();
65 PUP[i][3] = thisParticle->momentum().e();
66 PUP[i][4] = thisParticle->momentum().m();
67 IDUP[i] = thisParticle->pdg_id();
68 auto pv = thisParticle->production_vertex();
69 auto ev = thisParticle->end_vertex();
70 if (pv && ev ) ISTUP[i] = 2;
71 if (pv && !ev ) ISTUP[i] = 1;
72 if (thisParticle->status() == 4 || !pv ) ISTUP[i] = -1;
73 auto flow1 = thisParticle->attribute<HepMC3::IntAttribute>("flow1");
74 auto flow2 = thisParticle->attribute<HepMC3::IntAttribute>("flow2");
75 ICOLUP[i].first = flow1 ? flow1->value() : 0;
76 ICOLUP[i].second = flow2 ? flow2->value() : 0;
77 int l = 0;
78 int h = 0;
79 if (pv && !pv->particles_in().empty()) {
80 l = pv->particles_in().front()->id();
81 h = l;
82 for (const auto& p : pv->particles_in()) {
83 const int id = p->id();
84 l = std::min(l, id);
85 h = std::max(h, id);
86 }
87 }
88 MOTHUP[i].first = h;
89 MOTHUP[i].second = l;
90 }
91 return true;
92 }
93
94 bool from_string(const std::string &att) override {
95 std::istringstream iss(att);
96 iss >> NUP;
97 iss >> IDPRUP;
98 iss >> XWGTUP;
99 iss >> SCALUP;
100 iss >> AQEDUP;
101 iss >> AQCDUP;
102 //assume input is a trusted source
103 //coverity[tainted_data]
104 resize();
105 for ( int i = 0; i < NUP; ++i ){
106 iss >> IDUP[i];
107 iss >> ISTUP[i];
108 iss >> MOTHUP[i].first;
109 iss >> MOTHUP[i].second;
110 iss >> ICOLUP[i].first;
111 iss >> ICOLUP[i].second;
112 iss >> PUP[i][0];
113 iss >> PUP[i][1];
114 iss >> PUP[i][2];
115 iss >> PUP[i][3];
116 iss >> PUP[i][4];
117 iss >> VTIMUP[i];
118 iss >> SPINUP[i];
119 }
120 set_is_parsed(true);
121 return true;
122 }
123
124 bool to_string(std::string &fl) const override {
125 std::ostringstream file;
126 file << " " << std::setw(4) << NUP
127 << " " << std::setw(6) << IDPRUP
128 << " " << std::setw(14) << XWGTUP
129 << " " << std::setw(14) << SCALUP
130 << " " << std::setw(14) << AQEDUP
131 << " " << std::setw(14) << AQCDUP << "\n";
132 for ( int i = 0; i < NUP; ++i )
133 file << " " << std::setw(8) << IDUP[i]
134 << " " << std::setw(2) << ISTUP[i]
135 << " " << std::setw(4) << MOTHUP[i].first
136 << " " << std::setw(4) << MOTHUP[i].second
137 << " " << std::setw(4) << ICOLUP[i].first
138 << " " << std::setw(4) << ICOLUP[i].second
139 << " " << std::setw(14) << PUP[i][0]
140 << " " << std::setw(14) << PUP[i][1]
141 << " " << std::setw(14) << PUP[i][2]
142 << " " << std::setw(14) << PUP[i][3]
143 << " " << std::setw(14) << PUP[i][4]
144 << " " << std::setw(1) << VTIMUP[i]
145 << " " << std::setw(1) << SPINUP[i] << std::endl;
146 fl+=file.str();
147 return true;
148 }
149
150 void resize() {
151 IDUP.resize(NUP);
152 ISTUP.resize(NUP);
153 MOTHUP.resize(NUP);
154 ICOLUP.resize(NUP);
155 PUP.resize(NUP, std::vector<double>(5));
156 VTIMUP.resize(NUP);
157 SPINUP.resize(NUP);
158 }
159
160 int NUP=0;
161 int IDPRUP=0;
162 double XWGTUP=0;
163 double SCALUP=0;
164 double AQEDUP=0;
165 double AQCDUP=0;
166 std::vector<long> IDUP{};
167 std::vector<int> ISTUP{};
168 std::vector< std::pair<int,int> > MOTHUP{};
169 std::vector< std::pair<int,int> > ICOLUP{};
170 std::vector< std::vector<double> > PUP{};
171 std::vector<double> VTIMUP{};
172 std::vector<double> SPINUP{};
173};
174
175
176class GenEventBarcodes : public HepMC3::Attribute
177{
178public:
179 virtual bool from_string(const std::string & /*att*/) override { return false; }
180 virtual bool to_string(std::string &att) const override
181 {
182 att = "GenEventBarcodes";
183 return true;
184 }
185 ConstGenVertexPtr barcode_to_vertex (int id) const {
186 auto it = m_vertexBC.find (id);
187 if (it != m_vertexBC.end()) return it->second;
188 return nullptr;
189 }
191 auto it = m_vertexBC.find (id);
192 if (it != m_vertexBC.end()) return it->second;
193 return nullptr;
194 }
196 auto it = m_particleBC.find (id);
197 if (it != m_particleBC.end()) return it->second;
198 return nullptr;
199 }
201 auto it = m_particleBC.find (id);
202 if (it != m_particleBC.end()) return it->second;
203 return nullptr;
204 }
205
206 void add (GenVertexPtr p) {
207 if (!p) return;
208 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
209 if (barcode) {
210 m_vertexBC[barcode->value()] = std::move(p);
211 }
212 }
213
214 void remove (GenVertexPtr p) {
215 if (!p) return;
216 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
217 if (barcode) {
218 auto it = m_vertexBC.find (barcode->value());
219 if (it != m_vertexBC.end()) {
220 m_vertexBC.erase (it);
221 }
222 }
223 }
224
225 void add (GenParticlePtr p) {
226 if (!p) return;
227 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
228 if (barcode) {
229 m_particleBC[barcode->value()] = std::move(p);
230 }
231 }
232
233 void remove (GenParticlePtr p) {
234 if (!p) return;
235 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
236 if (barcode) {
237 auto it = m_particleBC.find (barcode->value());
238 if (it != m_particleBC.end()) {
239 m_particleBC.erase (it);
240 }
241 }
242 }
243
244 std::map<int, ConstGenVertexPtr> barcode_to_vertex_map() const {
245 std::map<int, ConstGenVertexPtr> ret;
246 for (const auto &bcvertpair: m_vertexBC)
247 ret.insert({bcvertpair.first,std::const_pointer_cast<const HepMC3::GenVertex>(bcvertpair.second)});
248 return ret;
249 }
250 std::map<int, ConstGenParticlePtr> barcode_to_particle_map() const {
251 std::map<int, ConstGenParticlePtr> ret;
252 for (const auto &bcpartpair: m_particleBC)
253 ret.insert({bcpartpair.first,std::const_pointer_cast<const HepMC3::GenParticle>(bcpartpair.second)});
254 return ret;
255 }
256 std::map<int,int> id_to_barcode_map() const {
257 std::map<int, int> ret;
258 for (const auto &bcvertpair: m_vertexBC) ret.insert({bcvertpair.second->id(),bcvertpair.first});
259 for (const auto &bcpartpair: m_particleBC) ret.insert({bcpartpair.second->id(),bcpartpair.first});
260 return ret;
261 }
262
263
264 void fillAttribute(GenEvent* e) {
265 const auto eventAttributes = e->attributes(); // this makes a copy
266 const auto barcodeAttributeIt = eventAttributes.find("barcode");
267 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
268
269 const auto &particles = e->particles();
270 for (size_t i = 1; i <= particles.size(); i++) {
271 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(i) > 0) {
272 const auto &ptr = barcodeAttributeIt->second.at(i);
273 if (ptr->is_parsed()) {
274 m_particleBC[static_cast<HepMC3::IntAttribute*>(ptr.get())->value()] = ptr->particle();
275 }
276 else {
277 m_particleBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->particle();
278 }
279 } else {
280 m_particleBC[i] = particles[i-1];
281 }
282 }
283 const auto &vertices = e->vertices();
284 for (size_t i = 1; i <= vertices.size(); i++) {
285 if (hasBarcodeAttribute && barcodeAttributeIt->second.count(-i) > 0) {
286 const auto &ptr = barcodeAttributeIt->second.at(-i);
287 if (ptr->is_parsed()) {
288 m_vertexBC[static_cast<HepMC3::IntAttribute*>(ptr.get())->value()] = ptr->vertex();
289 }
290 else {
291 m_vertexBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->vertex();
292 }
293 } else {
294 m_vertexBC[-i] = vertices[i-1];
295 }
296 }
297 set_is_parsed(true);
298 }
299
300private:
301 std::unordered_map<int, GenVertexPtr> m_vertexBC;
302 std::unordered_map<int, GenParticlePtr> m_particleBC;
303};
304
310class ShadowParticle : public HepMC3::Attribute {
311public:
312
314 ShadowParticle() {}
315
317 ShadowParticle(ConstGenParticlePtr p)
318 : Attribute(), m_shadow(p) {}
319
321 virtual bool from_string(const std::string &) override { return false; }
322
324 virtual bool to_string(std::string &att) const override
325 {
326 att = "ShadowParticle";
327 return true;
328 }
330 ConstGenParticlePtr value() const {
331 return m_shadow;
332 }
333
334private:
335
336 ConstGenParticlePtr m_shadow;
337};
338
339inline bool set_ll_event_number(HepMC3::GenEvent* e, long long int num){
340 e->add_attribute("long_long_event_number", std::make_shared<HepMC3::LongLongAttribute>(num));
341 return true;
342}
343inline long long int get_ll_event_number(const HepMC3::GenEvent* e){
344 auto at = e->attribute<HepMC3::LongLongAttribute>("long_long_event_number");
345 return at?at->value():e->event_number();
346}
347
348inline std::map<std::string, std::size_t> weights_map(const HepMC3::GenEvent* e) {
349 std::map<std::string, std::size_t> ret;
350 auto run = e->run_info();
351 if (!run) return ret;
352 std::vector<std::string> names = run->weight_names();
353 for (const auto& name: names) ret[name] = run->weight_index(name);
354 return ret;
355}
356
357inline std::vector<HepMC3::GenParticlePtr>::const_iterator begin(HepMC3::GenEvent& e) { return e.particles().begin(); }
358inline std::vector<HepMC3::GenParticlePtr>::const_iterator end(HepMC3::GenEvent& e) { return e.particles().end(); }
359inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator begin(const HepMC3::GenEvent& e) { return e.particles().begin(); }
360inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator end(const HepMC3::GenEvent& e) { return e.particles().end(); }
361
362inline GenEvent* newGenEvent(const int signal_process_id, const int event_number ) { // TODO Update event_number to long long int?
363 GenEvent* e= new GenEvent();
364 std::shared_ptr<HepMC3::IntAttribute> signal_process_id_A = std::make_shared<HepMC3::IntAttribute>(signal_process_id);
365 e->add_attribute("signal_process_id",signal_process_id_A);
366 e->add_attribute(s_barcodePluralStr, std::make_shared<GenEventBarcodes>());
367 e->set_event_number(event_number);
368 return e;
369}
370
371inline GenEvent* copyemptyGenEvent(const GenEvent* inEvt) {
372 GenEvent* e= new GenEvent();
373 e->set_event_number(inEvt->event_number());
374 e->weights()=inEvt->weights();
375 auto a_mpi = inEvt->attribute<HepMC3::IntAttribute>("mpi");
376 if (a_mpi) e->add_attribute("mpi",std::make_shared<HepMC3::IntAttribute>(*a_mpi));
377 auto a_signal_process_id = inEvt->attribute<HepMC3::IntAttribute>("signal_process_id");
378 if (a_signal_process_id) e->add_attribute("signal_process_id",std::make_shared<HepMC3::IntAttribute>(*a_signal_process_id));
379 auto a_event_scale = inEvt->attribute<HepMC3::DoubleAttribute>("event_scale");
380 if (a_event_scale) e->add_attribute("event_scale",std::make_shared<HepMC3::DoubleAttribute>(*a_event_scale));
381 auto a_alphaQCD = inEvt->attribute<HepMC3::DoubleAttribute>("alphaQCD");
382 if (a_alphaQCD) e->add_attribute("alphaQCD",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQCD));
383 auto a_alphaQED = inEvt->attribute<HepMC3::DoubleAttribute>("alphaQED");
384 if (a_alphaQED) e->add_attribute("alphaQED",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQED));
385 auto a_pi = inEvt->pdf_info();
386 if (a_pi) e->set_pdf_info(std::make_shared<HepMC3::GenPdfInfo>(*a_pi));
387 auto a_hi = inEvt->heavy_ion();
388 if (a_hi) e->set_heavy_ion(std::make_shared<HepMC3::GenHeavyIon>(*a_hi));
389 auto a_random_states = inEvt->attribute<HepMC3::VectorLongIntAttribute>("random_states");
390 if (a_random_states) e->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(*a_random_states));
391 e->add_attribute(s_barcodePluralStr, std::make_shared<GenEventBarcodes>());
392 return e;
393}
394
395inline void fillBarcodesAttribute(GenEvent* e) {
396 auto barcodes = e->attribute<GenEventBarcodes> (s_barcodePluralStr);
397 if (!barcodes) {
398 barcodes = std::make_shared<GenEventBarcodes>();
399 e->add_attribute(s_barcodePluralStr, barcodes);
400 }
401 // force re-parsing as calling barcodes->is_parsed() returns true here
402 barcodes->fillAttribute(e);
403}
404
405inline ConstGenVertexPtr barcode_to_vertex(const GenEvent* e, int id ) {
406 // Prefer to use optimized GenEvent barcodes attribute
407 const auto &barcodes = e->attribute<GenEventBarcodes> (s_barcodePluralStr);
408 if (barcodes) {
409 ConstGenVertexPtr ptr = barcodes->barcode_to_vertex (id);
410 if (ptr) return ptr;
411 }
412 // Fallback to unoptimized GenVertex barcode attribute
413 const auto eventAttributes = e->attributes(); // this makes a copy
414 const auto barcodeAttributeIt = eventAttributes.find(s_barcodeStr);
415 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
416
417 const auto &vertices = e->vertices();
418 if (hasBarcodeAttribute) {
419 for (size_t i = 1; i <= vertices.size(); i++) {
420 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
421 if (ptrIt != barcodeAttributeIt->second.end()) {
422 const auto &ptr = ptrIt->second;
423 if (ptr->is_parsed()) {
424 if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
425 return ptr->vertex();
426 }
427 }
428 else {
429 if (id == std::atoi(ptr->unparsed_string().c_str())) {
430 return ptr->vertex();
431 }
432 }
433 }
434 }
435 }
436 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
437 if (-id > 0 && -id <= static_cast<int>(vertices.size())) {
438 if (!vertices[-id-1]->attribute<HepMC3::IntAttribute>(s_barcodeStr)) {
439 return vertices[-id-1];
440 }
441 }
442 return HepMC3::ConstGenVertexPtr();
443}
444
445inline ConstGenParticlePtr barcode_to_particle(const GenEvent* e, int id ) {
446 // Prefer to use optimized GenEvent barcodes attribute
447 const auto &barcodes = e->attribute<GenEventBarcodes> (s_barcodePluralStr);
448 if (barcodes) {
449 ConstGenParticlePtr ptr = barcodes->barcode_to_particle (id);
450 if (ptr) return ptr;
451 }
452 // Fallback to unoptimized GenParticle barcode attribute
453 const auto eventAttributes = e->attributes(); // this makes a copy
454 const auto barcodeAttributeIt = eventAttributes.find(s_barcodeStr);
455 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
456
457 const auto &particles = e->particles();
458 if (hasBarcodeAttribute) {
459 for (size_t i = 1; i <= particles.size(); i++) {
460 const auto &ptrIt = barcodeAttributeIt->second.find(i);
461 if (ptrIt != barcodeAttributeIt->second.end()) {
462 const auto &ptr = ptrIt->second;
463 if (ptr->is_parsed()) {
464 if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
465 return ptr->particle();
466 }
467 }
468 else {
469 if (id == std::atoi(ptr->unparsed_string().c_str())) {
470 return ptr->particle();
471 }
472 }
473 }
474 }
475 }
476 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
477 if (id > 0 && id <= static_cast<int>(particles.size())) {
478 if (!particles[id-1]->attribute<HepMC3::IntAttribute>(s_barcodeStr)) {
479 return particles[id-1];
480 }
481 }
482 return HepMC3::ConstGenParticlePtr();
483}
484
485inline GenVertexPtr barcode_to_vertex(GenEvent* e, int id ) {
486 // Prefer to use optimized GenEvent barcodes attribute
487 const auto &barcodes = e->attribute<GenEventBarcodes> (s_barcodePluralStr);
488 if (barcodes) {
489 GenVertexPtr ptr = barcodes->barcode_to_vertex (id);
490 if (ptr) return ptr;
491 }
492 // Fallback to unoptimized GenVertex barcode attribute
493 const auto eventAttributes = e->attributes(); // this makes a copy
494 const auto barcodeAttributeIt = eventAttributes.find(s_barcodeStr);
495 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
496
497 const auto &vertices = e->vertices();
498 if (hasBarcodeAttribute) {
499 for (size_t i = 1; i <= vertices.size(); i++) {
500 const auto &ptrIt = barcodeAttributeIt->second.find(-i);
501 if (ptrIt != barcodeAttributeIt->second.end()) {
502 const auto &ptr = ptrIt->second;
503 if (ptr->is_parsed()) {
504 if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
505 return ptr->vertex();
506 }
507 }
508 else {
509 if (id == std::atoi(ptr->unparsed_string().c_str())) {
510 return ptr->vertex();
511 }
512 }
513 }
514 }
515 }
516 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
517 if (-id > 0 && -id <= static_cast<int>(vertices.size())) {
518 if (!vertices[-id-1]->attribute<HepMC3::IntAttribute>(s_barcodeStr)) {
519 return vertices[-id-1];
520 }
521 }
522 return HepMC3::GenVertexPtr();
523}
524
525inline GenParticlePtr barcode_to_particle(GenEvent* e, int id ) {
526 // Prefer to use optimized GenEvent barcodes attribute
527 const auto &barcodes = e->attribute<GenEventBarcodes> (s_barcodePluralStr);
528 if (barcodes) {
529 GenParticlePtr ptr = barcodes->barcode_to_particle (id);
530 if (ptr) return ptr;
531 }
532 // Fallback to unoptimized GenParticle barcode attribute
533 const auto eventAttributes = e->attributes(); // this makes a copy
534 const auto barcodeAttributeIt = eventAttributes.find(s_barcodeStr);
535 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
536
537 const auto &particles = e->particles();
538 if (hasBarcodeAttribute) {
539 for (size_t i = 1; i <= particles.size(); i++) {
540 const auto &ptrIt = barcodeAttributeIt->second.find(i);
541 if (ptrIt != barcodeAttributeIt->second.end()) {
542 const auto &ptr = ptrIt->second;
543 if (ptr->is_parsed()) {
544 if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
545 return ptr->particle();
546 }
547 }
548 else {
549 if (id == std::atoi(ptr->unparsed_string().c_str())) {
550 return ptr->particle();
551 }
552 }
553 }
554 }
555 }
556 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
557 if (id > 0 && id <= static_cast<int>(particles.size())) {
558 if (!particles[id-1]->attribute<HepMC3::IntAttribute>(s_barcodeStr)) {
559 return particles[id-1];
560 }
561 }
562 return HepMC3::GenParticlePtr();
563}
564
565inline int mpi(const GenEvent& evt) {
566 std::shared_ptr<HepMC3::IntAttribute> A_mpi=evt.attribute<HepMC3::IntAttribute>("mpi");
567 return A_mpi?(A_mpi->value()):0;
568}
569inline int mpi(const GenEvent* evt) {
570 std::shared_ptr<HepMC3::IntAttribute> A_mpi=evt->attribute<HepMC3::IntAttribute>("mpi");
571 return A_mpi?(A_mpi->value()):0;
572}
573
574inline int signal_process_id(const GenEvent& evt) {
575 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=evt.attribute<HepMC3::IntAttribute>("signal_process_id");
576 return A_signal_process_id?(A_signal_process_id->value()):0;
577}
578inline int signal_process_id(const GenEvent* evt) {
579 std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=evt->attribute<HepMC3::IntAttribute>("signal_process_id");
580 return A_signal_process_id?(A_signal_process_id->value()):0;
581}
582inline void set_signal_process_id(GenEvent* e, const int i=0) {
583 std::shared_ptr<HepMC3::IntAttribute> signal_process_id = std::make_shared<HepMC3::IntAttribute>(i);
584 e->add_attribute("signal_process_id",std::move(signal_process_id));
585}
586inline void set_mpi(GenEvent* e, const int i=0) {
587 std::shared_ptr<HepMC3::IntAttribute> mpi = std::make_shared<HepMC3::IntAttribute>(i);
588 e->add_attribute("mpi",std::move(mpi));
589}
590inline void set_random_states(GenEvent* e, std::vector<long int>& a) {
591 e->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(a));
592}
593template <class T> void set_signal_process_vertex(GenEvent* e, T& v) {
594 if (!v || !e) return;
595/* AV: HepMC2 adds the vertex to event */
596 e->add_vertex(v);
597 v->add_attribute("signal_process_vertex",std::make_shared<HepMC3::IntAttribute>(1));
598}
599inline ConstGenVertexPtr signal_process_vertex(const GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
600inline GenVertexPtr signal_process_vertex(GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
601inline bool valid_beam_particles(const GenEvent* e) {
602 if (!e) return false;
603 size_t nBeams = 0;
604 for (const auto& p : e->beams()) { if (p->status() == 4) ++nBeams; }
605 if (nBeams != 2) return false;
606 return true;
607}
608
609template <class T> bool suggest_barcode(T& p, int i) {
610 if (!p->parent_event()) return false;
611 auto barcodes = p->parent_event()->template attribute<GenEventBarcodes> (s_barcodePluralStr);
612 if (!barcodes) {
613 barcodes = std::make_shared<GenEventBarcodes>();
614 p->parent_event()->add_attribute(s_barcodePluralStr, barcodes);
615 }
616 barcodes->remove(p);
617 bool ret = p->add_attribute(s_barcodeStr,std::make_shared<HepMC3::IntAttribute>(i));
618 if (ret) barcodes->add(p);
619 return ret;
620}
621
622}
623
624#else
625
626#include "HepMC/GenEvent.h"
627#include "HepMC/GenVertex.h"
628#include "AtlasHepMC/GenVertex.h"
629#include "AtlasHepMC/Barcode.h"
630#include <memory>
631namespace HepMC {
632inline bool set_ll_event_number(HepMC::GenEvent* e, long long int num){
633 if (num > std::numeric_limits<int>::max()) return false;
634 e->set_event_number((int)num);
635 return true;
636}
637inline long long int get_ll_event_number(const HepMC::GenEvent* e){
638 return e->event_number();
639}
640inline GenEvent::particle_iterator begin(HepMC::GenEvent& e) { return e.particles_begin(); }
641inline GenEvent::particle_iterator end(HepMC::GenEvent& e) { return e.particles_end(); }
642inline GenEvent::particle_const_iterator begin(const HepMC::GenEvent& e) { return e.particles_begin(); }
643inline GenEvent::particle_const_iterator end(const HepMC::GenEvent& e) { return e.particles_end(); }
644inline GenEvent* newGenEvent(const int a, const int b ) { return new GenEvent(a,b); }
645inline GenVertex* signal_process_vertex(const GenEvent* e) { return e->signal_process_vertex(); }
646inline void fillBarcodesAttribute(GenEvent* ) { }
647inline GenVertex* barcode_to_vertex(const GenEvent* e, int id ) {return e->barcode_to_vertex(id);}
648inline GenParticle* barcode_to_particle(const GenEvent* e, int id ) {return e->barcode_to_particle(id);}
649inline int mpi(const GenEvent& e) {
650 return e.mpi();
651}
652inline int mpi(const GenEvent* e) {
653 return e->mpi();
654}
655inline int signal_process_id(const GenEvent& e) {
656 return e.signal_process_id();
657}
658inline int signal_process_id(const GenEvent* e) {
659 return e->signal_process_id();
660}
661inline void set_signal_process_id(GenEvent* e, const int i) {
662 e->set_signal_process_id(i);
663}
664inline void set_mpi(GenEvent* e, const int i) {
665 e->set_mpi(i);
666}
667template <class T> void set_random_states(GenEvent* e, std::vector<T> a) {
668 e->set_random_states(a);
669}
670template <class T> void set_signal_process_vertex(GenEvent* e, T v) {
671 e->set_signal_process_vertex(v);
672}
673inline GenEvent* copyemptyGenEvent(const GenEvent* inEvt) {
674 HepMC::GenEvent* outEvt = new HepMC::GenEvent( inEvt->signal_process_id(), inEvt->event_number() );
675 outEvt->set_mpi ( inEvt->mpi() );
676 outEvt->set_event_scale ( inEvt->event_scale() );
677 outEvt->set_alphaQCD ( inEvt->alphaQCD() );
678 outEvt->set_alphaQED ( inEvt->alphaQED() );
679 outEvt->weights() = inEvt->weights();
680 outEvt->set_random_states( inEvt->random_states() );
681 if ( nullptr != inEvt->heavy_ion() ) {
682 outEvt->set_heavy_ion ( *inEvt->heavy_ion() );
683 }
684 if ( nullptr != inEvt->pdf_info() ) {
685 outEvt->set_pdf_info ( *inEvt->pdf_info() );
686 }
687 return outEvt;
688}
689
690template <class T> bool suggest_barcode(T& p, int i) {return p.suggest_barcode(i);}
691template <class T> bool suggest_barcode(T* p, int i) {return p->suggest_barcode(i);}
692//Smart pointers should not be used with HepMC2. But it happens.
693template <> inline bool suggest_barcode<std::unique_ptr<HepMC::GenParticle> >(std::unique_ptr<HepMC::GenParticle>& p, int i) {return p->suggest_barcode(i);}
694
695namespace Print {
696inline void line(std::ostream& os,const GenEvent& e) {e.print(os);}
697inline void line(std::ostream& os,const GenEvent* e) {e->print(os);}
698inline void content(std::ostream& os,const GenEvent& e) {e.print(os);}
699inline void content(std::ostream& os,const GenEvent* e) {e->print(os);}
700}
701inline bool valid_beam_particles(const GenEvent* e) {return e->valid_beam_particles();}
702}
704#endif
705#endif
static std::string to_string(const std::vector< T > &v)
static Double_t a
@ GenParticle
bool add(const std::string &hname, TKey *tobj)
Definition fastadd.cxx:55
int ev
Definition globals.cxx:25
mapped_type at(key_type key) const
Look up an element in the map.
void line(std::ostream &os, const GenEvent &e)
Definition GenEvent.h:696
void content(std::ostream &os, const GenEvent &e)
Definition GenEvent.h:698
int signal_process_id(const GenEvent &e)
Definition GenEvent.h:655
void set_mpi(GenEvent *e, const int i)
Definition GenEvent.h:664
bool set_ll_event_number(HepMC::GenEvent *e, long long int num)
Definition GenEvent.h:632
void set_signal_process_vertex(GenEvent *e, T v)
Definition GenEvent.h:670
GenParticle * barcode_to_particle(const GenEvent *e, int id)
Definition GenEvent.h:648
int barcode(const T *p)
Definition Barcode.h:16
GenEvent::particle_iterator begin(HepMC::GenEvent &e)
Definition GenEvent.h:640
GenEvent::particle_iterator end(HepMC::GenEvent &e)
Definition GenEvent.h:641
void set_random_states(GenEvent *e, std::vector< T > a)
Definition GenEvent.h:667
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
Definition GenEvent.h:647
long long int get_ll_event_number(const HepMC::GenEvent *e)
Definition GenEvent.h:637
HepMC::GenVertex * GenVertexPtr
Definition GenVertex.h:59
bool suggest_barcode(T &p, int i)
Definition GenEvent.h:690
void set_signal_process_id(GenEvent *e, const int i)
Definition GenEvent.h:661
GenEvent * copyemptyGenEvent(const GenEvent *inEvt)
Definition GenEvent.h:673
void fillBarcodesAttribute(GenEvent *)
Definition GenEvent.h:646
GenParticle * GenParticlePtr
Definition GenParticle.h:37
const GenParticle * ConstGenParticlePtr
Definition GenParticle.h:38
GenVertex * signal_process_vertex(const GenEvent *e)
Definition GenEvent.h:645
int mpi(const GenEvent &e)
Definition GenEvent.h:649
bool valid_beam_particles(const GenEvent *e)
Definition GenEvent.h:701
const HepMC::GenVertex * ConstGenVertexPtr
Definition GenVertex.h:60
GenEvent * newGenEvent(const int a, const int b)
Definition GenEvent.h:644
l
Printing final latex table to .tex output file.
virtual bool resize(size_t sz) override
Change the size of all aux data vectors.
void * ptr(T *p)
Definition SGImplSvc.cxx:74
barcodes(beg, end, sz)
Definition Dumpers.py:2831
TFile * file
int run(int argc, char *argv[])