ATLAS Offline Software
T_Efficiency.h
Go to the documentation of this file.
1 /* emacs: this is -*- c++ -*- */
12 #ifndef TIDA_T_EFFICIENCY_H
13 #define TIDA_T_EFFICIENCY_H
14 
15 #include <iostream>
16 #include <string>
17 #include <cmath>
18 
19 #include "TPad.h"
20 #include "TH1.h"
21 #include "TGraphAsymmErrors.h"
22 
23 template<typename T>
24 class T_Efficiency {
25 
26 public:
27 
28  T_Efficiency(T* h, const std::string& n) {
29 
30  std::string effname = n;
31  if ( effname=="" ) {
32  effname = std::string(h->GetName())+"_eff";
33  }
34 
35  m_name = effname;
36 
37  m_hnumer = (T*)h->Clone( (effname+"_n").c_str() );
38  m_hdenom = (T*)h->Clone( (effname+"_d").c_str() );
39  m_heff = (T*)h->Clone( effname.c_str() );
40 
41  m_hmissed = (T*)h->Clone( (effname+"_missed").c_str() );
42 
43  m_hnumer->Reset();
44  m_hdenom->Reset();
45  m_heff->Reset();
46  m_hmissed->Reset();
47 
48  }
49 
50 
51  T_Efficiency(T* hnum, T* hden, const std::string& n, double ) {
52 
53  std::string effname = n+"_eff";
54 
55  m_name = effname;
56 
57  m_hnumer = (T*)hnum->Clone( (effname+"_n").c_str() );
58  m_hdenom = (T*)hden->Clone( (effname+"_d").c_str() );
59  m_heff = (T*)hnum->Clone( effname.c_str() );
60 
61  m_hmissed = (T*)hnum->Clone( (effname+"_missed").c_str() );
62 
63  m_heff->Reset();
64  m_hmissed->Reset();
65 
69  }
70 
71  virtual ~T_Efficiency() { }
72 
73  std::string name() const { return m_name; }
74 
75  T* Hist() { return m_heff; }
76 
77 
79 
80  void finalise(double scale=100) {
81  getibinvec();
82  m_heff->Reset();
83  for ( size_t i=0 ; i<m_ibin.size() ; i++ ) {
84  double n = m_hnumer->GetBinContent(m_ibin[i]);
85  double d = m_hdenom->GetBinContent(m_ibin[i]);
86 
87  double e = 0;
88  double ee = 0;
89  if ( d!=0 ) {
90  e = n/d;
91  ee = e*(1-e)/d;
92  }
93 
94  // need proper error calculation...
95  m_heff->SetBinContent( m_ibin[i], scale*e );
96  m_heff->SetBinError( m_ibin[i], scale*std::sqrt(ee) );
97 
98  }
99  }
100 
101 
104 
105  // 1D fill
106  // virtual void Fill( double x, double w=1) { }
107 
108  // virtual void FillDenom( double x, float w=1) { }
109 
110  // 2D fill
111  // virtual void Fill( double x, double y, double w=1) { }
112 
113  // virtual void FillDenom( double x, double y, float w=1) { }
114 
115 #if 0
116 
119 
120  void SetDenominator( T* h ) {
121  getibinvec();
122  for ( size_t i=0 ; i<m_ibin.size() ; i++ ) {
123  m_hdenom->SetBinContent( m_ibin[i], h->GetBinContent(m_ibin[i]) );
124  m_hdenom->SetBinError( m_ibin[i], h->GetBinError(m_ibin[i]) );
125  }
126  }
127 
128  virtual void SetNumerator( T* h ) {
129  getibinvec();
130  for ( size_t i=0 ; i<m_ibin.size() ; i++ ) {
131  m_hnumer->SetBinContent( m_ibin[i], h->GetBinContent(m_ibin[i]) );
132  m_hnumer->SetBinError( m_ibin[i], h->GetBinError(m_ibin[i]) );
133  }
134  }
135 
136 #endif
137 
139 
141 
142  getibinvec();
143 
144  double n_tot = 0;
145  double d_tot = 0;
146 
147  for ( size_t i=0 ; i<m_ibin.size() ; i++ ) {
148  double n = m_hnumer->GetBinContent(m_ibin[i]);
149  double d = m_hdenom->GetBinContent(m_ibin[i]);
150 
151  n_tot += n;
152  d_tot += d;
153  }
154 
155  if ( d_tot!=0 ) {
156  return n_tot / d_tot;
157  }
158 
159  return 0.;
160  }
161 
162  void Write() {
163  m_hnumer->Write();
164  m_hdenom->Write();
165  m_hmissed->Write();
166  m_heff->Write();
167  }
168 
169 protected:
170 
171  virtual void getibinvec(bool force=false) = 0;
172 
173 protected:
174 
175  TGraphAsymmErrors* BayesInternal( TH1* hn, TH1* hd, double scale=100) const {
176 
185 
186  for ( int i=1 ; i<=hd->GetNbinsX() ; i++ ) { // cppcheck-suppress [ctunullpointer, nullPointer]; false positive
187  double y = hd->GetBinContent(i);
188  if ( y==0 ) hd->SetBinContent(i, 1e-20);
189  }
190 
191  TGraphAsymmErrors* tg = new TGraphAsymmErrors( hn, hd, "cl=0.683 b(1,1) mode" );
192 
193 
194  double* x = tg->GetX();
195  double* y = tg->GetY();
196 
197  int n = tg->GetN();
198 
199  for ( int i=0 ; i<n ; i++ ) {
200 
201  y[i] *= scale;
202 
203  double yeup = tg->GetErrorYhigh(i);
204  double yedown = tg->GetErrorYlow(i);
205 
206  yeup *= scale;
207  yedown *= scale;
208 
215 
216  tg->SetPoint( i, x[i], y[i] );
217 
218  tg->SetPointEYhigh( i, yeup );
219  tg->SetPointEYlow( i, yedown );
220 
221  tg->SetPointEXhigh( i, 0 );
222  tg->SetPointEXlow( i, 0 );
223 
224  }
225 
226  return tg;
227 
228  }
229 
230 
231 protected:
232 
233  std::string m_name;
234 
237 
239 
241 
242  std::vector<int> m_ibin;
243 
244 };
245 
246 
247 
248 
249 #endif // TIDA_T_EFFICIENCY_H
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
T_Efficiency::T_Efficiency
T_Efficiency(T *hnum, T *hden, const std::string &n, double)
Definition: T_Efficiency.h:51
calibdata.force
bool force
Definition: calibdata.py:19
T_Efficiency::getibinvec
virtual void getibinvec(bool force=false)=0
T_Efficiency::T_Efficiency
T_Efficiency(T *h, const std::string &n)
Definition: T_Efficiency.h:28
hist_file_dump.d
d
Definition: hist_file_dump.py:137
T_Efficiency::Hist
T * Hist()
Definition: T_Efficiency.h:75
T_Efficiency::m_name
std::string m_name
Definition: T_Efficiency.h:233
T_Efficiency::finalise
void finalise(double scale=100)
actually calculate the efficiencies
Definition: T_Efficiency.h:80
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
x
#define x
T_Efficiency
Definition: T_Efficiency.h:24
T_Efficiency::Write
void Write()
Definition: T_Efficiency.h:162
T_Efficiency::m_hmissed
T * m_hmissed
Definition: T_Efficiency.h:238
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
perfmonmt-refit.n_tot
n_tot
Definition: perfmonmt-refit.py:104
T_Efficiency::m_heff
T * m_heff
Definition: T_Efficiency.h:240
y
#define y
h
T_Efficiency::m_ibin
std::vector< int > m_ibin
Definition: T_Efficiency.h:242
T_Efficiency::name
std::string name() const
Definition: T_Efficiency.h:73
T_Efficiency::~T_Efficiency
virtual ~T_Efficiency()
Definition: T_Efficiency.h:71
T_Efficiency::m_hdenom
T * m_hdenom
Definition: T_Efficiency.h:236
T_Efficiency::BayesInternal
TGraphAsymmErrors * BayesInternal(TH1 *hn, TH1 *hd, double scale=100) const
Definition: T_Efficiency.h:175
T_Efficiency::findTotalEfficiency
double findTotalEfficiency()
these 1D and 2D Fill versionms should never be called directly in fact, are they even needed at all ?
Definition: T_Efficiency.h:140
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
T_Efficiency::m_hnumer
T * m_hnumer
Definition: T_Efficiency.h:235