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