CMS 3D CMS Logo

JetFlavourClustering.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: JetFlavourClustering
4 // Class: JetFlavourClustering
5 //
72 //
73 // Original Author: Dinko Ferencek
74 // Created: Wed Nov 6 00:49:55 CET 2013
75 //
76 //
77 
78 // system include files
79 #include <memory>
80 
81 // user include files
84 
87 
89 
99 
100 #include "fastjet/JetDefinition.hh"
101 #include "fastjet/ClusterSequence.hh"
102 #include "fastjet/Selector.hh"
103 #include "fastjet/PseudoJet.hh"
104 
105 //
106 // constants, enums and typedefs
107 //
108 typedef std::shared_ptr<fastjet::ClusterSequence> ClusterSequencePtr;
109 typedef std::shared_ptr<fastjet::JetDefinition> JetDefPtr;
110 
111 //
112 // class declaration
113 //
114 class GhostInfo : public fastjet::PseudoJet::UserInfoBase {
115 public:
116  GhostInfo(const bool& isHadron,
117  const bool& isbHadron,
118  const bool& isParton,
119  const bool& isLepton,
122  m_type = 0;
123  if (isHadron)
124  m_type |= (1 << 0);
125  if (isbHadron)
126  m_type |= (1 << 1);
127  if (isParton)
128  m_type |= (1 << 2);
129  if (isLepton)
130  m_type |= (1 << 3);
131  }
132 
133  const bool isHadron() const { return (m_type & (1 << 0)); }
134  const bool isbHadron() const { return (m_type & (1 << 1)); }
135  const bool isParton() const { return (m_type & (1 << 2)); }
136  const bool isLepton() const { return (m_type & (1 << 3)); }
137  const reco::GenParticleRef& particleRef() const { return m_particleRef; }
138 
139 protected:
141  int m_type;
142 };
143 
145 public:
146  explicit JetFlavourClustering(const edm::ParameterSet&);
147  ~JetFlavourClustering() override;
148 
149  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
150 
151 private:
152  void produce(edm::Event&, const edm::EventSetup&) override;
153 
155  const double ghostRescaling,
156  const bool isHadron,
157  const bool isbHadron,
158  const bool isParton,
159  const bool isLepton,
160  std::vector<fastjet::PseudoJet>& constituents);
161 
163  const std::vector<fastjet::PseudoJet>& matchedJets,
164  std::vector<int>& matchedIndices);
166  const edm::Handle<edm::View<reco::Jet>>& matchedJets,
167  std::vector<int>& matchedIndices);
168  void matchSubjets(const std::vector<int>& groomedIndices,
169  const edm::Handle<edm::View<reco::Jet>>& groomedJets,
170  const edm::Handle<edm::View<reco::Jet>>& subjets,
171  std::vector<std::vector<int>>& matchedIndices);
172 
173  void setFlavours(const reco::GenParticleRefVector& clusteredbHadrons,
174  const reco::GenParticleRefVector& clusteredcHadrons,
175  const reco::GenParticleRefVector& clusteredPartons,
176  int& hadronFlavour,
177  int& partonFlavour);
178 
179  void assignToSubjets(const reco::GenParticleRefVector& clusteredParticles,
180  const edm::Handle<edm::View<reco::Jet>>& subjets,
181  const std::vector<int>& subjetIndices,
182  std::vector<reco::GenParticleRefVector>& assignedParticles);
183 
184  // ----------member data ---------------------------
185  const edm::EDGetTokenT<edm::View<reco::Jet>> jetsToken_; // Input jet collection
193 
195  const double rParam_;
196  const double jetPtMin_;
197  const double ghostRescaling_;
198  const double relPtTolerance_;
200  const bool useSubjets_;
201 
202  const bool useLeptons_;
203 
206 };
207 
208 //
209 // static data member definitions
210 //
211 
212 //
213 // constructors and destructor
214 //
216  : jetsToken_(consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("jets"))),
217  bHadronsToken_(consumes<reco::GenParticleRefVector>(iConfig.getParameter<edm::InputTag>("bHadrons"))),
218  cHadronsToken_(consumes<reco::GenParticleRefVector>(iConfig.getParameter<edm::InputTag>("cHadrons"))),
219  partonsToken_(consumes<reco::GenParticleRefVector>(iConfig.getParameter<edm::InputTag>("partons"))),
220  jetAlgorithm_(iConfig.getParameter<std::string>("jetAlgorithm")),
221  rParam_(iConfig.getParameter<double>("rParam")),
222 
223  jetPtMin_(
224  0.), // hardcoded to 0. since we simply want to recluster all input jets which already had some PtMin applied
225  ghostRescaling_(iConfig.exists("ghostRescaling") ? iConfig.getParameter<double>("ghostRescaling") : 1e-18),
226  relPtTolerance_(
227  iConfig.exists("relPtTolerance")
228  ? iConfig.getParameter<double>("relPtTolerance")
229  : 1e-03), // 0.1% relative difference in Pt should be sufficient to detect possible misconfigurations
230  hadronFlavourHasPriority_(iConfig.getParameter<bool>("hadronFlavourHasPriority")),
231  useSubjets_(iConfig.exists("groomedJets") && iConfig.exists("subjets")),
232  useLeptons_(iConfig.exists("leptons"))
233 
234 {
235  // register your products
236  produces<reco::JetFlavourInfoMatchingCollection>();
237  if (iConfig.existsAs<edm::InputTag>("weights"))
238  weightsToken_ = consumes<edm::ValueMap<float>>(iConfig.getParameter<edm::InputTag>("weights"));
239 
240  if (useSubjets_)
241  produces<reco::JetFlavourInfoMatchingCollection>("SubJets");
242 
243  // set jet algorithm
244  if (jetAlgorithm_ == "Kt")
245  fjJetDefinition_ = std::make_shared<fastjet::JetDefinition>(fastjet::kt_algorithm, rParam_);
246  else if (jetAlgorithm_ == "CambridgeAachen")
247  fjJetDefinition_ = std::make_shared<fastjet::JetDefinition>(fastjet::cambridge_algorithm, rParam_);
248  else if (jetAlgorithm_ == "AntiKt")
249  fjJetDefinition_ = std::make_shared<fastjet::JetDefinition>(fastjet::antikt_algorithm, rParam_);
250  else
251  throw cms::Exception("InvalidJetAlgorithm") << "Jet clustering algorithm is invalid: " << jetAlgorithm_
252  << ", use CambridgeAachen | Kt | AntiKt" << std::endl;
253 
254  if (useSubjets_) {
255  groomedJetsToken_ = consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("groomedJets"));
256  subjetsToken_ = consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("subjets"));
257  }
258  if (useLeptons_) {
259  leptonsToken_ = consumes<reco::GenParticleRefVector>(iConfig.getParameter<edm::InputTag>("leptons"));
260  }
261 }
262 
264  // do anything here that needs to be done at desctruction time
265  // (e.g. close files, deallocate resources etc.)
266 }
267 
268 //
269 // member functions
270 //
271 
272 // ------------ method called to produce the data ------------
275  iEvent.getByToken(jetsToken_, jets);
276 
279  if (useSubjets_) {
280  iEvent.getByToken(groomedJetsToken_, groomedJets);
281  iEvent.getByToken(subjetsToken_, subjets);
282  }
283 
285  iEvent.getByToken(bHadronsToken_, bHadrons);
286 
288  iEvent.getByToken(cHadronsToken_, cHadrons);
289 
291  iEvent.getByToken(partonsToken_, partons);
292 
295  iEvent.getByToken(weightsToken_, weights);
296 
298  if (useLeptons_)
299  iEvent.getByToken(leptonsToken_, leptons);
300 
301  auto jetFlavourInfos = std::make_unique<reco::JetFlavourInfoMatchingCollection>(reco::JetRefBaseProd(jets));
302  std::unique_ptr<reco::JetFlavourInfoMatchingCollection> subjetFlavourInfos;
303  if (useSubjets_)
304  subjetFlavourInfos = std::make_unique<reco::JetFlavourInfoMatchingCollection>(reco::JetRefBaseProd(subjets));
305 
306  // vector of constituents for reclustering jets and "ghosts"
307  std::vector<fastjet::PseudoJet> fjInputs;
308  unsigned int reserve = jets->size() * 128 + bHadrons->size() + cHadrons->size() + partons->size();
309  if (useLeptons_)
310  reserve += leptons->size();
311  fjInputs.reserve(reserve);
312  // loop over all input jets and collect all their constituents
313  for (edm::View<reco::Jet>::const_iterator it = jets->begin(); it != jets->end(); ++it) {
314  std::vector<edm::Ptr<reco::Candidate>> constituents = it->getJetConstituents();
315  std::vector<edm::Ptr<reco::Candidate>>::const_iterator m;
316  for (m = constituents.begin(); m != constituents.end(); ++m) {
317  reco::CandidatePtr constit = *m;
318  if (!constit.isNonnull() || !constit.isAvailable()) {
319  edm::LogError("MissingJetConstituent") << "Jet constituent required for jet reclustering is missing. "
320  "Reclustered jets are not guaranteed to reproduce the original jets!";
321  continue;
322  }
323  if (constit->pt() == 0) {
324  edm::LogWarning("NullTransverseMomentum") << "dropping input candidate with pt=0";
325  continue;
326  }
327  if (it->isWeighted()) {
329  throw cms::Exception("MissingConstituentWeight")
330  << "JetFlavourClustering: No weights (e.g. PUPPI) given for weighted jet collection" << std::endl;
331  float w = (*weights)[constit];
332  fjInputs.push_back(
333  fastjet::PseudoJet(constit->px() * w, constit->py() * w, constit->pz() * w, constit->energy() * w));
334  } else {
335  fjInputs.push_back(fastjet::PseudoJet(constit->px(), constit->py(), constit->pz(), constit->energy()));
336  }
337  }
338  }
339  // insert "ghost" b hadrons in the vector of constituents
340  insertGhosts(bHadrons, ghostRescaling_, true, true, false, false, fjInputs);
341  // insert "ghost" c hadrons in the vector of constituents
342  insertGhosts(cHadrons, ghostRescaling_, true, false, false, false, fjInputs);
343  // insert "ghost" partons in the vector of constituents
344  insertGhosts(partons, ghostRescaling_, false, false, true, false, fjInputs);
345  // if used, insert "ghost" leptons in the vector of constituents
346  if (useLeptons_)
347  insertGhosts(leptons, ghostRescaling_, false, false, false, true, fjInputs);
348 
349  // define jet clustering sequence
350  fjClusterSeq_ = std::make_shared<fastjet::ClusterSequence>(fjInputs, *fjJetDefinition_);
351  // recluster jet constituents and inserted "ghosts"
352  std::vector<fastjet::PseudoJet> inclusiveJets = fastjet::sorted_by_pt(fjClusterSeq_->inclusive_jets(jetPtMin_));
353 
354  if (inclusiveJets.size() < jets->size())
355  edm::LogError("TooFewReclusteredJets")
356  << "There are fewer reclustered (" << inclusiveJets.size() << ") than original jets (" << jets->size()
357  << "). Please check that the jet algorithm and jet size match those used for the original jet collection.";
358 
359  // match reclustered and original jets
360  std::vector<int> reclusteredIndices;
361  matchReclusteredJets(jets, inclusiveJets, reclusteredIndices);
362 
363  // match groomed and original jets
364  std::vector<int> groomedIndices;
365  if (useSubjets_) {
366  if (groomedJets->size() > jets->size())
367  edm::LogError("TooManyGroomedJets")
368  << "There are more groomed (" << groomedJets->size() << ") than original jets (" << jets->size()
369  << "). Please check that the two jet collections belong to each other.";
370 
371  matchGroomedJets(jets, groomedJets, groomedIndices);
372  }
373 
374  // match subjets and original jets
375  std::vector<std::vector<int>> subjetIndices;
376  if (useSubjets_) {
377  matchSubjets(groomedIndices, groomedJets, subjets, subjetIndices);
378  }
379 
380  // determine jet flavour
381  for (size_t i = 0; i < jets->size(); ++i) {
382  reco::GenParticleRefVector clusteredbHadrons;
383  reco::GenParticleRefVector clusteredcHadrons;
384  reco::GenParticleRefVector clusteredPartons;
385  reco::GenParticleRefVector clusteredLeptons;
386 
387  // if matching reclustered to original jets failed
388  if (reclusteredIndices.at(i) < 0) {
389  // set an empty JetFlavourInfo for this jet
390  (*jetFlavourInfos)[jets->refAt(i)] =
391  reco::JetFlavourInfo(clusteredbHadrons, clusteredcHadrons, clusteredPartons, clusteredLeptons, 0, 0);
392  } else if (jets->at(i).pt() == 0) {
393  edm::LogWarning("NullTransverseMomentum")
394  << "The original jet " << i << " has Pt=0. This is not expected so the jet will be skipped.";
395 
396  // set an empty JetFlavourInfo for this jet
397  (*jetFlavourInfos)[jets->refAt(i)] =
398  reco::JetFlavourInfo(clusteredbHadrons, clusteredcHadrons, clusteredPartons, clusteredLeptons, 0, 0);
399 
400  // if subjets are used
401  if (useSubjets_ && !subjetIndices.at(i).empty()) {
402  // loop over subjets
403  for (size_t sj = 0; sj < subjetIndices.at(i).size(); ++sj) {
404  // set an empty JetFlavourInfo for this subjet
405  (*subjetFlavourInfos)[subjets->refAt(subjetIndices.at(i).at(sj))] =
410  0,
411  0);
412  }
413  }
414  } else {
415  // since the "ghosts" are extremely soft, the configuration and ordering of the reclustered and original jets should in principle stay the same
416  if ((std::abs(inclusiveJets.at(reclusteredIndices.at(i)).pt() - jets->at(i).pt()) / jets->at(i).pt()) >
417  relPtTolerance_) {
418  if (jets->at(i).pt() < 10.) // special handling for low-Pt jets (Pt<10 GeV)
419  edm::LogWarning("JetPtMismatchAtLowPt")
420  << "The reclustered and original jet " << i << " have different Pt's ("
421  << inclusiveJets.at(reclusteredIndices.at(i)).pt() << " vs " << jets->at(i).pt()
422  << " GeV, respectively).\n"
423  << "Please check that the jet algorithm and jet size match those used for the original jet collection "
424  "and also make sure the original jets are uncorrected. In addition, make sure you are not using "
425  "CaloJets which are presently not supported.\n"
426  << "Since the mismatch is at low Pt (Pt<10 GeV), it is ignored and only a warning is issued.\n"
427  << "\nIn extremely rare instances the mismatch could be caused by a difference in the machine precision "
428  "in which case make sure the original jet collection is produced and reclustering is performed in the "
429  "same job.";
430  else
431  edm::LogError("JetPtMismatch")
432  << "The reclustered and original jet " << i << " have different Pt's ("
433  << inclusiveJets.at(reclusteredIndices.at(i)).pt() << " vs " << jets->at(i).pt()
434  << " GeV, respectively).\n"
435  << "Please check that the jet algorithm and jet size match those used for the original jet collection "
436  "and also make sure the original jets are uncorrected. In addition, make sure you are not using "
437  "CaloJets which are presently not supported.\n"
438  << "\nIn extremely rare instances the mismatch could be caused by a difference in the machine precision "
439  "in which case make sure the original jet collection is produced and reclustering is performed in the "
440  "same job.";
441  }
442 
443  // get jet constituents (sorted by Pt)
444  std::vector<fastjet::PseudoJet> constituents =
445  fastjet::sorted_by_pt(inclusiveJets.at(reclusteredIndices.at(i)).constituents());
446 
447  // loop over jet constituents and try to find "ghosts"
448  for (std::vector<fastjet::PseudoJet>::const_iterator it = constituents.begin(); it != constituents.end(); ++it) {
449  if (!it->has_user_info())
450  continue; // skip if not a "ghost"
451 
452  // "ghost" hadron
453  if (it->user_info<GhostInfo>().isHadron()) {
454  // "ghost" b hadron
455  if (it->user_info<GhostInfo>().isbHadron())
456  clusteredbHadrons.push_back(it->user_info<GhostInfo>().particleRef());
457  // "ghost" c hadron
458  else
459  clusteredcHadrons.push_back(it->user_info<GhostInfo>().particleRef());
460  }
461  // "ghost" parton
462  else if (it->user_info<GhostInfo>().isParton())
463  clusteredPartons.push_back(it->user_info<GhostInfo>().particleRef());
464  // "ghost" lepton
465  else if (it->user_info<GhostInfo>().isLepton())
466  clusteredLeptons.push_back(it->user_info<GhostInfo>().particleRef());
467  }
468 
469  int hadronFlavour = 0; // default hadron flavour set to 0 (= undefined)
470  int partonFlavour = 0; // default parton flavour set to 0 (= undefined)
471 
472  // set hadron- and parton-based flavours
473  setFlavours(clusteredbHadrons, clusteredcHadrons, clusteredPartons, hadronFlavour, partonFlavour);
474 
475  // set the JetFlavourInfo for this jet
476  (*jetFlavourInfos)[jets->refAt(i)] = reco::JetFlavourInfo(
477  clusteredbHadrons, clusteredcHadrons, clusteredPartons, clusteredLeptons, hadronFlavour, partonFlavour);
478  }
479 
480  // if subjets are used, determine their flavour
481  if (useSubjets_) {
482  if (subjetIndices.at(i).empty())
483  continue; // continue if the original jet does not have subjets assigned
484 
485  // define vectors of GenParticleRefVectors for hadrons and partons assigned to different subjets
486  std::vector<reco::GenParticleRefVector> assignedbHadrons(subjetIndices.at(i).size(),
488  std::vector<reco::GenParticleRefVector> assignedcHadrons(subjetIndices.at(i).size(),
490  std::vector<reco::GenParticleRefVector> assignedPartons(subjetIndices.at(i).size(), reco::GenParticleRefVector());
491  std::vector<reco::GenParticleRefVector> assignedLeptons(subjetIndices.at(i).size(), reco::GenParticleRefVector());
492 
493  // loop over clustered b hadrons and assign them to different subjets based on smallest dR
494  assignToSubjets(clusteredbHadrons, subjets, subjetIndices.at(i), assignedbHadrons);
495  // loop over clustered c hadrons and assign them to different subjets based on smallest dR
496  assignToSubjets(clusteredcHadrons, subjets, subjetIndices.at(i), assignedcHadrons);
497  // loop over clustered partons and assign them to different subjets based on smallest dR
498  assignToSubjets(clusteredPartons, subjets, subjetIndices.at(i), assignedPartons);
499  // if used, loop over clustered leptons and assign them to different subjets based on smallest dR
500  if (useLeptons_)
501  assignToSubjets(clusteredLeptons, subjets, subjetIndices.at(i), assignedLeptons);
502 
503  // loop over subjets and determine their flavour
504  for (size_t sj = 0; sj < subjetIndices.at(i).size(); ++sj) {
505  int subjetHadronFlavour = 0; // default hadron flavour set to 0 (= undefined)
506  int subjetPartonFlavour = 0; // default parton flavour set to 0 (= undefined)
507 
508  // set hadron- and parton-based flavours
509  setFlavours(assignedbHadrons.at(sj),
510  assignedcHadrons.at(sj),
511  assignedPartons.at(sj),
512  subjetHadronFlavour,
513  subjetPartonFlavour);
514 
515  // set the JetFlavourInfo for this subjet
516  (*subjetFlavourInfos)[subjets->refAt(subjetIndices.at(i).at(sj))] =
517  reco::JetFlavourInfo(assignedbHadrons.at(sj),
518  assignedcHadrons.at(sj),
519  assignedPartons.at(sj),
520  assignedLeptons.at(sj),
521  subjetHadronFlavour,
522  subjetPartonFlavour);
523  }
524  }
525  }
526 
527  //deallocate only at the end of the event processing
528  fjClusterSeq_.reset();
529 
530  // put jet flavour infos in the event
532  // put subjet flavour infos in the event
533  if (useSubjets_)
534  iEvent.put(std::move(subjetFlavourInfos), "SubJets");
535 }
536 
537 // ------------ method that inserts "ghost" particles in the vector of jet constituents ------------
539  const double ghostRescaling,
540  const bool isHadron,
541  const bool isbHadron,
542  const bool isParton,
543  const bool isLepton,
544  std::vector<fastjet::PseudoJet>& constituents) {
545  // insert "ghost" particles in the vector of jet constituents
546  for (reco::GenParticleRefVector::const_iterator it = particles->begin(); it != particles->end(); ++it) {
547  if ((*it)->pt() == 0) {
548  edm::LogInfo("NullTransverseMomentum") << "dropping input ghost candidate with pt=0";
549  continue;
550  }
551  fastjet::PseudoJet p((*it)->px(), (*it)->py(), (*it)->pz(), (*it)->energy());
552  p *= ghostRescaling; // rescale particle momentum
553  p.set_user_info(new GhostInfo(isHadron, isbHadron, isParton, isLepton, *it));
554  constituents.push_back(p);
555  }
556 }
557 
558 // ------------ method that matches reclustered and original jets based on minimum dR ------------
560  const std::vector<fastjet::PseudoJet>& reclusteredJets,
561  std::vector<int>& matchedIndices) {
562  std::vector<bool> matchedLocks(reclusteredJets.size(), false);
563 
564  for (size_t j = 0; j < jets->size(); ++j) {
565  double matchedDR2 = 1e9;
566  int matchedIdx = -1;
567 
568  for (size_t rj = 0; rj < reclusteredJets.size(); ++rj) {
569  if (matchedLocks.at(rj))
570  continue; // skip jets that have already been matched
571 
572  double tempDR2 = reco::deltaR2(jets->at(j).rapidity(),
573  jets->at(j).phi(),
574  reclusteredJets.at(rj).rapidity(),
575  reclusteredJets.at(rj).phi_std());
576  if (tempDR2 < matchedDR2) {
577  matchedDR2 = tempDR2;
578  matchedIdx = rj;
579  }
580  }
581 
582  if (matchedIdx >= 0) {
583  if (matchedDR2 > rParam_ * rParam_) {
584  edm::LogError("JetMatchingFailed") << "Matched reclustered jet " << matchedIdx << " and original jet " << j
585  << " are separated by dR=" << sqrt(matchedDR2)
586  << " which is greater than the jet size R=" << rParam_ << ".\n"
587  << "This is not expected so please check that the jet algorithm and jet "
588  "size match those used for the original jet collection.";
589  } else
590  matchedLocks.at(matchedIdx) = true;
591  } else
592  edm::LogError("JetMatchingFailed") << "Matching reclustered to original jets failed. Please check that the jet "
593  "algorithm and jet size match those used for the original jet collection.";
594 
595  matchedIndices.push_back(matchedIdx);
596  }
597 }
598 
599 // ------------ method that matches groomed and original jets based on minimum dR ------------
601  const edm::Handle<edm::View<reco::Jet>>& groomedJets,
602  std::vector<int>& matchedIndices) {
603  std::vector<bool> jetLocks(jets->size(), false);
604  std::vector<int> jetIndices;
605 
606  for (size_t gj = 0; gj < groomedJets->size(); ++gj) {
607  double matchedDR2 = 1e9;
608  int matchedIdx = -1;
609 
610  if (groomedJets->at(gj).pt() > 0.) // skip pathological cases of groomed jets with Pt=0
611  {
612  for (size_t j = 0; j < jets->size(); ++j) {
613  if (jetLocks.at(j))
614  continue; // skip jets that have already been matched
615 
616  double tempDR2 = reco::deltaR2(
617  jets->at(j).rapidity(), jets->at(j).phi(), groomedJets->at(gj).rapidity(), groomedJets->at(gj).phi());
618  if (tempDR2 < matchedDR2) {
619  matchedDR2 = tempDR2;
620  matchedIdx = j;
621  }
622  }
623  }
624 
625  if (matchedIdx >= 0) {
626  if (matchedDR2 > rParam_ * rParam_) {
627  edm::LogWarning("MatchedJetsFarApart")
628  << "Matched groomed jet " << gj << " and original jet " << matchedIdx
629  << " are separated by dR=" << sqrt(matchedDR2) << " which is greater than the jet size R=" << rParam_
630  << ".\n"
631  << "This is not expected so the matching of these two jets has been discarded. Please check that the two "
632  "jet collections belong to each other.";
633  matchedIdx = -1;
634  } else
635  jetLocks.at(matchedIdx) = true;
636  }
637  jetIndices.push_back(matchedIdx);
638  }
639 
640  for (size_t j = 0; j < jets->size(); ++j) {
641  std::vector<int>::iterator matchedIndex = std::find(jetIndices.begin(), jetIndices.end(), j);
642 
643  matchedIndices.push_back(matchedIndex != jetIndices.end() ? std::distance(jetIndices.begin(), matchedIndex) : -1);
644  }
645 }
646 
647 // ------------ method that matches subjets and original jets ------------
648 void JetFlavourClustering::matchSubjets(const std::vector<int>& groomedIndices,
649  const edm::Handle<edm::View<reco::Jet>>& groomedJets,
650  const edm::Handle<edm::View<reco::Jet>>& subjets,
651  std::vector<std::vector<int>>& matchedIndices) {
652  for (size_t g = 0; g < groomedIndices.size(); ++g) {
653  std::vector<int> subjetIndices;
654 
655  if (groomedIndices.at(g) >= 0) {
656  for (size_t s = 0; s < groomedJets->at(groomedIndices.at(g)).numberOfDaughters(); ++s) {
657  const edm::Ptr<reco::Candidate>& subjet = groomedJets->at(groomedIndices.at(g)).daughterPtr(s);
658 
659  for (size_t sj = 0; sj < subjets->size(); ++sj) {
660  if (subjet == edm::Ptr<reco::Candidate>(subjets->ptrAt(sj))) {
661  subjetIndices.push_back(sj);
662  break;
663  }
664  }
665  }
666 
667  if (subjetIndices.empty())
668  edm::LogError("SubjetMatchingFailed") << "Matching subjets to original jets failed. Please check that the "
669  "groomed jet and subjet collections belong to each other.";
670 
671  matchedIndices.push_back(subjetIndices);
672  } else
673  matchedIndices.push_back(subjetIndices);
674  }
675 }
676 
677 // ------------ method that sets hadron- and parton-based flavours ------------
679  const reco::GenParticleRefVector& clusteredcHadrons,
680  const reco::GenParticleRefVector& clusteredPartons,
681  int& hadronFlavour,
682  int& partonFlavour) {
683  reco::GenParticleRef hardestParton;
684  reco::GenParticleRef hardestLightParton;
685  reco::GenParticleRef flavourParton;
686 
687  // loop over clustered partons (already sorted by Pt)
688  for (reco::GenParticleRefVector::const_iterator it = clusteredPartons.begin(); it != clusteredPartons.end(); ++it) {
689  // hardest parton
690  if (hardestParton.isNull())
691  hardestParton = (*it);
692  // hardest light-flavour parton
693  if (hardestLightParton.isNull()) {
694  if (CandMCTagUtils::isLightParton(*(*it)))
695  hardestLightParton = (*it);
696  }
697  // c flavour
698  if (flavourParton.isNull() && (std::abs((*it)->pdgId()) == 4))
699  flavourParton = (*it);
700  // b flavour gets priority
701  if (std::abs((*it)->pdgId()) == 5) {
702  if (flavourParton.isNull())
703  flavourParton = (*it);
704  else if (std::abs(flavourParton->pdgId()) != 5)
705  flavourParton = (*it);
706  }
707  }
708 
709  // set hadron-based flavour
710  if (!clusteredbHadrons.empty())
711  hadronFlavour = 5;
712  else if (!clusteredcHadrons.empty() && clusteredbHadrons.empty())
713  hadronFlavour = 4;
714  // set parton-based flavour
715  if (flavourParton.isNull()) {
716  if (hardestParton.isNonnull())
717  partonFlavour = hardestParton->pdgId();
718  } else
719  partonFlavour = flavourParton->pdgId();
720 
721  // if enabled, check for conflicts between hadron- and parton-based flavours and give priority to the hadron-based flavour
723  if (hadronFlavour == 0 && (std::abs(partonFlavour) == 4 || std::abs(partonFlavour) == 5))
724  partonFlavour = (hardestLightParton.isNonnull() ? hardestLightParton->pdgId() : 0);
725  else if (hadronFlavour != 0 && std::abs(partonFlavour) != hadronFlavour)
727  }
728 }
729 
730 // ------------ method that assigns clustered particles to subjets ------------
732  const edm::Handle<edm::View<reco::Jet>>& subjets,
733  const std::vector<int>& subjetIndices,
734  std::vector<reco::GenParticleRefVector>& assignedParticles) {
735  // loop over clustered particles and assign them to different subjets based on smallest dR
736  for (reco::GenParticleRefVector::const_iterator it = clusteredParticles.begin(); it != clusteredParticles.end();
737  ++it) {
738  std::vector<double> dR2toSubjets;
739 
740  for (size_t sj = 0; sj < subjetIndices.size(); ++sj)
741  dR2toSubjets.push_back(reco::deltaR2((*it)->rapidity(),
742  (*it)->phi(),
743  subjets->at(subjetIndices.at(sj)).rapidity(),
744  subjets->at(subjetIndices.at(sj)).phi()));
745 
746  // find the closest subjet
747  int closestSubjetIdx =
748  std::distance(dR2toSubjets.begin(), std::min_element(dR2toSubjets.begin(), dR2toSubjets.end()));
749 
750  assignedParticles.at(closestSubjetIdx).push_back(*it);
751  }
752 }
753 
754 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
756  //The following says we do not know what parameters are allowed so do no validation
757  // Please change this to state exactly what you do use, even if it is no parameters
759  desc.setUnknown();
760  descriptions.addDefault(desc);
761 }
762 
763 //define this as a plug-in
edm::EDGetTokenT< reco::GenParticleRefVector > leptonsToken_
std::shared_ptr< fastjet::JetDefinition > JetDefPtr
bool isLightParton(const reco::Candidate &c)
Definition: CandMCTag.cc:55
bool empty() const
Is the RefVector empty.
Definition: RefVector.h:99
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
const bool isLepton() const
Clusters hadrons, partons, and jet contituents to determine the jet flavour.
T w() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void insertGhosts(const edm::Handle< reco::GenParticleRefVector > &particles, const double ghostRescaling, const bool isHadron, const bool isbHadron, const bool isParton, const bool isLepton, std::vector< fastjet::PseudoJet > &constituents)
const bool isParton() const
GhostInfo(const bool &isHadron, const bool &isbHadron, const bool &isParton, const bool &isLepton, const reco::GenParticleRef &particleRef)
edm::EDGetTokenT< edm::View< reco::Jet > > subjetsToken_
void assignToSubjets(const reco::GenParticleRefVector &clusteredParticles, const edm::Handle< edm::View< reco::Jet >> &subjets, const std::vector< int > &subjetIndices, std::vector< reco::GenParticleRefVector > &assignedParticles)
const reco::GenParticleRef m_particleRef
constexpr bool isUninitialized() const noexcept
Definition: EDGetToken.h:99
void setFlavours(const reco::GenParticleRefVector &clusteredbHadrons, const reco::GenParticleRefVector &clusteredcHadrons, const reco::GenParticleRefVector &clusteredPartons, int &hadronFlavour, int &partonFlavour)
const edm::EDGetTokenT< reco::GenParticleRefVector > bHadronsToken_
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:238
bool isLepton(const Candidate &part)
Definition: pdgIdUtils.h:13
bool existsAs(std::string const &parameterName, bool trackiness=true) const
checks if a parameter exists as a given type
Definition: ParameterSet.h:172
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
edm::EDGetTokenT< edm::ValueMap< float > > weightsToken_
bool isAvailable() const
Definition: Ptr.h:230
const edm::EDGetTokenT< reco::GenParticleRefVector > partonsToken_
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 g
Definition: Activities.doc:4
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::RefToBaseProd< reco::Jet > JetRefBaseProd
Definition: JetCollection.h:13
const edm::EDGetTokenT< reco::GenParticleRefVector > cHadronsToken_
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
void matchReclusteredJets(const edm::Handle< edm::View< reco::Jet >> &jets, const std::vector< fastjet::PseudoJet > &matchedJets, std::vector< int > &matchedIndices)
Definition: Jet.py:1
T sqrt(T t)
Definition: SSEVec.h:19
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:146
Class storing the jet flavour information.
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
JetFlavourClustering(const edm::ParameterSet &)
bool isNull() const
Checks for null.
Definition: Ref.h:235
void matchGroomedJets(const edm::Handle< edm::View< reco::Jet >> &jets, const edm::Handle< edm::View< reco::Jet >> &matchedJets, std::vector< int > &matchedIndices)
const std::string jetAlgorithm_
Log< level::Info, false > LogInfo
edm::RefVector< GenParticleCollection > GenParticleRefVector
vector of reference to GenParticle in the same collection
const reco::GenParticleRef & particleRef() const
std::shared_ptr< fastjet::ClusterSequence > ClusterSequencePtr
const edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken_
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
bool isParton(const reco::Candidate &c)
Definition: CandMCTag.cc:46
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
void produce(edm::Event &, const edm::EventSetup &) override
fixed size matrix
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
edm::EDGetTokenT< edm::View< reco::Jet > > groomedJetsToken_
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:67
void matchSubjets(const std::vector< int > &groomedIndices, const edm::Handle< edm::View< reco::Jet >> &groomedJets, const edm::Handle< edm::View< reco::Jet >> &subjets, std::vector< std::vector< int >> &matchedIndices)
Log< level::Warning, false > LogWarning
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
const bool isbHadron() const
ClusterSequencePtr fjClusterSeq_
def move(src, dest)
Definition: eostools.py:511
const bool isHadron() const