CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GEMDQMEfficiencyCalculator.cc
Go to the documentation of this file.
2 
5 
6 #include "TEfficiency.h"
7 
9 
11 
12 //
13 TProfile* GEMDQMEfficiencyCalculator::computeEfficiency(const TH1F* passed,
14  const TH1F* total,
15  const char* name,
16  const char* title) {
17  if (not TEfficiency::CheckConsistency(*passed, *total)) {
18  edm::LogError(kLogCategory_) << "failed to pass TEfficiency::CheckConsistency. " << name;
19  return nullptr;
20  }
21 
22  const TAxis* total_x = total->GetXaxis();
23 
24  TProfile* eff_profile = new TProfile(name, title, total_x->GetNbins(), total_x->GetXmin(), total_x->GetXmax());
25  eff_profile->GetXaxis()->SetTitle(total_x->GetTitle());
26  eff_profile->GetYaxis()->SetTitle("Efficiency");
27 
28  for (int bin = 1; bin <= total->GetNbinsX(); bin++) {
29  const double num_passed = passed->GetBinContent(bin);
30  const double num_total = total->GetBinContent(bin);
31 
32  if (num_total < 1) {
33  eff_profile->SetBinEntries(bin, 0);
34  continue;
35  }
36 
37  const double efficiency = num_passed / num_total;
38  const double lower_boundary = TEfficiency::ClopperPearson(num_total, num_passed, kConfidenceLevel_, false);
39  const double upper_boundary = TEfficiency::ClopperPearson(num_total, num_passed, kConfidenceLevel_, true);
40  const double error = std::max(efficiency - lower_boundary, upper_boundary - efficiency);
41  // NOTE tprofile
42  const double profile_error = std::hypot(efficiency, error);
43 
44  eff_profile->SetBinContent(bin, efficiency);
45  eff_profile->SetBinError(bin, profile_error);
46  eff_profile->SetBinEntries(bin, 1);
47  }
48 
49  return eff_profile;
50 }
51 
52 //
54  const TH2F* total,
55  const char* name,
56  const char* title) {
57  if (not TEfficiency::CheckConsistency(*passed, *total)) {
58  edm::LogError(kLogCategory_) << "failed to pass TEfficiency::CheckConsistency. " << name;
59  return nullptr;
60  }
61 
62  TEfficiency eff(*passed, *total);
63  auto eff_hist = dynamic_cast<TH2F*>(eff.CreateHistogram());
64  eff_hist->SetName(name);
65  eff_hist->SetTitle(title);
66 
67  const TAxis* total_x = total->GetXaxis();
68  TAxis* eff_hist_x = eff_hist->GetXaxis();
69  eff_hist_x->SetTitle(total_x->GetTitle());
70  for (int bin = 1; bin <= total->GetNbinsX(); bin++) {
71  const char* label = total_x->GetBinLabel(bin);
72  eff_hist_x->SetBinLabel(bin, label);
73  }
74 
75  const TAxis* total_y = total->GetYaxis();
76  TAxis* eff_hist_y = eff_hist->GetYaxis();
77  eff_hist_y->SetTitle(total_y->GetTitle());
78  for (int bin = 1; bin <= total->GetNbinsY(); bin++) {
79  const char* label = total_y->GetBinLabel(bin);
80  eff_hist_y->SetBinLabel(bin, label);
81  }
82 
83  return eff_hist;
84 }
85 
87  DQMStore::IGetter& igetter,
88  const std::string& folder) {
89  ibooker.setCurrentFolder(folder);
90  igetter.setCurrentFolder(folder);
91 
92  std::map<std::string, std::pair<const MonitorElement*, const MonitorElement*> > me_pairs;
93 
94  for (const std::string& name : igetter.getMEs()) {
95  const std::string fullpath = folder + "/" + name;
96  const MonitorElement* me = igetter.get(fullpath);
97  if (me == nullptr) {
98  edm::LogError(kLogCategory_) << "failed to get " << fullpath;
99  continue;
100  }
101 
102  const bool is_matched = name.find(kMatchedSuffix_) != std::string::npos;
103 
104  std::string key = name;
105  if (is_matched)
106  key.erase(key.find(kMatchedSuffix_), kMatchedSuffix_.length());
107 
108  if (me_pairs.find(key) == me_pairs.end()) {
109  me_pairs[key] = {nullptr, nullptr};
110  }
111 
112  if (is_matched)
113  me_pairs[key].first = me;
114  else
115  me_pairs[key].second = me;
116  }
117 
118  for (auto& [key, value] : me_pairs) {
119  const auto& [me_passed, me_total] = value;
120  if (me_passed == nullptr) {
121  LogDebug(kLogCategory_) << "numerator is missing. " << key;
122  continue;
123  }
124 
125  if (me_total == nullptr) {
126  LogDebug(kLogCategory_) << "denominator is missing. " << key;
127  continue;
128  }
129 
130  if (me_passed->kind() != me_total->kind()) {
131  edm::LogError(kLogCategory_) << "inconsistency between kinds of passed and total" << key;
132  continue;
133  }
134 
135  const std::string name = "eff_" + me_total->getName();
136  const std::string title = me_passed->getTitle();
137 
138  if (me_passed->kind() == MonitorElement::Kind::TH1F) {
139  TH1F* h_passed = me_passed->getTH1F();
140  if (h_passed == nullptr) {
141  edm::LogError(kLogCategory_) << "failed to get TH1F from passed " << key;
142  continue;
143  }
144  // h_passed->Sumw2();
145 
146  TH1F* h_total = me_total->getTH1F();
147  if (h_total == nullptr) {
148  edm::LogError(kLogCategory_) << "failed to get TH1F from total" << key;
149  continue;
150  }
151  // h_total->Sumw2();
152 
153  TProfile* eff = computeEfficiency(h_passed, h_total, name.c_str(), title.c_str());
154  if (eff == nullptr) {
155  edm::LogError(kLogCategory_) << "failed to compute the efficiency " << key;
156  continue;
157  }
158 
159  ibooker.bookProfile(name, eff);
160 
161  } else if (me_passed->kind() == MonitorElement::Kind::TH2F) {
162  TH2F* h_passed = me_passed->getTH2F();
163  if (h_passed == nullptr) {
164  edm::LogError(kLogCategory_) << "failed to get TH1F from passed " << key;
165  continue;
166  }
167  // h_passed->Sumw2();
168 
169  TH2F* h_total = me_total->getTH2F();
170  if (h_total == nullptr) {
171  edm::LogError(kLogCategory_) << "failed to get TH1F from total" << key;
172  continue;
173  }
174  // h_total->Sumw2();
175 
176  TH2F* eff = computeEfficiency(h_passed, h_total, name.c_str(), title.c_str());
177  if (eff == nullptr) {
178  edm::LogError(kLogCategory_) << "failed to compute the efficiency " << key;
179  continue;
180  }
181 
182  ibooker.book2D(name, eff);
183 
184  } else {
185  edm::LogError(kLogCategory_) << "not implemented";
186  continue;
187  }
188 
189  } // me_pairs
190 }
virtual TH2F * getTH2F() const
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
void drawEfficiency(DQMStore::IBooker &, DQMStore::IGetter &, const std::string &)
Log< level::Error, false > LogError
def fullpath
Definition: das_client.py:267
char const * label
MonitorElement * bookProfile(TString const &name, TString const &title, int nchX, double lowX, double highX, int, double lowY, double highY, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:322
TProfile * computeEfficiency(const TH1F *, const TH1F *, const char *, const char *)
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:673
tuple key
prepare the HTCondor submission files and eventually submit them
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:177
virtual std::vector< std::string > getMEs() const
Definition: DQMStore.cc:720
#define LogDebug(id)