CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HLTBTagPerformanceAnalyzer.cc
Go to the documentation of this file.
2 
3 using namespace edm;
4 using namespace reco;
5 
6 // find the index of the object key of an association vector closest to a given jet, within a given distance
7 template <typename T, typename V>
8 int closestJet(const RefToBase<reco::Jet> jet, const edm::AssociationVector<T, V> & association, double distance) {
9  int closest = -1;
10  for (unsigned int i = 0; i < association.size(); ++i) {
11  double d = ROOT::Math::VectorUtil::DeltaR(jet->momentum(), association[i].first->momentum());
12  if (d < distance) {
13  distance = d;
14  closest = i;
15  }
16  }
17  return closest;
18 }
19 
20 
21 // constructors and destructor
23 {
24  hlTriggerResults_ = consumes<edm::TriggerResults>(iConfig.getParameter<InputTag> ("TriggerResults"));
25  JetTagCollection_ = edm::vector_transform(iConfig.getParameter<std::vector<edm::InputTag> >( "JetTag" ), [this](edm::InputTag const & tag){return mayConsume< reco::JetTagCollection>(tag);});
26  m_mcPartons = consumes<JetFlavourMatchingCollection>(iConfig.getParameter<InputTag> ("mcPartons") );
27  hltPathNames_ = iConfig.getParameter< std::vector<std::string> > ("HLTPathNames");
28  edm::ParameterSet mc = iConfig.getParameter<edm::ParameterSet>("mcFlavours");
29  m_mcLabels = mc.getParameterNamesForType<std::vector<unsigned int> >();
30 
32  m_mcPartons_Label = label.module;
33 
34  for(unsigned int i=0; i<JetTagCollection_.size() ; i++){
35  EDConsumerBase::labelsForToken(JetTagCollection_[i],label);
36  JetTagCollection_Label.push_back(label.module);
37  }
38 
39  EDConsumerBase::labelsForToken(hlTriggerResults_,label);
40  hlTriggerResults_Label = label.module;
41 
42  for (unsigned int i = 0; i < m_mcLabels.size(); ++i)
43  m_mcFlavours.push_back( mc.getParameter<std::vector<unsigned int> >(m_mcLabels[i]) );
44  m_mcMatching = m_mcPartons_Label != "none" ;
45 
46  m_mcRadius=0.3;
47 }
48 
49 
51 {
52 
53  // do anything here that needs to be done at desctruction time
54  // (e.g. close files, deallocate resources etc.)
55 }
56 
58  triggerConfChanged_ = true;
59  EDConsumerBase::labelsForToken(hlTriggerResults_,label);
60 
61  hltConfigProvider_.init(iRun, iSetup, label.process, triggerConfChanged_);
62  const std::vector< std::string > & allHltPathNames = hltConfigProvider_.triggerNames();
63 
64  //fill hltPathIndexs_ with the trigger number of each hltPathNames_
65  for ( size_t trgs=0; trgs<hltPathNames_.size(); trgs++) {
66  unsigned int found = 1;
67  int it_mem = -1;
68  for (size_t it=0 ; it < allHltPathNames.size() ; ++it )
69  {
70  found = allHltPathNames.at(it).find(hltPathNames_[trgs]);
71  if ( found == 0 )
72  {
73  it_mem= (int) it;
74  }
75  }//for allallHltPathNames
76  hltPathIndexs_.push_back(it_mem);
77  }//for hltPathNames_
78 
79  //fill _isfoundHLTs for each hltPathNames_
80  for ( size_t trgs=0; trgs<hltPathNames_.size(); trgs++) {
81  if ( hltPathIndexs_[trgs] < 0 ) {
82  _isfoundHLTs.push_back(false);
83  }
84  else {
85  _isfoundHLTs.push_back(true);
86  }
87  }
88 }
89 
90 
92 {
93 
94  bool trigRes=false;
95  bool MCOK=false;
96  using namespace edm;
97 
98  //get triggerResults
99  Handle<TriggerResults> TriggerResulsHandler;
101  if ( hlTriggerResults_Label == "" || hlTriggerResults_Label == "NULL" )
102  {
103  edm::LogInfo("NoTriggerResults") << "TriggerResults ==> Empty";
104  return;
105  }
106  iEvent.getByToken(hlTriggerResults_, TriggerResulsHandler);
107  if (TriggerResulsHandler.isValid()) trigRes=true;
108  if ( !trigRes ) { edm::LogInfo("NoTriggerResults") << "TriggerResults ==> not readable"; return;}
109  const TriggerResults & triggerResults = *(TriggerResulsHandler.product());
110 
111  //get partons
112  if (m_mcMatching && m_mcPartons_Label!= "" && m_mcPartons_Label != "NULL" ) {
113  iEvent.getByToken(m_mcPartons, h_mcPartons);
114  if (h_mcPartons.isValid()) MCOK=true;
115  }
116 
117  //fill the 1D and 2D DQM plot
118  Handle<reco::JetTagCollection> JetTagHandler;
119  for (unsigned int ind=0; ind<hltPathNames_.size();ind++) {
120  bool BtagOK=false;
122  if ( !_isfoundHLTs[ind]) continue; //if the hltPath is not in the event, skip the event
123  if ( !triggerResults.accept(hltPathIndexs_[ind]) ) continue; //if the hltPath was not accepted skip the event
124 
125  //get JetTagCollection
126  if (JetTagCollection_Label[ind] != "" && JetTagCollection_Label[ind] != "NULL" )
127  {
128  iEvent.getByToken(JetTagCollection_[ind], JetTagHandler);
129  if (JetTagHandler.isValid()) BtagOK=true;
130  }
131 
132  //fill JetTag map
133  if (BtagOK) for ( auto iter = JetTagHandler->begin(); iter != JetTagHandler->end(); iter++ )
134  {
135  JetTag.insert(JetTagMap::value_type(iter->first, iter->second));
136  }
137  else {
138  edm::LogInfo("NoCollection") << "Collection " << JetTagCollection_Label[ind] << " ==> not found"; return;
139  }
140 
141  for (auto & BtagJT: JetTag) {
142  //fill 1D btag plot for 'all'
143  H1_.at(ind)[JetTagCollection_Label[ind]] -> Fill(std::fmax(0.0,BtagJT.second));
144  if (MCOK) {
145  int m = closestJet(BtagJT.first, *h_mcPartons, m_mcRadius);
146  unsigned int flavour = (m != -1) ? abs((*h_mcPartons)[m].second.getFlavour()) : 0;
147  for (unsigned int i = 0; i < m_mcLabels.size(); ++i) {
148  TString flavour_str= m_mcLabels[i].c_str();
149  flavours_t flav_collection= m_mcFlavours[i];
150  auto it = std::find(flav_collection.begin(), flav_collection.end(), flavour);
151  if (it== flav_collection.end()) continue;
152  TString label=JetTagCollection_Label[ind] + "__";
153  label+=flavour_str;
154  H1_.at(ind)[label.Data()]->Fill(std::fmax(0.0,BtagJT.second)); //fill 1D btag plot for 'b,c,uds'
155  label=JetTagCollection_Label[ind] + "___";
156  label+=flavour_str;
157  label+=TString("_disc_pT");
158  H2_.at(ind)[label.Data()]->Fill(std::fmax(0.0,BtagJT.second),BtagJT.first->pt()); //fill 2D btag, jetPt plot for 'b,c,uds'
159  }
160  }
161  }
162  }//for triggers
163 }
164 
165 
166 
167 
170 {
171  //book the DQM plots for each path and for each flavour
172  using namespace std;
173  assert(hltPathNames_.size()== JetTagCollection_.size());
174  std::string dqmFolder;
175  for (unsigned int ind=0; ind<hltPathNames_.size();ind++){
176  float btagL = 0.;
177  float btagU = 1.;
178  int btagBins = 100;
179  dqmFolder = Form("HLT/BTag/Discriminator/%s",hltPathNames_[ind].c_str());
180  H1_.push_back(std::map<std::string, MonitorElement *>());
181  H2_.push_back(std::map<std::string, MonitorElement *>());
182  ibooker.setCurrentFolder(dqmFolder);
183 
184  //book 1D btag plot for 'all'
185  if ( JetTagCollection_Label[ind] != "" && JetTagCollection_Label[ind] != "NULL" ) {
186  H1_.back()[JetTagCollection_Label[ind]] = ibooker.book1D(JetTagCollection_Label[ind] + "_all", (JetTagCollection_Label[ind]+ "_all").c_str(), btagBins, btagL, btagU );
187  H1_.back()[JetTagCollection_Label[ind]] -> setAxisTitle(JetTagCollection_Label[ind] +"discriminant",1);
188  }
189  int nBinsPt=60;
190  double pTmin=30;
191  double pTMax=330;
192 
193  for (unsigned int i = 0; i < m_mcLabels.size(); ++i)
194  {
195  TString flavour= m_mcLabels[i].c_str();
196  TString label;
197  if ( JetTagCollection_Label[ind] != "" && JetTagCollection_Label[ind] != "NULL" ) {
198  label=JetTagCollection_Label[ind]+"__";
199  label+=flavour;
200 
201  //book 1D btag plot for 'b,c,light,g'
202  H1_.back()[label.Data()] = ibooker.book1D(label.Data(), Form("%s %s",JetTagCollection_Label[ind].c_str(),flavour.Data()), btagBins, btagL, btagU );
203  H1_.back()[label.Data()]->setAxisTitle("disc",1);
204  label=JetTagCollection_Label[ind]+"___";
205  label+=flavour+TString("_disc_pT");
206 
207  //book 2D btag plot for 'b,c,light,g'
208  H2_.back()[label.Data()] = ibooker.book2D( label.Data(), label.Data(), btagBins, btagL, btagU, nBinsPt, pTmin, pTMax );
209  H2_.back()[label.Data()]->setAxisTitle("pT",2);
210  H2_.back()[label.Data()]->setAxisTitle("disc",1);
211  }
212  }
213  }
214 }
215 
216 //define this as a plug-in
218 
T getParameter(std::string const &) const
std::vector< flavour_t > flavours_t
int i
Definition: DBlmapReader.cc:9
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
JetFloatAssociation::value_type JetTag
Definition: JetTag.h:17
assert(m_qm.get())
virtual Vector momentum() const
spatial momentum vector
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
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
std::map< edm::RefToBase< reco::Jet >, float, JetRefCompare > JetTagMap
std::vector< std::string > getParameterNamesForType(bool trackiness=true) const
Definition: ParameterSet.h:194
tuple d
Definition: ztail.py:151
int iEvent
Definition: GenABIO.cc:230
int closestJet(const RefToBase< reco::Jet > jet, const edm::AssociationVector< T, V > &association, double distance)
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
void bookHistograms(DQMStore::IBooker &ibooker, edm::Run const &iRun, edm::EventSetup const &iSetup) override
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:115
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Container::value_type value_type
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:274
MonitorElement * book2D(Args &&...args)
Definition: DQMStore.h:133
virtual void analyze(const edm::Event &, const edm::EventSetup &)
static std::string const triggerResults("TriggerResults")
void labelsForToken(EDGetToken iToken, Labels &oLabels) const
void dqmBeginRun(const edm::Run &iRun, const edm::EventSetup &iSetup)
HLTBTagPerformanceAnalyzer(const edm::ParameterSet &)
int flavour(const Candidate &part)
Definition: pdgIdUtils.h:31
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
Definition: Run.h:43
size_type size() const