CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
BTagPerformanceAnalyzerOnData.cc
Go to the documentation of this file.
11 
12 using namespace reco;
13 using namespace edm;
14 using namespace std;
15 using namespace RecoBTag;
16 
18  partonKinematics(pSet.getParameter< bool >("partonKinematics")),
19  jetSelector(
20  pSet.getParameter<double>("etaMin"),
21  pSet.getParameter<double>("etaMax"),
22  pSet.getParameter<double>("ptRecJetMin"),
23  pSet.getParameter<double>("ptRecJetMax"),
24  0.0, 99999.0,
25  pSet.getParameter<double>("ratioMin"),
26  pSet.getParameter<double>("ratioMax")
27  ),
28  etaRanges(pSet.getParameter< vector<double> >("etaRanges")),
29  ptRanges(pSet.getParameter< vector<double> >("ptRanges")),
30  produceEps(pSet.getParameter< bool >("produceEps")),
31  producePs(pSet.getParameter< bool >("producePs")),
32  psBaseName(pSet.getParameter<std::string>( "psBaseName" )),
33  epsBaseName(pSet.getParameter<std::string>( "epsBaseName" )),
34  inputFile(pSet.getParameter<std::string>( "inputfile" )),
35  update(pSet.getParameter<bool>( "update" )),
36  allHisto(pSet.getParameter<bool>( "allHistograms" )),
37  finalize(pSet.getParameter< bool >("finalizePlots")),
38  finalizeOnly(pSet.getParameter< bool >("finalizeOnly")),
39  slInfoTag(pSet.getParameter<edm::InputTag>("softLeptonInfo")),
40  moduleConfig(pSet.getParameter< vector<edm::ParameterSet> >("tagConfig")),
41  mcPlots_(pSet.getParameter< bool >("mcPlots"))
42 {
43  bookHistos(pSet);
44 }
45 
47 {
48  //
49  // Book all histograms.
50  //
51 
52  if (update) {
53  //
54  // append the DQM file ... we should consider this experimental
55  // edm::Service<DQMStore>().operator->()->open(std::string((const char *)(inputFile)),"/");
56  // removed, a module will take care
57  }
58 
59  // parton p
60 // double pPartonMin = 0.0 ;
61 // double pPartonMax = 99999.9 ;
62 
63  // iterate over ranges:
64  const int iEtaStart = -1 ; // this will be the inactive one
65  const int iEtaEnd = etaRanges.size() - 1 ;
66  const int iPtStart = -1 ; // this will be the inactive one
67  const int iPtEnd = ptRanges.size() - 1 ;
68  setTDRStyle();
69 
70  TagInfoPlotterFactory theFactory;
71 
72  for (vector<edm::ParameterSet>::const_iterator iModule = moduleConfig.begin();
73  iModule != moduleConfig.end(); ++iModule) {
74 
75  const string& dataFormatType = iModule->exists("type") ?
76  iModule->getParameter<string>("type") :
77  "JetTag";
78  if (dataFormatType == "JetTag") {
79  const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
80  const string& folderName = iModule->getParameter<string>("folder");
81  jetTagInputTags.push_back(moduleLabel);
82  binJetTagPlotters.push_back(vector<JetTagPlotter*>()) ;
83  // Contains plots for each bin of rapidity and pt.
84  vector<BTagDifferentialPlot*> * differentialPlotsConstantEta = new vector<BTagDifferentialPlot*> () ;
85  vector<BTagDifferentialPlot*> * differentialPlotsConstantPt = new vector<BTagDifferentialPlot*> () ;
86  if (finalize && mcPlots_){
87  differentialPlots.push_back(vector<BTagDifferentialPlot*>());
88 
89  // the constant b-efficiency for the differential plots versus pt and eta
90  const double& effBConst =
91  iModule->getParameter<edm::ParameterSet>("parameters").getParameter<double>("effBConst");
92 
93  // the objects for the differential plots vs. eta,pt for
94 
95  for ( int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta ) {
96  BTagDifferentialPlot * etaConstDifferentialPlot = new BTagDifferentialPlot
97  (effBConst, BTagDifferentialPlot::constETA, moduleLabel.label());
98  differentialPlotsConstantEta->push_back ( etaConstDifferentialPlot );
99  }
100 
101  for ( int iPt = iPtStart ; iPt < iPtEnd ; ++iPt ) {
102  // differentialPlots for this pt bin
103  BTagDifferentialPlot * ptConstDifferentialPlot = new BTagDifferentialPlot
104  (effBConst, BTagDifferentialPlot::constPT, moduleLabel.label());
105  differentialPlotsConstantPt->push_back ( ptConstDifferentialPlot );
106  }
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 genertic b tag plotter
117  JetTagPlotter *jetTagPlotter = new JetTagPlotter(folderName, etaPtBin,
118  iModule->getParameter<edm::ParameterSet>("parameters"), mcPlots_, update, finalize);
119  binJetTagPlotters.back().push_back ( jetTagPlotter ) ;
120 
121  // Add to the corresponding differential plotters
122  if (finalize && mcPlots_){
123  (*differentialPlotsConstantEta)[iEta+1]->addBinPlotter ( jetTagPlotter ) ;
124  (*differentialPlotsConstantPt )[iPt+1] ->addBinPlotter ( jetTagPlotter ) ;
125  }
126  }
127  }
128 
129  // the objects for the differential plots vs. eta, pt: collect all from constant eta and constant pt
130  if (finalize && mcPlots_){
131  differentialPlots.back().reserve(differentialPlotsConstantEta->size()+differentialPlotsConstantPt->size()) ;
132  differentialPlots.back().insert(differentialPlots.back().end(), differentialPlotsConstantEta->begin(), differentialPlotsConstantEta->end());
133  differentialPlots.back().insert(differentialPlots.back().end(), differentialPlotsConstantPt->begin(), differentialPlotsConstantPt->end());
134 
135  edm::LogInfo("Info")
136  << "====>>>> ## sizeof differentialPlots = " << differentialPlots.size();
137 
138  // the intermediate ones are no longer needed
139  delete differentialPlotsConstantEta ;
140  delete differentialPlotsConstantPt ;
141  }
142  } else if(dataFormatType == "TagCorrelation") {
143  const InputTag& label1 = iModule->getParameter<InputTag>("label1");
144  const InputTag& label2 = iModule->getParameter<InputTag>("label2");
145  tagCorrelationInputTags.push_back(std::pair<edm::InputTag, edm::InputTag>(label1, label2));
146  binTagCorrelationPlotters.push_back(vector<TagCorrelationPlotter*>());
147 
148  // eta loop
149  for ( int iEta = iEtaStart ; iEta != iEtaEnd ; ++iEta) {
150  // pt loop
151  for( int iPt = iPtStart ; iPt != iPtEnd ; ++iPt) {
152  const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
153  // Instantiate the generic b tag correlation plotter
154  TagCorrelationPlotter* tagCorrelationPlotter = new TagCorrelationPlotter(label1.label(), label2.label(), etaPtBin,
155  iModule->getParameter<edm::ParameterSet>("parameters"),
156  mcPlots_, update);
157  binTagCorrelationPlotters.back().push_back(tagCorrelationPlotter);
158  }
159  }
160  } else {
161  // tag info retrievel is deferred (needs availability of EventSetup)
162  const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
163  const string& folderName = iModule->getParameter<string>("folder");
164 
165  tagInfoInputTags.push_back(vector<edm::InputTag>());
166  tiDataFormatType.push_back(dataFormatType);
167  binTagInfoPlotters.push_back(vector<BaseTagInfoPlotter*>()) ;
168 
169  // eta loop
170  for ( int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta ) {
171  // pt loop
172  for ( int iPt = iPtStart ; iPt < iPtEnd ; ++iPt ) {
173  const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
174 
175  // Instantiate the tagInfo plotter
176 
177  BaseTagInfoPlotter *jetTagPlotter = theFactory.buildPlotter(dataFormatType, moduleLabel.label(),
178  etaPtBin, iModule->getParameter<edm::ParameterSet>("parameters"), folderName,
180  binTagInfoPlotters.back().push_back ( jetTagPlotter ) ;
181  binTagInfoPlottersToModuleConfig.insert(make_pair(jetTagPlotter, iModule - moduleConfig.begin()));
182  }
183  }
184 
185  edm::LogInfo("Info")
186  << "====>>>> ## sizeof differentialPlots = " << differentialPlots.size();
187  }
188  }
189 
190 
191 }
192 
193 EtaPtBin BTagPerformanceAnalyzerOnData::getEtaPtBin(const int& iEta, const int& iPt)
194 {
195  // DEFINE BTagBin:
196  bool etaActive_ , ptActive_;
197  double etaMin_, etaMax_, ptMin_, ptMax_ ;
198 
199  if ( iEta != -1 ) {
200  etaActive_ = true ;
201  etaMin_ = etaRanges[iEta] ;
202  etaMax_ = etaRanges[iEta+1] ;
203  }
204  else {
205  etaActive_ = false ;
206  etaMin_ = etaRanges[0] ;
207  etaMax_ = etaRanges[etaRanges.size() - 1] ;
208  }
209 
210  if ( iPt != -1 ) {
211  ptActive_ = true ;
212  ptMin_ = ptRanges[iPt] ;
213  ptMax_ = ptRanges[iPt+1] ;
214  }
215  else {
216  ptActive_ = false ;
217  ptMin_ = ptRanges[0] ;
218  ptMax_ = ptRanges[ptRanges.size() - 1] ;
219  }
220  return EtaPtBin(etaActive_ , etaMin_ , etaMax_ ,
221  ptActive_ , ptMin_ , ptMax_ );
222 }
223 
225 {
226  for (unsigned int iJetLabel = 0; iJetLabel != binJetTagPlotters.size(); ++iJetLabel) {
227  int plotterSize = binJetTagPlotters[iJetLabel].size();
228  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
229  delete binJetTagPlotters[iJetLabel][iPlotter];
230  }
231  if (finalize && mcPlots_){
232  for (vector<BTagDifferentialPlot *>::iterator iPlotter = differentialPlots[iJetLabel].begin();
233  iPlotter != differentialPlots[iJetLabel].end(); ++ iPlotter) {
234  delete *iPlotter;
235  }
236  }
237  }
238 
239  for (vector<vector<TagCorrelationPlotter*> >::iterator iJetLabel = binTagCorrelationPlotters.begin();
240  iJetLabel != binTagCorrelationPlotters.end(); ++iJetLabel)
241  for(vector<TagCorrelationPlotter*>::iterator iPlotter = iJetLabel->begin(); iPlotter != iJetLabel->end(); ++iPlotter)
242  delete *iPlotter;
243 
244  for (unsigned int iJetLabel = 0; iJetLabel != binTagInfoPlotters.size(); ++iJetLabel) {
245  int plotterSize = binTagInfoPlotters[iJetLabel].size();
246  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
247  delete binTagInfoPlotters[iJetLabel][iPlotter];
248  }
249  }
250 }
251 
253 {
254  if (finalizeOnly) return;
255  //
256  //no flavour map needed here
257 
259  iEvent.getByLabel(slInfoTag, infoHandle);
260 
261 // Look first at the jetTags
262 
263  for (unsigned int iJetLabel = 0; iJetLabel != jetTagInputTags.size(); ++iJetLabel) {
265  iEvent.getByLabel(jetTagInputTags[iJetLabel], tagHandle);
266  //
267  // insert check on the presence of the collections
268  //
269 
270  if (!tagHandle.isValid()){
271  edm::LogWarning("BTagPerformanceAnalyzerOnData")<<" Collection "<<jetTagInputTags[iJetLabel]<<" not present. Skipping it for this event.";
272  continue;
273  }
274 
275  const reco::JetTagCollection & tagColl = *(tagHandle.product());
276  LogDebug("Info") << "Found " << tagColl.size() << " B candidates in collection " << jetTagInputTags[iJetLabel];
277 
278  int plotterSize = binJetTagPlotters[iJetLabel].size();
279  for (JetTagCollection::const_iterator tagI = tagColl.begin();
280  tagI != tagColl.end(); ++tagI) {
281  // Identify parton associated to jet.
282 
283  if (!jetSelector(*(tagI->first), -1, infoHandle)) continue;
284  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
285  bool inBin = binJetTagPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*tagI->first);
286  // Fill histograms if in desired pt/rapidity bin.
287  if (inBin)
288  binJetTagPlotters[iJetLabel][iPlotter]->analyzeTag(*tagI, -1);
289  }
290  }
291  }
292 
293 // Now look at Tag Correlations
294  for (unsigned int iJetLabel = 0; iJetLabel != tagCorrelationInputTags.size(); ++iJetLabel) {
295  const std::pair<edm::InputTag, edm::InputTag>& inputTags = tagCorrelationInputTags[iJetLabel];
297  iEvent.getByLabel(inputTags.first, tagHandle1);
298  const reco::JetTagCollection& tagColl1 = *(tagHandle1.product());
299 
301  iEvent.getByLabel(inputTags.second, tagHandle2);
302  const reco::JetTagCollection& tagColl2 = *(tagHandle2.product());
303 
304  int plotterSize = binTagCorrelationPlotters[iJetLabel].size();
305  for (JetTagCollection::const_iterator tagI = tagColl1.begin(); tagI != tagColl1.end(); ++tagI) {
306  if (!jetSelector(*(tagI->first), -1, infoHandle))
307  continue;
308 
309  for(int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
310  bool inBin = binTagCorrelationPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*(tagI->first));
311 
312  if(inBin)
313  {
314  double discr2 = tagColl2[tagI->first];
315  binTagCorrelationPlotters[iJetLabel][iPlotter]->analyzeTags(tagI->second, discr2, -1);
316  }
317  }
318  }
319  }
320 // Now look at the TagInfos
321 
322  for (unsigned int iJetLabel = 0; iJetLabel != tiDataFormatType.size(); ++iJetLabel) {
323  int plotterSize = binTagInfoPlotters[iJetLabel].size();
324  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter)
325  binTagInfoPlotters[iJetLabel][iPlotter]->setEventSetup(iSetup);
326 
327  vector<edm::InputTag> & inputTags = tagInfoInputTags[iJetLabel];
328  if (inputTags.empty()) {
329  // deferred retrieval of input tags
330  BaseTagInfoPlotter *firstPlotter = binTagInfoPlotters[iJetLabel][0];
331  int iModule = binTagInfoPlottersToModuleConfig[firstPlotter];
332  vector<string> labels = firstPlotter->tagInfoRequirements();
333  if (labels.empty())
334  labels.push_back("label");
335  for (vector<string>::const_iterator iLabels = labels.begin();
336  iLabels != labels.end(); ++iLabels) {
337  edm::InputTag inputTag =
338  moduleConfig[iModule].getParameter<InputTag>(*iLabels);
339  inputTags.push_back(inputTag);
340  }
341  }
342 
343  unsigned int nInputTags = inputTags.size();
344  vector< edm::Handle< View<BaseTagInfo> > > tagInfoHandles(nInputTags);
345  edm::ProductID jetProductID;
346  unsigned int nTagInfos = 0;
347  for (unsigned int iInputTags = 0; iInputTags < inputTags.size(); ++iInputTags) {
348  edm::Handle< View<BaseTagInfo> > & tagInfoHandle = tagInfoHandles[iInputTags];
349  iEvent.getByLabel(inputTags[iInputTags], tagInfoHandle);
350  //
351  // protect against missing products
352  //
353  if (tagInfoHandle.isValid() == false){
354  edm::LogWarning("BTagPerformanceAnalyzerOnData")<<" Collection "<<inputTags[iInputTags]<<" not present. Skipping it for this event.";
355  continue;
356  }
357 
358 
359  unsigned int size = tagInfoHandle->size();
360  LogDebug("Info") << "Found " << size << " B candidates in collection " << inputTags[iInputTags];
361  edm::ProductID thisProductID = (size > 0) ? (*tagInfoHandle)[0].jet().id() : edm::ProductID();
362  if (iInputTags == 0) {
363  jetProductID = thisProductID;
364  nTagInfos = size;
365  } else if (jetProductID != thisProductID)
366  throw cms::Exception("Configuration") << "TagInfos are referencing a different jet collection." << endl;
367  else if (nTagInfos != size)
368  throw cms::Exception("Configuration") << "TagInfo collections are having a different size." << endl;
369  }
370 
371  for (unsigned int iTagInfos = 0; iTagInfos < nTagInfos; ++iTagInfos) {
372  vector<const BaseTagInfo*> baseTagInfos(nInputTags);
373  edm::RefToBase<Jet> jetRef;
374  for (unsigned int iTagInfo = 0; iTagInfo < nInputTags; iTagInfo++) {
375  const BaseTagInfo &baseTagInfo = (*tagInfoHandles[iTagInfo])[iTagInfos];
376  if (iTagInfo == 0)
377  jetRef = baseTagInfo.jet();
378  else if (baseTagInfo.jet() != jetRef)
379  throw cms::Exception("Configuration") << "TagInfos pointing to different jets." << endl;
380  baseTagInfos[iTagInfo] = &baseTagInfo;
381  }
382 
383  if (!jetSelector(*jetRef, -1, infoHandle))
384  continue;
385 
386  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
387  bool inBin = binTagInfoPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*jetRef);
388  // Fill histograms if in desired pt/rapidity bin.
389  if (inBin)
390  binTagInfoPlotters[iJetLabel][iPlotter]->analyzeTag(baseTagInfos, -1);
391  }
392  }
393  }
394 }
395 
397 
398  if (finalize == false) return;
399  setTDRStyle();
400  for (unsigned int iJetLabel = 0; iJetLabel != binJetTagPlotters.size(); ++iJetLabel) {
401  int plotterSize = binJetTagPlotters[iJetLabel].size();
402  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
403  binJetTagPlotters[iJetLabel][iPlotter]->finalize();
404  // binJetTagPlotters[iJetLabel][iPlotter]->write(allHisto);
405  if (producePs) (*binJetTagPlotters[iJetLabel][iPlotter]).psPlot(psBaseName);
406  if (produceEps) (*binJetTagPlotters[iJetLabel][iPlotter]).epsPlot(epsBaseName);
407  }
408 
409  if (mcPlots_ == true){
410  for (vector<BTagDifferentialPlot *>::iterator iPlotter = differentialPlots[iJetLabel].begin();
411  iPlotter != differentialPlots[iJetLabel].end(); ++ iPlotter) {
412  (**iPlotter).process();
413  if (producePs) (**iPlotter).psPlot(psBaseName);
414  if (produceEps) (**iPlotter).epsPlot(epsBaseName);
415  // (**iPlotter).write(allHisto);
416  }
417  }
418  }
419  for (unsigned int iJetLabel = 0; iJetLabel != binTagInfoPlotters.size(); ++iJetLabel) {
420  int plotterSize = binTagInfoPlotters[iJetLabel].size();
421  for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
422  binTagInfoPlotters[iJetLabel][iPlotter]->finalize();
423 
424  // binTagInfoPlotters[iJetLabel][iPlotter]->write(allHisto);
425  if (producePs) (*binTagInfoPlotters[iJetLabel][iPlotter]).psPlot(psBaseName);
426  if (produceEps) (*binTagInfoPlotters[iJetLabel][iPlotter]).epsPlot(epsBaseName);
427  }
428  }
429 
430 
431 }
432 
433 
434 //define this as a plug-in
#define LogDebug(id)
std::vector< std::vector< BTagDifferentialPlot * > > differentialPlots
std::vector< std::vector< JetTagPlotter * > > binJetTagPlotters
std::map< BaseTagInfoPlotter *, size_t > binTagInfoPlottersToModuleConfig
transient_vector_type::const_iterator const_iterator
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< std::string > tiDataFormatType
const_iterator end() const
std::vector< std::pair< edm::InputTag, edm::InputTag > > tagCorrelationInputTags
BTagPerformanceAnalyzerOnData(const edm::ParameterSet &pSet)
virtual edm::RefToBase< Jet > jet(void) const
returns a polymorphic reference to the tagged jet
Definition: BaseTagInfo.h:24
int iEvent
Definition: GenABIO.cc:243
BaseTagInfoPlotter * buildPlotter(const std::string &dataFormatType, const std::string &tagName, const EtaPtBin &etaPtBin, const edm::ParameterSet &pSet, const std::string &folderName, const bool &update, const bool &mc, const bool &wf)
bool isValid() const
Definition: HandleBase.h:76
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
std::vector< std::vector< edm::InputTag > > tagInfoInputTags
virtual std::vector< std::string > tagInfoRequirements() const
void bookHistos(const edm::ParameterSet &pSet)
std::vector< edm::ParameterSet > moduleConfig
std::vector< edm::InputTag > jetTagInputTags
T const * product() const
Definition: Handle.h:74
EtaPtBin getEtaPtBin(const int &iEta, const int &iPt)
std::string const & label() const
Definition: InputTag.h:25
std::vector< std::vector< TagCorrelationPlotter * > > binTagCorrelationPlotters
void endRun(const edm::Run &run, const edm::EventSetup &es)
void setTDRStyle()
Definition: plotscripts.py:87
#define begin
Definition: vmac.h:31
#define update(a, b)
ProductIndex id() const
Definition: ProductID.h:38
virtual void analyze(const edm::Event &iEvent, const edm::EventSetup &iSetup)
const_iterator begin() const
tuple size
Write out results.
Definition: Run.h:33
std::vector< std::vector< BaseTagInfoPlotter * > > binTagInfoPlotters
size_type size() const