CMS 3D CMS Logo

VertexClassifier.cc
Go to the documentation of this file.
1 /*
2  * VertexClassifier.C
3  */
4 
5 #include <cmath>
6 #include <cstdlib>
7 #include <iostream>
8 
9 #include "HepPDT/ParticleID.hh"
10 
12 
13 #define update(a, b) \
14  do { \
15  (a) = (a) | (b); \
16  } while (0)
17 
19  : VertexCategories(),
20  tracer_(config, std::move(collector)),
21  hepMCLabel_(config.getUntrackedParameter<edm::InputTag>("hepMC")) {
22  collector.consumes<edm::HepMCProduct>(hepMCLabel_);
23  // Set the history depth after hadronization
24  tracer_.depth(-2);
25 
26  // Set the minimum decay length for detecting long decays
27  longLivedDecayLength_ = config.getUntrackedParameter<double>("longLivedDecayLength");
28 
29  // Set the distance for clustering vertices
30  vertexClusteringDistance_ = config.getUntrackedParameter<double>("vertexClusteringDistance");
31 }
32 
34  // Get the new event information for the tracer
35  tracer_.newEvent(event, setup);
36 
37  // Get hepmc of the event
38  event.getByLabel(hepMCLabel_, mcInformation_);
39 
40  // Get the partivle data table
42 
43  // Create the list of primary vertices associated to the event
45 }
46 
48  // Initializing the category vector
49  reset();
50 
51  // Associate and evaluate the vertex history (check for fakes)
52  if (tracer_.evaluate(vertex)) {
53  // Get all the information related to the simulation details
55 
56  // Get all the information related to decay process
58 
59  // Get information about conversion and other interactions
61 
62  // Get geometrical information about the vertices
64 
65  // Check for unkown classification
66  unknownVertex();
67  } else
68  flags_[Fake] = true;
69 
70  return *this;
71 }
72 
74  // Initializing the category vector
75  reset();
76 
77  // Trace the history for the given TP
78  tracer_.evaluate(vertex);
79 
80  // Check for a reconstructed track
82  flags_[Reconstructed] = true;
83  else
84  flags_[Reconstructed] = false;
85 
86  // Get all the information related to the simulation details
88 
89  // Get all the information related to decay process
91 
92  // Get information about conversion and other interactions
94 
95  // Get geometrical information about the vertices
97 
98  // Check for unkown classification
99  unknownVertex();
100 
101  return *this;
102 }
103 
105  // Get the event id for the initial TP.
106  EncodedEventId eventId = tracer_.simVertex()->eventId();
107  // Check for signal events
108  flags_[SignalEvent] = !eventId.bunchCrossing() && !eventId.event();
109 }
110 
112  // Get the generated vetices from track history
113  VertexHistory::GenVertexTrail const &genVertexTrail = tracer_.genVertexTrail();
114 
115  // Loop over the generated vertices
116  for (VertexHistory::GenVertexTrail::const_iterator ivertex = genVertexTrail.begin(); ivertex != genVertexTrail.end();
117  ++ivertex) {
118  // Get the pointer to the vertex by removing the const-ness (no const methos
119  // in HepMC::GenVertex)
120  HepMC::GenVertex *vertex = const_cast<HepMC::GenVertex *>(*ivertex);
121 
122  // Loop over the sources looking for specific decays
123  for (HepMC::GenVertex::particle_iterator iparent = vertex->particles_begin(HepMC::parents);
124  iparent != vertex->particles_end(HepMC::parents);
125  ++iparent) {
126  // Collect the pdgid of the parent
127  int pdgid = std::abs((*iparent)->pdg_id());
128  // Get particle type
130 
131  // Check if the particle type is valid one
132  if (particleID.isValid()) {
133  // Get particle data
134  ParticleData const *particleData = particleDataTable_->particle(particleID);
135  // Check if the particle exist in the table
136  if (particleData) {
137  // Check if their life time is bigger than longLivedDecayLength_
138  if (particleData->lifetime() > longLivedDecayLength_) {
139  // Check for B, C weak decays and long lived decays
140  update(flags_[BWeakDecay], particleID.hasBottom());
141  update(flags_[CWeakDecay], particleID.hasCharm());
142  update(flags_[LongLivedDecay], true);
143  }
144  // Check Tau, Ks and Lambda decay
145  update(flags_[TauDecay], pdgid == 15);
146  update(flags_[KsDecay], pdgid == 310);
147  update(flags_[LambdaDecay], pdgid == 3122);
148  update(flags_[JpsiDecay], pdgid == 443);
149  update(flags_[XiDecay], pdgid == 3312);
150  update(flags_[OmegaDecay], pdgid == 3334);
151  update(flags_[SigmaPlusDecay], pdgid == 3222);
152  update(flags_[SigmaMinusDecay], pdgid == 3112);
153  }
154  }
155  }
156  }
157 }
158 
160  VertexHistory::SimVertexTrail const &simVertexTrail = tracer_.simVertexTrail();
161 
162  for (VertexHistory::SimVertexTrail::const_iterator ivertex = simVertexTrail.begin(); ivertex != simVertexTrail.end();
163  ++ivertex) {
164  // pdgid of the real source parent vertex
165  int pdgid = 0;
166 
167  // select the original source in case of combined vertices
168  bool flag = false;
170 
171  for (its = (*ivertex)->sourceTracks_begin(); its != (*ivertex)->sourceTracks_end(); ++its) {
172  for (itd = (*ivertex)->daughterTracks_begin(); itd != (*ivertex)->daughterTracks_end(); ++itd)
173  if (itd != its) {
174  flag = true;
175  break;
176  }
177  if (flag)
178  break;
179  }
180  // Collect the pdgid of the original source track
181  if (its != (*ivertex)->sourceTracks_end())
182  pdgid = std::abs((*its)->pdgId());
183  else
184  pdgid = 0;
185 
186  // Geant4 process type is selected using first Geant4 vertex assigned to
187  // the TrackingVertex
188  unsigned int processG4 = 0;
189 
190  if ((*ivertex)->nG4Vertices() > 0) {
191  processG4 = (*(*ivertex)->g4Vertices_begin()).processType();
192  }
193 
194  unsigned int process = g4toCMSProcMap_.processId(processG4);
195 
196  // Flagging all the different processes
197  update(flags_[KnownProcess], process != CMS::Undefined && process != CMS::Unknown && process != CMS::Primary);
198 
203  update(flags_[DecayProcess], process == CMS::Decay);
206  update(flags_[EIoniProcess], process == CMS::EIoni);
207  update(flags_[HIoniProcess], process == CMS::HIoni);
208  update(flags_[MuIoniProcess], process == CMS::MuIoni);
209  update(flags_[PhotonProcess], process == CMS::Photon);
212  update(flags_[EBremProcess], process == CMS::EBrem);
214  update(flags_[MuBremProcess], process == CMS::MuBrem);
215  update(flags_[MuNuclProcess], process == CMS::MuNucl);
216 
217  // Loop over the simulated particles
218  for (TrackingVertex::tp_iterator iparticle = (*ivertex)->daughterTracks_begin();
219  iparticle != (*ivertex)->daughterTracks_end();
220  ++iparticle) {
221  if ((*iparticle)->numberOfTrackerLayers()) {
222  // Special treatment for decays
223  if (process == CMS::Decay) {
224  // Get particle type
226  // Check if the particle type is valid one
227  if (particleID.isValid()) {
228  // Get particle data
229  ParticleData const *particleData = particleDataTable_->particle(particleID);
230  // Check if the particle exist in the table
231  if (particleData) {
232  // Check if their life time is bigger than 1e-14
233  if (particleDataTable_->particle(particleID)->lifetime() > longLivedDecayLength_) {
234  // Check for B, C weak decays and long lived decays
235  update(flags_[BWeakDecay], particleID.hasBottom());
236  update(flags_[CWeakDecay], particleID.hasCharm());
237  update(flags_[LongLivedDecay], true);
238  }
239  // Check Tau, Ks and Lambda decay
240  update(flags_[TauDecay], pdgid == 15);
241  update(flags_[KsDecay], pdgid == 310);
242  update(flags_[LambdaDecay], pdgid == 3122);
243  update(flags_[JpsiDecay], pdgid == 443);
244  update(flags_[XiDecay], pdgid == 3312);
245  update(flags_[OmegaDecay], pdgid == 3334);
246  update(flags_[SigmaPlusDecay], pdgid == 3222);
247  update(flags_[SigmaMinusDecay], pdgid == 3112);
248  }
249  }
250  }
251  }
252  }
253  }
254 }
255 
257  // Helper class for clusterization
258  typedef std::multimap<double, HepMC::ThreeVector> Clusters;
259  typedef std::pair<double, HepMC::ThreeVector> ClusterPair;
260 
261  Clusters clusters;
262 
263  // Get the main primary vertex from the list
264  GeneratedPrimaryVertex const &genpv = genpvs_.back();
265 
266  // Get the generated history of the tracks
267  const VertexHistory::GenVertexTrail &genVertexTrail = tracer_.genVertexTrail();
268 
269  // Unit transformation from mm to cm
270  double const mm = 0.1;
271 
272  // Loop over the generated vertexes
273  for (VertexHistory::GenVertexTrail::const_iterator ivertex = genVertexTrail.begin(); ivertex != genVertexTrail.end();
274  ++ivertex) {
275  // Check vertex exist
276  if (*ivertex) {
277  // Measure the distance2 respecto the primary vertex
278  HepMC::ThreeVector p = (*ivertex)->point3d();
279  double distance =
280  sqrt(pow(p.x() * mm - genpv.x, 2) + pow(p.y() * mm - genpv.y, 2) + pow(p.z() * mm - genpv.z, 2));
281 
282  // If there is not any clusters add the first vertex.
283  if (clusters.empty()) {
284  clusters.insert(ClusterPair(distance, HepMC::ThreeVector(p.x() * mm, p.y() * mm, p.z() * mm)));
285  continue;
286  }
287 
288  // Check if there is already a cluster in the given distance from primary
289  // vertex
290  Clusters::const_iterator icluster = clusters.lower_bound(distance - vertexClusteringDistance_);
291 
292  if (icluster == clusters.upper_bound(distance + vertexClusteringDistance_)) {
293  clusters.insert(ClusterPair(distance, HepMC::ThreeVector(p.x() * mm, p.y() * mm, p.z() * mm)));
294  continue;
295  }
296 
297  bool cluster = false;
298 
299  // Looping over the vertex clusters of a given distance from primary
300  // vertex
301  for (; icluster != clusters.upper_bound(distance + vertexClusteringDistance_); ++icluster) {
302  double difference = sqrt(pow(p.x() * mm - icluster->second.x(), 2) + pow(p.y() * mm - icluster->second.y(), 2) +
303  pow(p.z() * mm - icluster->second.z(), 2));
304 
305  if (difference < vertexClusteringDistance_) {
306  cluster = true;
307  break;
308  }
309  }
310 
311  if (!cluster)
312  clusters.insert(ClusterPair(distance, HepMC::ThreeVector(p.x() * mm, p.y() * mm, p.z() * mm)));
313  }
314  }
315 
316  const VertexHistory::SimVertexTrail &simVertexTrail = tracer_.simVertexTrail();
317 
318  // Loop over the generated particles
319  for (VertexHistory::SimVertexTrail::const_reverse_iterator ivertex = simVertexTrail.rbegin();
320  ivertex != simVertexTrail.rend();
321  ++ivertex) {
322  // Look for those with production vertex
323  TrackingVertex::LorentzVector p = (*ivertex)->position();
324 
325  double distance = sqrt(pow(p.x() - genpv.x, 2) + pow(p.y() - genpv.y, 2) + pow(p.z() - genpv.z, 2));
326 
327  // If there is not any clusters add the first vertex.
328  if (clusters.empty()) {
329  clusters.insert(ClusterPair(distance, HepMC::ThreeVector(p.x(), p.y(), p.z())));
330  continue;
331  }
332 
333  // Check if there is already a cluster in the given distance from primary
334  // vertex
335  Clusters::const_iterator icluster = clusters.lower_bound(distance - vertexClusteringDistance_);
336 
337  if (icluster == clusters.upper_bound(distance + vertexClusteringDistance_)) {
338  clusters.insert(ClusterPair(distance, HepMC::ThreeVector(p.x(), p.y(), p.z())));
339  continue;
340  }
341 
342  bool cluster = false;
343 
344  // Looping over the vertex clusters of a given distance from primary vertex
345  for (; icluster != clusters.upper_bound(distance + vertexClusteringDistance_); ++icluster) {
346  double difference = sqrt(pow(p.x() - icluster->second.x(), 2) + pow(p.y() - icluster->second.y(), 2) +
347  pow(p.z() - icluster->second.z(), 2));
348 
349  if (difference < vertexClusteringDistance_) {
350  cluster = true;
351  break;
352  }
353  }
354 
355  if (!cluster)
356  clusters.insert(ClusterPair(distance, HepMC::ThreeVector(p.x(), p.y(), p.z())));
357  }
358 
359  if (clusters.size() == 1)
360  flags_[PrimaryVertex] = true;
361  else if (clusters.size() == 2)
362  flags_[SecondaryVertex] = true;
363  else
364  flags_[TertiaryVertex] = true;
365 }
366 
368  return !p->end_vertex() && p->status() == 1;
369 }
370 
372  const ParticleData *part = particleDataTable_->particle(p->pdg_id());
373  if (part)
374  return part->charge() != 0;
375  else {
376  // the new/improved particle table doesn't know anti-particles
377  return particleDataTable_->particle(-p->pdg_id()) != nullptr;
378  }
379 }
380 
382  genpvs_.clear();
383 
384  const HepMC::GenEvent *event = mcInformation_->GetEvent();
385 
386  if (event) {
387  int idx = 0;
388 
389  // Loop over the different GenVertex
390  for (HepMC::GenEvent::vertex_const_iterator ivertex = event->vertices_begin(); ivertex != event->vertices_end();
391  ++ivertex) {
392  bool hasParentVertex = false;
393 
394  // Loop over the parents looking to see if they are coming from a
395  // production vertex
396  for (HepMC::GenVertex::particle_iterator iparent = (*ivertex)->particles_begin(HepMC::parents);
397  iparent != (*ivertex)->particles_end(HepMC::parents);
398  ++iparent)
399  if ((*iparent)->production_vertex()) {
400  hasParentVertex = true;
401  break;
402  }
403 
404  // Reject those vertices with parent vertices
405  if (hasParentVertex)
406  continue;
407 
408  // Get the position of the vertex
409  HepMC::FourVector pos = (*ivertex)->position();
410 
411  double const mm = 0.1;
412 
413  GeneratedPrimaryVertex pv(pos.x() * mm, pos.y() * mm, pos.z() * mm);
414 
415  std::vector<GeneratedPrimaryVertex>::iterator ientry = genpvs_.begin();
416 
417  // Search for a VERY close vertex in the list
418  for (; ientry != genpvs_.end(); ++ientry) {
419  double distance = sqrt(pow(pv.x - ientry->x, 2) + pow(pv.y - ientry->y, 2) + pow(pv.z - ientry->z, 2));
420  if (distance < vertexClusteringDistance_)
421  break;
422  }
423 
424  // Check if there is not a VERY close vertex and added to the list
425  if (ientry == genpvs_.end())
426  ientry = genpvs_.insert(ientry, pv);
427 
428  // Add the vertex barcodes to the new or existent vertices
429  ientry->genVertex.push_back((*ivertex)->barcode());
430 
431  // Collect final state descendants
432  for (HepMC::GenVertex::particle_iterator idecendants = (*ivertex)->particles_begin(HepMC::descendants);
433  idecendants != (*ivertex)->particles_end(HepMC::descendants);
434  ++idecendants) {
435  if (isFinalstateParticle(*idecendants))
436  if (find(ientry->finalstateParticles.begin(), ientry->finalstateParticles.end(), (*idecendants)->barcode()) ==
437  ientry->finalstateParticles.end()) {
438  ientry->finalstateParticles.push_back((*idecendants)->barcode());
439  HepMC::FourVector m = (*idecendants)->momentum();
440 
441  ientry->ptot.setPx(ientry->ptot.px() + m.px());
442  ientry->ptot.setPy(ientry->ptot.py() + m.py());
443  ientry->ptot.setPz(ientry->ptot.pz() + m.pz());
444  ientry->ptot.setE(ientry->ptot.e() + m.e());
445  ientry->ptsq += m.perp() * m.perp();
446 
447  if (m.perp() > 0.8 && std::abs(m.pseudoRapidity()) < 2.5 && isCharged(*idecendants))
448  ientry->nGenTrk++;
449  }
450  }
451  idx++;
452  }
453  }
454 
455  std::sort(genpvs_.begin(), genpvs_.end());
456 }
edm::Handle< edm::HepMCProduct > mcInformation_
T getUntrackedParameter(std::string const &, T const &) const
VertexHistory tracer_
int event() const
get the contents of the subdetector field (should be protected?)
TPRegexp parents
Definition: eve_filter.cc:21
void newEvent(const edm::Event &, const edm::EventSetup &)
Pre-process event information (for accessing reconstruction information)
void vertexInformation()
Get geometrical information about the vertices.
std::vector< GeneratedPrimaryVertex > genpvs_
const edm::InputTag hepMCLabel_
SimVertexTrail const & simVertexTrail() const
Return all the simulated vertices in the history.
Definition: HistoryBase.h:52
bool isCharged(const HepMC::GenParticle *)
Get track history and classify it in function of their .
void reset()
Reset the categories flags.
bool isNonnull() const
Checks for non-null.
Definition: RefToBase.h:301
Definition: config.py:1
const reco::VertexBaseRef & recoVertex() const
Return a reference to the reconstructed track.
Definition: VertexHistory.h:56
bool isFinalstateParticle(const HepMC::GenParticle *)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
bool getData(T &iHolder) const
Definition: EventSetup.h:113
math::XYZTLorentzVectorD LorentzVector
bool evaluate(TrackingVertexRef tvr)
Evaluate track history using a TrackingParticleRef.
Definition: VertexHistory.h:38
Flags flags_
Flag containers.
T sqrt(T t)
Definition: SSEVec.h:19
int bunchCrossing() const
get the detector field from this detid
double longLivedDecayLength_
def pv(vc)
Definition: MetAnalyzer.py:7
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
std::vector< const HepMC::GenVertex * > GenVertexTrail
GenVertex trail type.
Definition: HistoryBase.h:24
edm::ESHandle< ParticleDataTable > particleDataTable_
const G4toCMSLegacyProcTypeMap g4toCMSProcMap_
const unsigned int processId(unsigned int g4ProcessId) const
Definition: Utils.cc:59
HepPDT::ParticleData ParticleData
const TrackingVertexRef & simVertex() const
Return the initial tracking vertex from the history.
Definition: HistoryBase.h:70
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:34
VertexClassifier(edm::ParameterSet const &pset, edm::ConsumesCollector &&)
Constructor by ParameterSet.
part
Definition: HCALResponse.h:20
void processesAtGenerator()
Get all the information related to decay process.
VertexClassifier const & evaluate(reco::VertexBaseRef const &)
Classify the RecoVertex in categories.
HLT enums.
void depth(int d)
Set the depth of the history.
Definition: HistoryBase.h:49
double vertexClusteringDistance_
virtual void newEvent(edm::Event const &, edm::EventSetup const &)
Pre-process event information (for accessing reconstraction information)
void processesAtSimulation()
Get information about conversion and other interactions.
GenVertexTrail const & genVertexTrail() const
Return all generated vertex in the history.
Definition: HistoryBase.h:58
#define update(a, b)
std::vector< TrackingVertexRef > SimVertexTrail
SimVertex trail type.
Definition: HistoryBase.h:33
Auxiliary class holding simulated primary vertices.
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:30
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
void simulationInformation()
Get all the information related to the simulation details.