ATLAS Offline Software
GenEvent.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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 #undef private
10 #undef protected
11 #include "HepMC3/GenEvent.h"
12 #include "HepMC3/GenHeavyIon.h"
13 #include "HepMC3/GenPdfInfo.h"
14 #include "HepMC3/PrintStreams.h"
15 #include "AtlasHepMC/Barcode.h"
16 #include "AtlasHepMC/GenVertex.h"
17 #include "AtlasHepMC/GenParticle.h"
19 
20 #include <unordered_map>
21 
22 namespace HepMC3 {
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(); }
27 }
28 
29 namespace HepMC {
30 using Print=HepMC3::Print;
31 using GenHeavyIon=HepMC3::GenHeavyIon;
32 using GenEvent=HepMC3::GenEvent;
33 
34 class ShortEventAttribute : public HepMC3::Attribute {
35 public:
36  ShortEventAttribute():HepMC3::Attribute(){}
37  ShortEventAttribute(const HepMC3::GenEvent* e):HepMC3::Attribute(){ from_event(e); }
38 
39  bool from_event(const HepMC3::GenEvent* e){
40  NUP=e->particles().size();
41  resize();
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;
51 
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;
68  int l = 0,h = 0;
69  if (pv) for (auto p: pv->particles_in()) { l= std::min(l,p->id()); h=std::max(h,p->id());}
70  MOTHUP[i].first = h;
71  MOTHUP[i].second = l;
72  }
73  return true;
74  }
75 
76  bool from_string(const std::string &att) override {
77  std::istringstream iss(att);
78  iss >> NUP;
79  iss >> IDPRUP;
80  iss >> XWGTUP;
81  iss >> SCALUP;
82  iss >> AQEDUP;
83  iss >> AQCDUP;
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 
156 class GenEventBarcodes : public HepMC3::Attribute
157 {
158 public:
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()] = 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()] = 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 
280 private:
281  std::unordered_map<int, GenVertexPtr> m_vertexBC;
282  std::unordered_map<int, GenParticlePtr> m_particleBC;
283 };
284 
290 class ShadowParticle : public HepMC3::Attribute {
291 public:
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 
314 private:
315 
316  ConstGenParticlePtr m_shadow;
317 };
318 
319 inline 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 }
323 inline 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 
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();
333  for (const auto& name: names) ret[name] = run->weight_index(name);
334  return ret;
335 }
336 
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(); }
341 
342 inline 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 
351 inline 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 
375 inline 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 
385 inline 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 
425 inline 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  }
463 }
464 
465 inline 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 
505 inline 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 
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;
548 }
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;
552 }
553 
554 inline 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 }
558 inline 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 }
562 inline 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 }
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));
569 }
570 inline void set_random_states(GenEvent* e, std::vector<long int>& a) {
571  e->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(a));
572 }
573 template <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 }
579 inline ConstGenVertexPtr signal_process_vertex(const GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
580 inline GenVertexPtr signal_process_vertex(GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
581 inline 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 
589 template <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 (barcodes && 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>
611 namespace HepMC {
612 inline 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 }
617 inline long long int get_ll_event_number(const HepMC::GenEvent* e){
618  return e->event_number();
619 }
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(); }
624 inline GenEvent* newGenEvent(const int a, const int b ) { return new GenEvent(a,b); }
625 inline GenVertex* signal_process_vertex(const GenEvent* e) { return e->signal_process_vertex(); }
626 inline void fillBarcodesAttribute(GenEvent* ) { }
627 inline GenVertex* barcode_to_vertex(const GenEvent* e, int id ) {return e->barcode_to_vertex(id);}
628 inline GenParticle* barcode_to_particle(const GenEvent* e, int id ) {return e->barcode_to_particle(id);}
629 inline int mpi(const GenEvent& e) {
630  return e.mpi();
631 }
632 inline int mpi(const GenEvent* e) {
633  return e->mpi();
634 }
635 inline int signal_process_id(const GenEvent& e) {
636  return e.signal_process_id();
637 }
638 inline int signal_process_id(const GenEvent* e) {
639  return e->signal_process_id();
640 }
641 inline void set_signal_process_id(GenEvent* e, const int i) {
642  e->set_signal_process_id(i);
643 }
644 inline void set_mpi(GenEvent* e, const int i) {
645  e->set_mpi(i);
646 }
647 template <class T> void set_random_states(GenEvent* e, std::vector<T> a) {
648  e->set_random_states(a);
649 }
650 template <class T> void set_signal_process_vertex(GenEvent* e, T v) {
651  e->set_signal_process_vertex(v);
652 }
653 inline 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 
670 template <class T> bool suggest_barcode(T& p, int i) {return p.suggest_barcode(i);}
671 template <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.
673 template <> inline bool suggest_barcode<std::unique_ptr<HepMC::GenParticle> >(std::unique_ptr<HepMC::GenParticle>& p, int i) {return p->suggest_barcode(i);}
674 
675 namespace Print {
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);}
678 inline void content(std::ostream& os,const GenEvent& e) {e.print(os);}
679 inline void content(std::ostream& os,const GenEvent* e) {e->print(os);}
680 }
681 inline bool valid_beam_particles(const GenEvent* e) {return e->valid_beam_particles();}
682 }
683 #include "AtlasHepMC/SimpleVector.h"
684 #endif
685 #endif
HepMC::GenVertexPtr
HepMC::GenVertex * GenVertexPtr
Definition: GenVertex.h:59
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
HepMC::suggest_barcode
bool suggest_barcode(T &p, int i)
Definition: GenEvent.h:670
HepMC::set_ll_event_number
bool set_ll_event_number(HepMC::GenEvent *e, long long int num)
Definition: GenEvent.h:612
GenEvent.h
HepMC::Print::content
void content(std::ostream &os, const GenEvent &e)
Definition: GenEvent.h:678
Barcode.h
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:635
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
HepMC::get_ll_event_number
long long int get_ll_event_number(const HepMC::GenEvent *e)
Definition: GenEvent.h:617
GenVertex.h
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
skel.it
it
Definition: skel.GENtoEVGEN.py:396
HepMC::newGenEvent
GenEvent * newGenEvent(const int a, const int b)
Definition: GenEvent.h:624
athena.value
value
Definition: athena.py:124
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:158
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
HepMC::Print::line
void line(std::ostream &os, const GenEvent &e)
Definition: GenEvent.h:676
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:626
GenParticle.h
HepMC::end
GenEvent::particle_iterator end(HepMC::GenEvent &e)
Definition: GenEvent.h:621
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
HepMC::begin
GenEvent::particle_const_iterator begin(const HepMC::GenEvent &e)
Definition: GenEvent.h:622
HepMC::set_signal_process_vertex
void set_signal_process_vertex(GenEvent *e, T v)
Definition: GenEvent.h:650
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ev
int ev
Definition: globals.cxx:25
SimpleVector.h
HepMC::barcode_to_particle
GenParticle * barcode_to_particle(const GenEvent *e, int id)
Definition: GenEvent.h:628
python.Dumpers.barcodes
def barcodes(beg, end, sz)
Definition: Dumpers.py:2800
lumiFormat.i
int i
Definition: lumiFormat.py:85
extractSporadic.h
list h
Definition: extractSporadic.py:97
python.subdetectors.mmg.names
names
Definition: mmg.py:8
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
file
TFile * file
Definition: tile_monitor.h:29
add
bool add(const std::string &hname, TKey *tobj)
Definition: fastadd.cxx:55
run
Definition: run.py:1
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
trigbs_pickEvents.num
num
Definition: trigbs_pickEvents.py:76
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
python.PyAthena.v
v
Definition: PyAthena.py:154
a
TList * a
Definition: liststreamerinfos.cxx:10
h
HepMC
Definition: Barcode.h:14
HepMC::valid_beam_particles
bool valid_beam_particles(const GenEvent *e)
Definition: GenEvent.h:681
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
python.changerun.pv
pv
Definition: changerun.py:81
dqm_persistency::Print
void Print(const PParameter *param, TDirectory *topdir, Option_t *opt="")
Definition: dqm_persistency_impl.cxx:161
HepMC::barcode_to_vertex
GenVertex * barcode_to_vertex(const GenEvent *e, int id)
Definition: GenEvent.h:627
HepMC::begin
GenEvent::particle_iterator begin(HepMC::GenEvent &e)
Definition: GenEvent.h:620
HepMC::mpi
int mpi(const GenEvent &e)
Definition: GenEvent.h:629
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
HepMC::ConstGenVertexPtr
const HepMC::GenVertex * ConstGenVertexPtr
Definition: GenVertex.h:60
HepMC::copyemptyGenEvent
GenEvent * copyemptyGenEvent(const GenEvent *inEvt)
Definition: GenEvent.h:653
HepMC::end
GenEvent::particle_const_iterator end(const HepMC::GenEvent &e)
Definition: GenEvent.h:623
HepMC::set_signal_process_id
void set_signal_process_id(GenEvent *e, const int i)
Definition: GenEvent.h:641
HepMC::set_mpi
void set_mpi(GenEvent *e, const int i)
Definition: GenEvent.h:644
GenParticle
@ GenParticle
Definition: TruthClasses.h:30
HepMC::set_random_states
void set_random_states(GenEvent *e, std::vector< T > a)
Definition: GenEvent.h:647
HepMC::signal_process_vertex
GenVertex * signal_process_vertex(const GenEvent *e)
Definition: GenEvent.h:625