Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:50

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 "DataFormats/HLTReco/interface/TriggerEvent.h"
0022 #include "DQMOffline/Trigger/interface/HLTDQMHist.h"
0023 #include "DQMOffline/Trigger/interface/VarRangeCutColl.h"
0024 #include "DQMOffline/Trigger/interface/FunctionDefs.h"
0025 #include "DQMOffline/Trigger/interface/UtilFuncs.h"
0026 #include "DQMServices/Core/interface/DQMStore.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0029 
0030 template <typename ObjType>
0031 class HLTDQMFilterEffHists {
0032 public:
0033   typedef dqm::legacy::DQMStore DQMStore;
0034 
0035   explicit HLTDQMFilterEffHists(const edm::ParameterSet& config, std::string baseHistName, std::string hltProcess);
0036 
0037   static edm::ParameterSetDescription makePSetDescription();
0038   static edm::ParameterSetDescription makePSetDescriptionHistConfigs();
0039 
0040   void bookHists(DQMStore::IBooker& iBooker, const std::vector<edm::ParameterSet>& histConfigs);
0041   void fillHists(const ObjType& obj,
0042                  const edm::Event& event,
0043                  const edm::EventSetup& setup,
0044                  const trigger::TriggerEvent& trigEvt);
0045 
0046 private:
0047   void book1D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig);
0048   void book2D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig);
0049 
0050 private:
0051   std::vector<std::unique_ptr<HLTDQMHist<ObjType>>> histsPass_;
0052   std::vector<std::unique_ptr<HLTDQMHist<ObjType>>> histsTot_;
0053   VarRangeCutColl<ObjType> rangeCuts_;
0054   std::string filterName_;
0055   std::string histTitle_;
0056   std::string folderName_;
0057   std::string baseHistName_;
0058   std::string hltProcess_;
0059 };
0060 
0061 template <typename ObjType>
0062 HLTDQMFilterEffHists<ObjType>::HLTDQMFilterEffHists(const edm::ParameterSet& config,
0063                                                     std::string baseHistName,
0064                                                     std::string hltProcess)
0065     : rangeCuts_(config.getParameter<std::vector<edm::ParameterSet>>("rangeCuts")),
0066       filterName_(config.getParameter<std::string>("filterName")),
0067       histTitle_(config.getParameter<std::string>("histTitle")),
0068       folderName_(config.getParameter<std::string>("folderName")),
0069       baseHistName_(std::move(baseHistName)),
0070       hltProcess_(std::move(hltProcess)) {}
0071 
0072 template <typename ObjType>
0073 edm::ParameterSetDescription HLTDQMFilterEffHists<ObjType>::makePSetDescription() {
0074   edm::ParameterSetDescription desc;
0075   desc.addVPSet("rangeCuts", VarRangeCut<ObjType>::makePSetDescription(), std::vector<edm::ParameterSet>());
0076   desc.add<std::string>("filterName", "");
0077   desc.add<std::string>("histTitle", "");
0078   desc.add<std::string>("folderName", "");
0079   return desc;
0080 }
0081 
0082 template <typename ObjType>
0083 edm::ParameterSetDescription HLTDQMFilterEffHists<ObjType>::makePSetDescriptionHistConfigs() {
0084   edm::ParameterSetDescription desc;
0085 
0086   //what this is doing is trival and is left as an exercise to the reader
0087   auto histDescCases =
0088       "1D" >> (edm::ParameterDescription<std::vector<double>>("binLowEdges", std::vector<double>(), true) and
0089                edm::ParameterDescription<std::string>("nameSuffex", "", true) and
0090                edm::ParameterDescription<std::string>("vsVar", "", true)) or
0091       "2D" >> (edm::ParameterDescription<std::vector<double>>("xBinLowEdges", std::vector<double>(), true) and
0092                edm::ParameterDescription<std::vector<double>>("yBinLowEdges", std::vector<double>(), true) and
0093                edm::ParameterDescription<std::string>("nameSuffex", "", true) and
0094                edm::ParameterDescription<std::string>("xVar", "", true) and
0095                edm::ParameterDescription<std::string>("yVar", "", true));
0096 
0097   desc.ifValue(edm::ParameterDescription<std::string>("histType", "1D", true), std::move(histDescCases));
0098   desc.addVPSet("rangeCuts", VarRangeCut<ObjType>::makePSetDescription(), std::vector<edm::ParameterSet>());
0099   return desc;
0100 }
0101 
0102 template <typename ObjType>
0103 void HLTDQMFilterEffHists<ObjType>::bookHists(DQMStore::IBooker& iBooker,
0104                                               const std::vector<edm::ParameterSet>& histConfigs) {
0105   iBooker.setCurrentFolder(folderName_);
0106   for (auto& histConfig : histConfigs) {
0107     std::string histType = histConfig.getParameter<std::string>("histType");
0108     if (histType == "1D") {
0109       book1D(iBooker, histConfig);
0110     } else if (histType == "2D") {
0111       book2D(iBooker, histConfig);
0112     } else {
0113       throw cms::Exception("ConfigError") << " histType " << histType << " not recognised" << std::endl;
0114     }
0115   }
0116 }
0117 
0118 template <typename ObjType>
0119 void HLTDQMFilterEffHists<ObjType>::book1D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig) {
0120   auto binLowEdgesDouble = histConfig.getParameter<std::vector<double>>("binLowEdges");
0121   std::vector<float> binLowEdges;
0122   binLowEdges.reserve(binLowEdgesDouble.size());
0123   for (double lowEdge : binLowEdgesDouble)
0124     binLowEdges.push_back(lowEdge);
0125   auto nameSuffex = histConfig.getParameter<std::string>("nameSuffex");
0126   auto mePass = iBooker.book1D((baseHistName_ + filterName_ + nameSuffex + "_pass").c_str(),
0127                                (histTitle_ + nameSuffex + " Pass").c_str(),
0128                                binLowEdges.size() - 1,
0129                                &binLowEdges[0]);
0130   std::unique_ptr<HLTDQMHist<ObjType>> hist;
0131   auto vsVar = histConfig.getParameter<std::string>("vsVar");
0132   auto vsVarFunc = hltdqm::getUnaryFuncFloat<ObjType>(vsVar);
0133   if (!vsVarFunc) {
0134     throw cms::Exception("ConfigError") << " vsVar " << vsVar << " is giving null ptr (likely empty) in " << __FILE__
0135                                         << "," << __LINE__ << std::endl;
0136   }
0137   VarRangeCutColl<ObjType> rangeCuts(histConfig.getParameter<std::vector<edm::ParameterSet>>("rangeCuts"));
0138   hist = std::make_unique<HLTDQMHist1D<ObjType, float>>(mePass, vsVar, vsVarFunc, rangeCuts);
0139   histsPass_.emplace_back(std::move(hist));
0140   auto meTot = iBooker.book1D((baseHistName_ + filterName_ + nameSuffex + "_tot").c_str(),
0141                               (histTitle_ + nameSuffex + " Total").c_str(),
0142                               binLowEdges.size() - 1,
0143                               &binLowEdges[0]);
0144   hist = std::make_unique<HLTDQMHist1D<ObjType, float>>(meTot, vsVar, vsVarFunc, rangeCuts);
0145   histsTot_.emplace_back(std::move(hist));
0146 }
0147 
0148 template <typename ObjType>
0149 void HLTDQMFilterEffHists<ObjType>::book2D(DQMStore::IBooker& iBooker, const edm::ParameterSet& histConfig) {
0150   auto xBinLowEdgesDouble = histConfig.getParameter<std::vector<double>>("xBinLowEdges");
0151   auto yBinLowEdgesDouble = histConfig.getParameter<std::vector<double>>("yBinLowEdges");
0152   std::vector<float> xBinLowEdges;
0153   std::vector<float> yBinLowEdges;
0154   xBinLowEdges.reserve(xBinLowEdgesDouble.size());
0155   for (double lowEdge : xBinLowEdgesDouble)
0156     xBinLowEdges.push_back(lowEdge);
0157   yBinLowEdges.reserve(yBinLowEdgesDouble.size());
0158   for (double lowEdge : yBinLowEdgesDouble)
0159     yBinLowEdges.push_back(lowEdge);
0160   auto nameSuffex = histConfig.getParameter<std::string>("nameSuffex");
0161   auto mePass = iBooker.book2D((baseHistName_ + filterName_ + nameSuffex + "_pass").c_str(),
0162                                (histTitle_ + nameSuffex + " Pass").c_str(),
0163                                xBinLowEdges.size() - 1,
0164                                &xBinLowEdges[0],
0165                                yBinLowEdges.size() - 1,
0166                                &yBinLowEdges[0]);
0167   std::unique_ptr<HLTDQMHist<ObjType>> hist;
0168   auto xVar = histConfig.getParameter<std::string>("xVar");
0169   auto yVar = histConfig.getParameter<std::string>("yVar");
0170   auto xVarFunc = hltdqm::getUnaryFuncFloat<ObjType>(xVar);
0171   auto yVarFunc = hltdqm::getUnaryFuncFloat<ObjType>(yVar);
0172   if (!xVarFunc || !yVarFunc) {
0173     throw cms::Exception("ConfigError") << " xVar " << xVar << " or yVar " << yVar
0174                                         << " is giving null ptr (likely empty str passed)" << std::endl;
0175   }
0176   VarRangeCutColl<ObjType> rangeCuts(histConfig.getParameter<std::vector<edm::ParameterSet>>("rangeCuts"));
0177 
0178   hist = std::make_unique<HLTDQMHist2D<ObjType, float>>(mePass, xVar, yVar, xVarFunc, yVarFunc, rangeCuts);
0179   histsPass_.emplace_back(std::move(hist));
0180 
0181   auto meTot = iBooker.book2D((baseHistName_ + filterName_ + nameSuffex + "_tot").c_str(),
0182                               (histTitle_ + nameSuffex + " Total").c_str(),
0183                               xBinLowEdges.size() - 1,
0184                               &xBinLowEdges[0],
0185                               yBinLowEdges.size() - 1,
0186                               &yBinLowEdges[0]);
0187 
0188   hist = std::make_unique<HLTDQMHist2D<ObjType, float>>(meTot, xVar, yVar, xVarFunc, yVarFunc, rangeCuts);
0189   histsTot_.emplace_back(std::move(hist));
0190 }
0191 
0192 template <typename ObjType>
0193 void HLTDQMFilterEffHists<ObjType>::fillHists(const ObjType& obj,
0194                                               const edm::Event& event,
0195                                               const edm::EventSetup& setup,
0196                                               const trigger::TriggerEvent& trigEvt) {
0197   for (auto& hist : histsTot_) {
0198     hist->fill(obj, event, setup, rangeCuts_);
0199   }
0200 
0201   if (hltdqm::passTrig(obj.eta(), obj.phi(), trigEvt, filterName_, hltProcess_)) {
0202     for (auto& hist : histsPass_) {
0203       hist->fill(obj, event, setup, rangeCuts_);
0204     }
0205   }
0206 }
0207 #endif