ATLAS Offline Software
HepMCHelper.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // STL includes
6 #include <set>
7 
8 // @TODO: probably replace this by an enum some day
9 #define ISF_UNDEF_CHARGE -99999.
10 
11 #include "AtlasHepMC/GenEvent.h"
12 #include "AtlasHepMC/IteratorRange.h"
13 #include "AtlasHepMC/Relatives.h"
14 /** constructor */
15 ISF::HepMCHelper::HepMCHelper() {
16 }
17 
18 /** emtpy destructor */
19 ISF::HepMCHelper::~HepMCHelper() {
20 }
21 
22 
23 HepMC::IteratorRange ISF::HepMCHelper::convertIteratorRange( int intItRange ) {
24  if (intItRange==0) return ( HepMC::parents );
25  else if (intItRange==1) return ( HepMC::family );
26  else if (intItRange==2) return ( HepMC::ancestors );
27  else if (intItRange==3) return ( HepMC::relatives );
28  else return ( HepMC::parents );
29 }
30 
31 #ifdef HEPMC3
32 HepMC::ConstGenParticlePtr ISF::HepMCHelper::findRealtiveWithPDG( HepMC::ConstGenParticlePtr& genParticle,
33  const HepMC::IteratorRange &relation,
34  const std::set<int> &relativesPDG ) {
35  // get range of relative particles
36  std::vector<HepMC::ConstGenParticlePtr> relativesRng;
37  switch(relation) {
38  case HepMC::parents: {
39  if (genParticle->production_vertex()) relativesRng= genParticle->production_vertex()->particles_in();
40  break;
41  }
42  case HepMC::family: {
43  if (genParticle->production_vertex()) relativesRng= genParticle->production_vertex()->particles_in();
44  std::vector<HepMC::ConstGenParticlePtr> temp;
45  if (genParticle->end_vertex()) temp=genParticle->end_vertex()->particles_out();
46  relativesRng.insert(relativesRng.end(),temp.begin(),temp.end());
47  break;
48  }
49  case HepMC::ancestors: {
50  relativesRng= HepMC::ancestor_particles(genParticle);
51  break;
52  }
53  case HepMC::relatives: {
54  relativesRng= HepMC::ancestor_particles(genParticle);
55  std::vector<HepMC::ConstGenParticlePtr> temp=HepMC::descendant_particles(genParticle);
56  relativesRng.insert(relativesRng.end(),temp.begin(),temp.end());
57  break;
58  }
59  default: {
60  if (genParticle->production_vertex()) relativesRng= genParticle->production_vertex()->particles_in();
61  break;
62  }
63  }
64  // loop over relatives
65  for (auto curRelative: relativesRng) {
66  if (relativesPDG.count( curRelative->pdg_id() ) != 0) return curRelative;
67  }
68 
69  return nullptr;
70 }
71 #else
72 HepMC::ConstGenParticlePtr ISF::HepMCHelper::findRealtiveWithPDG( HepMC::ConstGenParticlePtr& genParticle,
73  const HepMC::IteratorRange &relation,
74  const std::set<int> &relativesPDG ) {
75 
76  if (!genParticle) return 0;
77  // get range of relative particles
78  HepMC::ConstGenParticleProductionRange relativesRng = genParticle->particles_in( relation );
79 
80  // loop over relatives
81  HepMC::GenVertex::particle_iterator partIt = relativesRng.begin();
82  const HepMC::GenVertex::particle_iterator partEnd = relativesRng.end();
83  HepMC::ConstGenParticlePtr curRelative{};
84  bool found = false;
85  for ( ; (!found) && (partIt!=partEnd) ; ++partIt) {
86  curRelative = (*partIt) ;
87  // check if current relative particle PDG ID is found in the set
88  found = relativesPDG.count( curRelative->pdg_id() ) != 0;
89  }
90 
91  return (found ? curRelative : 0);
92 }
93 #endif
94