Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/Framework/interface/Frameworkfwd.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 #include "DQMServices/Core/interface/DQMEDHarvester.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 
0006 #include "DQMServices/Core/interface/DQMStore.h"
0007 #include "FWCore/ServiceRegistry/interface/Service.h"
0008 
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 
0011 #include "FWCore/ServiceRegistry/interface/Service.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 
0014 #include <set>
0015 #include <string>
0016 #include <vector>
0017 #include <TPRegexp.h>
0018 #include <cmath>
0019 #include <climits>
0020 #include <boost/tokenizer.hpp>
0021 #include <boost/regex.hpp>
0022 
0023 #include <TH1.h>
0024 #include <TEfficiency.h>
0025 
0026 using namespace edm;
0027 using namespace std;
0028 
0029 class HLTMuonRefMethod : public DQMEDHarvester {
0030 public:
0031   explicit HLTMuonRefMethod(const edm::ParameterSet& set);
0032   ~HLTMuonRefMethod() override = default;
0033   ;
0034 
0035   void beginJob() override;
0036   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0037   void dqmEndJob(DQMStore::IBooker&, DQMStore::IGetter&) override;
0038 
0039 private:
0040   // DQMStore* theDQM;
0041 
0042   std::vector<std::string> subDirs_;
0043   std::vector<std::string> hltTriggers_;
0044   std::vector<std::string> efficiency_;
0045   std::vector<std::string> refEff_;
0046   std::string refTriggers_;
0047 
0048   std::string outputFileName_;
0049 
0050   void findAllSubdirectories(DQMStore::IBooker& ibooker,
0051                              DQMStore::IGetter& igetter,
0052                              const std::string& dir,
0053                              std::set<std::string>* myList,
0054                              const TString& pattern);
0055 };
0056 
0057 HLTMuonRefMethod::HLTMuonRefMethod(const edm::ParameterSet& pset) {
0058   using VPSet = std::vector<edm::ParameterSet>;
0059   using vstring = std::vector<std::string>;
0060   using elsc = boost::escaped_list_separator<char>;
0061 
0062   subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
0063   hltTriggers_ = pset.getUntrackedParameter<vstring>("hltTriggers");
0064   refTriggers_ = pset.getUntrackedParameter<string>("refTriggers");
0065   efficiency_ = pset.getUntrackedParameter<vstring>("efficiency");
0066   refEff_ = pset.getUntrackedParameter<vstring>("refEff");
0067 }
0068 
0069 void HLTMuonRefMethod::beginJob() {}
0070 
0071 void HLTMuonRefMethod::dqmEndJob(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
0072   using vstring = std::vector<std::string>;
0073   boost::regex metacharacters{"[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]"};
0074   boost::smatch what;
0075 
0076   // theDQM = 0;
0077   // theDQM = Service<DQMStore>().operator->();
0078 
0079   ibooker.cd();
0080   set<string> subDirSet;
0081 
0082   for (auto iSubDir = subDirs_.begin(); iSubDir != subDirs_.end(); ++iSubDir) {
0083     string subDir = *iSubDir;
0084 
0085     if (subDir[subDir.size() - 1] == '/')
0086       subDir.erase(subDir.size() - 1);
0087 
0088     if (boost::regex_search(subDir, what, metacharacters)) {
0089       const string::size_type shiftPos = subDir.rfind('/');
0090       const string searchPath = subDir.substr(0, shiftPos);
0091       const string pattern = subDir.substr(shiftPos + 1, subDir.length());
0092       //std::cout << "\n\n\n\nLooking for all subdirs of " << subDir << std::endl;
0093 
0094       findAllSubdirectories(ibooker, igetter, searchPath, &subDirSet, pattern);
0095 
0096     } else {
0097       subDirSet.insert(subDir);
0098     }
0099   }
0100 
0101   for (auto const& subDir : subDirSet) {
0102     for (unsigned int iEff = 0; iEff != efficiency_.size(); ++iEff) {
0103       string eff = efficiency_[iEff];
0104 
0105       // Getting reference trigger efficiency
0106       MonitorElement* refEff = igetter.get(subDir + "/" + refTriggers_ + "/" + refEff_[iEff]);
0107 
0108       if (!refEff)
0109         continue;
0110 
0111       // looping over all reference triggers
0112       for (auto iTrigger = hltTriggers_.begin(); iTrigger != hltTriggers_.end(); ++iTrigger) {
0113         string trig = *iTrigger;
0114         MonitorElement* trigEff = igetter.get(subDir + "/" + trig + "/" + eff);
0115         if (!trigEff)
0116           continue;
0117         TH1* hRef = refEff->getTH1();
0118         TH1* hTrig = trigEff->getTH1();
0119         TH1* hEff = (TH1*)hTrig->Clone(("eff_" + eff + "_ref").c_str());
0120         hEff->SetTitle("Efficiency obtained with reference method");
0121         TClass* myHistClass = hTrig->IsA();
0122         TString histClassName = myHistClass->GetName();
0123 
0124         if (histClassName == "TH1F") {
0125           for (int bin = 0; bin < hEff->GetNbinsX(); ++bin) {
0126             hEff->SetBinContent(bin + 1, hEff->GetBinContent(bin + 1) * hRef->GetBinContent(bin + 1));
0127             hEff->SetBinError(bin + 1,
0128                               hEff->GetBinContent(bin + 1) * hRef->GetBinError(bin + 1) +
0129                                   hEff->GetBinError(bin + 1) * hRef->GetBinContent(bin + 1));
0130           }
0131           ibooker.cd(subDir + "/" + trig);
0132           ibooker.book1D(hEff->GetName(), (TH1F*)hEff);
0133 
0134         } else if (histClassName == "TH2F") {
0135           for (int i = 0; i < hRef->GetXaxis()->GetNbins(); ++i) {
0136             for (int j = 0; j < hRef->GetYaxis()->GetNbins(); ++j) {
0137               int bin = hEff->GetBin(i + 1, j + 1);
0138               hEff->SetBinContent(bin, hRef->GetBinContent(i + 1, j + 1) * hTrig->GetBinContent(i + 1));
0139             }
0140           }
0141           ibooker.cd(subDir + "/" + trig);
0142           ibooker.book2D(hEff->GetName(), (TH2F*)hEff);
0143         } else {
0144           LogInfo("HLTMuonRefMethod") << "Histo class type << " << histClassName << " not implemented";
0145         }
0146 
0147         delete hEff;
0148       }
0149     }
0150   }
0151 }
0152 
0153 void HLTMuonRefMethod::beginRun(const edm::Run& run, const edm::EventSetup& c) {}
0154 
0155 void HLTMuonRefMethod::findAllSubdirectories(DQMStore::IBooker& ibooker,
0156                                              DQMStore::IGetter& igetter,
0157                                              const std::string& dir,
0158                                              std::set<std::string>* myList,
0159                                              const TString& _pattern = TString("")) {
0160   TString pattern = _pattern;
0161   TPRegexp nonPerlWildcard("\\w\\*|^\\*");
0162 
0163   if (!igetter.dirExists(dir)) {
0164     LogError("DQMGenericClient") << " DQMGenericClient::findAllSubdirectories ==> Missing folder " << dir << " !!!";
0165     return;
0166   }
0167   if (pattern != "") {
0168     if (pattern.Contains(nonPerlWildcard))
0169       pattern.ReplaceAll("*", ".*");
0170     TPRegexp regexp(pattern);
0171     ibooker.cd(dir);
0172     vector<string> foundDirs = igetter.getSubdirs();
0173     for (auto iDir = foundDirs.begin(); iDir != foundDirs.end(); ++iDir) {
0174       TString dirName = iDir->substr(iDir->rfind('/') + 1, iDir->length());
0175       if (dirName.Contains(regexp))
0176         findAllSubdirectories(ibooker, igetter, *iDir, myList);
0177     }
0178   }
0179   //std::cout << "Looking for directory " << dir ;
0180   else if (igetter.dirExists(dir)) {
0181     //std::cout << "... it exists! Inserting it into the list ";
0182     myList->insert(dir);
0183     //std::cout << "... now list has size " << myList->size() << std::endl;
0184     ibooker.cd(dir);
0185     findAllSubdirectories(ibooker, igetter, dir, myList, "*");
0186   } else {
0187     //std::cout << "... DOES NOT EXIST!!! Skip bogus dir" << std::endl;
0188 
0189     LogInfo("DQMGenericClient") << "Trying to find sub-directories of " << dir << " failed because " << dir
0190                                 << " does not exist";
0191   }
0192   return;
0193 }
0194 
0195 DEFINE_FWK_MODULE(HLTMuonRefMethod);