ATLAS Offline Software
Loading...
Searching...
No Matches
GenEvent.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 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 <unordered_map>
19
20namespace HepMC3 {
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(); }
25}
26
27namespace HepMC {
28using Print=HepMC3::Print;
29using GenHeavyIon=HepMC3::GenHeavyIon;
30using GenEvent=HepMC3::GenEvent;
31
32class ShortEventAttribute : public HepMC3::Attribute {
33public:
34 ShortEventAttribute():HepMC3::Attribute(){}
35 ShortEventAttribute(const HepMC3::GenEvent* e):HepMC3::Attribute(){ from_event(e); }
36
37 bool from_event(const HepMC3::GenEvent* e){
38 NUP=e->particles().size();
39 resize();
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;
49
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;
66 int l = 0,h = 0;
67 if (pv) for (auto p: pv->particles_in()) { l= std::min(l,p->id()); h=std::max(h,p->id());}
68 MOTHUP[i].first = h;
69 MOTHUP[i].second = l;
70 }
71 return true;
72 }
73
74 bool from_string(const std::string &att) override {
75 std::istringstream iss(att);
76 iss >> NUP;
77 iss >> IDPRUP;
78 iss >> XWGTUP;
79 iss >> SCALUP;
80 iss >> AQEDUP;
81 iss >> AQCDUP;
82 //assume input is a trusted source
83 //coverity[tainted_data]
84 resize();
85 for ( int i = 0; i < NUP; ++i ){
86 iss >> IDUP[i];
87 iss >> ISTUP[i];
88 iss >> MOTHUP[i].first;
89 iss >> MOTHUP[i].second;
90 iss >> ICOLUP[i].first;
91 iss >> ICOLUP[i].second;
92 iss >> PUP[i][0];
93 iss >> PUP[i][1];
94 iss >> PUP[i][2];
95 iss >> PUP[i][3];
96 iss >> PUP[i][4];
97 iss >> VTIMUP[i];
98 iss >> SPINUP[i];
99 }
100 set_is_parsed(true);
101 return true;
102 }
103
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;
126 fl+=file.str();
127 return true;
128 }
129
130 void resize() {
131 IDUP.resize(NUP);
132 ISTUP.resize(NUP);
133 MOTHUP.resize(NUP);
134 ICOLUP.resize(NUP);
135 PUP.resize(NUP, std::vector<double>(5));
136 VTIMUP.resize(NUP);
137 SPINUP.resize(NUP);
138 }
139
140 int NUP=0;
141 int IDPRUP=0;
142 double XWGTUP=0;
143 double SCALUP=0;
144 double AQEDUP=0;
145 double AQCDUP=0;
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{};
153};
154
155
156class GenEventBarcodes : public HepMC3::Attribute
157{
158public:
159 virtual bool from_string(const std::string & /*att*/) override { return false; }
160 virtual bool to_string(std::string &att) const override
161 {
162 att = "GenEventBarcodes";
163 return true;
164 }
165 ConstGenVertexPtr barcode_to_vertex (int id) const {
166 auto it = m_vertexBC.find (id);
167 if (it != m_vertexBC.end()) return it->second;
168 return nullptr;
169 }
171 auto it = m_vertexBC.find (id);
172 if (it != m_vertexBC.end()) return it->second;
173 return nullptr;
174 }
176 auto it = m_particleBC.find (id);
177 if (it != m_particleBC.end()) return it->second;
178 return nullptr;
179 }
181 auto it = m_particleBC.find (id);
182 if (it != m_particleBC.end()) return it->second;
183 return nullptr;
184 }
185
186 void add (GenVertexPtr p) {
187 if (!p) return;
188 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
189 if (barcode) {
190 m_vertexBC[barcode->value()] = std::move(p);
191 }
192 }
193
194 void remove (GenVertexPtr p) {
195 if (!p) return;
196 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
197 if (barcode) {
198 auto it = m_vertexBC.find (barcode->value());
199 if (it != m_vertexBC.end()) {
200 m_vertexBC.erase (it);
201 }
202 }
203 }
204
205 void add (GenParticlePtr p) {
206 if (!p) return;
207 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
208 if (barcode) {
209 m_particleBC[barcode->value()] = std::move(p);
210 }
211 }
212
213 void remove (GenParticlePtr p) {
214 if (!p) return;
215 auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
216 if (barcode) {
217 auto it = m_particleBC.find (barcode->value());
218 if (it != m_particleBC.end()) {
219 m_particleBC.erase (it);
220 }
221 }
222 }
223
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)});
228 return ret;
229 }
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)});
234 return ret;
235 }
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});
240 return ret;
241 }
242
243
244 void fillAttribute(GenEvent* e) {
245 const auto eventAttributes = e->attributes(); // this makes a copy
246 const auto barcodeAttributeIt = eventAttributes.find("barcode");
247 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
248
249 const auto &particles = e->particles();
250 for (size_t i = 1; i <= particles.size(); i++) {
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();
255 }
256 else {
257 m_particleBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->particle();
258 }
259 } else {
260 m_particleBC[i] = particles[i-1];
261 }
262 }
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();
269 }
270 else {
271 m_vertexBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->vertex();
272 }
273 } else {
274 m_vertexBC[-i] = vertices[i-1];
275 }
276 }
277 set_is_parsed(true);
278 }
279
280private:
281 std::unordered_map<int, GenVertexPtr> m_vertexBC;
282 std::unordered_map<int, GenParticlePtr> m_particleBC;
283};
284
290class ShadowParticle : public HepMC3::Attribute {
291public:
292
294 ShadowParticle() {}
295
297 ShadowParticle(ConstGenParticlePtr p)
298 : Attribute(), m_shadow(p) {}
299
301 virtual bool from_string(const std::string &) override { return false; }
302
304 virtual bool to_string(std::string &att) const override
305 {
306 att = "ShadowParticle";
307 return true;
308 }
310 ConstGenParticlePtr value() const {
311 return m_shadow;
312 }
313
314private:
315
316 ConstGenParticlePtr m_shadow;
317};
318
319inline bool set_ll_event_number(HepMC3::GenEvent* e, long long int num){
320 e->add_attribute("long_long_event_number", std::make_shared<HepMC3::LongLongAttribute>(num));
321 return true;
322}
323inline long long int get_ll_event_number(const HepMC3::GenEvent* e){
324 auto at = e->attribute<HepMC3::LongLongAttribute>("long_long_event_number");
325 return at?at->value():e->event_number();
326}
327
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);
334 return ret;
335}
336
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(); }
341
342inline GenEvent* newGenEvent(const int signal_process_id, const int event_number ) { // TODO Update event_number to long long int?
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);
348 return e;
349}
350
351inline GenEvent* copyemptyGenEvent(const GenEvent* inEvt) {
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>());
372 return e;
373}
374
375inline void fillBarcodesAttribute(GenEvent* e) {
376 auto barcodes = e->attribute<GenEventBarcodes> ("barcodes");
377 if (!barcodes) {
378 barcodes = std::make_shared<GenEventBarcodes>();
379 e->add_attribute("barcodes", barcodes);
380 }
381 // force re-parsing as calling barcodes->is_parsed() returns true here
382 barcodes->fillAttribute(e);
383}
384
385inline ConstGenVertexPtr barcode_to_vertex(const GenEvent* e, int id ) {
386 // Prefer to use optimized GenEvent barcodes attribute
387 const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
388 if (barcodes) {
389 ConstGenVertexPtr ptr = barcodes->barcode_to_vertex (id);
390 if (ptr) return ptr;
391 }
392 // Fallback to unoptimized GenVertex barcode attribute
393 const auto eventAttributes = e->attributes(); // this makes a copy
394 const auto barcodeAttributeIt = eventAttributes.find("barcode");
395 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
396
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();
406 }
407 }
408 else {
409 if (id == std::atoi(ptr->unparsed_string().c_str())) {
410 return ptr->vertex();
411 }
412 }
413 }
414 }
415 }
416 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
417 if (-id > 0 && -id <= static_cast<int>(vertices.size())) {
418 if (!vertices[-id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
419 return vertices[-id-1];
420 }
421 }
422 return HepMC3::ConstGenVertexPtr();
423}
424
425inline ConstGenParticlePtr barcode_to_particle(const GenEvent* e, int id ) {
426 // Prefer to use optimized GenEvent barcodes attribute
427 const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
428 if (barcodes) {
429 ConstGenParticlePtr ptr = barcodes->barcode_to_particle (id);
430 if (ptr) return ptr;
431 }
432 // Fallback to unoptimized GenParticle barcode attribute
433 const auto eventAttributes = e->attributes(); // this makes a copy
434 const auto barcodeAttributeIt = eventAttributes.find("barcode");
435 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
436
437 const auto &particles = e->particles();
438 if (hasBarcodeAttribute) {
439 for (size_t i = 1; i <= particles.size(); i++) {
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();
446 }
447 }
448 else {
449 if (id == std::atoi(ptr->unparsed_string().c_str())) {
450 return ptr->particle();
451 }
452 }
453 }
454 }
455 }
456 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
457 if (id > 0 && id <= static_cast<int>(particles.size())) {
458 if (!particles[id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
459 return particles[id-1];
460 }
461 }
462 return HepMC3::ConstGenParticlePtr();
463}
464
465inline GenVertexPtr barcode_to_vertex(GenEvent* e, int id ) {
466 // Prefer to use optimized GenEvent barcodes attribute
467 const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
468 if (barcodes) {
469 GenVertexPtr ptr = barcodes->barcode_to_vertex (id);
470 if (ptr) return ptr;
471 }
472 // Fallback to unoptimized GenVertex barcode attribute
473 const auto eventAttributes = e->attributes(); // this makes a copy
474 const auto barcodeAttributeIt = eventAttributes.find("barcode");
475 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
476
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();
486 }
487 }
488 else {
489 if (id == std::atoi(ptr->unparsed_string().c_str())) {
490 return ptr->vertex();
491 }
492 }
493 }
494 }
495 }
496 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
497 if (-id > 0 && -id <= static_cast<int>(vertices.size())) {
498 if (!vertices[-id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
499 return vertices[-id-1];
500 }
501 }
502 return HepMC3::GenVertexPtr();
503}
504
505inline GenParticlePtr barcode_to_particle(GenEvent* e, int id ) {
506 // Prefer to use optimized GenEvent barcodes attribute
507 const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
508 if (barcodes) {
509 GenParticlePtr ptr = barcodes->barcode_to_particle (id);
510 if (ptr) return ptr;
511 }
512 // Fallback to unoptimized GenParticle barcode attribute
513 const auto eventAttributes = e->attributes(); // this makes a copy
514 const auto barcodeAttributeIt = eventAttributes.find("barcode");
515 const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
516
517 const auto &particles = e->particles();
518 if (hasBarcodeAttribute) {
519 for (size_t i = 1; i <= particles.size(); i++) {
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();
526 }
527 }
528 else {
529 if (id == std::atoi(ptr->unparsed_string().c_str())) {
530 return ptr->particle();
531 }
532 }
533 }
534 }
535 }
536 // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
537 if (id > 0 && id <= static_cast<int>(particles.size())) {
538 if (!particles[id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
539 return particles[id-1];
540 }
541 }
542 return HepMC3::GenParticlePtr();
543}
544
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;
548}
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;
552}
553
554inline int signal_process_id(const GenEvent& evt) {
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;
557}
558inline int signal_process_id(const GenEvent* evt) {
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;
561}
562inline void set_signal_process_id(GenEvent* e, const int i=0) {
563 std::shared_ptr<HepMC3::IntAttribute> signal_process_id = std::make_shared<HepMC3::IntAttribute>(i);
564 e->add_attribute("signal_process_id",std::move(signal_process_id));
565}
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));
569}
570inline void set_random_states(GenEvent* e, std::vector<long int>& a) {
571 e->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(a));
572}
573template <class T> void set_signal_process_vertex(GenEvent* e, T& v) {
574 if (!v || !e) return;
575/* AV: HepMC2 adds the vertex to event */
576 e->add_vertex(v);
577 v->add_attribute("signal_process_vertex",std::make_shared<HepMC3::IntAttribute>(1));
578}
579inline ConstGenVertexPtr signal_process_vertex(const GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
580inline GenVertexPtr signal_process_vertex(GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
581inline bool valid_beam_particles(const GenEvent* e) {
582 if (!e) return false;
583 size_t nBeams = 0;
584 for (const auto& p : e->beams()) { if (p->status() == 4) ++nBeams; }
585 if (nBeams != 2) return false;
586 return true;
587}
588
589template <class T> bool suggest_barcode(T& p, int i) {
590 if (!p->parent_event()) return false;
591 auto barcodes = p->parent_event()->template attribute<GenEventBarcodes> ("barcodes");
592 if (!barcodes) {
593 barcodes = std::make_shared<GenEventBarcodes>();
594 p->parent_event()->add_attribute("barcodes", barcodes);
595 }
596 barcodes->remove(p);
597 bool ret = p->add_attribute("barcode",std::make_shared<HepMC3::IntAttribute>(i));
598 if (ret) barcodes->add(p);
599 return ret;
600}
601
602}
603
604#else
605
606#include "HepMC/GenEvent.h"
607#include "HepMC/GenVertex.h"
608#include "AtlasHepMC/GenVertex.h"
609#include "AtlasHepMC/Barcode.h"
610#include <memory>
611namespace HepMC {
612inline bool set_ll_event_number(HepMC::GenEvent* e, long long int num){
613 if (num > std::numeric_limits<int>::max()) return false;
614 e->set_event_number((int)num);
615 return true;
616}
617inline long long int get_ll_event_number(const HepMC::GenEvent* e){
618 return e->event_number();
619}
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); }
625inline GenVertex* signal_process_vertex(const GenEvent* e) { return e->signal_process_vertex(); }
626inline void fillBarcodesAttribute(GenEvent* ) { }
627inline GenVertex* barcode_to_vertex(const GenEvent* e, int id ) {return e->barcode_to_vertex(id);}
628inline GenParticle* barcode_to_particle(const GenEvent* e, int id ) {return e->barcode_to_particle(id);}
629inline int mpi(const GenEvent& e) {
630 return e.mpi();
631}
632inline int mpi(const GenEvent* e) {
633 return e->mpi();
634}
635inline int signal_process_id(const GenEvent& e) {
636 return e.signal_process_id();
637}
638inline int signal_process_id(const GenEvent* e) {
639 return e->signal_process_id();
640}
641inline void set_signal_process_id(GenEvent* e, const int i) {
642 e->set_signal_process_id(i);
643}
644inline void set_mpi(GenEvent* e, const int i) {
645 e->set_mpi(i);
646}
647template <class T> void set_random_states(GenEvent* e, std::vector<T> a) {
648 e->set_random_states(a);
649}
650template <class T> void set_signal_process_vertex(GenEvent* e, T v) {
651 e->set_signal_process_vertex(v);
652}
653inline GenEvent* copyemptyGenEvent(const GenEvent* inEvt) {
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() );
663 }
664 if ( nullptr != inEvt->pdf_info() ) {
665 outEvt->set_pdf_info ( *inEvt->pdf_info() );
666 }
667 return outEvt;
668}
669
670template <class T> bool suggest_barcode(T& p, int i) {return p.suggest_barcode(i);}
671template <class T> bool suggest_barcode(T* p, int i) {return p->suggest_barcode(i);}
672//Smart pointers should not be used with HepMC2. But it happens.
673template <> inline bool suggest_barcode<std::unique_ptr<HepMC::GenParticle> >(std::unique_ptr<HepMC::GenParticle>& p, int i) {return p->suggest_barcode(i);}
674
675namespace Print {
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);}
680}
681inline bool valid_beam_particles(const GenEvent* e) {return e->valid_beam_particles();}
682}
684#endif
685#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
void line(std::ostream &os, const GenEvent &e)
Definition GenEvent.h:676
void content(std::ostream &os, const GenEvent &e)
Definition GenEvent.h:678
int signal_process_id(const GenEvent &e)
Definition GenEvent.h:635
void set_mpi(GenEvent *e, const int i)
Definition GenEvent.h:644
bool set_ll_event_number(HepMC::GenEvent *e, long long int num)
Definition GenEvent.h:612
void set_signal_process_vertex(GenEvent *e, T v)
Definition GenEvent.h:650
GenParticle * barcode_to_particle(const GenEvent *e, int id)
Definition GenEvent.h:628
int barcode(const T *p)
Definition Barcode.h:16
GenEvent::particle_iterator begin(HepMC::GenEvent &e)
Definition GenEvent.h:620
GenEvent::particle_iterator end(HepMC::GenEvent &e)
Definition GenEvent.h:621
void set_random_states(GenEvent *e, std::vector< T > a)
Definition GenEvent.h:647
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
Definition GenEvent.h:627
long long int get_ll_event_number(const HepMC::GenEvent *e)
Definition GenEvent.h:617
HepMC::GenVertex * GenVertexPtr
Definition GenVertex.h:59
bool suggest_barcode(T &p, int i)
Definition GenEvent.h:670
void set_signal_process_id(GenEvent *e, const int i)
Definition GenEvent.h:641
GenEvent * copyemptyGenEvent(const GenEvent *inEvt)
Definition GenEvent.h:653
void fillBarcodesAttribute(GenEvent *)
Definition GenEvent.h:626
GenParticle * GenParticlePtr
Definition GenParticle.h:37
const GenParticle * ConstGenParticlePtr
Definition GenParticle.h:38
GenVertex * signal_process_vertex(const GenEvent *e)
Definition GenEvent.h:625
int mpi(const GenEvent &e)
Definition GenEvent.h:629
bool valid_beam_particles(const GenEvent *e)
Definition GenEvent.h:681
const HepMC::GenVertex * ConstGenVertexPtr
Definition GenVertex.h:60
GenEvent * newGenEvent(const int a, const int b)
Definition GenEvent.h:624
l
Printing final latex table to .tex output file.
void * ptr(T *p)
Definition SGImplSvc.cxx:74
barcodes(beg, end, sz)
Definition Dumpers.py:2831
TFile * file