CMS 3D CMS Logo

GenHFHadronMatcher.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: GenHFHadronMatcher
4 // Class: GenHFHadronMatcher
5 //
6 
19 // Original Author: Nazar Bartosik,DESY
20 
21 // system include files
22 #include <memory>
23 #include <utility>
24 #include <vector>
25 #include <algorithm>
26 
27 // user include files
30 
33 
35 
40 
42 
46 
48 
49 //
50 // class declaration
51 //
52 
54 public:
55  explicit GenHFHadronMatcher(const edm::ParameterSet &);
56  ~GenHFHadronMatcher() override;
57 
58  static void fillDescriptions(edm::ConfigurationDescriptions &descriptions);
59 
60 private:
61  void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override;
62 
65  std::vector<int> &hadIndex,
66  std::vector<reco::GenParticle> &hadMothersGenPart,
67  std::vector<std::vector<int>> &hadMothersIndices,
68  std::vector<int> &hadLeptonIndex,
69  std::vector<int> &hadLeptonHadIndex,
70  std::vector<int> &hadLeptonViaTau,
71  std::vector<int> &hadFlavour,
72  std::vector<int> &hadFromTopWeakDecay,
73  std::vector<int> &hadBHadronId) const;
74  int analyzeMothers(const reco::Candidate *thisParticle,
75  int &topDaughterQId,
76  int &topBarDaughterQId,
77  std::vector<const reco::Candidate *> &hadMothers,
78  std::vector<std::vector<int>> &hadMothersIndices,
79  std::set<const reco::Candidate *> *analyzedParticles,
80  const int prevPartIndex) const;
81  bool putMotherIndex(std::vector<std::vector<int>> &hadMothersIndices, int partIndex, int mothIndex) const;
82  bool isHadron(const int flavour, const reco::Candidate *thisParticle) const;
83  bool isHadronPdgId(const int flavour, const int pdgId) const;
84  bool isMesonPdgId(const int flavour, const int pdgId) const;
85  bool isBaryonPdgId(const int flavour, const int pdgId) const;
86  int flavourSign(const int pdgId) const;
87  bool hasHadronDaughter(const int flavour, const reco::Candidate *thisParticle) const;
88  int idInList(std::vector<const reco::Candidate *> particleList, const reco::Candidate *particle) const;
89  int idInList(std::vector<int> list, const int value) const;
90  int findInMothers(int idx,
91  std::vector<int> &mothChains,
92  const std::vector<std::vector<int>> &hadMothersIndices,
93  const std::vector<reco::GenParticle> &hadMothers,
94  int status,
95  int pdgId,
96  bool pdgAbs,
97  int stopId,
98  int firstLast,
99  bool verbose) const;
100  bool isNeutralPdg(int pdgId) const;
101 
102  bool checkForLoop(std::vector<const reco::Candidate *> &particleChain, const reco::Candidate *particle) const;
103  std::string getParticleName(int id) const;
104 
105  bool fixExtraSameFlavours(const unsigned int hadId,
106  const std::vector<int> &hadIndices,
107  const std::vector<reco::GenParticle> &hadMothers,
108  const std::vector<std::vector<int>> &hadMothersIndices,
109  const std::vector<int> &isFromTopWeakDecay,
110  const std::vector<std::vector<int>> &LastQuarkIds,
111  const std::vector<std::vector<int>> &LastQuarkMotherIds,
112  std::vector<int> &lastQuarkIndices,
113  std::vector<int> &hadronFlavour,
114  std::set<int> &checkedHadronIds,
115  const int lastQuarkIndex) const;
116 
117  // ----------member data ---------------------------
120  const int flavour_;
121  const bool noBBbarResonances_;
123 
135 };
136 
137 //
138 // constants, enums and typedefs
139 //
140 
141 //
142 // static data member definitions
143 //
144 
145 //
146 // constructors and destructor
147 //
156 namespace {
157  std::string flavourName(int flavour) {
158  if (flavour == 5) {
159  return "B";
160  } else if (flavour == 4) {
161  return "C";
162  }
163  edm::LogError("GenHFHadronMatcher") << "Flavour option must be 4 (c-jet) or 5 (b-jet), but is: " << flavour
164  << ". Correct this!";
165  return std::string();
166  }
167 } // namespace
168 
170  : genParticlesToken_(consumes<reco::GenParticleCollection>(cfg.getParameter<edm::InputTag>("genParticles"))),
171  jetFlavourInfosToken_(
172  consumes<reco::JetFlavourInfoMatchingCollection>(cfg.getParameter<edm::InputTag>("jetFlavourInfos"))),
173  flavour_{std::abs(cfg.getParameter<int>("flavour"))},
174  noBBbarResonances_{cfg.getParameter<bool>("noBBbarResonances")},
175  onlyJetClusteredHadrons_{cfg.getParameter<bool>("onlyJetClusteredHadrons")},
176  flavourStr_{flavourName(flavour_)},
177  plusMothersToken_{produces<std::vector<reco::GenParticle>>(
178  "gen" + flavourStr_ +
179  "HadPlusMothers")}, // All mothers in all decay chains above any hadron of specified flavour
180  plusMothersIndicesToken_{produces<std::vector<std::vector<int>>>(
181  "gen" + flavourStr_ + "HadPlusMothersIndices")}, // Indices of mothers of each hadMother
182  indexToken_{
183  produces<std::vector<int>>("gen" + flavourStr_ + "HadIndex")}, // Index of hadron in the vector of hadMothers
184  flavourToken_{produces<std::vector<int>>(
185  "gen" + flavourStr_ +
186  "HadFlavour")}, // PdgId of the first non-b(c) quark mother with sign corresponding to hadron charge
187  jetIndexToken_{produces<std::vector<int>>(
188  "gen" + flavourStr_ + "HadJetIndex")}, // Index of genJet matched to each hadron by jet clustering algorithm
189  leptonIndexToken_{produces<std::vector<int>>(
190  "gen" + flavourStr_ +
191  "HadLeptonIndex")}, // Index of lepton found among the hadron decay products in the list of mothers
192  leptonHadronIndexToken_{produces<std::vector<int>>(
193  "gen" + flavourStr_ + "HadLeptonHadronIndex")}, // Index of hadron the lepton is associated to
194  leptonViaTauToken_{produces<std::vector<int>>(
195  "gen" + flavourStr_ + "HadLeptonViaTau")}, // Whether lepton comes directly from hadron or via tau decay
196  fromTopWeakDecayToken_{produces<std::vector<int>>(
197  "gen" + flavourStr_ +
198  "HadFromTopWeakDecay")}, // Tells whether the hadron appears in the chain after top decay
199  bHadronIdToken_{produces<std::vector<int>>("gen" + flavourStr_ + "HadBHadronId")}
200 // Index of a b-hadron which the current hadron comes from (for c-hadrons)
201 {
202  // Hadron matching products
203 }
204 
206 
207 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
214  desc.add<edm::InputTag>("genParticles")
215  ->setComment("Collection of GenParticle objects which contains all particles produced in the event");
216  desc.add<edm::InputTag>("jetFlavourInfos")
217  ->setComment(
218  "Output from the JetFlavour tool. Contains information about partons/hadrons/leptons associated to jets");
219  desc.add<bool>("noBBbarResonances", true)->setComment("Whether resonances should not be treated as hadrons");
220  desc.add<bool>("onlyJetClusteredHadrons", false)
221  ->setComment("Whether only hadrons that are matched to jets should be analysed. Runs x1000 faster in Sherpa");
222  desc.add<int>("flavour", 5)->setComment("Flavour of weakly decaying hadron that should be analysed (4-c, 5-b)");
223  descriptions.add("matchGenHFHadron", desc);
224 }
225 
226 //
227 // member functions
228 //
229 
230 // ------------ method called to produce the data ------------
232  using namespace edm;
233 
236 
239 
240  // Defining adron matching variables
241  std::vector<reco::GenParticle> hadMothers;
242  std::vector<std::vector<int>> hadMothersIndices;
243  std::vector<int> hadIndex;
244  std::vector<int> hadFlavour;
245  std::vector<int> hadJetIndex;
246  std::vector<int> hadLeptonIndex;
247  std::vector<int> hadLeptonHadIndex;
248  std::vector<int> hadLeptonViaTau;
249  std::vector<int> hadFromTopWeakDecay;
250  std::vector<int> hadBHadronId;
251 
252  hadJetIndex = findHadronJets(genParticles.product(),
253  jetFlavourInfos.product(),
254  hadIndex,
255  hadMothers,
256  hadMothersIndices,
257  hadLeptonIndex,
258  hadLeptonHadIndex,
259  hadLeptonViaTau,
260  hadFlavour,
261  hadFromTopWeakDecay,
262  hadBHadronId);
263 
264  // Putting products to the event
265  evt.emplace(plusMothersToken_, std::move(hadMothers));
266  evt.emplace(plusMothersIndicesToken_, std::move(hadMothersIndices));
267  evt.emplace(indexToken_, std::move(hadIndex));
268  evt.emplace(flavourToken_, std::move(hadFlavour));
269  evt.emplace(jetIndexToken_, std::move(hadJetIndex));
270  evt.emplace(leptonIndexToken_, std::move(hadLeptonIndex));
271  evt.emplace(leptonHadronIndexToken_, std::move(hadLeptonHadIndex));
272  evt.emplace(leptonViaTauToken_, std::move(hadLeptonViaTau));
273  evt.emplace(fromTopWeakDecayToken_, std::move(hadFromTopWeakDecay));
274  evt.emplace(bHadronIdToken_, std::move(hadBHadronId));
275 }
276 
301  std::vector<int> &hadIndex,
302  std::vector<reco::GenParticle> &hadMothers,
303  std::vector<std::vector<int>> &hadMothersIndices,
304  std::vector<int> &hadLeptonIndex,
305  std::vector<int> &hadLeptonHadIndex,
306  std::vector<int> &hadLeptonViaTau,
307  std::vector<int> &hadFlavour,
308  std::vector<int> &hadFromTopWeakDecay,
309  std::vector<int> &hadBHadronId) const {
310  std::vector<int> hadJetIndex;
311  std::vector<const reco::Candidate *> hadMothersCand;
312 
313  int topDaughterQId = -1;
314  int topBarDaughterQId = -1;
315 
316  // Looping over all jets to get hadrons associated to them
318  i_info != jetFlavourInfos->end();
319  ++i_info) {
320  reco::JetFlavourInfo jetInfo = i_info->second;
321  const int jetIndex = i_info - jetFlavourInfos->begin();
322  // Looping over each hadron associated with the jet and finding its origin
323  const reco::GenParticleRefVector &hadronsInJet = flavour_ == 5 ? jetInfo.getbHadrons()
324  : flavour_ == 4 ? jetInfo.getcHadrons()
326  for (reco::GenParticleRefVector::const_iterator hadron = hadronsInJet.begin(); hadron != hadronsInJet.end();
327  ++hadron) {
328  // Check that the hadron satisfies criteria configured in the module
329  if (!isHadron(flavour_, (&**hadron)))
330  continue;
331  if (hasHadronDaughter(flavour_, (reco::Candidate *)(&**hadron)))
332  continue;
333  // Scanning the chain starting from the hadron
334  int hadronIndex = analyzeMothers((reco::Candidate *)(&**hadron),
335  topDaughterQId,
336  topBarDaughterQId,
337  hadMothersCand,
338  hadMothersIndices,
339  nullptr,
340  -1);
341  // Storing the index of the hadron to the list
342  hadIndex.push_back(hadronIndex);
343  hadJetIndex.push_back(jetIndex); // Putting jet index to the result list
344  }
345  } // End of loop over jets
346 
347  // Access all hadrons which are not associated with jets, if requested
349  for (reco::GenParticleCollection::const_iterator i_particle = genParticles->begin();
350  i_particle != genParticles->end();
351  ++i_particle) {
352  const reco::GenParticle *thisParticle = &*i_particle;
353  if (!isHadron(flavour_, thisParticle))
354  continue;
355  // Skipping the hadron if it was already found directly from jets
356  if (std::find(hadMothersCand.begin(), hadMothersCand.end(), thisParticle) != hadMothersCand.end())
357  continue;
358 
359  // Scanning the chain starting from the hadron
360  int hadronIndex = analyzeMothers(
361  thisParticle, topDaughterQId, topBarDaughterQId, hadMothersCand, hadMothersIndices, nullptr, -1);
362  // Storing the index of the hadron to the list
363  hadIndex.push_back(hadronIndex);
364  hadJetIndex.push_back(-1); // Jet index undefined
365  }
366  }
367 
368  // Transfering Candidates to the list of processed particles for further analysis
369  for (int i = 0; i < (int)hadMothersCand.size(); i++) {
370  const reco::GenParticle *particle = dynamic_cast<const reco::GenParticle *>(hadMothersCand.at(i));
371  hadMothers.push_back(*particle);
372  }
373 
374  // Adding leptons from hadron decays
375  for (reco::GenParticleCollection::const_iterator i_particle = genParticles->begin();
376  i_particle != genParticles->end();
377  ++i_particle) {
378  const reco::GenParticle &lepton = *i_particle;
379  const int pdg_abs = lepton.pdgId();
380  // Skipping if not a lepton: e/mu
381  if (pdg_abs != 11 && pdg_abs != 13)
382  continue;
383  bool leptonViaTau = false;
384  const reco::Candidate *leptonMother = lepton.mother();
385  if (!leptonMother)
386  continue;
387  // Taking next mother if direct mother is a tau
388  if (std::abs(leptonMother->pdgId()) == 15) {
389  leptonViaTau = true;
390  leptonMother = leptonMother->mother();
391  }
392  // Skipping this lepton if its mother is not a proper hadron
393  if (leptonMother == nullptr or !isHadron(flavour_, leptonMother))
394  continue;
395  // Finding the index of this hadron in the list of analysed particles
396  size_t leptonHadronParticleIndex =
397  std::find(hadMothersCand.begin(), hadMothersCand.end(), leptonMother) - hadMothersCand.begin();
398  if (leptonHadronParticleIndex >= hadMothersCand.size())
399  continue;
400  // Finding the actual hadron index among those that were found
401  size_t leptonHadronIndex =
402  std::find(hadIndex.begin(), hadIndex.end(), leptonHadronParticleIndex) - hadIndex.begin();
403  if (leptonHadronIndex >= hadIndex.size())
404  continue;
405  // Putting the lepton, its index and hadron index to the corresponding lists
406  hadMothers.push_back(lepton);
407  const int leptonIndex = hadMothersCand.size() - 1;
408  hadLeptonIndex.push_back(leptonIndex);
409  hadLeptonViaTau.push_back(leptonViaTau);
410  hadLeptonHadIndex.push_back(leptonHadronIndex);
411  }
412 
413  // Checking mothers of hadrons in order to assign flags (where the hadron comes from)
414  unsigned int nHad = hadIndex.size();
415 
416  std::vector<std::vector<int>> LastQuarkMotherIds;
417  std::vector<std::vector<int>> LastQuarkIds;
418  std::vector<int> lastQuarkIndices(nHad, -1);
419 
420  // Looping over all hadrons
421  for (unsigned int hadNum = 0; hadNum < nHad; hadNum++) {
422  unsigned int hadIdx = hadIndex.at(hadNum); // Index of hadron in the hadMothers
423 
424  std::vector<int> FirstQuarkId;
425  std::vector<int> LastQuarkId;
426  std::vector<int> LastQuarkMotherId;
427 
428  const int hadronFlavourSign = flavourSign(hadMothers.at(hadIdx).pdgId());
429 
430  // Searching only first quark in the chain with the same flavour as hadron
431  if (hadronFlavourSign != 0) {
433  hadIdx, FirstQuarkId, hadMothersIndices, hadMothers, 0, hadronFlavourSign * flavour_, false, -1, 1, false);
434  }
435  // Searching for quarks with both flavours since it is a bb/cc resonance
436  else {
437  findInMothers(hadIdx, FirstQuarkId, hadMothersIndices, hadMothers, 0, flavour_, false, -1, 1, false);
438  findInMothers(hadIdx, FirstQuarkId, hadMothersIndices, hadMothers, 0, -1 * flavour_, false, -1, 1, false);
439  }
440 
441  // Finding last quark for each first quark
442  for (unsigned int qId = 0; qId < FirstQuarkId.size(); qId++) {
443  // Identifying the flavour of the first quark to find the last quark of the same flavour
444  const int quarkFlavourSign = flavourSign(hadMothers.at(FirstQuarkId.at(qId)).pdgId());
445  // Finding last quark of the hadron starting from the first quark
446  findInMothers(FirstQuarkId.at(qId),
447  LastQuarkId,
448  hadMothersIndices,
449  hadMothers,
450  0,
451  quarkFlavourSign * flavour_,
452  false,
453  -1,
454  2,
455  false);
456  } // End of loop over all first quarks of the hadron
457 
458  // Setting initial flavour of the hadron
459  int hadronFlavour = 0;
460 
461  // Initialising pairs of last quark index and its distance from the hadron (to sort by distance)
462  std::vector<std::pair<double, int>> lastQuark_dR_id_pairs;
463 
464  // Finding the closest quark in dR
465  for (unsigned int qId = 0; qId < LastQuarkId.size(); qId++) {
466  int qIdx = LastQuarkId.at(qId);
467  // Calculating the dR between hadron and quark
468  float dR = deltaR(hadMothers.at(hadIdx).eta(),
469  hadMothers.at(hadIdx).phi(),
470  hadMothers.at(qIdx).eta(),
471  hadMothers.at(qIdx).phi());
472 
473  std::pair<double, int> dR_hadId_pair(dR, qIdx);
474  lastQuark_dR_id_pairs.push_back(dR_hadId_pair);
475  } // End of loop over all last quarks of the hadron
476 
477  std::sort(lastQuark_dR_id_pairs.begin(), lastQuark_dR_id_pairs.end());
478 
479  if (lastQuark_dR_id_pairs.size() > 1) {
480  double dRratio =
481  (lastQuark_dR_id_pairs.at(1).first - lastQuark_dR_id_pairs.at(0).first) / lastQuark_dR_id_pairs.at(1).first;
482  int qIdx_closest = lastQuark_dR_id_pairs.at(0).second;
483  LastQuarkId.clear();
484  if (dRratio > 0.5)
485  LastQuarkId.push_back(qIdx_closest);
486  else
487  for (std::pair<double, int> qIdDrPair : lastQuark_dR_id_pairs)
488  LastQuarkId.push_back(qIdDrPair.second);
489  }
490  for (int qIdx : LastQuarkId) {
491  int qmIdx = hadMothersIndices.at(qIdx).at(0);
492  LastQuarkMotherId.push_back(qmIdx);
493  }
494 
495  if ((int)LastQuarkId.size() > 0)
496  lastQuarkIndices.at(hadNum) = 0; // Setting the first quark in array as a candidate if it exists
497 
498  LastQuarkIds.push_back(LastQuarkId);
499 
500  LastQuarkMotherIds.push_back(LastQuarkMotherId);
501 
502  if (LastQuarkMotherId.empty()) {
503  hadronFlavour = 0;
504  } else {
505  int qIdx = LastQuarkId.at(lastQuarkIndices.at(hadNum));
506  int qFlav = flavourSign(hadMothers.at(qIdx).pdgId());
507  hadronFlavour = qFlav * std::abs(hadMothers.at(LastQuarkMotherId.at(lastQuarkIndices.at(hadNum))).pdgId());
508  }
509  hadFlavour.push_back(hadronFlavour); // Adding hadron flavour to the list of flavours
510 
511  // Checking whether hadron comes from the Top weak decay
512  int isFromTopWeakDecay = 1;
513  std::vector<int> checkedParticles;
514  if (hadFlavour.at(hadNum) != 0) {
515  int lastQIndex = LastQuarkId.at(lastQuarkIndices.at(hadNum));
516  bool fromTB = topDaughterQId >= 0 ? findInMothers(lastQIndex,
517  checkedParticles,
518  hadMothersIndices,
519  hadMothers,
520  -1,
521  0,
522  false,
523  topDaughterQId,
524  2,
525  false) >= 0
526  : false;
527  checkedParticles.clear();
528  bool fromTbarB = topBarDaughterQId >= 0 ? findInMothers(lastQIndex,
529  checkedParticles,
530  hadMothersIndices,
531  hadMothers,
532  -1,
533  0,
534  false,
535  topBarDaughterQId,
536  2,
537  false) >= 0
538  : false;
539  checkedParticles.clear();
540  if (!fromTB && !fromTbarB) {
541  isFromTopWeakDecay = 0;
542  }
543  } else
544  isFromTopWeakDecay = 2;
545  hadFromTopWeakDecay.push_back(isFromTopWeakDecay);
546  int bHadronMotherId =
547  findInMothers(hadIdx, checkedParticles, hadMothersIndices, hadMothers, 0, 555555, true, -1, 1, false);
548  hadBHadronId.push_back(bHadronMotherId);
549 
550  if (!LastQuarkMotherId.empty()) {
551  std::set<int> checkedHadronIds;
552  fixExtraSameFlavours(hadNum,
553  hadIndex,
554  hadMothers,
555  hadMothersIndices,
556  hadFromTopWeakDecay,
557  LastQuarkIds,
558  LastQuarkMotherIds,
559  lastQuarkIndices,
560  hadFlavour,
561  checkedHadronIds,
562  0);
563  }
564 
565  } // End of loop over all hadrons
566 
567  return hadJetIndex;
568 }
569 
578 int GenHFHadronMatcher::idInList(std::vector<const reco::Candidate *> particleList,
579  const reco::Candidate *particle) const {
580  const unsigned int position = std::find(particleList.begin(), particleList.end(), particle) - particleList.begin();
581  if (position >= particleList.size())
582  return -1;
583 
584  return position;
585 }
586 
587 int GenHFHadronMatcher::idInList(std::vector<int> list, const int value) const {
588  const unsigned int position = std::find(list.begin(), list.end(), value) - list.begin();
589  if (position >= list.size())
590  return -1;
591 
592  return position;
593 }
594 
603 bool GenHFHadronMatcher::isHadron(const int flavour, const reco::Candidate *thisParticle) const {
604  return isHadronPdgId(flavour, thisParticle->pdgId());
605 }
606 
615 bool GenHFHadronMatcher::isHadronPdgId(const int flavour, const int pdgId) const {
617  return true;
618 
619  return false;
620 }
621 
630 bool GenHFHadronMatcher::isMesonPdgId(const int flavour, const int pdgId) const {
631  const int flavour_abs = std::abs(flavour);
632  if (flavour_abs != 5 && flavour_abs != 4)
633  return false;
634  const int pdgId_abs = std::abs(pdgId);
635 
636  if (pdgId_abs / 100 % 10 != flavour_abs)
637  return false;
638  // Excluding baryons
639  if (pdgId_abs / 1000 == flavour_abs)
640  return false;
641  // Excluding bb/cc resonances if required
642  if (noBBbarResonances_ && pdgId_abs / 10 % 100 == 11 * flavour_abs)
643  return false;
644 
645  return true;
646 }
647 
656 bool GenHFHadronMatcher::isBaryonPdgId(const int flavour, const int pdgId) const {
657  const int flavour_abs = std::abs(flavour);
658  if (flavour_abs != 5 && flavour_abs != 4)
659  return false;
660  const int pdgId_abs = std::abs(pdgId);
661 
662  if (pdgId_abs / 1000 != flavour_abs)
663  return false;
664 
665  return true;
666 }
667 
676  int flavourSign = pdgId / std::abs(pdgId);
677  // B mesons have opposite sign
678  if (isMesonPdgId(5, pdgId))
679  flavourSign *= -1;
680  // Returning 0 for bb/cc resonances
681  if (pdgId % 1000 / 10 / 11 > 0)
682  flavourSign = 0;
683 
684  return flavourSign;
685 }
686 
695 bool GenHFHadronMatcher::hasHadronDaughter(const int flavour, const reco::Candidate *thisParticle) const {
696  // Looping through daughters of the particle
697  bool hasDaughter = false;
698  for (int k = 0; k < (int)thisParticle->numberOfDaughters(); k++) {
699  if (!isHadron(flavour, thisParticle->daughter(k))) {
700  continue;
701  }
702  hasDaughter = true;
703  break;
704  }
705 
706  return hasDaughter;
707 }
708 
727  int &topDaughterQId,
728  int &topBarDaughterQId,
729  std::vector<const reco::Candidate *> &hadMothers,
730  std::vector<std::vector<int>> &hadMothersIndices,
731  std::set<const reco::Candidate *> *analyzedParticles,
732  const int prevPartIndex) const {
733  // Getting the index of the particle which is a hadron in the first call
734  int hadronIndex = -1; // Index of the hadron that is returned by this function
735  int index = idInList(hadMothers, thisParticle);
736  if (index < 0) { // If hadron is not in the list of mothers yet
737  hadMothers.push_back(thisParticle);
738  hadronIndex = hadMothers.size() - 1;
739  } else { // If hadron is in the list of mothers already
740  hadronIndex = index;
741  }
742 
743  int partIndex = -1; // Index of particle being checked in the list of mothers
744  partIndex = idInList(hadMothers, thisParticle);
745 
746  // Checking whether this particle is already in the chain of analyzed particles in order to identify a loop
747  bool isLoop = false;
748  if (!analyzedParticles) {
749  analyzedParticles = new std::set<const reco::Candidate *>;
750  }
751  for (unsigned int i = 0; i < analyzedParticles->size(); i++) {
752  if (analyzedParticles->count(thisParticle) <= 0) {
753  continue;
754  }
755  isLoop = true;
756  break;
757  }
758 
759  // If a loop has been detected
760  if (isLoop) {
761  if (prevPartIndex >= 0) {
762  putMotherIndex(hadMothersIndices, prevPartIndex, -1); // Setting mother index of previous particle to -1
763  }
764  return hadronIndex; // Stopping further processing of the current chain
765  }
766  analyzedParticles->insert(thisParticle);
767 
768  // Putting the mothers to the list of mothers
769  for (size_t iMother = 0; iMother < thisParticle->numberOfMothers(); ++iMother) {
770  const reco::Candidate *mother = thisParticle->mother(iMother);
771  int mothIndex = idInList(hadMothers, mother);
772  if (mothIndex == partIndex && partIndex >= 0) {
773  continue; // Skipping the mother that is its own daughter
774  }
775 
776  // If this mother isn't yet in the list and hadron or lepton is in the list
777  if (mothIndex < 0) {
778  hadMothers.push_back(mother);
779  mothIndex = hadMothers.size() - 1;
780  }
781  // If hadron has already been found in current chain and the mother isn't a duplicate of the particle being checked
782  if (mothIndex != partIndex && partIndex >= 0) {
783  putMotherIndex(hadMothersIndices, partIndex, mothIndex); // Putting the index of mother for current particle
784  }
786  mother, topDaughterQId, topBarDaughterQId, hadMothers, hadMothersIndices, analyzedParticles, partIndex);
787  // Setting the id of the particle which is a quark from the top decay
788  if (std::abs(mother->pdgId()) == 6) {
789  int &bId = mother->pdgId() < 0 ? topBarDaughterQId : topDaughterQId;
790  int thisFlav = std::abs(thisParticle->pdgId());
791  if (bId < 0) {
792  if (thisFlav <= 5)
793  bId = partIndex;
794  } else {
795  int bIdFlav = std::abs(hadMothers.at(bId)->pdgId());
796  if (bIdFlav != 5 && thisFlav == 5)
797  bId = partIndex;
798  else if (thisFlav == 5 && thisParticle->pt() > hadMothers.at(bId)->pt())
799  bId = partIndex;
800  } // If daughter quark of the top not found yet
801  } // If the mother is a top quark and hadron has been found
802  } // End of loop over mothers
803 
804  analyzedParticles->erase(thisParticle); // Removing current particle from the current chain that is being analyzed
805 
806  if (partIndex < 0) {
807  return hadronIndex; // Safety check
808  }
809 
810  // Adding -1 to the list of mother indices for current particle if it has no mothers (for consistency between numbering of indices and mothers)
811  if ((int)thisParticle->numberOfMothers() <= 0) {
812  putMotherIndex(hadMothersIndices, partIndex, -1);
813  }
814 
815  return hadronIndex;
816 }
817 
827 bool GenHFHadronMatcher::putMotherIndex(std::vector<std::vector<int>> &hadMothersIndices,
828  int partIndex,
829  int mothIndex) const {
830  // Putting vector of mothers indices for the given particle
831  bool inList = false;
832  if (partIndex < 0) {
833  return false;
834  }
835 
836  while ((int)hadMothersIndices.size() <= partIndex) { // If there is no list of mothers for current particle yet
837  std::vector<int> mothersIndices;
838  hadMothersIndices.push_back(mothersIndices);
839  }
840 
841  std::vector<int> *hadMotherIndices = &hadMothersIndices.at(partIndex);
842  // Removing other mothers if particle must have no mothers
843  if (mothIndex == -1) {
844  hadMotherIndices->clear();
845  } else {
846  // Checking if current mother is already in the list of theParticle's mothers
847  for (int k = 0; k < (int)hadMotherIndices->size(); k++) {
848  if (hadMotherIndices->at(k) != mothIndex && hadMotherIndices->at(k) != -1) {
849  continue;
850  }
851  inList = true;
852  break;
853  }
854  }
855  // Adding current mother to the list of mothers of this particle
856  if (!inList) {
857  hadMotherIndices->push_back(mothIndex);
858  }
859 
860  return inList;
861 }
862 
881  std::vector<int> &mothChains,
882  const std::vector<std::vector<int>> &hadMothersIndices,
883  const std::vector<reco::GenParticle> &hadMothers,
884  int status,
885  int pdgId,
886  bool pdgAbs = false,
887  int stopId = -1,
888  int firstLast = 0,
889  bool verbose = false) const {
890  int foundStopId = -1;
891  int pdg_1 = hadMothers.at(idx).pdgId();
892 
893  if ((int)hadMothersIndices.size() <= idx) {
894  if (verbose) {
895  printf(" Stopping checking particle %d. No mothers are stored.\n", idx);
896  }
897  return -1; // Skipping if no mothers are stored for this particle
898  }
899 
900  if (std::abs(hadMothers.at(idx).pdgId()) > 10 && std::abs(hadMothers.at(idx).pdgId()) < 19)
901  printf("Lepton: %d\n", hadMothers.at(idx).pdgId());
902 
903  std::vector<int> mothers = hadMothersIndices.at(idx);
904  unsigned int nMothers = mothers.size();
905  bool isCorrect = false; // Whether current particle is what is being searched
906  if (verbose) {
907  if (std::abs(hadMothers.at(idx).pdgId()) == 2212) {
908  printf("Chk: %d\tpdg: %d\tstatus: %d", idx, hadMothers.at(idx).pdgId(), hadMothers.at(idx).status());
909  } else {
910  printf(" Chk: %d(%d mothers)\tpdg: %d\tstatus: %d\tPt: %.3f\tEta: %.3f",
911  idx,
912  nMothers,
913  hadMothers.at(idx).pdgId(),
914  hadMothers.at(idx).status(),
915  hadMothers.at(idx).pt(),
916  hadMothers.at(idx).eta());
917  }
918  }
919  bool hasCorrectMothers = true;
920  if (nMothers < 1)
921  hasCorrectMothers = false;
922  else if (mothers.at(0) < 0)
923  hasCorrectMothers = false;
924  if (!hasCorrectMothers) {
925  if (verbose)
926  printf(" NO CORRECT MOTHER\n");
927  return -1;
928  }
929  // Stopping if the particular particle has been found
930  if (stopId >= 0 && idx == stopId)
931  return idx;
932 
933  // Stopping if the hadron of particular flavour has been found
934  if (pdgId % 111111 == 0 && pdgId != 0) {
935  if (isHadronPdgId(pdgId / 111111, hadMothers.at(idx).pdgId())) {
936  return idx;
937  }
938  }
939 
940  // Checking whether current mother satisfies selection criteria
941  if (((hadMothers.at(idx).pdgId() == pdgId && pdgAbs == false) ||
942  (std::abs(hadMothers.at(idx).pdgId()) == std::abs(pdgId) && pdgAbs == true)) &&
943  (hadMothers.at(idx).status() == status || status == 0) && hasCorrectMothers) {
944  isCorrect = true;
945  // Adding to the list of candidates if not there and if mother of this quark has correct flavour sign
946  const bool inList = std::find(mothChains.begin(), mothChains.end(), idx) != mothChains.end();
947  if (!inList && mothers.at(0) >= 0 && (hadMothers.at(idx).pdgId() * pdgId > 0 || !pdgAbs)) {
948  if (firstLast == 0 || firstLast == 1) {
949  mothChains.push_back(idx);
950  }
951  if (verbose) {
952  printf(" *");
953  }
954  }
955  if (verbose) {
956  printf(" +++");
957  }
958  }
959  if (verbose) {
960  printf("\n");
961  }
962 
963  if (isCorrect && firstLast == 1) {
964  return -1; // Stopping if only the first particle in the chain is looked for
965  }
966 
967  // Checking next level mothers
968  unsigned int nDifferingMothers = 0;
969  for (unsigned int i = 0; i < nMothers; i++) {
970  int idx2 = mothers.at(i);
971  if (idx2 < 0) {
972  if (verbose)
973  printf("^^^ Has no mother\n");
974  continue; // Skipping if mother's id is -1 (no mother), that means current particle is a proton
975  }
976  if (idx2 == idx) {
977  if (verbose)
978  printf("^^^ Stored as its own mother\n");
979  continue; // Skipping if particle is stored as its own mother
980  }
981  int pdg_2 = hadMothers[idx2].pdgId();
982  // Inverting the flavour if bb oscillation detected
983  if (isHadronPdgId(pdgId, pdg_1) && isHadronPdgId(pdgId, pdg_2) && pdg_1 * pdg_2 < 0) {
984  pdgId *= -1;
985  if (verbose)
986  printf("######### Inverting flavour of the hadron\n");
987  }
988  // Counting how many mothers are different from this particle
989  if ((std::abs(pdg_2) != std::abs(pdgId) && pdgAbs == true) || (pdg_2 != pdgId && pdgAbs == false)) {
990  nDifferingMothers++;
991  }
992 
993  // Checking next level mother
994  if (verbose) {
995  printf("Checking mother %d out of %d mothers (%d -> %d), looking for pdgId: %d\n", i, nMothers, idx, idx2, pdgId);
996  }
997  if (firstLast == 2 && pdg_1 != pdg_2)
998  continue; // Requiring the same flavour when finding the last quark
999  foundStopId = findInMothers(
1000  idx2, mothChains, hadMothersIndices, hadMothers, status, pdgId, pdgAbs, stopId, firstLast, verbose);
1001  }
1002  // Storing this particle if all its mothers are of another type and the last of its kind should be stored
1003  if (firstLast == 2 && isCorrect && nDifferingMothers >= nMothers) {
1004  if (verbose) {
1005  printf("Checking particle %d once more to store it as the last quark\n", idx);
1006  }
1007  foundStopId = findInMothers(idx, mothChains, hadMothersIndices, hadMothers, 0, pdgId, pdgAbs, stopId, 1, verbose);
1008  }
1009 
1010  return foundStopId;
1011 }
1012 
1022  const int neutralPdgs_array[] = {9, 21, 22, 23, 25};
1023  const std::vector<int> neutralPdgs(neutralPdgs_array, neutralPdgs_array + sizeof(neutralPdgs_array) / sizeof(int));
1024  if (std::find(neutralPdgs.begin(), neutralPdgs.end(), std::abs(pdgId)) == neutralPdgs.end())
1025  return false;
1026 
1027  return true;
1028 }
1029 
1044 bool GenHFHadronMatcher::fixExtraSameFlavours(const unsigned int hadId,
1045  const std::vector<int> &hadIndices,
1046  const std::vector<reco::GenParticle> &hadMothers,
1047  const std::vector<std::vector<int>> &hadMothersIndices,
1048  const std::vector<int> &isFromTopWeakDecay,
1049  const std::vector<std::vector<int>> &LastQuarkIds,
1050  const std::vector<std::vector<int>> &LastQuarkMotherIds,
1051  std::vector<int> &lastQuarkIndices,
1052  std::vector<int> &hadronFlavour,
1053  std::set<int> &checkedHadronIds,
1054  const int lastQuarkIndex) const {
1055  if (checkedHadronIds.count(hadId) != 0)
1056  return false; // Hadron already checked previously and should be skipped
1057  checkedHadronIds.insert(hadId); // Putting hadron to the list of checked ones in this run
1058 
1059  if (lastQuarkIndex < 0)
1060  return false;
1061  if ((int)LastQuarkIds.at(hadId).size() < lastQuarkIndex + 1)
1062  return false;
1063  int LastQuarkId = LastQuarkIds.at(hadId).at(lastQuarkIndex);
1064  int LastQuarkMotherId = LastQuarkMotherIds.at(hadId).at(lastQuarkIndex);
1065  int qmFlav = hadMothers.at(LastQuarkId).pdgId() < 0 ? -1 : 1;
1066  int hadFlavour = qmFlav * std::abs(hadMothers.at(LastQuarkMotherId).pdgId());
1067  bool ambiguityResolved = true;
1068  // If last quark has inconsistent flavour with its mother, setting the hadron flavour to gluon
1069  if ((hadMothers.at(LastQuarkId).pdgId() * hadMothers.at(LastQuarkMotherId).pdgId() < 0 &&
1070  !isNeutralPdg(hadMothers.at(LastQuarkMotherId).pdgId())) ||
1071  // If particle not coming from the Top weak decay has Top flavour
1072  (std::abs(hadronFlavour.at(hadId)) == 6 && isFromTopWeakDecay.at(hadId) == 0)) {
1073  if ((int)LastQuarkIds.at(hadId).size() > lastQuarkIndex + 1)
1074  fixExtraSameFlavours(hadId,
1075  hadIndices,
1076  hadMothers,
1077  hadMothersIndices,
1078  isFromTopWeakDecay,
1079  LastQuarkIds,
1080  LastQuarkMotherIds,
1081  lastQuarkIndices,
1082  hadronFlavour,
1083  checkedHadronIds,
1084  lastQuarkIndex + 1);
1085  else
1086  hadronFlavour.at(hadId) = qmFlav * 21;
1087  return true;
1088  }
1089 
1090  int nSameFlavourHadrons = 0;
1091  // Looping over all previous hadrons
1092  for (unsigned int iHad = 0; iHad < hadronFlavour.size(); iHad++) {
1093  if (iHad == hadId)
1094  continue;
1095  int theLastQuarkIndex = lastQuarkIndices.at(iHad);
1096  if (theLastQuarkIndex < 0)
1097  continue;
1098  if ((int)LastQuarkMotherIds.at(iHad).size() <= theLastQuarkIndex)
1099  continue;
1100  int theLastQuarkMotherId = LastQuarkMotherIds.at(iHad).at(theLastQuarkIndex);
1101  int theHadFlavour = hadronFlavour.at(iHad);
1102  // Skipping hadrons with different flavour
1103  if (theHadFlavour == 0 || std::abs(theHadFlavour) == 21)
1104  continue;
1105  if (theHadFlavour != hadFlavour || theLastQuarkMotherId != LastQuarkMotherId)
1106  continue;
1107  ambiguityResolved = false;
1108  nSameFlavourHadrons++;
1109 
1110  // Checking other b-quark mother candidates of this hadron
1111  if ((int)LastQuarkIds.at(hadId).size() > lastQuarkIndex + 1) {
1112  if (fixExtraSameFlavours(hadId,
1113  hadIndices,
1114  hadMothers,
1115  hadMothersIndices,
1116  isFromTopWeakDecay,
1117  LastQuarkIds,
1118  LastQuarkMotherIds,
1119  lastQuarkIndices,
1120  hadronFlavour,
1121  checkedHadronIds,
1122  lastQuarkIndex + 1)) {
1123  ambiguityResolved = true;
1124  break;
1125  }
1126  } else
1127  // Checking other b-quark mother candidates of the particular previous hadron
1128  if ((int)LastQuarkIds.at(iHad).size() > theLastQuarkIndex + 1) {
1129  if (fixExtraSameFlavours(iHad,
1130  hadIndices,
1131  hadMothers,
1132  hadMothersIndices,
1133  isFromTopWeakDecay,
1134  LastQuarkIds,
1135  LastQuarkMotherIds,
1136  lastQuarkIndices,
1137  hadronFlavour,
1138  checkedHadronIds,
1139  theLastQuarkIndex + 1)) {
1140  ambiguityResolved = true;
1141  break;
1142  };
1143  }
1144 
1145  } // End of loop over all previous hadrons
1146 
1147  checkedHadronIds.erase(hadId); // Removing the hadron from the list of checked hadrons
1148  if (nSameFlavourHadrons > 0 && !ambiguityResolved) {
1149  hadronFlavour.at(hadId) = qmFlav * 21;
1150  return true;
1151  }
1152  lastQuarkIndices.at(hadId) = lastQuarkIndex;
1153  hadronFlavour.at(hadId) = hadFlavour;
1154  return true;
1155 }
1156 
1157 //define this as a plug-in
std::vector< GenParticle > GenParticleCollection
collection of GenParticles
bool fixExtraSameFlavours(const unsigned int hadId, const std::vector< int > &hadIndices, const std::vector< reco::GenParticle > &hadMothers, const std::vector< std::vector< int >> &hadMothersIndices, const std::vector< int > &isFromTopWeakDecay, const std::vector< std::vector< int >> &LastQuarkIds, const std::vector< std::vector< int >> &LastQuarkMotherIds, std::vector< int > &lastQuarkIndices, std::vector< int > &hadronFlavour, std::set< int > &checkedHadronIds, const int lastQuarkIndex) const
const edm::EDPutTokenT< std::vector< reco::GenParticle > > plusMothersToken_
virtual const Candidate * daughter(size_type i) const =0
return daughter at a given position, i = 0, ... numberOfDaughters() - 1 (read only mode) ...
const Candidate * mother(size_type=0) const override
return mother at a given position, i = 0, ... numberOfMothers() - 1 (read only mode) ...
bool isHadronPdgId(const int flavour, const int pdgId) const
Check the pdgId if it represents a hadron of particular flavour.
const edm::EDPutTokenT< std::vector< std::vector< int > > > plusMothersIndicesToken_
const edm::EDGetTokenT< reco::GenParticleCollection > genParticlesToken_
virtual const Candidate * mother(size_type i=0) const =0
return pointer to mother
transient_vector_type::const_iterator const_iterator
const GenParticleRefVector & getbHadrons() const
Return a vector of GenParticleRef&#39;s to b hadrons clustered inside the jet.
std::string getParticleName(int id) const
virtual double pt() const =0
transverse momentum
bool verbose
bool isMesonPdgId(const int flavour, const int pdgId) const
Check the pdgId if it represents a meson of particular flavour.
virtual size_type numberOfMothers() const =0
number of mothers (zero or one in most of but not all the cases)
const edm::EDPutTokenT< std::vector< int > > bHadronIdToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:526
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
const edm::EDPutTokenT< std::vector< int > > leptonIndexToken_
bool checkForLoop(std::vector< const reco::Candidate *> &particleChain, const reco::Candidate *particle) const
int pdgId() const final
PDG identifier.
virtual size_type numberOfDaughters() const =0
number of daughters
const edm::EDPutTokenT< std::vector< int > > leptonHadronIndexToken_
int idInList(std::vector< const reco::Candidate *> particleList, const reco::Candidate *particle) const
Check if the cpecified particle is already in the list of particles.
bool isNeutralPdg(int pdgId) const
Check whether a given pdgId represents neutral particle.
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
const edm::EDPutTokenT< std::vector< int > > jetIndexToken_
Class storing the jet flavour information.
const std::string flavourStr_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: value.py:1
const bool onlyJetClusteredHadrons_
const edm::EDPutTokenT< std::vector< int > > leptonViaTauToken_
virtual int pdgId() const =0
PDG identifier.
edm::RefVector< GenParticleCollection > GenParticleRefVector
vector of reference to GenParticle in the same collection
int analyzeMothers(const reco::Candidate *thisParticle, int &topDaughterQId, int &topBarDaughterQId, std::vector< const reco::Candidate *> &hadMothers, std::vector< std::vector< int >> &hadMothersIndices, std::set< const reco::Candidate *> *analyzedParticles, const int prevPartIndex) const
do a recursive search for the mother particles until the b-quark is found or the absolute mother is f...
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
description of the run-time parameters
OrphanHandle< PROD > emplace(EDPutTokenT< PROD > token, Args &&... args)
puts a new product
Definition: Event.h:430
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
void add(std::string const &label, ParameterSetDescription const &psetDescription)
fixed size matrix
HLT enums.
static int position[264][3]
Definition: ReadPGInfo.cc:289
bool hasHadronDaughter(const int flavour, const reco::Candidate *thisParticle) const
Check if the particle has bHadron among daughters.
GenHFHadronMatcher(const edm::ParameterSet &)
bool putMotherIndex(std::vector< std::vector< int >> &hadMothersIndices, int partIndex, int mothIndex) const
puts mother index to the list of mothers of particle, if it isn&#39;t there already
const GenParticleRefVector & getcHadrons() const
Return a vector of GenParticleRef&#39;s to c hadrons clustered inside the jet.
const edm::EDGetTokenT< reco::JetFlavourInfoMatchingCollection > jetFlavourInfosToken_
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
int findInMothers(int idx, std::vector< int > &mothChains, const std::vector< std::vector< int >> &hadMothersIndices, const std::vector< reco::GenParticle > &hadMothers, int status, int pdgId, bool pdgAbs, int stopId, int firstLast, bool verbose) const
helper function to find indices of particles with particular pdgId and status from the list of mother...
std::vector< int > findHadronJets(const reco::GenParticleCollection *genParticles, const reco::JetFlavourInfoMatchingCollection *jetFlavourInfos, std::vector< int > &hadIndex, std::vector< reco::GenParticle > &hadMothersGenPart, std::vector< std::vector< int >> &hadMothersIndices, std::vector< int > &hadLeptonIndex, std::vector< int > &hadLeptonHadIndex, std::vector< int > &hadLeptonViaTau, std::vector< int > &hadFlavour, std::vector< int > &hadFromTopWeakDecay, std::vector< int > &hadBHadronId) const
identify the jets that contain b-hadrons
int flavourSign(const int pdgId) const
Sign of the flavour (matter/antimatter)
const edm::EDPutTokenT< std::vector< int > > fromTopWeakDecayToken_
bool isBaryonPdgId(const int flavour, const int pdgId) const
Check the pdgId if it represents a baryon of particular flavour.
def move(src, dest)
Definition: eostools.py:511
Finds the origin of each heavy flavour hadron and associated jets to it.
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
bool isHadron(const int flavour, const reco::Candidate *thisParticle) const
Check the pdgId of a given particle if it is a hadron.
const edm::EDPutTokenT< std::vector< int > > indexToken_
const edm::EDPutTokenT< std::vector< int > > flavourToken_