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_ = pset.getUntrackedParameter<std::string>("logCategory");
10 }
11 
13 
15  const TH1F* passed, const TH1F* total, const char* name, const char* title, const double confidence_level) {
16  if (not TEfficiency::CheckConsistency(*passed, *total)) {
17  edm::LogError(log_category_) << "failed to pass TEfficiency::CheckConsistency. " << name << std::endl;
18  return nullptr;
19  }
20 
21  const TAxis* total_x = total->GetXaxis();
22 
23  TProfile* eff_profile = new TProfile(name, title, total_x->GetNbins(), total_x->GetXmin(), total_x->GetXmax());
24  eff_profile->GetXaxis()->SetTitle(total_x->GetTitle());
25  eff_profile->GetYaxis()->SetTitle("#epsilon");
26 
27  for (int bin = 1; bin < total->GetNbinsX(); bin++) {
28  double num_passed = passed->GetBinContent(bin);
29  double num_total = total->GetBinContent(bin);
30 
31  if (num_total < 1) {
32  eff_profile->SetBinEntries(bin, 0);
33  continue;
34  }
35 
36  double efficiency = num_passed / num_total;
37 
38  double lower_bound = TEfficiency::ClopperPearson(num_total, num_passed, confidence_level, false);
39  double upper_bound = TEfficiency::ClopperPearson(num_total, num_passed, confidence_level, true);
40 
42  double error = std::hypot(efficiency, width);
43 
44  eff_profile->SetBinContent(bin, efficiency);
45  eff_profile->SetBinError(bin, error);
46  eff_profile->SetBinEntries(bin, 1);
47  }
48 
49  return eff_profile;
50 }
51 
53  const TH2F* total,
54  const char* name,
55  const char* title) {
56  if (not TEfficiency::CheckConsistency(*passed, *total)) {
57  edm::LogError(log_category_) << "failed to pass TEfficiency::CheckConsistency. " << name << std::endl;
58  return nullptr;
59  }
60 
61  TEfficiency eff(*passed, *total);
62  TH2F* eff_hist = dynamic_cast<TH2F*>(eff.CreateHistogram());
63  eff_hist->SetName(name);
64  eff_hist->SetTitle(title);
65 
66  const TAxis* total_x = total->GetXaxis();
67  TAxis* eff_hist_x = eff_hist->GetXaxis();
68  eff_hist_x->SetTitle(total_x->GetTitle());
69  for (int bin = 1; bin <= total->GetNbinsX(); bin++) {
70  const char* label = total_x->GetBinLabel(bin);
71  eff_hist_x->SetBinLabel(bin, label);
72  }
73 
74  const TAxis* total_y = total->GetYaxis();
75  TAxis* eff_hist_y = eff_hist->GetYaxis();
76  eff_hist_y->SetTitle(total_y->GetTitle());
77  for (int bin = 1; bin <= total->GetNbinsY(); bin++) {
78  const char* label = total_y->GetBinLabel(bin);
79  eff_hist_y->SetBinLabel(bin, label);
80  }
81 
82  return eff_hist;
83 }
84 
86  const std::string efficiency_folder = folder_ + "/Efficiency/";
87  ibooker.setCurrentFolder(efficiency_folder);
88  igetter.setCurrentFolder(efficiency_folder);
89 
90  std::map<std::string, std::pair<const MonitorElement*, const MonitorElement*> > me_pairs;
91 
92  const std::string matched = "_matched";
93 
94  for (const std::string& name : igetter.getMEs()) {
95  const std::string fullpath = efficiency_folder + name;
96  const MonitorElement* me = igetter.get(fullpath);
97  if (me == nullptr) {
98  edm::LogError(log_category_) << "failed to get " << fullpath << std::endl;
99  continue;
100  }
101 
102  const bool is_matched = name.find(matched) != std::string::npos;
103 
104  std::string key = name;
105  if (is_matched)
106  key.erase(key.find(matched), matched.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  edm::LogError(log_category_) << "numerator is missing. " << key << std::endl;
122  }
123 
124  if (me_total == nullptr) {
125  edm::LogError(log_category_) << "denominator is missing. " << key << std::endl;
126  continue;
127  }
128 
129  if (me_passed->kind() != me_total->kind()) {
130  edm::LogError(log_category_) << "inconsistency between kinds of passed and total" << key << std::endl;
131  continue;
132  }
133 
134  const std::string name = "eff_" + me_total->getName();
135  const std::string title = me_passed->getTitle();
136 
137  if (me_passed->kind() == MonitorElement::Kind::TH1F) {
138  TH1F* h_passed = me_passed->getTH1F();
139  if (h_passed == nullptr) {
140  edm::LogError(log_category_) << "failed to get TH1F from passed " << key << std::endl;
141  continue;
142  }
143  h_passed->Sumw2();
144 
145  TH1F* h_total = me_total->getTH1F();
146  if (h_total == nullptr) {
147  edm::LogError(log_category_) << "failed to get TH1F from total" << key << std::endl;
148  continue;
149  }
150  h_total->Sumw2();
151 
152  TProfile* eff = computeEfficiency(h_passed, h_total, name.c_str(), title.c_str());
153  if (eff == nullptr) {
154  edm::LogError(log_category_) << "failed to compute the efficiency " << key << std::endl;
155  continue;
156  }
157 
158  ibooker.bookProfile(name, eff);
159 
160  } else if (me_passed->kind() == MonitorElement::Kind::TH2F) {
161  TH2F* h_passed = me_passed->getTH2F();
162  if (h_passed == nullptr) {
163  edm::LogError(log_category_) << "failed to get TH1F from passed " << key << std::endl;
164  continue;
165  }
166  h_passed->Sumw2();
167 
168  TH2F* h_total = me_total->getTH2F();
169  if (h_total == nullptr) {
170  edm::LogError(log_category_) << "failed to get TH1F from total" << key << std::endl;
171  continue;
172  }
173  h_total->Sumw2();
174 
175  TH2F* eff = computeEfficiency(h_passed, h_total, name.c_str(), title.c_str());
176  if (eff == nullptr) {
177  edm::LogError(log_category_) << "failed to compute the efficiency " << key << std::endl;
178  continue;
179  }
180 
181  ibooker.book2D(name, eff);
182 
183  } else {
184  edm::LogError(log_category_) << "not implemented" << std::endl;
185  continue;
186  }
187  } // me_pairs
188 }
189 
191  std::vector<std::string> tokens;
192  size_t delimiter_pos;
193  size_t delimiter_len = delimiter.length();
194  while ((delimiter_pos = name.find('_')) != std::string::npos) {
195  tokens.push_back(name.substr(0, delimiter_pos));
196  name.erase(0, delimiter_pos + delimiter_len);
197  }
198  tokens.push_back(name);
199  return tokens;
200 }
201 
202 std::tuple<std::string, int, bool, int> GEMEfficiencyHarvester::parseResidualName(const std::string org_name,
203  const std::string prefix) {
204  std::string name = org_name;
205 
206  // residual_x_ge-11_odd_ieta4 or residdual_x_ge+21_ieta3
207  // residual_x: prefix
208  name.erase(name.find(prefix), prefix.length());
209  name.erase(name.find("_ge"), 3);
210 
211  const std::vector<std::string>&& tokens = splitString(name, "_");
212  const size_t num_tokens = tokens.size();
213 
214  if ((num_tokens != 2) and (num_tokens != 3)) {
215  return std::make_tuple("", -1, false, -1);
216  }
217 
218  // station != 1
219  std::string region_sign = tokens.front().substr(0, 1);
220 
221  TString station_str = tokens.front().substr(1, 1);
222  TString ieta_str = tokens.back().substr(4, 1);
223  TString superchamber_str = (num_tokens == 3) ? tokens[1] : "";
224 
225  int station = station_str.IsDigit() ? station_str.Atoi() : -1;
226  int ieta = ieta_str.IsDigit() ? ieta_str.Atoi() : -1;
227 
228  bool is_odd;
229  if (station == 1) {
230  if (superchamber_str.EqualTo("odd"))
231  is_odd = true;
232  else if (superchamber_str.EqualTo("even"))
233  is_odd = false;
234  else
235  return std::make_tuple("", -1, false, -1);
236  } else {
237  is_odd = false;
238  }
239 
240  return std::make_tuple(region_sign, station, is_odd, ieta);
241 }
242 
244  DQMStore::IGetter& igetter,
245  const std::string prefix) {
246  const std::string resolution_folder = folder_ + "/Resolution/";
247 
248  igetter.setCurrentFolder(resolution_folder);
249  ibooker.setCurrentFolder(resolution_folder);
250 
251  std::map<std::tuple<std::string, int, bool>, std::vector<std::pair<int, TH1F*> > > res_data;
252 
253  for (const std::string& name : igetter.getMEs()) {
254  if (name.find(prefix) == std::string::npos)
255  continue;
256 
257  const std::string fullpath = resolution_folder + name;
258  const MonitorElement* me = igetter.get(fullpath);
259  if (me == nullptr) {
260  edm::LogError(log_category_) << "failed to get " << fullpath << std::endl;
261  continue;
262  }
263 
264  TH1F* hist = me->getTH1F();
265  if (hist == nullptr) {
266  edm::LogError(log_category_) << "failed to get TH1F" << std::endl;
267  continue;
268  }
269 
270  auto&& [region_sign, station, is_odd, ieta] = parseResidualName(name, prefix);
271  if (region_sign.empty() or station < 0 or ieta < 0) {
272  edm::LogError(log_category_) << "failed to parse the name of the residual histogram: " << name << std::endl;
273  continue;
274  }
275 
276  const std::tuple<std::string, int, bool> key{region_sign, station, is_odd};
277 
278  if (res_data.find(key) == res_data.end()) {
279  res_data.insert({key, std::vector<std::pair<int, TH1F*> >()});
280  }
281  res_data[key].emplace_back(ieta, hist);
282  } // MonitorElement
283 
285  // NOTE
287  for (auto [key, ieta_data] : res_data) {
288  if (ieta_data.empty()) {
289  continue;
290  }
291 
292  TString tmp_title{ieta_data.front().second->GetTitle()};
293  const TObjArray* tokens = tmp_title.Tokenize(":");
294  TString title = dynamic_cast<TObjString*>(tokens->At(0))->GetString();
295 
296  auto&& [region_sign, station, is_odd] = key;
297  TString&& name = TString::Format("%s_ge%s%d1", prefix.data(), region_sign.c_str(), station);
298  title += TString::Format("GE %s%d/1", region_sign.c_str(), station);
299  if (station == 1) {
300  name += (is_odd ? "_odd" : "_even");
301  title += (is_odd ? ", Odd Superchambers" : ", Even Superchambers");
302  }
303 
304  const int num_etas = ieta_data.size();
305 
306  TH2F* profile = new TH2F(name, title, num_etas, 0.5, num_etas + 0.5, 2, -0.5, 1.5);
307  auto x_axis = profile->GetXaxis();
308 
309  x_axis->SetTitle("i#eta");
310  for (int ieta = 1; ieta <= num_etas; ieta++) {
311  const std::string&& label = std::to_string(ieta);
312  x_axis->SetBinLabel(ieta, label.c_str());
313  }
314 
315  profile->GetYaxis()->SetBinLabel(1, "Mean");
316  profile->GetYaxis()->SetBinLabel(2, "Std. Dev.");
317 
318  for (auto [ieta, hist] : ieta_data) {
319  profile->SetBinContent(ieta, 1, hist->GetMean());
320  profile->SetBinContent(ieta, 2, hist->GetStdDev());
321 
322  profile->SetBinError(ieta, 1, hist->GetMeanError());
323  profile->SetBinError(ieta, 2, hist->GetStdDevError());
324  }
325 
326  ibooker.book2D(name, profile);
327  }
328 }
329 
331  doEfficiency(ibooker, igetter);
332  doResolution(ibooker, igetter, "residual_phi");
333 }
muonTagProbeFilters_cff.matched
matched
Definition: muonTagProbeFilters_cff.py:62
ApeEstimator_cff.width
width
Definition: ApeEstimator_cff.py:24
MessageLogger.h
reco_skim_cfg_mod.fullpath
fullpath
Definition: reco_skim_cfg_mod.py:202
GEMEfficiencyHarvester::folder_
std::string folder_
Definition: GEMEfficiencyHarvester.h:28
makeHLTPrescaleTable.delimiter
delimiter
Definition: makeHLTPrescaleTable.py:181
GEMEfficiencyHarvester::parseResidualName
std::tuple< std::string, int, bool, int > parseResidualName(std::string, const std::string)
Definition: GEMEfficiencyHarvester.cc:202
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
dqm::implementation::NavigatorBase::setCurrentFolder
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:32
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:29
relativeConstraints.error
error
Definition: relativeConstraints.py:53
MonitorElementData::Kind::TH2F
GEMEfficiencyHarvester::~GEMEfficiencyHarvester
~GEMEfficiencyHarvester() override
Definition: GEMEfficiencyHarvester.cc:12
GEMEfficiencyHarvester.h
compare.hist
hist
Definition: compare.py:376
cuda_std::upper_bound
__host__ constexpr __device__ RandomIt upper_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
Definition: cudastdAlgorithm.h:45
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
cuda_std::lower_bound
__host__ constexpr __device__ RandomIt lower_bound(RandomIt first, RandomIt last, const T &value, Compare comp={})
Definition: cudastdAlgorithm.h:27
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
PostProcessor_cff.profile
profile
Definition: PostProcessor_cff.py:38
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
edm::ParameterSet
Definition: ParameterSet.h:36
edm::LogError
Definition: MessageLogger.h:183
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
GEMEfficiencyHarvester::splitString
std::vector< std::string > splitString(std::string, const std::string)
Definition: GEMEfficiencyHarvester.cc:190
value
Definition: value.py:1
GEMEfficiencyHarvester::doResolution
void doResolution(DQMStore::IBooker &, DQMStore::IGetter &, const std::string)
Definition: GEMEfficiencyHarvester.cc:243
newFWLiteAna.bin
bin
Definition: newFWLiteAna.py:161
overlapproblemtsosanalyzer_cfi.title
title
Definition: overlapproblemtsosanalyzer_cfi.py:7
GEMEfficiencyHarvester::computeEfficiency
TProfile * computeEfficiency(const TH1F *, const TH1F *, const char *, const char *, const double confidence_level=0.683)
Definition: GEMEfficiencyHarvester.cc:14
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:330
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:85
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