Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:13:23

0001 #include "DQMOffline/Trigger/interface/EgHLTOfflineSummaryClient.h"
0002 
0003 #include "FWCore/Framework/interface/Run.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 
0010 #include "DQMServices/Core/interface/DQMStore.h"
0011 
0012 #include "DQMOffline/Trigger/interface/EgHLTTrigTools.h"
0013 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0014 #include <boost/algorithm/string.hpp>
0015 #include <fnmatch.h>
0016 
0017 EgHLTOfflineSummaryClient::EgHLTOfflineSummaryClient(const edm::ParameterSet& iConfig)
0018     : egHLTSumHistName_("egHLTTrigSum"), isSetup_(false) {
0019   dirName_ = iConfig.getParameter<std::string>(
0020       "DQMDirName");  //only one chance to get this, if we every have another shot, remember to check isSetup is okay
0021   dbe_ = edm::Service<DQMStore>().operator->();
0022   if (!dbe_) {
0023     edm::LogError("EgHLTOfflineSummaryClient")
0024         << "unable to get DQMStore service, no summary histograms will be produced";
0025   } else {
0026     dbe_->setCurrentFolder(dirName_);
0027   }
0028 
0029   eleHLTFilterNames_ = iConfig.getParameter<std::vector<std::string> >("eleHLTFilterNames");
0030   phoHLTFilterNames_ = iConfig.getParameter<std::vector<std::string> >("phoHLTFilterNames");
0031   eleHLTFilterNamesForSumBit_ = iConfig.getParameter<std::vector<std::string> >("eleHLTFilterNamesForSumBit");
0032   phoHLTFilterNamesForSumBit_ = iConfig.getParameter<std::vector<std::string> >("phoHLTFilterNamesForSumBit");
0033 
0034   filterInactiveTriggers_ = iConfig.getParameter<bool>("filterInactiveTriggers");
0035   hltTag_ = iConfig.getParameter<std::string>("hltTag");
0036 
0037   usePathNames_ = iConfig.getParameter<bool>("usePathNames");
0038 
0039   //std::vector<std::string> egHLTSumQTests = iConfig.getParameter<std::vector<std::string> >("egHLTSumQTests");
0040   // splitStringsToPairs_(egHLTSumQTests,egHLTSumHistXBins_);
0041 
0042   fillQTestData_(iConfig, egHLTSumHistXBins_, "egHLTSumQTests");
0043   fillQTestData_(iConfig, eleQTestsForSumBit_, "egHLTEleQTestsForSumBit");
0044   fillQTestData_(iConfig, phoQTestsForSumBit_, "egHLTPhoQTestsForSumBit");
0045 
0046   runClientEndLumiBlock_ = iConfig.getParameter<bool>("runClientEndLumiBlock");
0047   runClientEndRun_ = iConfig.getParameter<bool>("runClientEndRun");
0048   runClientEndJob_ = iConfig.getParameter<bool>("runClientEndJob");
0049 
0050   //egHLTSumHistXBins_.push_back(std::make_pair("Ele Rel Trig Eff",&EgHLTOfflineSummaryClient::eleTrigRelEffQTestResult_));
0051   //egHLTSumHistXBins_.push_back(std::make_pair("Pho Rel Trig Eff",&EgHLTOfflineSummaryClient::phoTrigRelEffQTestResult_));
0052   //egHLTSumHistXBins_.push_back(std::make_pair("Ele T&P Trig Eff",&EgHLTOfflineSummaryClient::eleTrigTPEffQTestResult_));
0053   //egHLTSumHistXBins_.push_back(std::make_pair("Triggered Ele",&EgHLTOfflineSummaryClient::trigEleQTestResult_));
0054   //egHLTSumHistXBins_.push_back(std::make_pair("Triggered Pho",&EgHLTOfflineSummaryClient::trigPhoQTestResult_));
0055 }
0056 
0057 EgHLTOfflineSummaryClient::~EgHLTOfflineSummaryClient() = default;
0058 
0059 void EgHLTOfflineSummaryClient::beginJob() {}
0060 
0061 void EgHLTOfflineSummaryClient::endJob() {
0062   if (runClientEndJob_)
0063     runClient_();
0064 }
0065 
0066 void EgHLTOfflineSummaryClient::beginRun(const edm::Run& run, const edm::EventSetup& c) {
0067   if (!isSetup_) {
0068     bool changed;
0069     HLTConfigProvider hltConfig;
0070     hltConfig.init(run, c, hltTag_, changed);
0071     if (filterInactiveTriggers_) {
0072       std::vector<std::string> activeFilters;
0073       std::vector<std::string> activeEleFilters;
0074       std::vector<std::string> activeEle2LegFilters;
0075       std::vector<std::string> activePhoFilters;
0076       std::vector<std::string> activePho2LegFilters;
0077 
0078       egHLT::trigTools::getActiveFilters(
0079           hltConfig, activeFilters, activeEleFilters, activeEle2LegFilters, activePhoFilters, activePho2LegFilters);
0080 
0081       egHLT::trigTools::filterInactiveTriggers(eleHLTFilterNames_, activeFilters);
0082       egHLT::trigTools::filterInactiveTriggers(phoHLTFilterNames_, activePhoFilters);
0083       egHLT::trigTools::filterInactiveTriggers(eleHLTFilterNamesForSumBit_, activeEleFilters);
0084       egHLT::trigTools::filterInactiveTriggers(phoHLTFilterNamesForSumBit_, activePhoFilters);
0085     }
0086     getEgHLTFiltersToMon_(egHLTFiltersToMon_);
0087 
0088     if (usePathNames_)
0089       egHLT::trigTools::translateFiltersToPathNames(hltConfig, egHLTFiltersToMon_, egHLTFiltersToMonPaths_);
0090     isSetup_ = true;
0091   }
0092 }
0093 
0094 void EgHLTOfflineSummaryClient::endRun(const edm::Run& run, const edm::EventSetup& c) {
0095   if (runClientEndRun_)
0096     runClient_();
0097 }
0098 
0099 //dummy analysis function
0100 void EgHLTOfflineSummaryClient::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {}
0101 
0102 void EgHLTOfflineSummaryClient::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& c) {
0103   if (runClientEndLumiBlock_)
0104     runClient_();
0105 }
0106 
0107 void EgHLTOfflineSummaryClient::runClient_() {
0108   MonitorElement* egHLTSumME = getEgHLTSumHist_();
0109 
0110   for (size_t filterNr = 0; filterNr < egHLTFiltersToMon_.size(); filterNr++) {
0111     for (size_t xBinNr = 0; xBinNr < egHLTSumHistXBins_.size(); xBinNr++) {
0112       //egHLTSumHist->setBinContent(xBinNr+1,filterNr+1,(*egHLTSumHistXBins_[xBinNr].second)(egHLTFiltersToMon_[filterNr].c_str()));
0113       egHLTSumME->setBinContent(
0114           xBinNr + 1,
0115           filterNr + 1,
0116           getQTestResults_(egHLTFiltersToMon_[filterNr], egHLTSumHistXBins_[xBinNr].qTestPatterns));
0117     }
0118   }
0119 
0120   MonitorElement* hltEleSumBit = dbe_->get("HLT/EventInfo/reportSummaryContents/HLT_Electron");
0121   MonitorElement* hltPhoSumBit = dbe_->get("HLT/EventInfo/reportSummaryContents/HLT_Photon");
0122   dbe_->setCurrentFolder("HLT/EventInfo/reportSummaryContents/");
0123   if (hltEleSumBit == nullptr)
0124     hltEleSumBit = dbe_->bookFloat("HLT_Electron");
0125   if (hltPhoSumBit == nullptr)
0126     hltPhoSumBit = dbe_->bookFloat("HLT_Photon");
0127 
0128   float eleSumBit = 1.;
0129   for (size_t filterNr = 0; filterNr < eleHLTFilterNamesForSumBit_.size() && eleSumBit == 1;
0130        filterNr++) {  //breaks as soon as a test fails
0131     for (size_t testNr = 0; testNr < eleQTestsForSumBit_.size() && eleSumBit == 1; testNr++) {
0132       if (getQTestResults_(eleHLTFilterNamesForSumBit_[filterNr], eleQTestsForSumBit_[testNr].qTestPatterns) == 0)
0133         eleSumBit = 0;
0134     }
0135   }
0136   hltEleSumBit->Fill(eleSumBit);
0137 
0138   float phoSumBit = 1.;
0139   for (size_t filterNr = 0; filterNr < phoHLTFilterNamesForSumBit_.size() && phoSumBit == 1;
0140        filterNr++) {  //breaks as soon as a test fails
0141     for (size_t testNr = 0; testNr < phoQTestsForSumBit_.size() && phoSumBit == 1; testNr++) {
0142       if (getQTestResults_(phoHLTFilterNamesForSumBit_[filterNr], phoQTestsForSumBit_[testNr].qTestPatterns) == 0)
0143         phoSumBit = 0;
0144     }
0145   }
0146   hltPhoSumBit->Fill(phoSumBit);
0147 }
0148 void EgHLTOfflineSummaryClient::splitStringsToPairs_(const std::vector<std::string>& stringsToSplit,
0149                                                      std::vector<std::pair<std::string, std::string> >& splitStrings) {
0150   splitStrings.clear();
0151   splitStrings.reserve(stringsToSplit.size());
0152   for (auto const& stringNr : stringsToSplit) {
0153     std::vector<std::string> tempSplitStrings;
0154     boost::split(tempSplitStrings, stringNr, boost::is_any_of(std::string(":")));
0155     if (tempSplitStrings.size() == 2) {
0156       splitStrings.push_back(std::make_pair(tempSplitStrings[0], tempSplitStrings[1]));
0157     } else {
0158       edm::LogWarning("EgHLTOfflineSummaryClient")
0159           << " Error : entry " << stringNr
0160           << " is not of form A:B, ignoring (ie this quailty test isnt being included in the sumamry hist) ";
0161     }
0162   }
0163 }
0164 
0165 EgHLTOfflineSummaryClient::MonitorElement* EgHLTOfflineSummaryClient::getEgHLTSumHist_() {
0166   MonitorElement* egHLTSumHist = dbe_->get(dirName_ + "/" + egHLTSumHistName_);
0167   if (egHLTSumHist == nullptr) {
0168     auto* hist = new TH2F(egHLTSumHistName_.c_str(),
0169                           "E/g HLT Offline Summary",
0170                           egHLTSumHistXBins_.size(),
0171                           0.,
0172                           1.,
0173                           egHLTFiltersToMon_.size(),
0174                           0.,
0175                           1.);
0176     for (size_t xBinNr = 0; xBinNr < egHLTSumHistXBins_.size(); xBinNr++) {
0177       hist->GetXaxis()->SetBinLabel(xBinNr + 1, egHLTSumHistXBins_[xBinNr].name.c_str());
0178     }
0179 
0180     for (size_t yBinNr = 0; yBinNr < egHLTFiltersToMon_.size(); yBinNr++) {
0181       if (usePathNames_ && egHLTFiltersToMonPaths_.size() == egHLTFiltersToMon_.size()) {
0182         hist->GetYaxis()->SetBinLabel(yBinNr + 1, egHLTFiltersToMonPaths_[yBinNr].c_str());
0183       } else {
0184         hist->GetYaxis()->SetBinLabel(yBinNr + 1, egHLTFiltersToMon_[yBinNr].c_str());
0185       }
0186     }
0187     for (size_t xBinNr = 0; xBinNr < egHLTSumHistXBins_.size(); xBinNr++) {
0188       for (size_t yBinNr = 0; yBinNr < egHLTFiltersToMon_.size(); yBinNr++) {
0189         hist->SetBinContent(xBinNr + 1, yBinNr + 1, -2);
0190       }
0191     }
0192 
0193     dbe_->setCurrentFolder(dirName_);
0194     egHLTSumHist = dbe_->book2D(egHLTSumHistName_, hist);
0195   }
0196   return egHLTSumHist;
0197 }
0198 
0199 //this function puts every e/g trigger monitored in a std::vector
0200 //this is *very* similar to EgHLTOfflineSource::getHLTFilterNamesUsed but
0201 //it differs in the fact it only gets the E/g primary triggers not the backups
0202 //due to the design, to ensure we get every filter, filters will be inserted multiple times
0203 //eg electron filters will contain photon triggers which are also in the photon filters
0204 //but only want one copy in the vector
0205 //this function is intended to be called once per job so some inefficiency can can be tolerated
0206 //therefore we will use a std::set to ensure ensure that each filtername is only inserted once
0207 //and then convert to a std::vector
0208 void EgHLTOfflineSummaryClient::getEgHLTFiltersToMon_(std::vector<std::string>& filterNames) const {
0209   std::set<std::string> filterNameSet;
0210   for (auto const& eleHLTFilterName : eleHLTFilterNames_)
0211     filterNameSet.insert(eleHLTFilterName);
0212   for (auto const& phoHLTFilterName : phoHLTFilterNames_)
0213     filterNameSet.insert(phoHLTFilterName);
0214 
0215   //right all the triggers are inserted once and only once in the set, convert to vector
0216   //very lazy, create a new vector so can use the constructor and then use swap to transfer
0217   std::vector<std::string>(filterNameSet.begin(), filterNameSet.end()).swap(filterNames);
0218 }
0219 
0220 //only returns 0 or 1, 0 is bad, one is good and if the test is not found defaults to good
0221 //(this is because its a dumb algorithm, photon tests are run for electron triggers which unsurprisingly are not found)
0222 int EgHLTOfflineSummaryClient::getQTestResults_(const std::string& filterName,
0223                                                 const std::vector<std::string>& patterns) const {
0224   int nrFail = 0;
0225   int nrQTests = 0;
0226   for (auto const& pattern : patterns) {
0227     auto filterpattern = filterName + pattern;
0228     std::vector<MonitorElement*> monElems = dbe_->getAllContents(dirName_);
0229     // std::cout <<"mon elem "<<dirName_+"/"+filterName+patterns[patternNr]<<"nr monElems "<<monElems.size()<<std::endl;
0230     for (auto& monElem : monElems) {
0231       const auto& name = monElem->getName();
0232       int match = fnmatch(filterpattern.c_str(), name.c_str(), 0);
0233       if (match == FNM_NOMATCH)
0234         continue;
0235 
0236       std::vector<QReport*> qTests = monElem->getQReports();
0237       nrQTests += qTests.size();
0238       //  std::cout <<monElems[monElemNr]->getName()<<" "<<monElems[monElemNr]->hasError()<<" nr test "<<qTests.size()<<std::endl;
0239       if (monElem->hasError())
0240         nrFail++;
0241     }
0242   }
0243   if (nrQTests == 0)
0244     return -1;
0245   else if (nrFail == 0)
0246     return 1;
0247   else
0248     return 0;
0249 }
0250 
0251 void EgHLTOfflineSummaryClient::fillQTestData_(const edm::ParameterSet& iConfig,
0252                                                std::vector<SumHistBinData>& qTests,
0253                                                const std::string& label) {
0254   std::vector<edm::ParameterSet> qTestPara = iConfig.getParameter<std::vector<edm::ParameterSet> >(label);
0255   qTests.resize(qTestPara.size());
0256   for (size_t testNr = 0; testNr < qTestPara.size(); testNr++) {
0257     qTests[testNr].name = qTestPara[testNr].getParameter<std::string>("name");
0258     qTests[testNr].qTestPatterns = qTestPara[testNr].getParameter<std::vector<std::string> >("qTestsToCheck");
0259   }
0260 }
0261 
0262 // int EgHLTOfflineSummaryClient::eleTrigRelEffQTestResult_(const std::string& filterName)const
0263 // {
0264 
0265 // }
0266 
0267 // int EgHLTOfflineSummaryClient::phoTrigRelEffQTestResult_(const std::string& filterName)const
0268 // {
0269 
0270 // }
0271 
0272 // int EgHLTOfflineSummaryClient::eleTrigTPEffQTestResult_(const std::string& filterName)const
0273 // {
0274 
0275 // }
0276 
0277 // int EgHLTOfflineSummaryClient::trigEleQTestResult_(const std::string& filterName)const
0278 // {
0279 
0280 // }
0281 
0282 // int EgHLTOfflineSummaryClient::trigPhoQTestResult_(const std::string& filterName)const
0283 // {
0284 
0285 // }