CMS 3D CMS Logo

HLTBTagPerformanceAnalyzer.cc
Go to the documentation of this file.
2 #include <algorithm>
3 #include <set>
4 
5 using namespace edm;
6 using namespace reco;
7 
8 // find the index of the object key of an association vector closest to a given
9 // jet, within a given distance
10 template <typename T, typename V>
12  int closest = -1;
13  for (unsigned int i = 0; i < association.size(); ++i) {
14  double d = ROOT::Math::VectorUtil::DeltaR(jet->momentum(), association[i].first->momentum());
15  if (d < distance) {
16  distance = d;
17  closest = i;
18  }
19  }
20  return closest;
21 }
22 
23 std::set<std::string> keepSetJet{"jetNSecondaryVertices",
24  "jetNSelectedTracks",
25  "jetNTracks",
26  "Jet_JP",
27  "chargedHadronEnergyFraction",
28  "neutralHadronEnergyFraction",
29  "photonEnergyFraction",
30  "electronEnergyFraction",
31  "muonEnergyFraction",
32  "chargedHadronMultiplicity",
33  "neutralHadronMultiplicity",
34  "photonMultiplicity",
35  "electronMultiplicity",
36  "muonMultiplicity",
37  "hadronMultiplicity",
38  "hadronPhotonMultiplicity",
39  "totalMultiplicity"};
40 
41 std::set<std::string> keepSetTrack{
42  "trackChi2", "trackNTotalHits", "trackNPixelHits", "trackSip3dVal", "trackSip3dSig",
43  "trackSip2dVal", "trackSip2dSig", "trackPtRel", "trackDeltaR", "trackPtRatio",
44  "trackSip3dSig_0", "trackSip3dSig_1", "trackSip3dSig_2", "trackSip3dSig_3", "trackMomentum",
45  "trackEta", "trackPhi", "trackDecayLenVal", "trackDecayLenSig", "trackJetDistVal",
46  "trackJetDistSig", "trackSumJetEtRatio", "trackSumJetDeltaR", "trackEtaRel"
47 
48 };
49 std::set<std::string> keepSetVtx{"vertexMass",
50  "vertexNTracks"
51  "vertexFitProb",
52  "vertexCategory",
53  "vertexEnergyRatio",
54  "vertexJetDeltaR",
55  "vertexBoostOverSqrtJetPt",
56  "flightDistance1dVal",
57  "flightDistance1dSig",
58  "flightDistance2dVal",
59  "flightDistance2dSig",
60  "flightDistance3dVal",
61  "flightDistance3dSig"};
62 std::set<std::string> keepSet;
63 
64 // constructors and destructor
66  mainFolder_ = iConfig.getParameter<std::string>("mainFolder");
67  hlTriggerResults_ = consumes<edm::TriggerResults>(iConfig.getParameter<InputTag>("TriggerResults"));
68  JetTagCollection_ =
69  edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag>>("JetTag"),
70  [this](edm::InputTag const &tag) { return mayConsume<reco::JetTagCollection>(tag); });
71  // shallowTagInfosTokenCalo_ =
72  // consumes<std::vector<reco::ShallowTagInfo> >
73  // (edm::InputTag("hltDeepCombinedSecondaryVertexBJetTagsInfosCalo"));
74  shallowTagInfosTokenPf_ =
75  consumes<std::vector<reco::ShallowTagInfo>>(edm::InputTag("hltDeepCombinedSecondaryVertexBJetTagsInfos"));
76  m_mcPartons = consumes<JetFlavourMatchingCollection>(iConfig.getParameter<InputTag>("mcPartons"));
77  hltPathNames_ = iConfig.getParameter<std::vector<std::string>>("HLTPathNames");
78  edm::ParameterSet mc = iConfig.getParameter<edm::ParameterSet>("mcFlavours");
79  m_mcLabels = mc.getParameterNamesForType<std::vector<unsigned int>>();
80 
82  m_mcPartons_Label = label.module;
83 
84  for (unsigned int i = 0; i < JetTagCollection_.size(); i++) {
85  EDConsumerBase::labelsForToken(JetTagCollection_[i], label);
86  JetTagCollection_Label.push_back(label.module);
87  }
88 
89  EDConsumerBase::labelsForToken(hlTriggerResults_, label);
90  hlTriggerResults_Label = label.module;
91 
92  for (unsigned int i = 0; i < m_mcLabels.size(); ++i)
93  m_mcFlavours.push_back(mc.getParameter<std::vector<unsigned int>>(m_mcLabels[i]));
94  m_mcMatching = m_mcPartons_Label != "none";
95 
96  m_mcRadius = 0.3;
97 
98  HCALSpecialsNames[HEP17] = "HEP17";
99  HCALSpecialsNames[HEP18] = "HEP18";
100  HCALSpecialsNames[HEM17] = "HEM17";
101 }
102 
104  // do anything here that needs to be done at desctruction time
105  // (e.g. close files, deallocate resources etc.)
106 }
107 
109  // Make a combined set of inputs avaiable
110  std::set_union(std::begin(keepSetJet),
114  std::inserter(keepSet, std::begin(keepSet)));
115  std::set_union(std::begin(keepSet),
116  std::end(keepSet),
119  std::inserter(keepSet, std::begin(keepSet)));
120  triggerConfChanged_ = true;
121  EDConsumerBase::labelsForToken(hlTriggerResults_, label);
122 
123  hltConfigProvider_.init(iRun, iSetup, label.process, triggerConfChanged_);
124  const std::vector<std::string> &allHltPathNames = hltConfigProvider_.triggerNames();
125 
126  // fill hltPathIndexs_ with the trigger number of each hltPathNames_
127  for (size_t trgs = 0; trgs < hltPathNames_.size(); trgs++) {
128  unsigned int found = 1;
129  int it_mem = -1;
130  for (size_t it = 0; it < allHltPathNames.size(); ++it) {
131  found = allHltPathNames.at(it).find(hltPathNames_[trgs]);
132  if (found == 0) {
133  it_mem = (int)it;
134  }
135  } // for allallHltPathNames
136  hltPathIndexs_.push_back(it_mem);
137  } // for hltPathNames_
138 
139  // fill _isfoundHLTs for each hltPathNames_
140  for (size_t trgs = 0; trgs < hltPathNames_.size(); trgs++) {
141  if (hltPathIndexs_[trgs] < 0) {
142  _isfoundHLTs.push_back(false);
143  } else {
144  _isfoundHLTs.push_back(true);
145  }
146  }
147 }
148 
150  bool trigRes = false;
151  bool MCOK = false;
152  using namespace edm;
153 
154  // get triggerResults
155  Handle<TriggerResults> TriggerResulsHandler;
157  if (hlTriggerResults_Label.empty() || hlTriggerResults_Label == "NULL") {
158  edm::LogInfo("NoTriggerResults") << "TriggerResults ==> Empty";
159  return;
160  }
161  iEvent.getByToken(hlTriggerResults_, TriggerResulsHandler);
162  if (TriggerResulsHandler.isValid())
163  trigRes = true;
164  if (!trigRes) {
165  edm::LogInfo("NoTriggerResults") << "TriggerResults ==> not readable";
166  return;
167  }
168  const TriggerResults &triggerResults = *(TriggerResulsHandler.product());
169 
170  // get partons
171  if (m_mcMatching && !m_mcPartons_Label.empty() && m_mcPartons_Label != "NULL") {
172  iEvent.getByToken(m_mcPartons, h_mcPartons);
173  if (h_mcPartons.isValid())
174  MCOK = true;
175  }
176 
177  // fill the 1D and 2D DQM plot
178  Handle<reco::JetTagCollection> JetTagHandler;
179  for (unsigned int ind = 0; ind < hltPathNames_.size(); ind++) {
180  bool BtagOK = false;
182  if (!_isfoundHLTs[ind])
183  continue; // if the hltPath is not in the event, skip the event
184  if (!triggerResults.accept(hltPathIndexs_[ind]))
185  continue; // if the hltPath was not accepted skip the event
186 
187  // get JetTagCollection
188  if (!JetTagCollection_Label[ind].empty() && JetTagCollection_Label[ind] != "NULL") {
189  iEvent.getByToken(JetTagCollection_[ind], JetTagHandler);
190  iEvent.getByToken(shallowTagInfosTokenPf_, shallowTagInfosPf);
191  // iEvent.getByToken(shallowTagInfosTokenCalo_,
192  // shallowTagInfosCalo);
193  if (JetTagHandler.isValid())
194  BtagOK = true;
195  }
196 
197  // fill JetTag map
198  if (BtagOK)
199  for (auto iter = JetTagHandler->begin(); iter != JetTagHandler->end(); iter++) {
200  JetTag.insert(JetTagMap::value_type(iter->first, iter->second));
201  }
202  else {
203  edm::LogInfo("NoCollection") << "Collection " << JetTagCollection_Label[ind] << " ==> not found";
204  return;
205  }
206  // fill Inputs for All
207  if (shallowTagInfosPf.isValid()) {
208  for (auto &info : *(shallowTagInfosPf)) {
209  TaggingVariableList vars = info.taggingVariables();
210  for (auto entry = vars.begin(); entry != vars.end(); ++entry) {
211  if (keepSet.find(TaggingVariableTokens[entry->first]) !=
212  keepSet.end()) { // if Input name in defined list to keep
213  try {
214  H1_.at(ind)[TaggingVariableTokens[entry->first]]->Fill(std::fmax(0.0, entry->second));
215  } catch (const std::exception &e) {
216  continue;
217  }
218  } else
219  continue;
220  }
221  }
222  } else {
223  edm::LogInfo("NoCollection") << "No shallowTagInfosPf collection";
224  }
225  // fill tagging
226  for (auto &BtagJT : JetTag) {
227  std::map<HCALSpecials, bool> inmodule;
228  inmodule[HEP17] = (BtagJT.first->phi() >= -0.87) && (BtagJT.first->phi() < -0.52) && (BtagJT.first->eta() > 1.3);
229  inmodule[HEP18] = (BtagJT.first->phi() >= -0.52) && (BtagJT.first->phi() < -0.17) && (BtagJT.first->eta() > 1.3);
230  inmodule[HEM17] = (BtagJT.first->phi() >= -0.87) && (BtagJT.first->phi() < -0.52) && (BtagJT.first->eta() < -1.3);
231 
232  // fill 1D btag plot for 'all'
233  H1_.at(ind)[JetTagCollection_Label[ind]]->Fill(std::fmax(0.0, BtagJT.second));
234  for (auto i : HCALSpecialsNames) {
235  if (inmodule[i.first])
236  H1mod_.at(ind)[JetTagCollection_Label[ind]][i.first]->Fill(std::fmax(0.0, BtagJT.second));
237  }
238  if (MCOK) {
239  int m = closestJet(BtagJT.first, *h_mcPartons, m_mcRadius);
240  unsigned int flavour = (m != -1) ? abs((*h_mcPartons)[m].second.getFlavour()) : 0;
241  for (unsigned int i = 0; i < m_mcLabels.size(); ++i) {
242  std::string flavour_str = m_mcLabels[i];
243  flavours_t flav_collection = m_mcFlavours[i];
244  auto it = std::find(flav_collection.begin(), flav_collection.end(), flavour);
245  if (it == flav_collection.end())
246  continue;
247  std::string label = JetTagCollection_Label[ind] + "__";
248  label += flavour_str;
249  H1_.at(ind)[label]->Fill(std::fmax(0.0, BtagJT.second)); // fill 1D btag plot for 'b,c,uds'
250  for (auto j : HCALSpecialsNames) {
251  if (inmodule[j.first])
252  H1mod_.at(ind)[label][j.first]->Fill(
253  std::fmax(0.0, BtagJT.second)); // fill 1D btag plot for 'b,c,uds' in
254  // modules (HEP17 etc.)
255  }
256  label = JetTagCollection_Label[ind] + "___";
257  label += flavour_str;
258  std::string labelEta = label;
259  std::string labelPhi = label;
260  std::string labelEtaPhi = label;
261  std::string labelEtaPhi_threshold = label;
262  label += "_disc_pT";
263  H2_.at(ind)[label]->Fill(std::fmax(0.0, BtagJT.second),
264  BtagJT.first->pt()); // fill 2D btag, jetPt plot for 'b,c,uds'
265  for (auto j : HCALSpecialsNames) {
266  if (inmodule[j.first])
267  H2mod_.at(ind)[label][j.first]->Fill(std::fmax(0.0, BtagJT.second), BtagJT.first->pt());
268  }
269  labelEta += "_disc_eta";
270  H2Eta_.at(ind)[labelEta]->Fill(std::fmax(0.0, BtagJT.second),
271  BtagJT.first->eta()); // fill 2D btag, jetEta plot for 'b,c,uds'
272  labelPhi += "_disc_phi";
273  H2Phi_.at(ind)[labelPhi]->Fill(std::fmax(0.0, BtagJT.second),
274  BtagJT.first->phi()); // fill 2D btag, jetPhi plot for 'b,c,uds'
275  labelEtaPhi += "_eta_phi";
276  H2EtaPhi_.at(ind)[labelEtaPhi]->Fill(BtagJT.first->eta(),
277  BtagJT.first->phi()); // fill 2D btag, jetPhi plot for 'b,c,uds'
278  labelEtaPhi_threshold += "_eta_phi_disc05";
279  if (BtagJT.second > 0.5) {
280  H2EtaPhi_threshold_.at(ind)[labelEtaPhi_threshold]->Fill(
281  BtagJT.first->eta(),
282  BtagJT.first->phi()); // fill 2D btag, jetPhi plot for 'b,c,uds'
283  }
284  }
285  }
286  }
287  } // for triggers
288 }
289 
293  edm::Run const &iRun,
294  edm::EventSetup const &iSetup) {
295  // book the DQM plots for each path and for each flavour
296  using namespace std;
297  assert(hltPathNames_.size() == JetTagCollection_.size());
298  std::string dqmFolder;
299  for (unsigned int ind = 0; ind < hltPathNames_.size(); ind++) {
300  float btagL = 0.;
301  float btagU = 1.;
302  int btagBins = 100;
303  dqmFolder = Form("%s/Discriminator/%s", mainFolder_.c_str(), hltPathNames_[ind].c_str());
304  H1_.push_back(std::map<std::string, MonitorElement *>());
305  H2_.push_back(std::map<std::string, MonitorElement *>());
306  H1mod_.push_back(std::map<std::string, std::map<HCALSpecials, MonitorElement *>>());
307  H2mod_.push_back(std::map<std::string, std::map<HCALSpecials, MonitorElement *>>());
308  H2Eta_.push_back(std::map<std::string, MonitorElement *>());
309  H2Phi_.push_back(std::map<std::string, MonitorElement *>());
310  H2EtaPhi_.push_back(std::map<std::string, MonitorElement *>());
311  H2EtaPhi_threshold_.push_back(std::map<std::string, MonitorElement *>());
312  ibooker.setCurrentFolder(dqmFolder);
313 
314  // book 1D btag plot for 'all'
315  if (!JetTagCollection_Label[ind].empty() && JetTagCollection_Label[ind] != "NULL") {
316  H1_.back()[JetTagCollection_Label[ind]] = ibooker.book1D(
317  JetTagCollection_Label[ind] + "_all", JetTagCollection_Label[ind] + "_all", btagBins, btagL, btagU);
318  H1_.back()[JetTagCollection_Label[ind]]->setAxisTitle(JetTagCollection_Label[ind] + "discriminant", 1);
319  // Input storing
320  ibooker.setCurrentFolder(dqmFolder + "/inputs");
321  ibooker.setCurrentFolder(dqmFolder + "/inputs/Jet");
322  for (int i = 0; i < 100; i++) {
323  if (keepSetJet.find(TaggingVariableTokens[i]) != keepSetJet.end()) { // if input name in defined set
325  H1_.back()[inpt] = ibooker.book1D(inpt, inpt, 105, -5, 100.);
326  H1_.back()[inpt]->setAxisTitle(inpt, 1);
327  } else
328  continue;
329  }
330  ibooker.setCurrentFolder(dqmFolder + "/inputs/Track");
331  for (int i = 0; i < 100; i++) {
332  if (keepSetTrack.find(TaggingVariableTokens[i]) != keepSetTrack.end()) { // if input name in defined set
334  H1_.back()[inpt] = ibooker.book1D(inpt, inpt, 105, -5, 100.);
335  H1_.back()[inpt]->setAxisTitle(inpt, 1);
336  } else
337  continue;
338  }
339  ibooker.setCurrentFolder(dqmFolder + "/inputs/Vertex");
340  for (int i = 0; i < 100; i++) {
341  if (keepSetVtx.find(TaggingVariableTokens[i]) != keepSetVtx.end()) { // if input name in defined set
343  H1_.back()[inpt] = ibooker.book1D(inpt, inpt, 105, -5, 100.);
344  H1_.back()[inpt]->setAxisTitle(inpt, 1);
345  } else
346  continue;
347  }
348 
349  for (auto i : HCALSpecialsNames) {
350  ibooker.setCurrentFolder(dqmFolder + "/" + i.second);
351  H1mod_.back()[JetTagCollection_Label[ind]][i.first] = ibooker.book1D(
352  JetTagCollection_Label[ind] + "_all", JetTagCollection_Label[ind] + "_all", btagBins, btagL, btagU);
353  H1mod_.back()[JetTagCollection_Label[ind]][i.first]->setAxisTitle(JetTagCollection_Label[ind] + "discriminant",
354  1);
355  }
356  ibooker.setCurrentFolder(dqmFolder);
357  }
358  int nBinsPt = 60;
359  double pTmin = 30;
360  double pTMax = 330;
361  int nBinsPhi = 54;
362  double phimin = -M_PI;
363  double phiMax = M_PI;
364  int nBinsEta = 40;
365  double etamin = -2.4;
366  double etaMax = 2.4;
367 
368  for (unsigned int i = 0; i < m_mcLabels.size(); ++i) {
369  std::string flavour = m_mcLabels[i];
371  std::string labelEta;
372  std::string labelPhi;
373  std::string labelEtaPhi;
374  std::string labelEtaPhi_threshold;
375  if (!JetTagCollection_Label[ind].empty() && JetTagCollection_Label[ind] != "NULL") {
376  label = JetTagCollection_Label[ind] + "__";
377  label += flavour;
378 
379  // book 1D btag plot for 'b,c,light,g'
380  H1_.back()[label] = ibooker.book1D(
381  label, Form("%s %s", JetTagCollection_Label[ind].c_str(), flavour.c_str()), btagBins, btagL, btagU);
382  H1_.back()[label]->setAxisTitle("disc", 1);
383  for (auto j : HCALSpecialsNames) {
384  ibooker.setCurrentFolder(dqmFolder + "/" + j.second);
385  H1mod_.back()[label][j.first] = ibooker.book1D(
386  label, Form("%s %s", JetTagCollection_Label[ind].c_str(), flavour.c_str()), btagBins, btagL, btagU);
387  H1mod_.back()[label][j.first]->setAxisTitle("disc", 1);
388  }
389  ibooker.setCurrentFolder(dqmFolder);
390  label = JetTagCollection_Label[ind] + "___";
391  labelEta = label;
392  labelPhi = label;
393  labelEtaPhi = label;
394  labelEtaPhi_threshold = label;
395  label += flavour + "_disc_pT";
396  labelEta += flavour + "_disc_eta";
397  labelPhi += flavour + "_disc_phi";
398  labelEtaPhi += flavour + "_eta_phi";
399  labelEtaPhi_threshold += flavour + "_eta_phi_disc05";
400 
401  // book 2D btag plot for 'b,c,light,g'
402  H2_.back()[label] = ibooker.book2D(label, label, btagBins, btagL, btagU, nBinsPt, pTmin, pTMax);
403  H2_.back()[label]->setAxisTitle("pT", 2);
404  H2_.back()[label]->setAxisTitle("disc", 1);
405  for (auto j : HCALSpecialsNames) {
406  ibooker.setCurrentFolder(dqmFolder + "/" + j.second);
407  H2mod_.back()[label][j.first] = ibooker.book2D(label, label, btagBins, btagL, btagU, nBinsPt, pTmin, pTMax);
408  H2mod_.back()[label][j.first]->setAxisTitle("pT", 2);
409  H2mod_.back()[label][j.first]->setAxisTitle("disc", 1);
410  }
411  ibooker.setCurrentFolder(dqmFolder);
412  H2Eta_.back()[labelEta] = ibooker.book2D(labelEta, labelEta, btagBins, btagL, btagU, nBinsEta, etamin, etaMax);
413  H2Eta_.back()[labelEta]->setAxisTitle("eta", 2);
414  H2Eta_.back()[labelEta]->setAxisTitle("disc", 1);
415  H2Phi_.back()[labelPhi] = ibooker.book2D(labelPhi, labelPhi, btagBins, btagL, btagU, nBinsPhi, phimin, phiMax);
416  H2Phi_.back()[labelPhi]->setAxisTitle("phi", 2);
417  H2Phi_.back()[labelPhi]->setAxisTitle("disc", 1);
418  H2EtaPhi_.back()[labelEtaPhi] =
419  ibooker.book2D(labelEtaPhi, labelEtaPhi, nBinsEta, etamin, etaMax, nBinsPhi, phimin, phiMax);
420  H2EtaPhi_.back()[labelEtaPhi]->setAxisTitle("phi", 2);
421  H2EtaPhi_.back()[labelEtaPhi]->setAxisTitle("eta", 1);
422  H2EtaPhi_threshold_.back()[labelEtaPhi_threshold] = ibooker.book2D(
423  labelEtaPhi_threshold, labelEtaPhi_threshold, nBinsEta, etamin, etaMax, nBinsPhi, phimin, phiMax);
424  H2EtaPhi_threshold_.back()[labelEtaPhi_threshold]->setAxisTitle("phi", 2);
425  H2EtaPhi_threshold_.back()[labelEtaPhi_threshold]->setAxisTitle("eta", 1);
426  }
427  }
428  }
429 }
430 
431 // define this as a plug-in
T getParameter(std::string const &) const
std::vector< flavour_t > flavours_t
static const TGPicture * info(bool iBackgroundIsBlack)
const char *const TaggingVariableTokens[]
std::set< std::string > keepSetTrack
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void analyze(const edm::Event &, const edm::EventSetup &) override
JetFloatAssociation::value_type JetTag
Definition: JetTag.h:17
bool accept() const
Has at least one path accepted the event?
const_iterator end() const
const_iterator begin() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
std::map< edm::RefToBase< reco::Jet >, float, JetRefCompare > JetTagMap
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:169
std::set< std::string > keepSetVtx
char const * label
Vector momentum() const final
spatial momentum vector
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:268
int closestJet(const RefToBase< reco::Jet > jet, const edm::AssociationVector< T, V > &association, double distance)
void dqmBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup) override
void bookHistograms(DQMStore::IBooker &ibooker, edm::Run const &iRun, edm::EventSetup const &iSetup) override
const_iterator end() const
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:106
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define end
Definition: vmac.h:39
bool isValid() const
Definition: HandleBase.h:74
#define M_PI
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
T const * product() const
Definition: Handle.h:74
MonitorElement * book2D(Args &&...args)
Definition: DQMStore.h:109
static std::string const triggerResults("TriggerResults")
void labelsForToken(EDGetToken iToken, Labels &oLabels) const
fixed size matrix
#define begin
Definition: vmac.h:32
HLT enums.
HLTBTagPerformanceAnalyzer(const edm::ParameterSet &)
std::set< std::string > keepSet
vars
Definition: DeepTauId.cc:77
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
const_iterator begin() const
std::set< std::string > keepSetJet
Definition: Run.h:45
size_type size() const