Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-10 02:54:05

0001 #ifndef DQMOnline_Trigger_HLTDQMHistColl_h
0002 #define DQMOnline_Trigger_HLTDQMHistColl_h
0003 
0004 //********************************************************************************
0005 //
0006 // Description:
0007 //   This contains a collection of HLTDQMHists used to measure the efficiency of a
0008 //   specified filter. It is resonsible for booking and filling the histograms
0009 //   For every hist specified, it books two, one to record the
0010 //   total objects passing sample selection passed to the class and then one for those objects
0011 //   which then pass the HLT filter
0012 //   The class contains a simple selection cuts (mostly intended for kinematic range cuts)
0013 //   which are passed to the histograms as they are filled.
0014 //   The cuts are passed to the histograms as some histograms will ignore certain cuts
0015 //   for example, eff vs Et will ignore any global Et cuts applied
0016 //
0017 // Author : Sam Harper , RAL, May 2017
0018 //
0019 //***********************************************************************************
0020 
0021 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0023 
0024 #include "DQMServices/Core/interface/DQMStore.h"
0025 
0026 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0027 
0028 #include "DQMOffline/Trigger/interface/HLTDQMHist.h"
0029 #include "DQMOffline/Trigger/interface/VarRangeCutColl.h"
0030 #include "DQMOffline/Trigger/interface/FunctionDefs.h"
0031 #include "DQMOffline/Trigger/interface/UtilFuncs.h"
0032 
0033 template <typename ObjType>
0034 class HLTDQMFilterEffHists {
0035 public:
0036   typedef dqm::legacy::MonitorElement MonitorElement;
0037   typedef dqm::legacy::DQMStore DQMStore;
0038 
0039   explicit HLTDQMFilterEffHists(const edm::ParameterSet& config, std::string baseHistName, std::string hltProcess);
0040 
0041   static edm::ParameterSetDescription makePSetDescription();
0042   static edm::ParameterSetDescription makePSetDescriptionHistConfigs();
0043 
0044   void bookHists(DQMStore::IBooker& iBooker, const std::vector<edm::ParameterSet>& histConfigs);
0045   void fillHists(const ObjType& obj,
0046                  const edm::Event& event,
0047                  const edm::EventSetup& setup,
0048                  const trigger::TriggerEvent& trigEvt);
0049 
0050 private:
0051   void book1D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig);
0052   void book2D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig);
0053 
0054 private:
0055   std::vector<std::unique_ptr<HLTDQMHist<ObjType> > > histsPass_;
0056   std::vector<std::unique_ptr<HLTDQMHist<ObjType> > > histsTot_;
0057   VarRangeCutColl<ObjType> rangeCuts_;
0058   std::string filterName_;
0059   std::string histTitle_;
0060   std::string folderName_;
0061   std::string baseHistName_;
0062   std::string hltProcess_;
0063 };
0064 
0065 template <typename ObjType>
0066 HLTDQMFilterEffHists<ObjType>::HLTDQMFilterEffHists(const edm::ParameterSet& config,
0067                                                     std::string baseHistName,
0068                                                     std::string hltProcess)
0069     : rangeCuts_(config.getParameter<std::vector<edm::ParameterSet> >("rangeCuts")),
0070       filterName_(config.getParameter<std::string>("filterName")),
0071       histTitle_(config.getParameter<std::string>("histTitle")),
0072       folderName_(config.getParameter<std::string>("folderName")),
0073       baseHistName_(std::move(baseHistName)),
0074       hltProcess_(std::move(hltProcess)) {}
0075 
0076 template <typename ObjType>
0077 edm::ParameterSetDescription HLTDQMFilterEffHists<ObjType>::makePSetDescription() {
0078   edm::ParameterSetDescription desc;
0079   desc.addVPSet("rangeCuts", VarRangeCut<ObjType>::makePSetDescription(), std::vector<edm::ParameterSet>());
0080   desc.add<std::string>("filterName", "");
0081   desc.add<std::string>("histTitle", "");
0082   desc.add<std::string>("folderName", "");
0083   return desc;
0084 }
0085 
0086 template <typename ObjType>
0087 edm::ParameterSetDescription HLTDQMFilterEffHists<ObjType>::makePSetDescriptionHistConfigs() {
0088   edm::ParameterSetDescription desc;
0089 
0090   //what this is doing is trival and is left as an exercise to the reader
0091   auto histDescCases =
0092       "1D" >> (edm::ParameterDescription<std::vector<double> >("binLowEdges", std::vector<double>(), true) and
0093                edm::ParameterDescription<std::string>("nameSuffex", "", true) and
0094                edm::ParameterDescription<std::string>("vsVar", "", true)) or
0095       "2D" >> (edm::ParameterDescription<std::vector<double> >("xBinLowEdges", std::vector<double>(), true) and
0096                edm::ParameterDescription<std::vector<double> >("yBinLowEdges", std::vector<double>(), true) and
0097                edm::ParameterDescription<std::string>("nameSuffex", "", true) and
0098                edm::ParameterDescription<std::string>("xVar", "", true) and
0099                edm::ParameterDescription<std::string>("yVar", "", true));
0100 
0101   desc.ifValue(edm::ParameterDescription<std::string>("histType", "1D", true), std::move(histDescCases));
0102   desc.addVPSet("rangeCuts", VarRangeCut<ObjType>::makePSetDescription(), std::vector<edm::ParameterSet>());
0103   return desc;
0104 }
0105 
0106 template <typename ObjType>
0107 void HLTDQMFilterEffHists<ObjType>::bookHists(DQMStore::IBooker& iBooker,
0108                                               const std::vector<edm::ParameterSet>& histConfigs) {
0109   iBooker.setCurrentFolder(folderName_);
0110   for (auto& histConfig : histConfigs) {
0111     std::string histType = histConfig.getParameter<std::string>("histType");
0112     if (histType == "1D") {
0113       book1D(iBooker, histConfig);
0114     } else if (histType == "2D") {
0115       book2D(iBooker, histConfig);
0116     } else {
0117       throw cms::Exception("ConfigError") << " histType " << histType << " not recognised" << std::endl;
0118     }
0119   }
0120 }
0121 
0122 template <typename ObjType>
0123 void HLTDQMFilterEffHists<ObjType>::book1D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig) {
0124   auto binLowEdgesDouble = histConfig.getParameter<std::vector<double> >("binLowEdges");
0125   std::vector<float> binLowEdges;
0126   binLowEdges.reserve(binLowEdgesDouble.size());
0127   for (double lowEdge : binLowEdgesDouble)
0128     binLowEdges.push_back(lowEdge);
0129   auto nameSuffex = histConfig.getParameter<std::string>("nameSuffex");
0130   auto mePass = iBooker.book1D((baseHistName_ + filterName_ + nameSuffex + "_pass").c_str(),
0131                                (histTitle_ + nameSuffex + " Pass").c_str(),
0132                                binLowEdges.size() - 1,
0133                                &binLowEdges[0]);
0134   std::unique_ptr<HLTDQMHist<ObjType> > hist;
0135   auto vsVar = histConfig.getParameter<std::string>("vsVar");
0136   auto vsVarFunc = hltdqm::getUnaryFuncFloat<ObjType>(vsVar);
0137   if (!vsVarFunc) {
0138     throw cms::Exception("ConfigError") << " vsVar " << vsVar << " is giving null ptr (likely empty) in " << __FILE__
0139                                         << "," << __LINE__ << std::endl;
0140   }
0141   VarRangeCutColl<ObjType> rangeCuts(histConfig.getParameter<std::vector<edm::ParameterSet> >("rangeCuts"));
0142   hist = std::make_unique<HLTDQMHist1D<ObjType, float> >(mePass->getTH1(), vsVar, vsVarFunc, rangeCuts);
0143   histsPass_.emplace_back(std::move(hist));
0144   auto meTot = iBooker.book1D((baseHistName_ + filterName_ + nameSuffex + "_tot").c_str(),
0145                               (histTitle_ + nameSuffex + " Total").c_str(),
0146                               binLowEdges.size() - 1,
0147                               &binLowEdges[0]);
0148   hist = std::make_unique<HLTDQMHist1D<ObjType, float> >(meTot->getTH1(), vsVar, vsVarFunc, rangeCuts);
0149   histsTot_.emplace_back(std::move(hist));
0150 }
0151 
0152 template <typename ObjType>
0153 void HLTDQMFilterEffHists<ObjType>::book2D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig) {
0154   auto xBinLowEdgesDouble = histConfig.getParameter<std::vector<double> >("xBinLowEdges");
0155   auto yBinLowEdgesDouble = histConfig.getParameter<std::vector<double> >("yBinLowEdges");
0156   std::vector<float> xBinLowEdges;
0157   std::vector<float> yBinLowEdges;
0158   xBinLowEdges.reserve(xBinLowEdgesDouble.size());
0159   for (double lowEdge : xBinLowEdgesDouble)
0160     xBinLowEdges.push_back(lowEdge);
0161   yBinLowEdges.reserve(yBinLowEdgesDouble.size());
0162   for (double lowEdge : yBinLowEdgesDouble)
0163     yBinLowEdges.push_back(lowEdge);
0164   auto nameSuffex = histConfig.getParameter<std::string>("nameSuffex");
0165   auto mePass = iBooker.book2D((baseHistName_ + filterName_ + nameSuffex + "_pass").c_str(),
0166                                (histTitle_ + nameSuffex + " Pass").c_str(),
0167                                xBinLowEdges.size() - 1,
0168                                &xBinLowEdges[0],
0169                                yBinLowEdges.size() - 1,
0170                                &yBinLowEdges[0]);
0171   std::unique_ptr<HLTDQMHist<ObjType> > hist;
0172   auto xVar = histConfig.getParameter<std::string>("xVar");
0173   auto yVar = histConfig.getParameter<std::string>("yVar");
0174   auto xVarFunc = hltdqm::getUnaryFuncFloat<ObjType>(xVar);
0175   auto yVarFunc = hltdqm::getUnaryFuncFloat<ObjType>(yVar);
0176   if (!xVarFunc || !yVarFunc) {
0177     throw cms::Exception("ConfigError") << " xVar " << xVar << " or yVar " << yVar
0178                                         << " is giving null ptr (likely empty str passed)" << std::endl;
0179   }
0180   VarRangeCutColl<ObjType> rangeCuts(histConfig.getParameter<std::vector<edm::ParameterSet> >("rangeCuts"));
0181 
0182   //really? really no MonitorElement::getTH2...sigh
0183   hist = std::make_unique<HLTDQMHist2D<ObjType, float> >(
0184       static_cast<TH2*>(mePass->getTH1()), xVar, yVar, xVarFunc, yVarFunc, rangeCuts);
0185   histsPass_.emplace_back(std::move(hist));
0186 
0187   auto meTot = iBooker.book2D((baseHistName_ + filterName_ + nameSuffex + "_tot").c_str(),
0188                               (histTitle_ + nameSuffex + " Total").c_str(),
0189                               xBinLowEdges.size() - 1,
0190                               &xBinLowEdges[0],
0191                               yBinLowEdges.size() - 1,
0192                               &yBinLowEdges[0]);
0193 
0194   hist = std::make_unique<HLTDQMHist2D<ObjType, float> >(
0195       static_cast<TH2*>(meTot->getTH1()), xVar, yVar, xVarFunc, yVarFunc, rangeCuts);
0196   histsTot_.emplace_back(std::move(hist));
0197 }
0198 
0199 template <typename ObjType>
0200 void HLTDQMFilterEffHists<ObjType>::fillHists(const ObjType& obj,
0201                                               const edm::Event& event,
0202                                               const edm::EventSetup& setup,
0203                                               const trigger::TriggerEvent& trigEvt) {
0204   for (auto& hist : histsTot_) {
0205     hist->fill(obj, event, setup, rangeCuts_);
0206   }
0207 
0208   if (hltdqm::passTrig(obj.eta(), obj.phi(), trigEvt, filterName_, hltProcess_)) {
0209     for (auto& hist : histsPass_) {
0210       hist->fill(obj, event, setup, rangeCuts_);
0211     }
0212   }
0213 }
0214 #endif