CMS 3D CMS Logo

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