CMS 3D CMS Logo

BTagPerformanceAnalyzerOnData.cc
Go to the documentation of this file.
5 
6 using namespace reco;
7 using namespace edm;
8 using namespace std;
9 using namespace RecoBTag;
10 
12  jetSelector(
13  pSet.getParameter<double>("etaMin"),
14  pSet.getParameter<double>("etaMax"),
15  pSet.getParameter<double>("ptRecJetMin"),
16  pSet.getParameter<double>("ptRecJetMax"),
17  0.0, 99999.0,
18  pSet.getParameter<double>("ratioMin"),
19  pSet.getParameter<double>("ratioMax"),
20  pSet.getParameter<bool>( "doJetID" )
21  ),
22  etaRanges(pSet.getParameter< vector<double> >("etaRanges")),
23  ptRanges(pSet.getParameter< vector<double> >("ptRanges")),
24  doJEC(pSet.getParameter<bool>( "doJEC" )),
25  moduleConfig(pSet.getParameter< vector<edm::ParameterSet> >("tagConfig"))
26 {
27  genToken = mayConsume<GenEventInfoProduct>(edm::InputTag("generator"));
28  slInfoToken = consumes<SoftLeptonTagInfoCollection>(pSet.getParameter<InputTag>("softLeptonInfo"));
29  jecMCToken = mayConsume<JetCorrector>(pSet.getParameter<edm::InputTag>( "JECsourceMC" ));
30  jecDataToken = consumes<JetCorrector>(pSet.getParameter<edm::InputTag>( "JECsourceData" ));
31 
32  if (etaRanges.size() <= 1)
33  etaRanges = { pSet.getParameter<double>("etaMin"), pSet.getParameter<double>("etaMax") };
34  if (ptRanges.size() <= 1)
35  ptRanges = { pSet.getParameter<double>("ptRecJetMin"), pSet.getParameter<double>("ptRecJetMax") };
36 
37  for (vector<edm::ParameterSet>::const_iterator iModule = moduleConfig.begin();
38  iModule != moduleConfig.end(); ++iModule) {
39 
40  const string& dataFormatType = iModule->exists("type") ?
41  iModule->getParameter<string>("type") :
42  "JetTag";
43  if (dataFormatType == "JetTag") {
44  const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
45  jetTagInputTags.push_back(moduleLabel);
46  binJetTagPlotters.push_back(vector<std::unique_ptr<JetTagPlotter>>()) ;
47  jetTagToken.push_back(consumes<JetTagCollection>(moduleLabel));
48  }
49  else if(dataFormatType == "TagCorrelation") {
50  const InputTag& label1 = iModule->getParameter<InputTag>("label1");
51  const InputTag& label2 = iModule->getParameter<InputTag>("label2");
52  tagCorrelationInputTags.push_back(std::pair<edm::InputTag, edm::InputTag>(label1, label2));
53  binTagCorrelationPlotters.push_back(vector<std::unique_ptr<TagCorrelationPlotter>>());
54  tagCorrelationToken.push_back(std::pair< edm::EDGetTokenT<reco::JetTagCollection>, edm::EDGetTokenT<reco::JetTagCollection> >(consumes<JetTagCollection>(label1), consumes<JetTagCollection>(label2)));
55  }
56  else {
57  vector<edm::InputTag> vIP;
58  tiDataFormatType.push_back(dataFormatType);
59  binTagInfoPlotters.push_back(vector<std::unique_ptr<BaseTagInfoPlotter>>()) ;
60  std::vector< edm::EDGetTokenT<edm::View<reco::BaseTagInfo>> > tokens;
61  if(dataFormatType == "GenericMVA") {
62  const std::vector<InputTag> listInfo = iModule->getParameter<vector<InputTag>>("listTagInfos");
63  for(unsigned int ITi=0; ITi<listInfo.size(); ITi++) {
64  tokens.push_back(consumes< View<BaseTagInfo> >(listInfo[ITi]));
65  vIP.push_back(listInfo[ITi]);
66  }
67  }
68  else {
69  const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
70  tokens.push_back(consumes< View<BaseTagInfo> >(moduleLabel));
71  vIP.push_back(moduleLabel);
72  }
73  tagInfoToken.push_back(tokens);
74  tagInfoInputTags.push_back(vIP);
75  }
76  }
77 }
78 
80 {
81  // Book all histograms.
82 
83  // iterate over ranges:
84  const int iEtaStart = -1 ; // this will be the inactive one
85  const int iEtaEnd = etaRanges.size() > 2 ? etaRanges.size() - 1 : 0; // if there is only one bin defined, leave it as the inactive one
86  const int iPtStart = -1 ; // this will be the inactive one
87  const int iPtEnd = ptRanges.size() > 2 ? ptRanges.size() - 1 : 0; // if there is only one bin defined, leave it as the inactive one
88  setTDRStyle();
89 
90  TagInfoPlotterFactory theFactory;
91  int iTag = -1; int iTagCorr = -1; int iInfoTag = -1;
92  for (vector<edm::ParameterSet>::const_iterator iModule = moduleConfig.begin();
93  iModule != moduleConfig.end(); ++iModule) {
94 
95  const string& dataFormatType = iModule->exists("type") ?
96  iModule->getParameter<string>("type") :
97  "JetTag";
98  if (dataFormatType == "JetTag") {
99  iTag++;
100  const string& folderName = iModule->getParameter<string>("folder");
101 
102  bool doDifferentialPlots = false;
103  double discrCut = -999.;
104  if (iModule->exists("differentialPlots") && iModule->getParameter<bool>("differentialPlots") == true) {
105  doDifferentialPlots = true;
106  discrCut = iModule->getParameter<double>("discrCut");
107  }
108 
109  // eta loop
110  for (int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta) {
111  // pt loop
112  for (int iPt = iPtStart ; iPt < iPtEnd ; ++iPt) {
113 
114  const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
115 
116  // Instantiate the generic b tag plotter
117  binJetTagPlotters.at(iTag).push_back(std::make_unique<JetTagPlotter>(folderName, etaPtBin, iModule->getParameter<edm::ParameterSet>("parameters"), 0, false, ibook, false, doDifferentialPlots, discrCut));
118 
119  }
120  }
121 
122  } else if (dataFormatType == "TagCorrelation") {
123  iTagCorr++;
124  const InputTag& label1 = iModule->getParameter<InputTag>("label1");
125  const InputTag& label2 = iModule->getParameter<InputTag>("label2");
126 
127  // eta loop
128  for (int iEta = iEtaStart ; iEta != iEtaEnd ; ++iEta) {
129  // pt loop
130  for(int iPt = iPtStart ; iPt != iPtEnd ; ++iPt) {
131  const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
132  // Instantiate the generic b tag correlation plotter
133  binTagCorrelationPlotters.at(iTagCorr).push_back(
134  std::make_unique<TagCorrelationPlotter>(label1.label(), label2.label(), etaPtBin,
135  iModule->getParameter<edm::ParameterSet>("parameters"),
136  0, false, false, ibook)
137  );
138  }
139  }
140  } else {
141  iInfoTag++;
142  // tag info retrievel is deferred (needs availability of EventSetup)
143  const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
144  const string& folderName = iModule->getParameter<string>("folder");
145 
146  // eta loop
147  for (int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta) {
148  // pt loop
149  for (int iPt = iPtStart ; iPt < iPtEnd ; ++iPt) {
150  const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
151 
152  // Instantiate the tagInfo plotter
153  binTagInfoPlotters.at(iInfoTag).push_back(theFactory.buildPlotter(dataFormatType, moduleLabel.label(),
154  etaPtBin, iModule->getParameter<edm::ParameterSet>("parameters"), folderName,
155  0, false, ibook)
156  );
157  }
158  }
159  }
160  }
161 }
162 
163 EtaPtBin BTagPerformanceAnalyzerOnData::getEtaPtBin(const int& iEta, const int& iPt)
164 {
165  // DEFINE BTagBin:
166  bool etaActive_ , ptActive_;
167  double etaMin_, etaMax_, ptMin_, ptMax_;
168 
169  if ( iEta != -1 ) {
170  etaActive_ = true;
171  etaMin_ = etaRanges[iEta];
172  etaMax_ = etaRanges[iEta+1];
173  }
174  else {
175  etaActive_ = false;
176  etaMin_ = etaRanges[0];
177  etaMax_ = etaRanges[etaRanges.size() - 1];
178  }
179 
180  if ( iPt != -1 ) {
181  ptActive_ = true;
182  ptMin_ = ptRanges[iPt];
183  ptMax_ = ptRanges[iPt+1];
184  }
185  else {
186  ptActive_ = false;
187  ptMin_ = ptRanges[0];
188  ptMax_ = ptRanges[ptRanges.size() - 1];
189  }
190  return EtaPtBin(etaActive_, etaMin_, etaMax_, ptActive_, ptMin_ , ptMax_);
191 }
192 
194 {
195  /*for (unsigned int iJetLabel = 0; iJetLabel != binJetTagPlotters.size(); ++iJetLabel) {
196  int plotterSize = binJetTagPlotters[iJetLabel].size();
197  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
198  delete binJetTagPlotters[iJetLabel][iPlotter];
199  }
200  }
201 
202  for (vector<vector<TagCorrelationPlotter*> >::iterator iJetLabel = binTagCorrelationPlotters.begin();
203  iJetLabel != binTagCorrelationPlotters.end(); ++iJetLabel)
204  for(vector<TagCorrelationPlotter*>::iterator iPlotter = iJetLabel->begin(); iPlotter != iJetLabel->end(); ++iPlotter)
205  delete *iPlotter;
206 
207  for (unsigned int iJetLabel = 0; iJetLabel != binTagInfoPlotters.size(); ++iJetLabel) {
208  int plotterSize = binTagInfoPlotters[iJetLabel].size();
209  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
210  delete binTagInfoPlotters[iJetLabel][iPlotter];
211  }
212  }*/
213 }
214 
216 {
217 
219  iEvent.getByToken(slInfoToken, infoHandle);
220 
221  //Get JEC
222  const JetCorrector* corrector = nullptr;
223  if(doJEC) {
224  edm::Handle<GenEventInfoProduct> genInfoHandle; //check if data or MC
225  iEvent.getByToken(genToken, genInfoHandle);
226  edm::Handle<JetCorrector> corrHandle;
227  if( !genInfoHandle.isValid() ) iEvent.getByToken(jecDataToken, corrHandle);
228  else iEvent.getByToken(jecMCToken, corrHandle);
229  corrector = corrHandle.product();
230  }
231 
232  // Look first at the jetTags
233 
234  for (unsigned int iJetLabel = 0; iJetLabel != jetTagInputTags.size(); ++iJetLabel) {
236  iEvent.getByToken(jetTagToken[iJetLabel], tagHandle);
237  //
238  // insert check on the presence of the collections
239  //
240 
241  if (!tagHandle.isValid()) {
242  edm::LogWarning("BTagPerformanceAnalyzerOnData")<<" Collection "<<jetTagInputTags[iJetLabel]<<" not present. Skipping it for this event.";
243  continue;
244  }
245 
246  const reco::JetTagCollection & tagColl = *(tagHandle.product());
247  LogDebug("Info") << "Found " << tagColl.size() << " B candidates in collection " << jetTagInputTags[iJetLabel];
248 
249  int plotterSize = binJetTagPlotters[iJetLabel].size();
250  for (JetTagCollection::const_iterator tagI = tagColl.begin(); tagI != tagColl.end(); ++tagI) {
251 
252  //JEC
253  double jec = 1.0;
254  if(doJEC && corrector) {
255  jec = corrector->correction(*(tagI->first));
256  }
257 
258  if (!jetSelector(*(tagI->first), -1, infoHandle, jec))
259  continue;
260 
261  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
262  bool inBin = binJetTagPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*tagI->first, jec);
263  // Fill histograms if in desired pt/rapidity bin.
264  if (inBin)
265  binJetTagPlotters[iJetLabel][iPlotter]->analyzeTag(*tagI, jec, -1);
266  }
267  }
268  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
269  binJetTagPlotters[iJetLabel][iPlotter]->analyzeTag();
270  }
271  }
272 
273  // Now look at Tag Correlations
274  for (unsigned int iJetLabel = 0; iJetLabel != tagCorrelationInputTags.size(); ++iJetLabel) {
275  const std::pair< edm::EDGetTokenT<reco::JetTagCollection>, edm::EDGetTokenT<reco::JetTagCollection> >& inputTokens = tagCorrelationToken[iJetLabel];
277  iEvent.getByToken(inputTokens.first, tagHandle1);
278  const reco::JetTagCollection& tagColl1 = *(tagHandle1.product());
279 
281  iEvent.getByToken(inputTokens.second, tagHandle2);
282  const reco::JetTagCollection& tagColl2 = *(tagHandle2.product());
283 
284  int plotterSize = binTagCorrelationPlotters[iJetLabel].size();
285  for (JetTagCollection::const_iterator tagI = tagColl1.begin(); tagI != tagColl1.end(); ++tagI) {
286  //JEC
287  double jec = 1.0;
288  if(doJEC && corrector) {
289  jec = corrector->correction(*(tagI->first));
290  }
291 
292  if (!jetSelector(*(tagI->first), -1, infoHandle, jec))
293  continue;
294 
295  for(int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
296  bool inBin = binTagCorrelationPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*(tagI->first), jec);
297 
298  if(inBin)
299  {
300  double discr2 = tagColl2[tagI->first];
301  binTagCorrelationPlotters[iJetLabel][iPlotter]->analyzeTags(tagI->second, discr2, -1);
302  }
303  }
304  }
305  }
306 
307  // Now look at the TagInfos
308  for (unsigned int iJetLabel = 0; iJetLabel != tiDataFormatType.size(); ++iJetLabel) {
309  int plotterSize = binTagInfoPlotters[iJetLabel].size();
310  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter)
311  binTagInfoPlotters[iJetLabel][iPlotter]->setEventSetup(iSetup);
312 
313  vector<edm::EDGetTokenT<edm::View<reco::BaseTagInfo>> > & tokens = tagInfoToken[iJetLabel];
314  //check number of tag infos = expected number of tag infos
315  vector<string> labels = binTagInfoPlotters[iJetLabel][0]->tagInfoRequirements();
316  if (labels.empty())
317  labels.push_back("label");
318  if (labels.size() != tokens.size()) throw cms::Exception("Configuration") << "Different number of Tag Infos than expected" << labels.size() << tokens.size() << endl;
319 
320  unsigned int nInputTags = tokens.size();
321  vector< edm::Handle< View<BaseTagInfo> > > tagInfoHandles(nInputTags);
322  edm::ProductID jetProductID;
323  unsigned int nTagInfos = 0;
324  for (unsigned int iInputTags = 0; iInputTags < tokens.size(); ++iInputTags) {
325  edm::Handle< View<BaseTagInfo> > & tagInfoHandle = tagInfoHandles[iInputTags];
326  iEvent.getByToken(tokens[iInputTags], tagInfoHandle);
327  //
328  // protect against missing products
329  //
330  if (tagInfoHandle.isValid() == false) {
331  edm::LogWarning("BTagPerformanceAnalyzerOnData")<<" Collection "<<tagInfoInputTags[iJetLabel][iInputTags]<<" not present. Skipping it for this event.";
332  continue;
333  }
334 
335  unsigned int size = tagInfoHandle->size();
336  LogDebug("Info") << "Found " << size << " B candidates in collection " << tagInfoInputTags[iJetLabel][iInputTags];
337  edm::ProductID thisProductID = (size > 0) ? (*tagInfoHandle)[0].jet().id() : edm::ProductID();
338  if (iInputTags == 0) {
339  jetProductID = thisProductID;
340  nTagInfos = size;
341  } else if (jetProductID != thisProductID)
342  throw cms::Exception("Configuration") << "TagInfos are referencing a different jet collection." << endl;
343  else if (nTagInfos != size)
344  throw cms::Exception("Configuration") << "TagInfo collections are having a different size." << endl;
345  }
346 
347  for (unsigned int iTagInfos = 0; iTagInfos < nTagInfos; ++iTagInfos) {
348  vector<const BaseTagInfo*> baseTagInfos(nInputTags);
349  edm::RefToBase<Jet> jetRef;
350  for (unsigned int iTagInfo = 0; iTagInfo < nInputTags; iTagInfo++) {
351  const BaseTagInfo &baseTagInfo = (*tagInfoHandles[iTagInfo])[iTagInfos];
352  if (iTagInfo == 0)
353  jetRef = baseTagInfo.jet();
354  else if (baseTagInfo.jet() != jetRef)
355  throw cms::Exception("Configuration") << "TagInfos pointing to different jets." << endl;
356  baseTagInfos[iTagInfo] = &baseTagInfo;
357  }
358 
359  //JEC
360  double jec = 1.0;
361  if(doJEC && corrector) {
362  jec = corrector->correction(*(jetRef));
363  }
364 
365  if (!jetSelector(*jetRef, -1, infoHandle, jec))
366  continue;
367 
368  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
369  bool inBin = binTagInfoPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*jetRef, jec);
370  // Fill histograms if in desired pt/rapidity bin.
371  if (inBin)
372  binTagInfoPlotters[iJetLabel][iPlotter]->analyzeTag(baseTagInfos, jec, -1);
373  }
374  }
375  }
376 }
377 
378 //define this as a plug-in
#define LogDebug(id)
size
Write out results.
T getParameter(std::string const &) const
std::vector< edm::EDGetTokenT< reco::JetTagCollection > > jetTagToken
transient_vector_type::const_iterator const_iterator
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
edm::EDGetTokenT< reco::JetCorrector > jecMCToken
std::vector< std::string > tiDataFormatType
std::vector< std::pair< edm::EDGetTokenT< reco::JetTagCollection >, edm::EDGetTokenT< reco::JetTagCollection > > > tagCorrelationToken
const_iterator end() const
double correction(const LorentzVector &fJet) const
get correction using Jet information only
Definition: JetCorrector.h:49
std::vector< std::pair< edm::InputTag, edm::InputTag > > tagCorrelationInputTags
std::vector< std::vector< edm::EDGetTokenT< edm::View< reco::BaseTagInfo > > > > tagInfoToken
BTagPerformanceAnalyzerOnData(const edm::ParameterSet &pSet)
edm::EDGetTokenT< GenEventInfoProduct > genToken
virtual edm::RefToBase< Jet > jet(void) const
returns a polymorphic reference to the tagged jet
Definition: BaseTagInfo.h:24
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
def setTDRStyle()
Definition: plotscripts.py:89
std::vector< std::vector< std::unique_ptr< BaseTagInfoPlotter > > > binTagInfoPlotters
std::vector< std::vector< std::unique_ptr< TagCorrelationPlotter > > > binTagCorrelationPlotters
bool isValid() const
Definition: HandleBase.h:74
edm::EDGetTokenT< reco::SoftLeptonTagInfoCollection > slInfoToken
std::vector< std::vector< edm::InputTag > > tagInfoInputTags
void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup) override
T const * product() const
Definition: Handle.h:74
Definition: Tools.h:23
std::vector< std::vector< std::unique_ptr< JetTagPlotter > > > binJetTagPlotters
std::vector< edm::ParameterSet > moduleConfig
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
std::vector< edm::InputTag > jetTagInputTags
EtaPtBin getEtaPtBin(const int &iEta, const int &iPt)
std::string const & label() const
Definition: InputTag.h:36
fixed size matrix
HLT enums.
edm::EDGetTokenT< reco::JetCorrector > jecDataToken
ProductIndex id() const
Definition: ProductID.h:38
std::unique_ptr< BaseTagInfoPlotter > buildPlotter(const std::string &dataFormatType, const std::string &tagName, const EtaPtBin &etaPtBin, const edm::ParameterSet &pSet, const std::string &folderName, unsigned int mc, bool wf, DQMStore::IBooker &ibook)
const_iterator begin() const
Definition: Run.h:45
size_type size() const