Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:17:09

0001 // -*- C++ -*-
0002 //
0003 // Package:    TestBtagPayloads
0004 // Class:      TestBtagPayloads
0005 //
0006 /**\class TestBtagPayloads.cc
0007 
0008  Description: [one line class summary]
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Michael Segala
0015 //         Created:  Wed Jun 20 02:47:47 CDT 2012
0016 //
0017 //
0018 
0019 // system include files
0020 #include <iostream>
0021 #include <memory>
0022 #include <map>
0023 #include <stdio.h>
0024 
0025 // user include files
0026 #include "FWCore/Framework/interface/Frameworkfwd.h"
0027 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0028 
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/MakerMacros.h"
0031 
0032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0033 #include "FWCore/Framework/interface/EventSetup.h"
0034 #include "FWCore/Framework/interface/ESHandle.h"
0035 
0036 #include "RecoBTag/Records/interface/BTagPerformanceRecord.h"
0037 #include "CondFormats/PhysicsToolsObjects/interface/BinningPointByMap.h"
0038 #include "RecoBTag/PerformanceDB/interface/BtagPerformance.h"
0039 
0040 class TestBtagPayloads : public edm::one::EDAnalyzer<> {
0041 public:
0042   explicit TestBtagPayloads(const edm::ParameterSet&);
0043 
0044 private:
0045   void analyze(const edm::Event&, const edm::EventSetup&) final;
0046 
0047   const edm::ESGetToken<BtagPerformance, BTagPerformanceRecord> ttToken_;
0048   const edm::ESGetToken<BtagPerformance, BTagPerformanceRecord> muToken_;
0049   const edm::ESGetToken<BtagPerformance, BTagPerformanceRecord> mistagToken_;
0050 
0051   std::map<std::string, PerformanceResult::ResultType> measureMap_;
0052 
0053   const std::vector<std::string> measureName_;
0054   const std::vector<std::string> measureType_;
0055 
0056   std::vector<edm::ESGetToken<BtagPerformance, BTagPerformanceRecord>> measureTokens_;
0057 };
0058 
0059 namespace {
0060   //Possible algorithms: TTBARDISCRIMBTAGCSV, TTBARDISCRIMBTAGJP, TTBARDISCRIMBTAGTCHP
0061   constexpr char const* const ttName = "TTBARDISCRIMBTAGCSV";
0062 
0063   //Possible algorithms: MUJETSWPBTAGCSVL,  MUJETSWPBTAGCSVM,   MUJETSWPBTAGCSVT
0064   //                     MUJETSWPBTAGJPL,   MUJETSWPBTAGJPM,    MUJETSWPBTAGJPT
0065   //                                                            MUJETSWPBTAGTCHPT
0066   constexpr char const* const muName = "MUJETSWPBTAGCSVL";
0067 
0068   //Possible algorithms: MISTAGCSVLAB,  MISTAGCSVMAB,   MISTAGCSVTAB
0069   //                     MISTAGJPLAB,   MISTAGJPMAB,    MISTAGJPTAB                // Data period 2012 AB
0070   //                                                    MISTAGTCHPTAB
0071   //
0072   //                     MISTAGCSVLABCD,  MISTAGCSVMABCD,   MISTAGCSVTABCD
0073   //                     MISTAGJPLABCD,   MISTAGJPMABCD,    MISTAGJPTABCD          // Data period 2012 ABCD
0074   //                                                        MISTAGTCHPTABCD
0075   //
0076   //                     MISTAGCSVLC,  MISTAGCSVMC,   MISTAGCSVTC
0077   //                     MISTAGJPLC,   MISTAGJPMC,    MISTAGJPTC                   // Data period 2012 C
0078   //                                                  MISTAGTCHPTC
0079   //
0080   //                     MISTAGCSVLD,  MISTAGCSVMD,   MISTAGCSVTD
0081   //                     MISTAGJPLD,   MISTAGJPMD,    MISTAGJPTD                   // Data period 2012 D
0082   //                                                  MISTAGTCHPTD
0083   constexpr char const* const mistagName = "MISTAGTCHPTD";
0084 }  // namespace
0085 TestBtagPayloads::TestBtagPayloads(const edm::ParameterSet& iConfig)
0086     : ttToken_(esConsumes(edm::ESInputTag("", ttName))),
0087       muToken_(esConsumes(edm::ESInputTag("", muName))),
0088       mistagToken_(esConsumes(edm::ESInputTag("", mistagName))),
0089       //Possible algorithms: TTBARWPBTAGCSVL,  TTBARWPBTAGCSVM,   TTBARWPBTAGCSVT
0090       //                     TTBARWPBTAGJPL,   TTBARWPBTAGJPM,    TTBARWPBTAGJPT
0091       //                                                          TTBARWPBTAGTCHPT
0092       measureName_({"TTBARWPBTAGCSVL",
0093                     "TTBARWPBTAGCSVL",
0094                     "TTBARWPBTAGCSVL",
0095                     "TTBARWPBTAGCSVL",
0096                     "TTBARWPBTAGJPT",
0097                     "TTBARWPBTAGJPT",
0098                     "TTBARWPBTAGJPT",
0099                     "TTBARWPBTAGJPT"}),
0100       measureType_({"BTAGBEFFCORR",
0101                     "BTAGBERRCORR",
0102                     "BTAGCEFFCORR",
0103                     "BTAGCERRCORR",
0104                     "BTAGBEFFCORR",
0105                     "BTAGBERRCORR",
0106                     "BTAGCEFFCORR",
0107                     "BTAGCERRCORR"}) {
0108   if (measureName_.size() != measureType_.size()) {
0109     std::cout << "measureName_, measureType_ size mismatch!" << std::endl;
0110     exit(-1);
0111   }
0112 
0113   measureMap_["BTAGBEFFCORR"] = PerformanceResult::BTAGBEFFCORR;
0114   measureMap_["BTAGBERRCORR"] = PerformanceResult::BTAGBERRCORR;
0115   measureMap_["BTAGCEFFCORR"] = PerformanceResult::BTAGCEFFCORR;
0116   measureMap_["BTAGCERRCORR"] = PerformanceResult::BTAGCERRCORR;
0117 
0118   measureTokens_.reserve(measureName_.size());
0119 
0120   //only call esConsumes if the name changes, else reuse the token
0121   std::map<std::string, edm::ESGetToken<BtagPerformance, BTagPerformanceRecord>> tokens;
0122   for (auto const& n : measureName_) {
0123     auto insert = tokens.insert({n, {}});
0124     if (insert.second) {
0125       insert.first->second = esConsumes(edm::ESInputTag("", n));
0126     }
0127     measureTokens_.push_back(insert.first->second);
0128   }
0129 }
0130 
0131 //
0132 // member functions
0133 //
0134 
0135 // ------------ method called for each event  ------------
0136 void TestBtagPayloads::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0137   using namespace edm;
0138   edm::ESHandle<BtagPerformance> perfH;
0139   BinningPointByMap p;
0140 
0141   //
0142   //++++++++++++------  TESTING FOR TTBAR SF's and efficiencies using CONTINIOUS DISCRIMINATORS      --------+++++++++++++
0143   //
0144 
0145   printf("\033[22;31m \n TESTING FOR TTBAR SF's and efficiencies using CONTINIOUS DISCRIMINATORS \n\033[0m");
0146 
0147   std::cout << " Studying performance with label " << ttName << std::endl;
0148   const BtagPerformance& perf = iSetup.getData(ttToken_);
0149 
0150   std::cout << " My Performance Object is indeed a " << typeid(&perf).name() << std::endl;
0151 
0152   std::cout << " The WP is defined by a cut at " << perf.workingPoint().cut() << std::endl;
0153   std::cout << " Discriminant is " << perf.workingPoint().discriminantName() << std::endl;
0154   std::cout << " Is cut based WP " << perf.workingPoint().cutBased() << std::endl;
0155 
0156   p.insert(BinningVariables::JetEta, 0.6);
0157   p.insert(BinningVariables::Discriminator, 0.23);
0158 
0159   std::cout << " test eta=0.6, discrim = 0.23" << std::endl;
0160   std::cout << " bSF/bFSerr ?" << perf.isResultOk(PerformanceResult::BTAGBEFFCORR, p) << "/"
0161             << perf.isResultOk(PerformanceResult::BTAGBERRCORR, p) << std::endl;
0162   std::cout << " bSF/bSFerr =" << perf.getResult(PerformanceResult::BTAGBEFFCORR, p) << "/"
0163             << perf.getResult(PerformanceResult::BTAGBERRCORR, p) << std::endl;
0164 
0165   std::cout << std::endl;
0166   std::cout << std::endl;
0167 
0168   //
0169   //++++++++++++------  TESTING FOR TTBAR WP's   --------+++++++++++++
0170   //
0171 
0172   printf("\033[22;31m TESTING FOR TTBAR WP's \n\033[0m");
0173 
0174   for (size_t iMeasure = 0; iMeasure < measureName_.size(); iMeasure++) {
0175     std::cout << "Testing: " << measureName_[iMeasure] << " of type " << measureType_[iMeasure] << std::endl;
0176 
0177     //Setup our measurement
0178     const BtagPerformance& perf2 = iSetup.getData(measureTokens_[iMeasure]);
0179 
0180     //Working point
0181     std::cout << "Working point: " << perf2.workingPoint().cut() << std::endl;
0182     //Setup the point we wish to test!
0183     BinningPointByMap measurePoint;
0184     measurePoint.insert(BinningVariables::JetEt, 50);
0185     measurePoint.insert(BinningVariables::JetEta, 0.6);
0186 
0187     std::cout << "Is it OK? " << perf2.isResultOk(measureMap_[measureType_[iMeasure]], measurePoint)
0188               << " result at 50 GeV, 0,6 |eta| = " << perf2.getResult(measureMap_[measureType_[iMeasure]], measurePoint)
0189               << std::endl;
0190   }
0191 
0192   std::cout << std::endl;
0193   std::cout << std::endl;
0194 
0195   //
0196   //++++++++++++------  TESTING FOR Mu+Jets WP's   --------+++++++++++++
0197   //
0198 
0199   printf("\033[22;31m TESTING FOR Mu+Jets WP's \n\033[0m");
0200 
0201   std::cout << " Studying performance with label " << muName << std::endl;
0202   const BtagPerformance& perf3 = iSetup.getData(muToken_);
0203 
0204   std::cout << " My Performance Object is indeed a " << typeid(&perf3).name() << std::endl;
0205 
0206   std::cout << " The WP is defined by a cut at " << perf3.workingPoint().cut() << std::endl;
0207   std::cout << " Discriminant is " << perf3.workingPoint().discriminantName() << std::endl;
0208   std::cout << " Is cut based WP " << perf3.workingPoint().cutBased() << std::endl;
0209 
0210   p.insert(BinningVariables::JetEta, 0.6);
0211   p.insert(BinningVariables::JetEt, 50);
0212 
0213   std::cout << " test eta=0.6, et = 50" << std::endl;
0214   std::cout << " bSF/bFSerr ?" << perf3.isResultOk(PerformanceResult::BTAGBEFFCORR, p) << "/"
0215             << perf3.isResultOk(PerformanceResult::BTAGBERRCORR, p) << std::endl;
0216   std::cout << " bSF/bSFerr =" << perf3.getResult(PerformanceResult::BTAGBEFFCORR, p) << "/"
0217             << perf3.getResult(PerformanceResult::BTAGBERRCORR, p) << std::endl;
0218 
0219   std::cout << std::endl;
0220   std::cout << std::endl;
0221 
0222   //
0223   //++++++++++++------  TESTING FOR Mu+Jets Mistags   --------+++++++++++++
0224   //
0225 
0226   printf("\033[22;31m TESTING FOR Mu+Jets Mistags \n\033[0m");
0227 
0228   std::cout << " Studying performance with label " << mistagName << std::endl;
0229   const BtagPerformance& perf4 = iSetup.getData(mistagToken_);
0230 
0231   std::cout << " My Performance Object is indeed a " << typeid(&perf4).name() << std::endl;
0232 
0233   std::cout << " The WP is defined by a cut at " << perf4.workingPoint().cut() << std::endl;
0234   std::cout << " Discriminant is " << perf4.workingPoint().discriminantName() << std::endl;
0235   std::cout << " Is cut based WP " << perf4.workingPoint().cutBased() << std::endl;
0236 
0237   p.insert(BinningVariables::JetEta, 0.6);
0238   p.insert(BinningVariables::JetEt, 50);
0239 
0240   std::cout << " test eta=0.6, et = 50" << std::endl;
0241   std::cout << " leff ?" << perf4.isResultOk(PerformanceResult::BTAGLEFF, p) << std::endl;
0242   std::cout << " leff =" << perf4.getResult(PerformanceResult::BTAGLEFF, p) << std::endl;
0243   std::cout << " bSF/bFSerr ?" << perf4.isResultOk(PerformanceResult::BTAGLEFFCORR, p) << "/"
0244             << perf4.isResultOk(PerformanceResult::BTAGLERRCORR, p) << std::endl;
0245   std::cout << " bSF/bSFerr =" << perf4.getResult(PerformanceResult::BTAGLEFFCORR, p) << "/"
0246             << perf4.getResult(PerformanceResult::BTAGLERRCORR, p) << std::endl;
0247 }
0248 
0249 //define this as a plug-in
0250 DEFINE_FWK_MODULE(TestBtagPayloads);