CMS 3D CMS Logo

HeavyFlavorHarvesting.cc
Go to the documentation of this file.
10 
11 #include "TH1F.h"
12 #include "TH2F.h"
13 #include "TH3F.h"
14 #include "RVersion.h"
15 
16 #include "TEfficiency.h"
17 
18 using namespace edm;
19 using namespace std;
20 
22 public:
24  ~HeavyFlavorHarvesting() override;
25  // virtual void endRun(const edm::Run &, const edm::EventSetup &) override;
26 private:
27  void calculateEfficiency(const ParameterSet& pset, DQMStore::IBooker&, DQMStore::IGetter&);
28  void calculateEfficiency1D(TH1* num, TH1* den, string name, DQMStore::IBooker&, DQMStore::IGetter&);
29  void calculateEfficiency2D(TH2F* num, TH2F* den, string name, DQMStore::IBooker&, DQMStore::IGetter&);
30 
33 
34 protected:
35  void dqmEndJob(DQMStore::IBooker&, DQMStore::IGetter&) override; //performed in the endJob
36 };
37 
39  : myDQMrootFolder(pset.getUntrackedParameter<string>("MyDQMrootFolder")),
40  efficiencies(pset.getUntrackedParameter<VParameterSet>("Efficiencies")) {}
41 
43  for (VParameterSet::const_iterator pset = efficiencies.begin(); pset != efficiencies.end(); pset++) {
44  calculateEfficiency(*pset, ibooker_, igetter_);
45  }
46 }
47 
49  DQMStore::IBooker& ibooker_,
50  DQMStore::IGetter& igetter_) {
51  //get hold of numerator and denominator histograms
52  vector<string> numDenEffMEnames = pset.getUntrackedParameter<vector<string> >("NumDenEffMEnames");
53  if (numDenEffMEnames.size() != 3) {
54  LogDebug("HLTriggerOfflineHeavyFlavor") << "NumDenEffMEnames must have three names" << endl;
55  return;
56  }
57  string denMEname = myDQMrootFolder + "/" + numDenEffMEnames[1];
58  string numMEname = myDQMrootFolder + "/" + numDenEffMEnames[0];
59  MonitorElement* denME = igetter_.get(denMEname);
60  MonitorElement* numME = igetter_.get(numMEname);
61  if (denME == nullptr || numME == nullptr) {
62  LogDebug("HLTriggerOfflineHeavyFlavor") << "Could not find MEs: " << denMEname << " or " << numMEname << endl;
63  return;
64  }
65  TH1* den = denME->getTH1();
66  TH1* num = numME->getTH1();
67  //check compatibility of the histograms
68  if (den->GetNbinsX() != num->GetNbinsX() || den->GetNbinsY() != num->GetNbinsY() ||
69  den->GetNbinsZ() != num->GetNbinsZ()) {
70  LogDebug("HLTriggerOfflineHeavyFlavor")
71  << "Monitoring elements " << numMEname << " and " << denMEname << "are incompatible" << endl;
72  return;
73  }
74  //figure out the directory and efficiency name
75  string effName = numDenEffMEnames[2];
76  string effDir = myDQMrootFolder;
77  string::size_type slashPos = effName.rfind('/');
78  if (string::npos != slashPos) {
79  effDir += "/" + effName.substr(0, slashPos);
80  effName.erase(0, slashPos + 1);
81  }
82  ibooker_.setCurrentFolder(effDir);
83  //calculate the efficiencies
84  int dimensions = num->GetDimension();
85  if (dimensions == 1) {
86  calculateEfficiency1D(num, den, effName, ibooker_, igetter_);
87  } else if (dimensions == 2) {
88  calculateEfficiency2D((TH2F*)num, (TH2F*)den, effName, ibooker_, igetter_);
89  TH1D* numX = ((TH2F*)num)->ProjectionX();
90  TH1D* denX = ((TH2F*)den)->ProjectionX();
91  calculateEfficiency1D(numX, denX, effName + "X", ibooker_, igetter_);
92  delete numX;
93  delete denX;
94  TH1D* numY = ((TH2F*)num)->ProjectionY();
95  TH1D* denY = ((TH2F*)den)->ProjectionY();
96  calculateEfficiency1D(numY, denY, effName + "Y", ibooker_, igetter_);
97  delete numY;
98  delete denY;
99  } else {
100  return;
101  }
102 }
103 
105  TH1* num, TH1* den, string effName, DQMStore::IBooker& ibooker_, DQMStore::IGetter& igetter_) {
106  TProfile* eff;
107  if (num->GetXaxis()->GetXbins()->GetSize() == 0) {
108  eff = new TProfile(effName.c_str(),
109  effName.c_str(),
110  num->GetXaxis()->GetNbins(),
111  num->GetXaxis()->GetXmin(),
112  num->GetXaxis()->GetXmax());
113  } else {
114  eff = new TProfile(
115  effName.c_str(), effName.c_str(), num->GetXaxis()->GetNbins(), num->GetXaxis()->GetXbins()->GetArray());
116  }
117  eff->SetTitle(effName.c_str());
118  eff->SetXTitle(num->GetXaxis()->GetTitle());
119  eff->SetYTitle("Efficiency");
120  eff->SetOption("PE");
121  eff->SetLineColor(2);
122  eff->SetLineWidth(2);
123  eff->SetMarkerStyle(20);
124  eff->SetMarkerSize(0.8);
125  eff->GetYaxis()->SetRangeUser(-0.001, 1.001);
126  eff->SetStats(kFALSE);
127  for (int i = 1; i <= num->GetNbinsX(); i++) {
128  double e, low, high;
129  if (int(den->GetBinContent(i)) > 0.)
130  e = double(num->GetBinContent(i)) / double(den->GetBinContent(i));
131  else
132  e = 0.;
133  low = TEfficiency::Wilson((double)den->GetBinContent(i), (double)num->GetBinContent(i), 0.683, false);
134  high = TEfficiency::Wilson((double)den->GetBinContent(i), (double)num->GetBinContent(i), 0.683, true);
135 
136  double err = e - low > high - e ? e - low : high - e;
137  //here is the trick to store info in TProfile:
138  eff->SetBinContent(i, e);
139  eff->SetBinEntries(i, 1);
140  eff->SetBinError(i, sqrt(e * e + err * err));
141  }
142  ibooker_.bookProfile(effName, eff);
143  delete eff;
144 }
145 
147  TH2F* num, TH2F* den, string effName, DQMStore::IBooker& ibooker_, DQMStore::IGetter& igetter_) {
148  TProfile2D* eff;
149  if (num->GetXaxis()->GetXbins()->GetSize() == 0 && num->GetYaxis()->GetXbins()->GetSize() == 0) {
150  eff = new TProfile2D(effName.c_str(),
151  effName.c_str(),
152  num->GetXaxis()->GetNbins(),
153  num->GetXaxis()->GetXmin(),
154  num->GetXaxis()->GetXmax(),
155  num->GetYaxis()->GetNbins(),
156  num->GetYaxis()->GetXmin(),
157  num->GetYaxis()->GetXmax());
158  } else if (num->GetXaxis()->GetXbins()->GetSize() != 0 && num->GetYaxis()->GetXbins()->GetSize() == 0) {
159  eff = new TProfile2D(effName.c_str(),
160  effName.c_str(),
161  num->GetXaxis()->GetNbins(),
162  num->GetXaxis()->GetXbins()->GetArray(),
163  num->GetYaxis()->GetNbins(),
164  num->GetYaxis()->GetXmin(),
165  num->GetYaxis()->GetXmax());
166  } else if (num->GetXaxis()->GetXbins()->GetSize() == 0 && num->GetYaxis()->GetXbins()->GetSize() != 0) {
167  eff = new TProfile2D(effName.c_str(),
168  effName.c_str(),
169  num->GetXaxis()->GetNbins(),
170  num->GetXaxis()->GetXmin(),
171  num->GetXaxis()->GetXmax(),
172  num->GetYaxis()->GetNbins(),
173  num->GetYaxis()->GetXbins()->GetArray());
174  } else {
175  eff = new TProfile2D(effName.c_str(),
176  effName.c_str(),
177  num->GetXaxis()->GetNbins(),
178  num->GetXaxis()->GetXbins()->GetArray(),
179  num->GetYaxis()->GetNbins(),
180  num->GetYaxis()->GetXbins()->GetArray());
181  }
182  eff->SetTitle(effName.c_str());
183  eff->SetXTitle(num->GetXaxis()->GetTitle());
184  eff->SetYTitle(num->GetYaxis()->GetTitle());
185  eff->SetZTitle("Efficiency");
186  eff->SetOption("colztexte");
187  eff->GetZaxis()->SetRangeUser(-0.001, 1.001);
188  eff->SetStats(kFALSE);
189  for (int i = 0; i < num->GetSize(); i++) {
190  double e, low, high;
191  if (int(den->GetBinContent(i)) > 0.)
192  e = double(num->GetBinContent(i)) / double(den->GetBinContent(i));
193  else
194  e = 0.;
195  low = TEfficiency::Wilson((double)den->GetBinContent(i), (double)num->GetBinContent(i), 0.683, false);
196  high = TEfficiency::Wilson((double)den->GetBinContent(i), (double)num->GetBinContent(i), 0.683, true);
197 
198  double err = e - low > high - e ? e - low : high - e;
199  //here is the trick to store info in TProfile:
200  eff->SetBinContent(i, e);
201  eff->SetBinEntries(i, 1);
202  eff->SetBinError(i, sqrt(e * e + err * err));
203  }
204  ibooker_.bookProfile2D(effName, eff);
205  delete eff;
206 }
207 
209 
210 //define this as a plug-in
MonitorElement * bookProfile2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, double lowZ, double highZ, char const *option="s", FUNC onbooking=NOOP())
Definition: DQMStore.h:485
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
std::vector< ParameterSet > VParameterSet
Definition: ParameterSet.h:35
void calculateEfficiency1D(TH1 *num, TH1 *den, string name, DQMStore::IBooker &, DQMStore::IGetter &)
uint16_t size_type
const VParameterSet efficiencies
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:408
T sqrt(T t)
Definition: SSEVec.h:19
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HeavyFlavorHarvesting(const edm::ParameterSet &pset)
void calculateEfficiency2D(TH2F *num, TH2F *den, string name, DQMStore::IBooker &, DQMStore::IGetter &)
void dqmEndJob(DQMStore::IBooker &, DQMStore::IGetter &) override
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:712
virtual TH1 * getTH1() const
HLT enums.
void calculateEfficiency(const ParameterSet &pset, DQMStore::IBooker &, DQMStore::IGetter &)
#define LogDebug(id)