CMS 3D CMS Logo

HLTMuonRefMethod.cc
Go to the documentation of this file.
5 
8 
10 
13 
14 #include <set>
15 #include <string>
16 #include <vector>
17 #include <TPRegexp.h>
18 #include <cmath>
19 #include <climits>
20 #include <boost/tokenizer.hpp>
21 #include <boost/regex.hpp>
22 
23 #include <TH1.h>
24 #include <TEfficiency.h>
25 
26 using namespace edm;
27 using namespace std;
28 
30 public:
31  explicit HLTMuonRefMethod(const edm::ParameterSet& set);
32  ~HLTMuonRefMethod() override = default;
33  ;
34 
35  void beginJob() override;
36  void beginRun(const edm::Run&, const edm::EventSetup&) override;
37  void dqmEndJob(DQMStore::IBooker&, DQMStore::IGetter&) override;
38 
39 private:
40  // DQMStore* theDQM;
41 
42  std::vector<std::string> subDirs_;
43  std::vector<std::string> hltTriggers_;
44  std::vector<std::string> efficiency_;
45  std::vector<std::string> refEff_;
47 
49 
50  void findAllSubdirectories(DQMStore::IBooker& ibooker,
51  DQMStore::IGetter& igetter,
52  const std::string& dir,
53  std::set<std::string>* myList,
54  const TString& pattern);
55 };
56 
58  using VPSet = std::vector<edm::ParameterSet>;
59  using vstring = std::vector<std::string>;
60  using elsc = boost::escaped_list_separator<char>;
61 
62  subDirs_ = pset.getUntrackedParameter<vstring>("subDirs");
63  hltTriggers_ = pset.getUntrackedParameter<vstring>("hltTriggers");
64  refTriggers_ = pset.getUntrackedParameter<string>("refTriggers");
65  efficiency_ = pset.getUntrackedParameter<vstring>("efficiency");
66  refEff_ = pset.getUntrackedParameter<vstring>("refEff");
67 }
68 
70 
72  using vstring = std::vector<std::string>;
73  boost::regex metacharacters{"[\\^\\$\\.\\*\\+\\?\\|\\(\\)\\{\\}\\[\\]]"};
74  boost::smatch what;
75 
76  // theDQM = 0;
77  // theDQM = Service<DQMStore>().operator->();
78 
79  ibooker.cd();
80  set<string> subDirSet;
81 
82  for (auto iSubDir = subDirs_.begin(); iSubDir != subDirs_.end(); ++iSubDir) {
83  string subDir = *iSubDir;
84 
85  if (subDir[subDir.size() - 1] == '/')
86  subDir.erase(subDir.size() - 1);
87 
88  if (boost::regex_search(subDir, what, metacharacters)) {
89  const string::size_type shiftPos = subDir.rfind('/');
90  const string searchPath = subDir.substr(0, shiftPos);
91  const string pattern = subDir.substr(shiftPos + 1, subDir.length());
92  //std::cout << "\n\n\n\nLooking for all subdirs of " << subDir << std::endl;
93 
94  findAllSubdirectories(ibooker, igetter, searchPath, &subDirSet, pattern);
95 
96  } else {
97  subDirSet.insert(subDir);
98  }
99  }
100 
101  for (auto const& subDir : subDirSet) {
102  for (unsigned int iEff = 0; iEff != efficiency_.size(); ++iEff) {
103  string eff = efficiency_[iEff];
104 
105  // Getting reference trigger efficiency
106  MonitorElement* refEff = igetter.get(subDir + "/" + refTriggers_ + "/" + refEff_[iEff]);
107 
108  if (!refEff)
109  continue;
110 
111  // looping over all reference triggers
112  for (auto iTrigger = hltTriggers_.begin(); iTrigger != hltTriggers_.end(); ++iTrigger) {
113  string trig = *iTrigger;
114  MonitorElement* trigEff = igetter.get(subDir + "/" + trig + "/" + eff);
115  if (!trigEff)
116  continue;
117  TH1* hRef = refEff->getTH1();
118  TH1* hTrig = trigEff->getTH1();
119  TH1* hEff = (TH1*)hTrig->Clone(("eff_" + eff + "_ref").c_str());
120  hEff->SetTitle("Efficiency obtained with reference method");
121  TClass* myHistClass = hTrig->IsA();
122  TString histClassName = myHistClass->GetName();
123 
124  if (histClassName == "TH1F") {
125  for (int bin = 0; bin < hEff->GetNbinsX(); ++bin) {
126  hEff->SetBinContent(bin + 1, hEff->GetBinContent(bin + 1) * hRef->GetBinContent(bin + 1));
127  hEff->SetBinError(bin + 1,
128  hEff->GetBinContent(bin + 1) * hRef->GetBinError(bin + 1) +
129  hEff->GetBinError(bin + 1) * hRef->GetBinContent(bin + 1));
130  }
131  ibooker.cd(subDir + "/" + trig);
132  ibooker.book1D(hEff->GetName(), (TH1F*)hEff);
133 
134  } else if (histClassName == "TH2F") {
135  for (int i = 0; i < hRef->GetXaxis()->GetNbins(); ++i) {
136  for (int j = 0; j < hRef->GetYaxis()->GetNbins(); ++j) {
137  int bin = hEff->GetBin(i + 1, j + 1);
138  hEff->SetBinContent(bin, hRef->GetBinContent(i + 1, j + 1) * hTrig->GetBinContent(i + 1));
139  }
140  }
141  ibooker.cd(subDir + "/" + trig);
142  ibooker.book2D(hEff->GetName(), (TH2F*)hEff);
143  } else {
144  LogInfo("HLTMuonRefMethod") << "Histo class type << " << histClassName << " not implemented";
145  }
146 
147  delete hEff;
148  }
149  }
150  }
151 }
152 
154 
156  DQMStore::IGetter& igetter,
157  const std::string& dir,
158  std::set<std::string>* myList,
159  const TString& _pattern = TString("")) {
160  TString pattern = _pattern;
161  TPRegexp nonPerlWildcard("\\w\\*|^\\*");
162 
163  if (!igetter.dirExists(dir)) {
164  LogError("DQMGenericClient") << " DQMGenericClient::findAllSubdirectories ==> Missing folder " << dir << " !!!";
165  return;
166  }
167  if (pattern != "") {
168  if (pattern.Contains(nonPerlWildcard))
169  pattern.ReplaceAll("*", ".*");
170  TPRegexp regexp(pattern);
171  ibooker.cd(dir);
172  vector<string> foundDirs = igetter.getSubdirs();
173  for (auto iDir = foundDirs.begin(); iDir != foundDirs.end(); ++iDir) {
174  TString dirName = iDir->substr(iDir->rfind('/') + 1, iDir->length());
175  if (dirName.Contains(regexp))
176  findAllSubdirectories(ibooker, igetter, *iDir, myList);
177  }
178  }
179  //std::cout << "Looking for directory " << dir ;
180  else if (igetter.dirExists(dir)) {
181  //std::cout << "... it exists! Inserting it into the list ";
182  myList->insert(dir);
183  //std::cout << "... now list has size " << myList->size() << std::endl;
184  ibooker.cd(dir);
185  findAllSubdirectories(ibooker, igetter, dir, myList, "*");
186  } else {
187  //std::cout << "... DOES NOT EXIST!!! Skip bogus dir" << std::endl;
188 
189  LogInfo("DQMGenericClient") << "Trying to find sub-directories of " << dir << " failed because " << dir
190  << " does not exist";
191  }
192  return;
193 }
194 
std::vector< std::string > efficiency_
void beginJob() override
vector< string > vstring
Definition: ExoticaDQM.cc:7
virtual bool dirExists(std::string const &path) const
Definition: DQMStore.cc:769
Log< level::Error, false > LogError
uint16_t size_type
void beginJob()
Definition: Breakpoints.cc:14
HLTMuonRefMethod(const edm::ParameterSet &set)
std::vector< std::string > subDirs_
void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void beginRun(const edm::Run &, const edm::EventSetup &) override
std::string refTriggers_
Log< level::Info, false > LogInfo
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:221
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:712
std::vector< std::string > refEff_
virtual TH1 * getTH1() const
std::vector< std::string > hltTriggers_
HLT enums.
std::string outputFileName_
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
Definition: Run.h:45
void findAllSubdirectories(DQMStore::IBooker &ibooker, DQMStore::IGetter &igetter, const std::string &dir, std::set< std::string > *myList, const TString &pattern)
virtual DQM_DEPRECATED std::vector< std::string > getSubdirs() const
Definition: DQMStore.cc:739