CMS 3D CMS Logo

HistoryBase.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
4 
6 
7 
9 {
10  // fill the trace of reco::GenParticle for correct reading of the History flags etc.
11  // This is called from the TrackingParticle->genParticle_begin() which is a reco::GenParticle
12 
13  // Take onlt genParticles with a status() smaller than "depth_". Typically depth is 2 and so you trace back untill you reach the hard partons
14  // see for status(): https://twiki.cern.ch/twiki/bin/view/CMSPublic/WorkBookGenParticleCandidate#GenPCand
15  if ( genParticle->status() <= abs(depth_) && (genParticle->pdgId() < 88 || genParticle->pdgId() > 99) )
16  {
17  //If the particle is already in the history, it is looping and you should stop
18  if ( recoGenParticleTrailHelper_.find(genParticle) != recoGenParticleTrailHelper_.end() ){
19  return;
20  }
21  recoGenParticleTrail_.push_back(genParticle);
22  recoGenParticleTrailHelper_.insert(genParticle);
23  // Get the genParticle's mother and trace its history
24  if (genParticle->mother() != nullptr){
25  traceRecoGenHistory( (const reco::GenParticle *)genParticle->mother() );
26  }
27  }
28 }
29 
30 
32 {
33  // Third stop criteria: status abs(depth_) particles after the hadronization.
34  // The after hadronization is done by detecting the pdg_id pythia code from 88 to 99
35  if ( genParticle->status() <= abs(depth_) && (genParticle->pdg_id() < 88 || genParticle->pdg_id() > 99) )
36  {
37  genParticleTrail_.push_back(genParticle);
38  // Get the producer vertex and trace it history
39  traceGenHistory( genParticle->production_vertex() );
40  }
41 }
42 
43 
44 void HistoryBase::traceGenHistory(HepMC::GenVertex const * genVertex)
45 {
46  // Verify if has a vertex associated
47  if (genVertex)
48  {
49  // Skip if already exist in the collection
50  if ( genVertexTrailHelper_.find(genVertex) != genVertexTrailHelper_.end() )
51  return;
52  // Add vertex to the history
53  genVertexTrail_.push_back(genVertex);
54  genVertexTrailHelper_.insert(genVertex);
55  // Verify if the vertex has incoming particles
56  if ( genVertex->particles_in_size() )
57  traceGenHistory( *(genVertex->particles_in_const_begin()) );
58  }
59 }
60 
61 
62 bool HistoryBase::traceSimHistory(TrackingParticleRef const & trackingParticle, int depth)
63 {
64  // first stop condition: if the required depth is reached
65  if ( depth == depth_ && depth_ >= 0 ) return true;
66 
67  // second stop condition: if a gen particle is associated to the TP
68  if ( !trackingParticle->genParticles().empty() )
69  {
70  LogDebug("TrackHistory") << "Particle " << trackingParticle->pdgId() << " has a GenParicle image."
71  << std::endl;
72 
73  traceRecoGenHistory(&(**(trackingParticle->genParticle_begin())));
74 
75 
76  }
77 
78  LogDebug("TrackHistory") << "No GenParticle image for " << trackingParticle->pdgId() << std::endl;
79 
80  // if no association between TP and genParticles is found: get a reference to the TP's parent vertex and trace its history
81  return traceSimHistory( trackingParticle->parentVertex(), depth );
82 }
83 
84 
85 bool HistoryBase::traceSimHistory(TrackingVertexRef const & trackingVertex, int depth)
86 {
87  // verify if the parent vertex exists
88  if ( trackingVertex.isNonnull() )
89  {
90  // save the vertex in the trail
91  simVertexTrail_.push_back(trackingVertex);
92 
93  if ( !trackingVertex->sourceTracks().empty() )
94  {
95  LogDebug("TrackHistory") << "Moving on to the parent particle." << std::endl;
96 
97  // select the original source in case of combined vertices
98  bool flag = false;
100 
101  for (its = trackingVertex->sourceTracks_begin(); its != trackingVertex->sourceTracks_end(); its++)
102  {
103  for (itd = trackingVertex->daughterTracks_begin(); itd != trackingVertex->daughterTracks_end(); itd++)
104  if (itd != its)
105  {
106  flag = true;
107  break;
108  }
109  if (flag)
110  break;
111  }
112 
113  if(!flag)
114  return false;
115 
116  // verify if the new particle is not in the trail (looping partiles)
117  if (
118  std::find(
119  simParticleTrail_.begin(),
120  simParticleTrail_.end(),
121  *its
122  ) != simParticleTrail_.end()
123  )
124  {
125  LogDebug("TrackHistory") << "WARNING: Looping track found." << std::endl;
126  return false;
127  }
128 
129  // save particle in the trail
130  simParticleTrail_.push_back(*its);
131  return traceSimHistory (*its, --depth);
132  }
133  else if ( !trackingVertex->genVertices().empty() )
134  {
135  // navigate over all the associated generated vertexes
136  LogDebug("TrackHistory") << "Vertex has " << trackingVertex->genVertices().size() << "GenVertex image." << std::endl;
137  for (
138  TrackingVertex::genv_iterator ivertex = trackingVertex->genVertices_begin();
139  ivertex != trackingVertex->genVertices_end();
140  ++ivertex
141  )
142  traceGenHistory(&(**(ivertex)));
143  return true;
144  }
145  else
146  {
147  LogDebug("TrackHistory") << "WARNING: Source track for tracking vertex cannot be found." << std::endl;
148  }
149  }
150  else
151  {
152  LogDebug("TrackHistory") << " WARNING: Vertex cannot be found.";
153  }
154 
155  return false;
156 }
157 
#define LogDebug(id)
int pdgId() const final
PDG identifier.
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:253
RecoGenParticleTrail recoGenParticleTrail_
Definition: HistoryBase.h:147
SimVertexTrail simVertexTrail_
Definition: HistoryBase.h:148
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
GenVertexTrail genVertexTrail_
Definition: HistoryBase.h:145
void traceGenHistory(HepMC::GenParticle const *)
Trace all the simulated information for a given pointer to a HepMC::GenParticle.
Definition: HistoryBase.cc:31
GenVertexTrailHelper genVertexTrailHelper_
Definition: HistoryBase.h:152
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void traceRecoGenHistory(reco::GenParticle const *)
Trace all the simulated information for a given pointer to a reco::GenParticle.
Definition: HistoryBase.cc:8
SimParticleTrail simParticleTrail_
Definition: HistoryBase.h:149
void depth(int d)
Set the depth of the history.
Definition: HistoryBase.h:53
int status() const final
status word
bool traceSimHistory(TrackingParticleRef const &, int)
Trace all the simulated information for a given reference to a TrackingParticle.
Definition: HistoryBase.cc:62
RecoGenParticleTrailHelper recoGenParticleTrailHelper_
Definition: HistoryBase.h:153
GenParticleTrail genParticleTrail_
Definition: HistoryBase.h:146
const HepMC::GenParticle * genParticle() const
Returns a pointer to most primitive status 1 or 2 particle in the genParticleTrail_.
Definition: HistoryBase.h:101
const Candidate * mother(size_type=0) const override
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...