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  //coverity [tainted_data]
85  resize();
86  for ( int i = 0; i < NUP; ++i ){
87  iss >> IDUP[i];
88  iss >> ISTUP[i];
89  iss >> MOTHUP[i].first;
90  iss >> MOTHUP[i].second;
91  iss >> ICOLUP[i].first;
92  iss >> ICOLUP[i].second;
93  iss >> PUP[i][0];
94  iss >> PUP[i][1];
95  iss >> PUP[i][2];
96  iss >> PUP[i][3];
97  iss >> PUP[i][4];
98  iss >> VTIMUP[i];
99  iss >> SPINUP[i];
100  }
101  set_is_parsed(true);
102  return true;
103  }
104 
105  bool to_string(std::string &fl) const override {
106  std::ostringstream file;
107  file << " " << std::setw(4) << NUP
108  << " " << std::setw(6) << IDPRUP
109  << " " << std::setw(14) << XWGTUP
110  << " " << std::setw(14) << SCALUP
111  << " " << std::setw(14) << AQEDUP
112  << " " << std::setw(14) << AQCDUP << "\n";
113  for ( int i = 0; i < NUP; ++i )
114  file << " " << std::setw(8) << IDUP[i]
115  << " " << std::setw(2) << ISTUP[i]
116  << " " << std::setw(4) << MOTHUP[i].first
117  << " " << std::setw(4) << MOTHUP[i].second
118  << " " << std::setw(4) << ICOLUP[i].first
119  << " " << std::setw(4) << ICOLUP[i].second
120  << " " << std::setw(14) << PUP[i][0]
121  << " " << std::setw(14) << PUP[i][1]
122  << " " << std::setw(14) << PUP[i][2]
123  << " " << std::setw(14) << PUP[i][3]
124  << " " << std::setw(14) << PUP[i][4]
125  << " " << std::setw(1) << VTIMUP[i]
126  << " " << std::setw(1) << SPINUP[i] << std::endl;
127  fl+=file.str();
128  return true;
129  }
130 
131  void resize() {
132  IDUP.resize(NUP);
133  ISTUP.resize(NUP);
134  MOTHUP.resize(NUP);
135  ICOLUP.resize(NUP);
136  PUP.resize(NUP, std::vector<double>(5));
137  VTIMUP.resize(NUP);
138  SPINUP.resize(NUP);
139  }
140 
141  int NUP=0;
142  int IDPRUP=0;
143  double XWGTUP=0;
144  double SCALUP=0;
145  double AQEDUP=0;
146  double AQCDUP=0;
147  std::vector<long> IDUP{};
148  std::vector<int> ISTUP{};
149  std::vector< std::pair<int,int> > MOTHUP{};
150  std::vector< std::pair<int,int> > ICOLUP{};
151  std::vector< std::vector<double> > PUP{};
152  std::vector<double> VTIMUP{};
153  std::vector<double> SPINUP{};
154 };
155 
156 
157 class GenEventBarcodes : public HepMC3::Attribute
158 {
159 public:
160  virtual bool from_string(const std::string & /*att*/) override { return false; }
161  virtual bool to_string(std::string &att) const override
162  {
163  att = "GenEventBarcodes";
164  return true;
165  }
166  ConstGenVertexPtr barcode_to_vertex (int id) const {
167  auto it = m_vertexBC.find (id);
168  if (it != m_vertexBC.end()) return it->second;
169  return nullptr;
170  }
172  auto it = m_vertexBC.find (id);
173  if (it != m_vertexBC.end()) return it->second;
174  return nullptr;
175  }
177  auto it = m_particleBC.find (id);
178  if (it != m_particleBC.end()) return it->second;
179  return nullptr;
180  }
182  auto it = m_particleBC.find (id);
183  if (it != m_particleBC.end()) return it->second;
184  return nullptr;
185  }
186 
187  void add (GenVertexPtr p) {
188  if (!p) return;
189  auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
190  if (barcode) {
191  m_vertexBC[barcode->value()] = std::move(p);
192  }
193  }
194 
195  void remove (GenVertexPtr p) {
196  if (!p) return;
197  auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
198  if (barcode) {
199  auto it = m_vertexBC.find (barcode->value());
200  if (it != m_vertexBC.end()) {
201  m_vertexBC.erase (it);
202  }
203  }
204  }
205 
206  void add (GenParticlePtr p) {
207  if (!p) return;
208  auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
209  if (barcode) {
210  m_particleBC[barcode->value()] = std::move(p);
211  }
212  }
213 
214  void remove (GenParticlePtr p) {
215  if (!p) return;
216  auto barcode = p->attribute<HepMC3::IntAttribute>("barcode");
217  if (barcode) {
218  auto it = m_particleBC.find (barcode->value());
219  if (it != m_particleBC.end()) {
220  m_particleBC.erase (it);
221  }
222  }
223  }
224 
225  std::map<int, ConstGenVertexPtr> barcode_to_vertex_map() const {
226  std::map<int, ConstGenVertexPtr> ret;
227  for (const auto &bcvertpair: m_vertexBC)
228  ret.insert({bcvertpair.first,std::const_pointer_cast<const HepMC3::GenVertex>(bcvertpair.second)});
229  return ret;
230  }
231  std::map<int, ConstGenParticlePtr> barcode_to_particle_map() const {
232  std::map<int, ConstGenParticlePtr> ret;
233  for (const auto &bcpartpair: m_particleBC)
234  ret.insert({bcpartpair.first,std::const_pointer_cast<const HepMC3::GenParticle>(bcpartpair.second)});
235  return ret;
236  }
237  std::map<int,int> id_to_barcode_map() const {
238  std::map<int, int> ret;
239  for (const auto &bcvertpair: m_vertexBC) ret.insert({bcvertpair.second->id(),bcvertpair.first});
240  for (const auto &bcpartpair: m_particleBC) ret.insert({bcpartpair.second->id(),bcpartpair.first});
241  return ret;
242  }
243 
244 
245  void fillAttribute(GenEvent* e) {
246  const auto eventAttributes = e->attributes(); // this makes a copy
247  const auto barcodeAttributeIt = eventAttributes.find("barcode");
248  const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
249 
250  const auto &particles = e->particles();
251  for (size_t i = 1; i <= particles.size(); i++) {
252  if (hasBarcodeAttribute && barcodeAttributeIt->second.count(i) > 0) {
253  const auto &ptr = barcodeAttributeIt->second.at(i);
254  if (ptr->is_parsed()) {
255  m_particleBC[static_cast<HepMC3::IntAttribute*>(ptr.get())->value()] = ptr->particle();
256  }
257  else {
258  m_particleBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->particle();
259  }
260  } else {
261  m_particleBC[i] = particles[i-1];
262  }
263  }
264  const auto &vertices = e->vertices();
265  for (size_t i = 1; i <= vertices.size(); i++) {
266  if (hasBarcodeAttribute && barcodeAttributeIt->second.count(-i) > 0) {
267  const auto &ptr = barcodeAttributeIt->second.at(-i);
268  if (ptr->is_parsed()) {
269  m_vertexBC[static_cast<HepMC3::IntAttribute*>(ptr.get())->value()] = ptr->vertex();
270  }
271  else {
272  m_vertexBC[std::atoi(ptr->unparsed_string().c_str())] = ptr->vertex();
273  }
274  } else {
275  m_vertexBC[-i] = vertices[i-1];
276  }
277  }
278  set_is_parsed(true);
279  }
280 
281 private:
282  std::unordered_map<int, GenVertexPtr> m_vertexBC;
283  std::unordered_map<int, GenParticlePtr> m_particleBC;
284 };
285 
291 class ShadowParticle : public HepMC3::Attribute {
292 public:
293 
295  ShadowParticle() {}
296 
298  ShadowParticle(ConstGenParticlePtr p)
299  : Attribute(), m_shadow(p) {}
300 
302  virtual bool from_string(const std::string &) override { return false; }
303 
305  virtual bool to_string(std::string &att) const override
306  {
307  att = "ShadowParticle";
308  return true;
309  }
311  ConstGenParticlePtr value() const {
312  return m_shadow;
313  }
314 
315 private:
316 
317  ConstGenParticlePtr m_shadow;
318 };
319 
320 inline bool set_ll_event_number(HepMC3::GenEvent* e, long long int num){
321  e->add_attribute("long_long_event_number", std::make_shared<HepMC3::LongLongAttribute>(num));
322  return true;
323 }
324 inline long long int get_ll_event_number(const HepMC3::GenEvent* e){
325  auto at = e->attribute<HepMC3::LongLongAttribute>("long_long_event_number");
326  return at?at->value():e->event_number();
327 }
328 
329 inline std::map<std::string, std::size_t> weights_map(const HepMC3::GenEvent* e) {
330  std::map<std::string, std::size_t> ret;
331  auto run = e->run_info();
332  if (!run) return ret;
333  std::vector<std::string> names = run->weight_names();
334  for (const auto& name: names) ret[name] = run->weight_index(name);
335  return ret;
336 }
337 
338 inline std::vector<HepMC3::GenParticlePtr>::const_iterator begin(HepMC3::GenEvent& e) { return e.particles().begin(); }
339 inline std::vector<HepMC3::GenParticlePtr>::const_iterator end(HepMC3::GenEvent& e) { return e.particles().end(); }
340 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator begin(const HepMC3::GenEvent& e) { return e.particles().begin(); }
341 inline std::vector<HepMC3::ConstGenParticlePtr>::const_iterator end(const HepMC3::GenEvent& e) { return e.particles().end(); }
342 
343 inline GenEvent* newGenEvent(const int signal_process_id, const int event_number ) { // TODO Update event_number to long long int?
344  GenEvent* e= new GenEvent();
345  std::shared_ptr<HepMC3::IntAttribute> signal_process_id_A = std::make_shared<HepMC3::IntAttribute>(signal_process_id);
346  e->add_attribute("signal_process_id",signal_process_id_A);
347  e->add_attribute("barcodes", std::make_shared<GenEventBarcodes>());
348  e->set_event_number(event_number);
349  return e;
350 }
351 
352 inline GenEvent* copyemptyGenEvent(const GenEvent* inEvt) {
353  GenEvent* e= new GenEvent();
354  e->set_event_number(inEvt->event_number());
355  e->weights()=inEvt->weights();
356  auto a_mpi = inEvt->attribute<HepMC3::IntAttribute>("mpi");
357  if (a_mpi) e->add_attribute("mpi",std::make_shared<HepMC3::IntAttribute>(*a_mpi));
358  auto a_signal_process_id = inEvt->attribute<HepMC3::IntAttribute>("signal_process_id");
359  if (a_signal_process_id) e->add_attribute("signal_process_id",std::make_shared<HepMC3::IntAttribute>(*a_signal_process_id));
360  auto a_event_scale = inEvt->attribute<HepMC3::DoubleAttribute>("event_scale");
361  if (a_event_scale) e->add_attribute("event_scale",std::make_shared<HepMC3::DoubleAttribute>(*a_event_scale));
362  auto a_alphaQCD = inEvt->attribute<HepMC3::DoubleAttribute>("alphaQCD");
363  if (a_alphaQCD) e->add_attribute("alphaQCD",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQCD));
364  auto a_alphaQED = inEvt->attribute<HepMC3::DoubleAttribute>("alphaQED");
365  if (a_alphaQED) e->add_attribute("alphaQED",std::make_shared<HepMC3::DoubleAttribute>(*a_alphaQED));
366  auto a_pi = inEvt->pdf_info();
367  if (a_pi) e->set_pdf_info(std::make_shared<HepMC3::GenPdfInfo>(*a_pi));
368  auto a_hi = inEvt->heavy_ion();
369  if (a_hi) e->set_heavy_ion(std::make_shared<HepMC3::GenHeavyIon>(*a_hi));
370  auto a_random_states = inEvt->attribute<HepMC3::VectorLongIntAttribute>("random_states");
371  if (a_random_states) e->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(*a_random_states));
372  e->add_attribute("barcodes", std::make_shared<GenEventBarcodes>());
373  return e;
374 }
375 
376 inline void fillBarcodesAttribute(GenEvent* e) {
377  auto barcodes = e->attribute<GenEventBarcodes> ("barcodes");
378  if (!barcodes) {
379  barcodes = std::make_shared<GenEventBarcodes>();
380  e->add_attribute("barcodes", barcodes);
381  }
382  // force re-parsing as calling barcodes->is_parsed() returns true here
383  barcodes->fillAttribute(e);
384 }
385 
386 inline ConstGenVertexPtr barcode_to_vertex(const GenEvent* e, int id ) {
387  // Prefer to use optimized GenEvent barcodes attribute
388  const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
389  if (barcodes) {
390  ConstGenVertexPtr ptr = barcodes->barcode_to_vertex (id);
391  if (ptr) return ptr;
392  }
393  // Fallback to unoptimized GenVertex barcode attribute
394  const auto eventAttributes = e->attributes(); // this makes a copy
395  const auto barcodeAttributeIt = eventAttributes.find("barcode");
396  const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
397 
398  const auto &vertices = e->vertices();
399  if (hasBarcodeAttribute) {
400  for (size_t i = 1; i <= vertices.size(); i++) {
401  const auto &ptrIt = barcodeAttributeIt->second.find(-i);
402  if (ptrIt != barcodeAttributeIt->second.end()) {
403  const auto &ptr = ptrIt->second;
404  if (ptr->is_parsed()) {
405  if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
406  return ptr->vertex();
407  }
408  }
409  else {
410  if (id == std::atoi(ptr->unparsed_string().c_str())) {
411  return ptr->vertex();
412  }
413  }
414  }
415  }
416  }
417  // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
418  if (-id > 0 && -id <= static_cast<int>(vertices.size())) {
419  if (!vertices[-id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
420  return vertices[-id-1];
421  }
422  }
423  return HepMC3::ConstGenVertexPtr();
424 }
425 
426 inline ConstGenParticlePtr barcode_to_particle(const GenEvent* e, int id ) {
427  // Prefer to use optimized GenEvent barcodes attribute
428  const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
429  if (barcodes) {
430  ConstGenParticlePtr ptr = barcodes->barcode_to_particle (id);
431  if (ptr) return ptr;
432  }
433  // Fallback to unoptimized GenParticle barcode attribute
434  const auto eventAttributes = e->attributes(); // this makes a copy
435  const auto barcodeAttributeIt = eventAttributes.find("barcode");
436  const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
437 
438  const auto &particles = e->particles();
439  if (hasBarcodeAttribute) {
440  for (size_t i = 1; i <= particles.size(); i++) {
441  const auto &ptrIt = barcodeAttributeIt->second.find(i);
442  if (ptrIt != barcodeAttributeIt->second.end()) {
443  const auto &ptr = ptrIt->second;
444  if (ptr->is_parsed()) {
445  if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
446  return ptr->particle();
447  }
448  }
449  else {
450  if (id == std::atoi(ptr->unparsed_string().c_str())) {
451  return ptr->particle();
452  }
453  }
454  }
455  }
456  }
457  // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
458  if (id > 0 && id <= static_cast<int>(particles.size())) {
459  if (!particles[id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
460  return particles[id-1];
461  }
462  }
464 }
465 
466 inline GenVertexPtr barcode_to_vertex(GenEvent* e, int id ) {
467  // Prefer to use optimized GenEvent barcodes attribute
468  const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
469  if (barcodes) {
470  GenVertexPtr ptr = barcodes->barcode_to_vertex (id);
471  if (ptr) return ptr;
472  }
473  // Fallback to unoptimized GenVertex barcode attribute
474  const auto eventAttributes = e->attributes(); // this makes a copy
475  const auto barcodeAttributeIt = eventAttributes.find("barcode");
476  const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
477 
478  const auto &vertices = e->vertices();
479  if (hasBarcodeAttribute) {
480  for (size_t i = 1; i <= vertices.size(); i++) {
481  const auto &ptrIt = barcodeAttributeIt->second.find(-i);
482  if (ptrIt != barcodeAttributeIt->second.end()) {
483  const auto &ptr = ptrIt->second;
484  if (ptr->is_parsed()) {
485  if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
486  return ptr->vertex();
487  }
488  }
489  else {
490  if (id == std::atoi(ptr->unparsed_string().c_str())) {
491  return ptr->vertex();
492  }
493  }
494  }
495  }
496  }
497  // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
498  if (-id > 0 && -id <= static_cast<int>(vertices.size())) {
499  if (!vertices[-id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
500  return vertices[-id-1];
501  }
502  }
503  return HepMC3::GenVertexPtr();
504 }
505 
506 inline GenParticlePtr barcode_to_particle(GenEvent* e, int id ) {
507  // Prefer to use optimized GenEvent barcodes attribute
508  const auto &barcodes = e->attribute<GenEventBarcodes> ("barcodes");
509  if (barcodes) {
510  GenParticlePtr ptr = barcodes->barcode_to_particle (id);
511  if (ptr) return ptr;
512  }
513  // Fallback to unoptimized GenParticle barcode attribute
514  const auto eventAttributes = e->attributes(); // this makes a copy
515  const auto barcodeAttributeIt = eventAttributes.find("barcode");
516  const bool hasBarcodeAttribute = barcodeAttributeIt != eventAttributes.end();
517 
518  const auto &particles = e->particles();
519  if (hasBarcodeAttribute) {
520  for (size_t i = 1; i <= particles.size(); i++) {
521  const auto &ptrIt = barcodeAttributeIt->second.find(i);
522  if (ptrIt != barcodeAttributeIt->second.end()) {
523  const auto &ptr = ptrIt->second;
524  if (ptr->is_parsed()) {
525  if (id == static_cast<HepMC3::IntAttribute*>(ptr.get())->value()) {
526  return ptr->particle();
527  }
528  }
529  else {
530  if (id == std::atoi(ptr->unparsed_string().c_str())) {
531  return ptr->particle();
532  }
533  }
534  }
535  }
536  }
537  // No barcodes attribute, so assume that we are passing the id member variable instead of a barcode
538  if (id > 0 && id <= static_cast<int>(particles.size())) {
539  if (!particles[id-1]->attribute<HepMC3::IntAttribute>("barcode")) {
540  return particles[id-1];
541  }
542  }
543  return HepMC3::GenParticlePtr();
544 }
545 
546 inline int mpi(const GenEvent& evt) {
547  std::shared_ptr<HepMC3::IntAttribute> A_mpi=evt.attribute<HepMC3::IntAttribute>("mpi");
548  return A_mpi?(A_mpi->value()):0;
549 }
550 inline int mpi(const GenEvent* evt) {
551  std::shared_ptr<HepMC3::IntAttribute> A_mpi=evt->attribute<HepMC3::IntAttribute>("mpi");
552  return A_mpi?(A_mpi->value()):0;
553 }
554 
555 inline int signal_process_id(const GenEvent& evt) {
556  std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=evt.attribute<HepMC3::IntAttribute>("signal_process_id");
557  return A_signal_process_id?(A_signal_process_id->value()):0;
558 }
559 inline int signal_process_id(const GenEvent* evt) {
560  std::shared_ptr<HepMC3::IntAttribute> A_signal_process_id=evt->attribute<HepMC3::IntAttribute>("signal_process_id");
561  return A_signal_process_id?(A_signal_process_id->value()):0;
562 }
563 inline void set_signal_process_id(GenEvent* e, const int i=0) {
564  std::shared_ptr<HepMC3::IntAttribute> signal_process_id = std::make_shared<HepMC3::IntAttribute>(i);
565  e->add_attribute("signal_process_id",std::move(signal_process_id));
566 }
567 inline void set_mpi(GenEvent* e, const int i=0) {
568  std::shared_ptr<HepMC3::IntAttribute> mpi = std::make_shared<HepMC3::IntAttribute>(i);
569  e->add_attribute("mpi",std::move(mpi));
570 }
571 inline void set_random_states(GenEvent* e, std::vector<long int>& a) {
572  e->add_attribute("random_states",std::make_shared<HepMC3::VectorLongIntAttribute>(a));
573 }
574 template <class T> void set_signal_process_vertex(GenEvent* e, T& v) {
575  if (!v || !e) return;
576 /* AV: HepMC2 adds the vertex to event */
577  e->add_vertex(v);
578  v->add_attribute("signal_process_vertex",std::make_shared<HepMC3::IntAttribute>(1));
579 }
580 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; }
581 inline GenVertexPtr signal_process_vertex(GenEvent* e) { for (auto v: e->vertices()) if (v->attribute<HepMC3::IntAttribute>("signal_process_vertex")) return v; return nullptr; }
582 inline bool valid_beam_particles(const GenEvent* e) {
583  if (!e) return false;
584  size_t nBeams = 0;
585  for (const auto& p : e->beams()) { if (p->status() == 4) ++nBeams; }
586  if (nBeams != 2) return false;
587  return true;
588 }
589 
590 template <class T> bool suggest_barcode(T& p, int i) {
591  if (!p->parent_event()) return false;
592  auto barcodes = p->parent_event()->template attribute<GenEventBarcodes> ("barcodes");
593  if (!barcodes) {
594  barcodes = std::make_shared<GenEventBarcodes>();
595  p->parent_event()->add_attribute("barcodes", barcodes);
596  }
597  barcodes->remove(p);
598  bool ret = p->add_attribute("barcode",std::make_shared<HepMC3::IntAttribute>(i));
599  if (barcodes && ret) barcodes->add(p);
600  return ret;
601 }
602 
603 }
604 
605 #else
606 
607 #include "HepMC/GenEvent.h"
608 #include "HepMC/GenVertex.h"
609 #include "AtlasHepMC/GenVertex.h"
610 #include "AtlasHepMC/Barcode.h"
611 #include <memory>
612 namespace HepMC {
613 inline bool set_ll_event_number(HepMC::GenEvent* e, long long int num){
614  if (num > std::numeric_limits<int>::max()) return false;
615  e->set_event_number((int)num);
616  return true;
617 }
618 inline long long int get_ll_event_number(const HepMC::GenEvent* e){
619  return e->event_number();
620 }
621 inline GenEvent::particle_iterator begin(HepMC::GenEvent& e) { return e.particles_begin(); }
622 inline GenEvent::particle_iterator end(HepMC::GenEvent& e) { return e.particles_end(); }
623 inline GenEvent::particle_const_iterator begin(const HepMC::GenEvent& e) { return e.particles_begin(); }
624 inline GenEvent::particle_const_iterator end(const HepMC::GenEvent& e) { return e.particles_end(); }
625 inline GenEvent* newGenEvent(const int a, const int b ) { return new GenEvent(a,b); }
626 inline GenVertex* signal_process_vertex(const GenEvent* e) { return e->signal_process_vertex(); }
627 inline void fillBarcodesAttribute(GenEvent* ) { }
628 inline GenVertex* barcode_to_vertex(const GenEvent* e, int id ) {return e->barcode_to_vertex(id);}
629 inline GenParticle* barcode_to_particle(const GenEvent* e, int id ) {return e->barcode_to_particle(id);}
630 inline int mpi(const GenEvent& e) {
631  return e.mpi();
632 }
633 inline int mpi(const GenEvent* e) {
634  return e->mpi();
635 }
636 inline int signal_process_id(const GenEvent& e) {
637  return e.signal_process_id();
638 }
639 inline int signal_process_id(const GenEvent* e) {
640  return e->signal_process_id();
641 }
642 inline void set_signal_process_id(GenEvent* e, const int i) {
643  e->set_signal_process_id(i);
644 }
645 inline void set_mpi(GenEvent* e, const int i) {
646  e->set_mpi(i);
647 }
648 template <class T> void set_random_states(GenEvent* e, std::vector<T> a) {
649  e->set_random_states(a);
650 }
651 template <class T> void set_signal_process_vertex(GenEvent* e, T v) {
652  e->set_signal_process_vertex(v);
653 }
654 inline GenEvent* copyemptyGenEvent(const GenEvent* inEvt) {
655  HepMC::GenEvent* outEvt = new HepMC::GenEvent( inEvt->signal_process_id(), inEvt->event_number() );
656  outEvt->set_mpi ( inEvt->mpi() );
657  outEvt->set_event_scale ( inEvt->event_scale() );
658  outEvt->set_alphaQCD ( inEvt->alphaQCD() );
659  outEvt->set_alphaQED ( inEvt->alphaQED() );
660  outEvt->weights() = inEvt->weights();
661  outEvt->set_random_states( inEvt->random_states() );
662  if ( nullptr != inEvt->heavy_ion() ) {
663  outEvt->set_heavy_ion ( *inEvt->heavy_ion() );
664  }
665  if ( nullptr != inEvt->pdf_info() ) {
666  outEvt->set_pdf_info ( *inEvt->pdf_info() );
667  }
668  return outEvt;
669 }
670 
671 template <class T> bool suggest_barcode(T& p, int i) {return p.suggest_barcode(i);}
672 template <class T> bool suggest_barcode(T* p, int i) {return p->suggest_barcode(i);}
673 //Smart pointers should not be used with HepMC2. But it happens.
674 template <> inline bool suggest_barcode<std::unique_ptr<HepMC::GenParticle> >(std::unique_ptr<HepMC::GenParticle>& p, int i) {return p->suggest_barcode(i);}
675 
676 namespace Print {
677 inline void line(std::ostream& os,const GenEvent& e) {e.print(os);}
678 inline void line(std::ostream& os,const GenEvent* e) {e->print(os);}
679 inline void content(std::ostream& os,const GenEvent& e) {e.print(os);}
680 inline void content(std::ostream& os,const GenEvent* e) {e->print(os);}
681 }
682 inline bool valid_beam_particles(const GenEvent* e) {return e->valid_beam_particles();}
683 }
684 #include "AtlasHepMC/SimpleVector.h"
685 #endif
686 #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:671
HepMC::set_ll_event_number
bool set_ll_event_number(HepMC::GenEvent *e, long long int num)
Definition: GenEvent.h:613
GenEvent.h
HepMC::Print::content
void content(std::ostream &os, const GenEvent &e)
Definition: GenEvent.h:679
Barcode.h
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:636
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:618
GenVertex.h
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
skel.it
it
Definition: skel.GENtoEVGEN.py:407
HepMC::newGenEvent
GenEvent * newGenEvent(const int a, const int b)
Definition: GenEvent.h:625
athena.value
value
Definition: athena.py:124
UploadAMITag.l
list l
Definition: UploadAMITag.larcaf.py:157
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
MuonR4::to_string
std::string to_string(const SectorProjector proj)
Definition: MsTrackSeeder.cxx:74
HepMC::Print::line
void line(std::ostream &os, const GenEvent &e)
Definition: GenEvent.h:677
HepMC::fillBarcodesAttribute
void fillBarcodesAttribute(GenEvent *)
Definition: GenEvent.h:627
GenParticle.h
HepMC::end
GenEvent::particle_iterator end(HepMC::GenEvent &e)
Definition: GenEvent.h:622
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:623
HepMC::set_signal_process_vertex
void set_signal_process_vertex(GenEvent *e, T v)
Definition: GenEvent.h:651
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
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:629
python.Dumpers.barcodes
def barcodes(beg, end, sz)
Definition: Dumpers.py:2831
lumiFormat.i
int i
Definition: lumiFormat.py:85
extractSporadic.h
list h
Definition: extractSporadic.py:96
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:240
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:76
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:682
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
python.changerun.pv
pv
Definition: changerun.py:79
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:628
HepMC::begin
GenEvent::particle_iterator begin(HepMC::GenEvent &e)
Definition: GenEvent.h:621
HepMC::mpi
int mpi(const GenEvent &e)
Definition: GenEvent.h:630
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:654
HepMC::end
GenEvent::particle_const_iterator end(const HepMC::GenEvent &e)
Definition: GenEvent.h:624
HepMC::set_signal_process_id
void set_signal_process_id(GenEvent *e, const int i)
Definition: GenEvent.h:642
HepMC::set_mpi
void set_mpi(GenEvent *e, const int i)
Definition: GenEvent.h:645
GenParticle
@ GenParticle
Definition: TruthClasses.h:30
HepMC::set_random_states
void set_random_states(GenEvent *e, std::vector< T > a)
Definition: GenEvent.h:648
HepMC::signal_process_vertex
GenVertex * signal_process_vertex(const GenEvent *e)
Definition: GenEvent.h:626