CMS 3D CMS Logo

GEMEfficiencyHarvester.cc
Go to the documentation of this file.
2 
4 
5 #include "TEfficiency.h"
6 
8  folder_ = pset.getUntrackedParameter<std::string>("folder");
9  log_category_ = "GEMEfficiencyHarvester";
10 }
11 
13 
16  desc.addUntracked<std::string>("folder", "GEM/Efficiency/type0");
17  descriptions.add("gemEfficiencyHarvesterDefault", desc);
18 }
19 
21  const TH1F* passed, const TH1F* total, const char* name, const char* title, const double confidence_level) {
22  if (not TEfficiency::CheckConsistency(*passed, *total)) {
23  edm::LogError(log_category_) << "failed to pass TEfficiency::CheckConsistency. " << name << std::endl;
24  return nullptr;
25  }
26 
27  const TAxis* total_x = total->GetXaxis();
28 
29  TProfile* eff_profile = new TProfile(name, title, total_x->GetNbins(), total_x->GetXmin(), total_x->GetXmax());
30  eff_profile->GetXaxis()->SetTitle(total_x->GetTitle());
31  eff_profile->GetYaxis()->SetTitle("Efficiency");
32 
33  for (int bin = 1; bin <= total->GetNbinsX(); bin++) {
34  double num_passed = passed->GetBinContent(bin);
35  double num_total = total->GetBinContent(bin);
36 
37  if (num_total < 1) {
38  eff_profile->SetBinEntries(bin, 0);
39  continue;
40  }
41 
42  double efficiency = num_passed / num_total;
43 
44  double lower_bound = TEfficiency::ClopperPearson(num_total, num_passed, confidence_level, false);
45  double upper_bound = TEfficiency::ClopperPearson(num_total, num_passed, confidence_level, true);
46 
48  double error = std::hypot(efficiency, width);
49 
50  eff_profile->SetBinContent(bin, efficiency);
51  eff_profile->SetBinError(bin, error);
52  eff_profile->SetBinEntries(bin, 1);
53  }
54 
55  return eff_profile;
56 }
57 
59  const TH2F* total,
60  const char* name,
61  const char* title) {
62  if (not TEfficiency::CheckConsistency(*passed, *total)) {
63  edm::LogError(log_category_) << "failed to pass TEfficiency::CheckConsistency. " << name << std::endl;
64  return nullptr;
65  }
66 
67  TEfficiency eff(*passed, *total);
68  TH2F* eff_hist = dynamic_cast<TH2F*>(eff.CreateHistogram());
69  eff_hist->SetName(name);
70  eff_hist->SetTitle(title);
71 
72  const TAxis* total_x = total->GetXaxis();
73  TAxis* eff_hist_x = eff_hist->GetXaxis();
74  eff_hist_x->SetTitle(total_x->GetTitle());
75  for (int bin = 1; bin <= total->GetNbinsX(); bin++) {
76  const char* label = total_x->GetBinLabel(bin);
77  eff_hist_x->SetBinLabel(bin, label);
78  }
79 
80  const TAxis* total_y = total->GetYaxis();
81  TAxis* eff_hist_y = eff_hist->GetYaxis();
82  eff_hist_y->SetTitle(total_y->GetTitle());
83  for (int bin = 1; bin <= total->GetNbinsY(); bin++) {
84  const char* label = total_y->GetBinLabel(bin);
85  eff_hist_y->SetBinLabel(bin, label);
86  }
87 
88  return eff_hist;
89 }
90 
92  const std::string efficiency_folder = folder_ + "/Efficiency/";
93  ibooker.setCurrentFolder(efficiency_folder);
94  igetter.setCurrentFolder(efficiency_folder);
95 
96  std::map<std::string, std::pair<const MonitorElement*, const MonitorElement*> > me_pairs;
97 
98  const std::string matched = "_matched";
99 
100  for (const std::string& name : igetter.getMEs()) {
101  const std::string fullpath = efficiency_folder + name;
102  const MonitorElement* me = igetter.get(fullpath);
103  if (me == nullptr) {
104  edm::LogError(log_category_) << "failed to get " << fullpath << std::endl;
105  continue;
106  }
107 
108  const bool is_matched = name.find(matched) != std::string::npos;
109 
110  std::string key = name;
111  if (is_matched)
112  key.erase(key.find(matched), matched.length());
113 
114  if (me_pairs.find(key) == me_pairs.end()) {
115  me_pairs[key] = {nullptr, nullptr};
116  }
117 
118  if (is_matched)
119  me_pairs[key].first = me;
120  else
121  me_pairs[key].second = me;
122  }
123 
124  for (auto&& [key, value] : me_pairs) {
125  const auto& [me_passed, me_total] = value;
126  if (me_passed == nullptr) {
127  edm::LogError(log_category_) << "numerator is missing. " << key << std::endl;
128  continue;
129  }
130 
131  if (me_total == nullptr) {
132  edm::LogError(log_category_) << "denominator is missing. " << key << std::endl;
133  continue;
134  }
135 
136  if (me_passed->kind() != me_total->kind()) {
137  edm::LogError(log_category_) << "inconsistency between kinds of passed and total" << key << std::endl;
138  continue;
139  }
140 
141  const std::string name = "eff_" + me_total->getName();
142  const std::string title = me_passed->getTitle();
143 
144  if (me_passed->kind() == MonitorElement::Kind::TH1F) {
145  TH1F* h_passed = me_passed->getTH1F();
146  if (h_passed == nullptr) {
147  edm::LogError(log_category_) << "failed to get TH1F from passed " << key << std::endl;
148  continue;
149  }
150  h_passed->Sumw2();
151 
152  TH1F* h_total = me_total->getTH1F();
153  if (h_total == nullptr) {
154  edm::LogError(log_category_) << "failed to get TH1F from total" << key << std::endl;
155  continue;
156  }
157  h_total->Sumw2();
158 
159  TProfile* eff = computeEfficiency(h_passed, h_total, name.c_str(), title.c_str());
160  if (eff == nullptr) {
161  edm::LogError(log_category_) << "failed to compute the efficiency " << key << std::endl;
162  continue;
163  }
164 
165  ibooker.bookProfile(name, eff);
166 
167  } else if (me_passed->kind() == MonitorElement::Kind::TH2F) {
168  TH2F* h_passed = me_passed->getTH2F();
169  if (h_passed == nullptr) {
170  edm::LogError(log_category_) << "failed to get TH1F from passed " << key << std::endl;
171  continue;
172  }
173  h_passed->Sumw2();
174 
175  TH2F* h_total = me_total->getTH2F();
176  if (h_total == nullptr) {
177  edm::LogError(log_category_) << "failed to get TH1F from total" << key << std::endl;
178  continue;
179  }
180  h_total->Sumw2();
181 
182  TH2F* eff = computeEfficiency(h_passed, h_total, name.c_str(), title.c_str());
183  if (eff == nullptr) {
184  edm::LogError(log_category_) << "failed to compute the efficiency " << key << std::endl;
185  continue;
186  }
187 
188  ibooker.book2D(name, eff);
189 
190  } else {
191  edm::LogError(log_category_) << "not implemented" << std::endl;
192  continue;
193  }
194  } // me_pairs
195 }
196 
198  std::vector<std::string> tokens;
199  size_t delimiter_pos;
200  size_t delimiter_len = delimiter.length();
201  while ((delimiter_pos = name.find('_')) != std::string::npos) {
202  tokens.push_back(name.substr(0, delimiter_pos));
203  name.erase(0, delimiter_pos + delimiter_len);
204  }
205  tokens.push_back(name);
206  return tokens;
207 }
208 
209 std::tuple<std::string, int, int> GEMEfficiencyHarvester::parseResidualName(const std::string org_name,
210  const std::string prefix) {
211  std::string name = org_name;
212 
213  // e.g. residual_rdphi_GE-11_R4 -> _GE-11_R4
214  name.erase(name.find(prefix), prefix.length());
215 
216  // _GE-11_R4 -> -11_R4
217  name.erase(name.find("_GE"), 3);
218 
219  // -11_R4 -> (-11, R4)
220  const std::vector<std::string>&& tokens = splitString(name, "_");
221  const size_t num_tokens = tokens.size();
222 
223  if (num_tokens != 2) {
224  return std::make_tuple("", -1, -1);
225  }
226 
227  // '-'11
228  std::string region_sign = tokens.front().substr(0, 1);
229  // -'1'1
230  TString station_str = tokens.front().substr(1, 1);
231 
232  // R'4' or R'16'
233  TString ieta_str = tokens.back().substr(1);
234 
235  const int station = station_str.IsDigit() ? station_str.Atoi() : -1;
236  const int ieta = ieta_str.IsDigit() ? ieta_str.Atoi() : -1;
237 
238  return std::make_tuple(region_sign, station, ieta);
239 }
240 
242  DQMStore::IGetter& igetter,
243  const std::string prefix) {
244  const std::string resolution_folder = folder_ + "/Resolution/";
245 
246  igetter.setCurrentFolder(resolution_folder);
247  ibooker.setCurrentFolder(resolution_folder);
248 
249  // (histogram, (region_sign, station), ieta)
250  std::vector<std::tuple<const TH1F*, std::pair<std::string, int>, int> > hist_vector;
251  // (region_sign, station)
252  std::vector<std::pair<std::string, int> > re_st_vec;
253  // ieta
254  std::vector<int> ieta_vec;
255 
256  for (const std::string& name : igetter.getMEs()) {
257  if (name.find(prefix) == std::string::npos)
258  continue;
259 
260  const std::string fullpath = resolution_folder + name;
261  const MonitorElement* me = igetter.get(fullpath);
262  if (me == nullptr) {
263  edm::LogError(log_category_) << "failed to get " << fullpath << std::endl;
264  continue;
265  }
266 
267  const TH1F* hist = me->getTH1F();
268  if (hist == nullptr) {
269  edm::LogError(log_category_) << "failed to get TH1F" << std::endl;
270  continue;
271  }
272 
273  auto&& [region_sign, station, ieta] = parseResidualName(name, prefix);
274  if (region_sign.empty() or station < 0 or ieta < 0) {
275  edm::LogError(log_category_) << "failed to parse the name of the residual histogram: " << name << std::endl;
276  continue;
277  }
278  std::pair<std::string, int> region_station(region_sign, station);
279 
280  hist_vector.emplace_back(hist, region_station, ieta);
281  if (std::find(re_st_vec.begin(), re_st_vec.end(), region_station) == re_st_vec.end())
282  re_st_vec.push_back(region_station);
283  if (std::find(ieta_vec.begin(), ieta_vec.end(), ieta) == ieta_vec.end())
284  ieta_vec.push_back(ieta);
285  } // MonitorElement
286 
287  if (hist_vector.empty()) {
288  edm::LogError(log_category_) << "failed to find " << prefix << std::endl;
289  return;
290  }
291 
292  // NOTE
293  // GE-2/1, GE-1/1, GE-0/1, GE+0/1, GE+1/1, GE+2/1
294  auto f_sort = [](const std::pair<std::string, int>& lhs, const std::pair<std::string, int>& rhs) -> bool {
295  if (lhs.first == rhs.first) {
296  if (lhs.first == "-")
297  return lhs.second > rhs.second;
298  else
299  return lhs.second < rhs.second;
300 
301  } else {
302  return (lhs.first == "-");
303  }
304  };
305 
306  std::sort(re_st_vec.begin(), re_st_vec.end(), f_sort);
307  std::sort(ieta_vec.begin(), ieta_vec.end());
308 
309  const int num_st = re_st_vec.size();
310  const int num_ieta = ieta_vec.size();
311 
312  // NOTE
313  TString tmp_title{std::get<0>(hist_vector.front())->GetTitle()};
314 
315  const TObjArray* tokens = tmp_title.Tokenize(":");
316  const TString title_prefix = dynamic_cast<TObjString*>(tokens->At(0))->GetString();
317 
318  const TString h_mean_name = prefix + "_mean";
319  const TString h_stddev_name = prefix + "_stddev";
320  const TString h_skewness_name = prefix + "_skewness";
321 
322  const TString h_mean_title = title_prefix + " : Mean";
323  const TString h_stddev_title = title_prefix + " : Standard Deviation";
324  const TString h_skewness_title = title_prefix + " : Skewness";
325 
326  TH2F* h_mean = new TH2F(h_mean_name, h_mean_title, num_ieta, 0.5, num_ieta + 0.5, num_st, 0.5, num_st + 0.5);
327  // x-axis
328  h_mean->GetXaxis()->SetTitle("i#eta");
329  for (unsigned int idx = 0; idx < ieta_vec.size(); idx++) {
330  const int xbin = idx + 1;
331  const char* label = Form("%d", ieta_vec[idx]);
332  h_mean->GetXaxis()->SetBinLabel(xbin, label);
333  }
334  // y-axis
335  for (unsigned int idx = 0; idx < re_st_vec.size(); idx++) {
336  auto [region_sign, station] = re_st_vec[idx];
337  const char* label = Form("GE%s%d/1", region_sign.c_str(), station);
338  const int ybin = idx + 1;
339  h_mean->GetYaxis()->SetBinLabel(ybin, label);
340  }
341 
342  TH2F* h_stddev = dynamic_cast<TH2F*>(h_mean->Clone(h_stddev_name));
343  TH2F* h_skewness = dynamic_cast<TH2F*>(h_mean->Clone(h_skewness_name));
344 
345  h_stddev->SetTitle(h_stddev_title);
346  h_skewness->SetTitle(h_skewness_title);
347 
348  // NOTE
349  for (auto [hist, region_station, ieta] : hist_vector) {
350  const int xbin = findResolutionBin(ieta, ieta_vec);
351  if (xbin < 0) {
352  edm::LogError(log_category_) << "found a wrong x bin = " << xbin << std::endl;
353  continue;
354  }
355 
356  const int ybin = findResolutionBin(region_station, re_st_vec);
357  if (ybin < 0) {
358  edm::LogError(log_category_) << "found a wrong y bin = " << ybin << std::endl;
359  continue;
360  }
361 
362  h_mean->SetBinContent(xbin, ybin, hist->GetMean());
363  h_stddev->SetBinContent(xbin, ybin, hist->GetStdDev());
364  // FIXME
365  // `GetSkewness` seems to returns nan when its histogram has no entry..
366  const double skewness = hist->GetSkewness();
367  if (not std::isnan(skewness))
368  h_skewness->SetBinContent(xbin, ybin, skewness);
369 
370  h_mean->SetBinError(xbin, ybin, hist->GetMeanError());
371  h_stddev->SetBinError(xbin, ybin, hist->GetStdDevError());
372  h_skewness->SetBinError(xbin, ybin, hist->GetSkewness(11));
373  }
374 
375  for (auto&& each : {h_mean, h_stddev, h_skewness}) {
376  ibooker.book2D(each->GetName(), each);
377  }
378 }
379 
381  doEfficiency(ibooker, igetter);
382  doResolution(ibooker, igetter, "residual_rphi");
383 }
muonTagProbeFilters_cff.matched
matched
Definition: muonTagProbeFilters_cff.py:62
pfDeepBoostedJetPreprocessParams_cfi.upper_bound
upper_bound
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:16
ApeEstimator_cff.width
width
Definition: ApeEstimator_cff.py:24
runGCPTkAlMap.title
string title
Definition: runGCPTkAlMap.py:94
MessageLogger.h
GEMEfficiencyHarvester::parseResidualName
std::tuple< std::string, int, int > parseResidualName(std::string, const std::string)
Definition: GEMEfficiencyHarvester.cc:209
reco_skim_cfg_mod.fullpath
fullpath
Definition: reco_skim_cfg_mod.py:202
GEMEfficiencyHarvester::findResolutionBin
int findResolutionBin(const T &, const std::vector< T > &)
Definition: GEMEfficiencyHarvester.h:46
GEMEfficiencyHarvester::folder_
std::string folder_
Definition: GEMEfficiencyHarvester.h:41
makeHLTPrescaleTable.delimiter
delimiter
Definition: makeHLTPrescaleTable.py:181
relativeConstraints.station
station
Definition: relativeConstraints.py:67
MonitorElementData::Kind::TH1F
dqm::implementation::IGetter::getMEs
virtual std::vector< std::string > getMEs() const
Definition: DQMStore.cc:698
GEMEfficiencyHarvester::GEMEfficiencyHarvester
GEMEfficiencyHarvester(const edm::ParameterSet &)
Definition: GEMEfficiencyHarvester.cc:7
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
dqm::implementation::NavigatorBase::setCurrentFolder
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
GEMEfficiencyHarvester::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &)
Definition: GEMEfficiencyHarvester.cc:14
dqm::legacy::MonitorElement
Definition: MonitorElement.h:461
L1TObjectsTimingClient_cff.efficiency
efficiency
Definition: L1TObjectsTimingClient_cff.py:10
GEMEfficiencyHarvester::log_category_
std::string log_category_
Definition: GEMEfficiencyHarvester.h:42
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
relativeConstraints.error
error
Definition: relativeConstraints.py:53
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
MonitorElementData::Kind::TH2F
GEMEfficiencyHarvester::~GEMEfficiencyHarvester
~GEMEfficiencyHarvester() override
Definition: GEMEfficiencyHarvester.cc:12
GEMEfficiencyHarvester.h
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
compare.hist
hist
Definition: compare.py:376
dqm::implementation::IBooker::bookProfile
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
CommonMethods.isnan
def isnan(num)
Definition: CommonMethods.py:98
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
pfDeepBoostedJetPreprocessParams_cfi.lower_bound
lower_bound
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:15
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
edm::ParameterSet
Definition: ParameterSet.h:47
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
GEMEfficiencyHarvester::splitString
std::vector< std::string > splitString(std::string, const std::string)
Definition: GEMEfficiencyHarvester.cc:197
value
Definition: value.py:1
GEMEfficiencyHarvester::doResolution
void doResolution(DQMStore::IBooker &, DQMStore::IGetter &, const std::string)
Definition: GEMEfficiencyHarvester.cc:241
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
GEMEfficiencyHarvester::computeEfficiency
TProfile * computeEfficiency(const TH1F *, const TH1F *, const char *, const char *, const double confidence_level=0.683)
Definition: GEMEfficiencyHarvester.cc:20
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
dqm::implementation::IGetter
Definition: DQMStore.h:484
relativeConstraints.value
value
Definition: relativeConstraints.py:53
dqm::implementation::IBooker::book2D
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
TriggerAnalyzer.passed
passed
Definition: TriggerAnalyzer.py:62
dqm::implementation::IGetter::get
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:651
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
or
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
GEMEfficiencyHarvester::dqmEndJob
void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &) override
Definition: GEMEfficiencyHarvester.cc:380
dqm::implementation::IBooker
Definition: DQMStore.h:43
dqmMemoryStats.total
total
Definition: dqmMemoryStats.py:152
hlt_dqm_clientPB-live_cfg.me
me
Definition: hlt_dqm_clientPB-live_cfg.py:61
crabWrapper.key
key
Definition: crabWrapper.py:19
GEMEfficiencyHarvester::doEfficiency
void doEfficiency(DQMStore::IBooker &, DQMStore::IGetter &)
Definition: GEMEfficiencyHarvester.cc:91
label
const char * label
Definition: PFTauDecayModeTools.cc:11
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
ZMuMuAnalysisNtupler_cff.prefix
prefix
Definition: ZMuMuAnalysisNtupler_cff.py:14
dqm::legacy::MonitorElement::getTH2F
virtual TH2F * getTH2F() const
Definition: MonitorElement.h:490