00001 #include "FWCore/Framework/interface/MakerMacros.h"
00002 #include "FWCore/Framework/interface/Frameworkfwd.h"
00003 #include "FWCore/Utilities/interface/Exception.h"
00004 #include "DQMOffline/RecoB/plugins/BTagPerformanceAnalyzerOnData.h"
00005 #include "DQMOffline/RecoB/interface/JetTagPlotter.h"
00006 #include "DQMOffline/RecoB/interface/TagInfoPlotterFactory.h"
00007 #include "DQMOffline/RecoB/interface/Tools.h"
00008 #include "DataFormats/Common/interface/View.h"
00009 #include "DQMServices/Core/interface/DQMStore.h"
00010 #include "FWCore/ServiceRegistry/interface/Service.h"
00011
00012 using namespace reco;
00013 using namespace edm;
00014 using namespace std;
00015 using namespace RecoBTag;
00016
00017 BTagPerformanceAnalyzerOnData::BTagPerformanceAnalyzerOnData(const edm::ParameterSet& pSet) :
00018 partonKinematics(pSet.getParameter< bool >("partonKinematics")),
00019 jetSelector(
00020 pSet.getParameter<double>("etaMin"),
00021 pSet.getParameter<double>("etaMax"),
00022 pSet.getParameter<double>("ptRecJetMin"),
00023 pSet.getParameter<double>("ptRecJetMax"),
00024 0.0, 99999.0,
00025 pSet.getParameter<double>("ratioMin"),
00026 pSet.getParameter<double>("ratioMax")
00027 ),
00028 etaRanges(pSet.getParameter< vector<double> >("etaRanges")),
00029 ptRanges(pSet.getParameter< vector<double> >("ptRanges")),
00030 produceEps(pSet.getParameter< bool >("produceEps")),
00031 producePs(pSet.getParameter< bool >("producePs")),
00032 psBaseName(pSet.getParameter<std::string>( "psBaseName" )),
00033 epsBaseName(pSet.getParameter<std::string>( "epsBaseName" )),
00034 inputFile(pSet.getParameter<std::string>( "inputfile" )),
00035 update(pSet.getParameter<bool>( "update" )),
00036 allHisto(pSet.getParameter<bool>( "allHistograms" )),
00037 finalize(pSet.getParameter< bool >("finalizePlots")),
00038 finalizeOnly(pSet.getParameter< bool >("finalizeOnly")),
00039 slInfoTag(pSet.getParameter<edm::InputTag>("softLeptonInfo")),
00040 moduleConfig(pSet.getParameter< vector<edm::ParameterSet> >("tagConfig")),
00041 mcPlots_(pSet.getParameter< bool >("mcPlots"))
00042 {
00043 bookHistos(pSet);
00044 }
00045
00046 void BTagPerformanceAnalyzerOnData::bookHistos(const edm::ParameterSet& pSet)
00047 {
00048
00049
00050
00051
00052 if (update) {
00053
00054
00055
00056
00057 }
00058
00059
00060
00061
00062
00063
00064 const int iEtaStart = -1 ;
00065 const int iEtaEnd = etaRanges.size() - 1 ;
00066 const int iPtStart = -1 ;
00067 const int iPtEnd = ptRanges.size() - 1 ;
00068 setTDRStyle();
00069
00070 TagInfoPlotterFactory theFactory;
00071
00072 for (vector<edm::ParameterSet>::const_iterator iModule = moduleConfig.begin();
00073 iModule != moduleConfig.end(); ++iModule) {
00074
00075 const string& dataFormatType = iModule->exists("type") ?
00076 iModule->getParameter<string>("type") :
00077 "JetTag";
00078 if (dataFormatType == "JetTag") {
00079 const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
00080 const string& folderName = iModule->getParameter<string>("folder");
00081 jetTagInputTags.push_back(moduleLabel);
00082 binJetTagPlotters.push_back(vector<JetTagPlotter*>()) ;
00083
00084 vector<BTagDifferentialPlot*> * differentialPlotsConstantEta = new vector<BTagDifferentialPlot*> () ;
00085 vector<BTagDifferentialPlot*> * differentialPlotsConstantPt = new vector<BTagDifferentialPlot*> () ;
00086 if (finalize && mcPlots_){
00087 differentialPlots.push_back(vector<BTagDifferentialPlot*>());
00088
00089
00090 const double& effBConst =
00091 iModule->getParameter<edm::ParameterSet>("parameters").getParameter<double>("effBConst");
00092
00093
00094
00095 for ( int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta ) {
00096 BTagDifferentialPlot * etaConstDifferentialPlot = new BTagDifferentialPlot
00097 (effBConst, BTagDifferentialPlot::constETA, moduleLabel.label());
00098 differentialPlotsConstantEta->push_back ( etaConstDifferentialPlot );
00099 }
00100
00101 for ( int iPt = iPtStart ; iPt < iPtEnd ; ++iPt ) {
00102
00103 BTagDifferentialPlot * ptConstDifferentialPlot = new BTagDifferentialPlot
00104 (effBConst, BTagDifferentialPlot::constPT, moduleLabel.label());
00105 differentialPlotsConstantPt->push_back ( ptConstDifferentialPlot );
00106 }
00107 }
00108
00109
00110 for ( int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta ) {
00111
00112 for ( int iPt = iPtStart ; iPt < iPtEnd ; ++iPt ) {
00113
00114 const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
00115
00116
00117 JetTagPlotter *jetTagPlotter = new JetTagPlotter(folderName, etaPtBin,
00118 iModule->getParameter<edm::ParameterSet>("parameters"), mcPlots_, update, finalize);
00119 binJetTagPlotters.back().push_back ( jetTagPlotter ) ;
00120
00121
00122 if (finalize && mcPlots_){
00123 (*differentialPlotsConstantEta)[iEta+1]->addBinPlotter ( jetTagPlotter ) ;
00124 (*differentialPlotsConstantPt )[iPt+1] ->addBinPlotter ( jetTagPlotter ) ;
00125 }
00126 }
00127 }
00128
00129
00130 if (finalize && mcPlots_){
00131 differentialPlots.back().reserve(differentialPlotsConstantEta->size()+differentialPlotsConstantPt->size()) ;
00132 differentialPlots.back().insert(differentialPlots.back().end(), differentialPlotsConstantEta->begin(), differentialPlotsConstantEta->end());
00133 differentialPlots.back().insert(differentialPlots.back().end(), differentialPlotsConstantPt->begin(), differentialPlotsConstantPt->end());
00134
00135 edm::LogInfo("Info")
00136 << "====>>>> ## sizeof differentialPlots = " << differentialPlots.size();
00137
00138
00139 delete differentialPlotsConstantEta ;
00140 delete differentialPlotsConstantPt ;
00141 }
00142 } else if(dataFormatType == "TagCorrelation") {
00143 const InputTag& label1 = iModule->getParameter<InputTag>("label1");
00144 const InputTag& label2 = iModule->getParameter<InputTag>("label2");
00145 tagCorrelationInputTags.push_back(std::pair<edm::InputTag, edm::InputTag>(label1, label2));
00146 binTagCorrelationPlotters.push_back(vector<TagCorrelationPlotter*>());
00147
00148
00149 for ( int iEta = iEtaStart ; iEta != iEtaEnd ; ++iEta) {
00150
00151 for( int iPt = iPtStart ; iPt != iPtEnd ; ++iPt) {
00152 const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
00153
00154 TagCorrelationPlotter* tagCorrelationPlotter = new TagCorrelationPlotter(label1.label(), label2.label(), etaPtBin,
00155 iModule->getParameter<edm::ParameterSet>("parameters"),
00156 mcPlots_, update);
00157 binTagCorrelationPlotters.back().push_back(tagCorrelationPlotter);
00158 }
00159 }
00160 } else {
00161
00162 const InputTag& moduleLabel = iModule->getParameter<InputTag>("label");
00163 const string& folderName = iModule->getParameter<string>("folder");
00164
00165 tagInfoInputTags.push_back(vector<edm::InputTag>());
00166 tiDataFormatType.push_back(dataFormatType);
00167 binTagInfoPlotters.push_back(vector<BaseTagInfoPlotter*>()) ;
00168
00169
00170 for ( int iEta = iEtaStart ; iEta < iEtaEnd ; ++iEta ) {
00171
00172 for ( int iPt = iPtStart ; iPt < iPtEnd ; ++iPt ) {
00173 const EtaPtBin& etaPtBin = getEtaPtBin(iEta, iPt);
00174
00175
00176
00177 BaseTagInfoPlotter *jetTagPlotter = theFactory.buildPlotter(dataFormatType, moduleLabel.label(),
00178 etaPtBin, iModule->getParameter<edm::ParameterSet>("parameters"), folderName,
00179 update, mcPlots_, finalize);
00180 binTagInfoPlotters.back().push_back ( jetTagPlotter ) ;
00181 binTagInfoPlottersToModuleConfig.insert(make_pair(jetTagPlotter, iModule - moduleConfig.begin()));
00182 }
00183 }
00184
00185 edm::LogInfo("Info")
00186 << "====>>>> ## sizeof differentialPlots = " << differentialPlots.size();
00187 }
00188 }
00189
00190
00191 }
00192
00193 EtaPtBin BTagPerformanceAnalyzerOnData::getEtaPtBin(const int& iEta, const int& iPt)
00194 {
00195
00196 bool etaActive_ , ptActive_;
00197 double etaMin_, etaMax_, ptMin_, ptMax_ ;
00198
00199 if ( iEta != -1 ) {
00200 etaActive_ = true ;
00201 etaMin_ = etaRanges[iEta] ;
00202 etaMax_ = etaRanges[iEta+1] ;
00203 }
00204 else {
00205 etaActive_ = false ;
00206 etaMin_ = etaRanges[0] ;
00207 etaMax_ = etaRanges[etaRanges.size() - 1] ;
00208 }
00209
00210 if ( iPt != -1 ) {
00211 ptActive_ = true ;
00212 ptMin_ = ptRanges[iPt] ;
00213 ptMax_ = ptRanges[iPt+1] ;
00214 }
00215 else {
00216 ptActive_ = false ;
00217 ptMin_ = ptRanges[0] ;
00218 ptMax_ = ptRanges[ptRanges.size() - 1] ;
00219 }
00220 return EtaPtBin(etaActive_ , etaMin_ , etaMax_ ,
00221 ptActive_ , ptMin_ , ptMax_ );
00222 }
00223
00224 BTagPerformanceAnalyzerOnData::~BTagPerformanceAnalyzerOnData()
00225 {
00226 for (unsigned int iJetLabel = 0; iJetLabel != binJetTagPlotters.size(); ++iJetLabel) {
00227 int plotterSize = binJetTagPlotters[iJetLabel].size();
00228 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00229 delete binJetTagPlotters[iJetLabel][iPlotter];
00230 }
00231 if (finalize && mcPlots_){
00232 for (vector<BTagDifferentialPlot *>::iterator iPlotter = differentialPlots[iJetLabel].begin();
00233 iPlotter != differentialPlots[iJetLabel].end(); ++ iPlotter) {
00234 delete *iPlotter;
00235 }
00236 }
00237 }
00238
00239 for (vector<vector<TagCorrelationPlotter*> >::iterator iJetLabel = binTagCorrelationPlotters.begin();
00240 iJetLabel != binTagCorrelationPlotters.end(); ++iJetLabel)
00241 for(vector<TagCorrelationPlotter*>::iterator iPlotter = iJetLabel->begin(); iPlotter != iJetLabel->end(); ++iPlotter)
00242 delete *iPlotter;
00243
00244 for (unsigned int iJetLabel = 0; iJetLabel != binTagInfoPlotters.size(); ++iJetLabel) {
00245 int plotterSize = binTagInfoPlotters[iJetLabel].size();
00246 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00247 delete binTagInfoPlotters[iJetLabel][iPlotter];
00248 }
00249 }
00250 }
00251
00252 void BTagPerformanceAnalyzerOnData::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00253 {
00254 if (finalizeOnly) return;
00255
00256
00257
00258 edm::Handle<reco::SoftLeptonTagInfoCollection> infoHandle;
00259 iEvent.getByLabel(slInfoTag, infoHandle);
00260
00261
00262
00263 for (unsigned int iJetLabel = 0; iJetLabel != jetTagInputTags.size(); ++iJetLabel) {
00264 edm::Handle<reco::JetTagCollection> tagHandle;
00265 iEvent.getByLabel(jetTagInputTags[iJetLabel], tagHandle);
00266
00267
00268
00269
00270 if (!tagHandle.isValid()){
00271 edm::LogWarning("BTagPerformanceAnalyzerOnData")<<" Collection "<<jetTagInputTags[iJetLabel]<<" not present. Skipping it for this event.";
00272 continue;
00273 }
00274
00275 const reco::JetTagCollection & tagColl = *(tagHandle.product());
00276 LogDebug("Info") << "Found " << tagColl.size() << " B candidates in collection " << jetTagInputTags[iJetLabel];
00277
00278 int plotterSize = binJetTagPlotters[iJetLabel].size();
00279 for (JetTagCollection::const_iterator tagI = tagColl.begin();
00280 tagI != tagColl.end(); ++tagI) {
00281
00282
00283 if (!jetSelector(*(tagI->first), -1, infoHandle)) continue;
00284 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00285 bool inBin = binJetTagPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*tagI->first);
00286
00287 if (inBin)
00288 binJetTagPlotters[iJetLabel][iPlotter]->analyzeTag(*tagI, -1);
00289 }
00290 }
00291 }
00292
00293
00294 for (unsigned int iJetLabel = 0; iJetLabel != tagCorrelationInputTags.size(); ++iJetLabel) {
00295 const std::pair<edm::InputTag, edm::InputTag>& inputTags = tagCorrelationInputTags[iJetLabel];
00296 edm::Handle<reco::JetTagCollection> tagHandle1;
00297 iEvent.getByLabel(inputTags.first, tagHandle1);
00298 const reco::JetTagCollection& tagColl1 = *(tagHandle1.product());
00299
00300 edm::Handle<reco::JetTagCollection> tagHandle2;
00301 iEvent.getByLabel(inputTags.second, tagHandle2);
00302 const reco::JetTagCollection& tagColl2 = *(tagHandle2.product());
00303
00304 int plotterSize = binTagCorrelationPlotters[iJetLabel].size();
00305 for (JetTagCollection::const_iterator tagI = tagColl1.begin(); tagI != tagColl1.end(); ++tagI) {
00306 if (!jetSelector(*(tagI->first), -1, infoHandle))
00307 continue;
00308
00309 for(int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00310 bool inBin = binTagCorrelationPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*(tagI->first));
00311
00312 if(inBin)
00313 {
00314 double discr2 = tagColl2[tagI->first];
00315 binTagCorrelationPlotters[iJetLabel][iPlotter]->analyzeTags(tagI->second, discr2, -1);
00316 }
00317 }
00318 }
00319 }
00320
00321
00322 for (unsigned int iJetLabel = 0; iJetLabel != tiDataFormatType.size(); ++iJetLabel) {
00323 int plotterSize = binTagInfoPlotters[iJetLabel].size();
00324 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter)
00325 binTagInfoPlotters[iJetLabel][iPlotter]->setEventSetup(iSetup);
00326
00327 vector<edm::InputTag> & inputTags = tagInfoInputTags[iJetLabel];
00328 if (inputTags.empty()) {
00329
00330 BaseTagInfoPlotter *firstPlotter = binTagInfoPlotters[iJetLabel][0];
00331 int iModule = binTagInfoPlottersToModuleConfig[firstPlotter];
00332 vector<string> labels = firstPlotter->tagInfoRequirements();
00333 if (labels.empty())
00334 labels.push_back("label");
00335 for (vector<string>::const_iterator iLabels = labels.begin();
00336 iLabels != labels.end(); ++iLabels) {
00337 edm::InputTag inputTag =
00338 moduleConfig[iModule].getParameter<InputTag>(*iLabels);
00339 inputTags.push_back(inputTag);
00340 }
00341 }
00342
00343 unsigned int nInputTags = inputTags.size();
00344 vector< edm::Handle< View<BaseTagInfo> > > tagInfoHandles(nInputTags);
00345 edm::ProductID jetProductID;
00346 unsigned int nTagInfos = 0;
00347 for (unsigned int iInputTags = 0; iInputTags < inputTags.size(); ++iInputTags) {
00348 edm::Handle< View<BaseTagInfo> > & tagInfoHandle = tagInfoHandles[iInputTags];
00349 iEvent.getByLabel(inputTags[iInputTags], tagInfoHandle);
00350
00351
00352
00353 if (tagInfoHandle.isValid() == false){
00354 edm::LogWarning("BTagPerformanceAnalyzerOnData")<<" Collection "<<inputTags[iInputTags]<<" not present. Skipping it for this event.";
00355 continue;
00356 }
00357
00358
00359 unsigned int size = tagInfoHandle->size();
00360 LogDebug("Info") << "Found " << size << " B candidates in collection " << inputTags[iInputTags];
00361 edm::ProductID thisProductID = (size > 0) ? (*tagInfoHandle)[0].jet().id() : edm::ProductID();
00362 if (iInputTags == 0) {
00363 jetProductID = thisProductID;
00364 nTagInfos = size;
00365 } else if (jetProductID != thisProductID)
00366 throw cms::Exception("Configuration") << "TagInfos are referencing a different jet collection." << endl;
00367 else if (nTagInfos != size)
00368 throw cms::Exception("Configuration") << "TagInfo collections are having a different size." << endl;
00369 }
00370
00371 for (unsigned int iTagInfos = 0; iTagInfos < nTagInfos; ++iTagInfos) {
00372 vector<const BaseTagInfo*> baseTagInfos(nInputTags);
00373 edm::RefToBase<Jet> jetRef;
00374 for (unsigned int iTagInfo = 0; iTagInfo < nInputTags; iTagInfo++) {
00375 const BaseTagInfo &baseTagInfo = (*tagInfoHandles[iTagInfo])[iTagInfos];
00376 if (iTagInfo == 0)
00377 jetRef = baseTagInfo.jet();
00378 else if (baseTagInfo.jet() != jetRef)
00379 throw cms::Exception("Configuration") << "TagInfos pointing to different jets." << endl;
00380 baseTagInfos[iTagInfo] = &baseTagInfo;
00381 }
00382
00383 if (!jetSelector(*jetRef, -1, infoHandle))
00384 continue;
00385
00386 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00387 bool inBin = binTagInfoPlotters[iJetLabel][iPlotter]->etaPtBin().inBin(*jetRef);
00388
00389 if (inBin)
00390 binTagInfoPlotters[iJetLabel][iPlotter]->analyzeTag(baseTagInfos, -1);
00391 }
00392 }
00393 }
00394 }
00395
00396 void BTagPerformanceAnalyzerOnData::endRun(const edm::Run & run, const edm::EventSetup & es){
00397
00398 if (finalize == false) return;
00399 setTDRStyle();
00400 for (unsigned int iJetLabel = 0; iJetLabel != binJetTagPlotters.size(); ++iJetLabel) {
00401 int plotterSize = binJetTagPlotters[iJetLabel].size();
00402 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00403 binJetTagPlotters[iJetLabel][iPlotter]->finalize();
00404
00405 if (producePs) (*binJetTagPlotters[iJetLabel][iPlotter]).psPlot(psBaseName);
00406 if (produceEps) (*binJetTagPlotters[iJetLabel][iPlotter]).epsPlot(epsBaseName);
00407 }
00408
00409 if (mcPlots_ == true){
00410 for (vector<BTagDifferentialPlot *>::iterator iPlotter = differentialPlots[iJetLabel].begin();
00411 iPlotter != differentialPlots[iJetLabel].end(); ++ iPlotter) {
00412 (**iPlotter).process();
00413 if (producePs) (**iPlotter).psPlot(psBaseName);
00414 if (produceEps) (**iPlotter).epsPlot(epsBaseName);
00415
00416 }
00417 }
00418 }
00419 for (unsigned int iJetLabel = 0; iJetLabel != binTagInfoPlotters.size(); ++iJetLabel) {
00420 int plotterSize = binTagInfoPlotters[iJetLabel].size();
00421 for (int iPlotter = 0; iPlotter != plotterSize; ++iPlotter) {
00422 binTagInfoPlotters[iJetLabel][iPlotter]->finalize();
00423
00424
00425 if (producePs) (*binTagInfoPlotters[iJetLabel][iPlotter]).psPlot(psBaseName);
00426 if (produceEps) (*binTagInfoPlotters[iJetLabel][iPlotter]).epsPlot(epsBaseName);
00427 }
00428 }
00429
00430
00431 }
00432
00433
00434
00435 DEFINE_FWK_MODULE(BTagPerformanceAnalyzerOnData);