Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TEfficiency.h
Go to the documentation of this file.
1 #ifndef ROOT_TEfficiency
2 #define ROOT_TEfficiency
3 
4 //standard header
5 #include <vector>
6 #include <utility>
7 
8 //ROOT header
9 #include "TNamed.h"
10 
11 #include "TAttLine.h"
12 
13 #include "TAttFill.h"
14 
15 #include "TAttMarker.h"
16 
17 #include "TFitResultPtr.h"
18 
19 
20 class TCollection;
21 class TF1;
22 class TGraphAsymmErrors;
23 class TH1;
24 class TH2;
25 class TList;
26 
27 class TEfficiency: public TNamed, public TAttLine, public TAttFill, public TAttMarker
28 {
29 public:
30  //enumaration type for different statistic options for calculating confidence intervals
31  //kF* ... frequentist methods; kB* ... bayesian methods
32  enum EStatOption {
33  kFCP = 0, //Clopper-Pearson interval (recommended by PDG)
34  kFNormal, //normal approximation
35  kFWilson, //Wilson interval
36  kFAC, //Agresti-Coull interval
37  kFFC, //Feldman-Cousins interval
38  kBJeffrey, //Jeffrey interval (Prior ~ Beta(0.5,0.5)
39  kBUniform, //Prior ~ Uniform = Beta(1,1)
40  kBBayesian, //user specified Prior ~ Beta(fBeta_alpha,fBeta_beta)
41  kMidP //Mid-P Lancaster interval
42  };
43 
44 protected:
45 
46  Double_t fBeta_alpha; //global parameter for prior beta distribution (default = 1)
47  Double_t fBeta_beta; //global parameter for prior beta distribution (default = 1)
48  std::vector<std::pair<Double_t, Double_t> > fBeta_bin_params; // parameter for prior beta distribution different bin by bin
49  // (default vector is empty)
50  Double_t (*fBoundary)(Double_t,Double_t,Double_t,Bool_t); //!pointer to a method calculating the boundaries of confidence intervals
51  Double_t fConfLevel; //confidence level (default = 0.683, 1 sigma)
52  TDirectory* fDirectory; //!pointer to directory holding this TEfficiency object
53  TList* fFunctions; //->pointer to list of functions
54  TGraphAsymmErrors* fPaintGraph; //!temporary graph for painting
55  TH2* fPaintHisto; //!temporary histogram for painting
56  TH1* fPassedHistogram; //histogram for events which passed certain criteria
57  EStatOption fStatisticOption; //defines how the confidence intervals are determined
58  TH1* fTotalHistogram; //histogram for total number of events
59  Double_t fWeight; //weight for all events (default = 1)
60 
61  enum EStatusBits {
62  kIsBayesian = BIT(14), //bayesian statistics are used
63  kPosteriorMode = BIT(15), //use posterior mean for best estimate (Bayesian statistics)
64  kShortestInterval = BIT(16), // use shortest interval
65  kUseBinPrior = BIT(17), // use a different prior for each bin
66  kUseWeights = BIT(18) // use weights
67  };
68 
69  void Build(const char* name,const char* title);
70  void FillGraph(TGraphAsymmErrors * graph, Option_t * opt) const;
71  void FillHistogram(TH2 * h2) const;
72 
73 public:
74  TEfficiency();
75  TEfficiency(const TH1& passed,const TH1& total);
76  TEfficiency(const char* name,const char* title,Int_t nbins,
77  const Double_t* xbins);
78  TEfficiency(const char* name,const char* title,Int_t nbins,Double_t xlow,
79  Double_t xup);
80  TEfficiency(const char* name,const char* title,Int_t nbinsx,
81  Double_t xlow,Double_t xup,Int_t nbinsy,Double_t ylow,
82  Double_t yup);
83  TEfficiency(const char* name,const char* title,Int_t nbinsx,
84  const Double_t* xbins,Int_t nbinsy,const Double_t* ybins);
85  TEfficiency(const char* name,const char* title,Int_t nbinsx,
86  Double_t xlow,Double_t xup,Int_t nbinsy,Double_t ylow,
87  Double_t yup,Int_t nbinsz,Double_t zlow,Double_t zup);
88  TEfficiency(const char* name,const char* title,Int_t nbinsx,
89  const Double_t* xbins,Int_t nbinsy,const Double_t* ybins,
90  Int_t nbinsz,const Double_t* zbins);
91  TEfficiency(const TEfficiency& heff);
92  ~TEfficiency();
93 
94  void Add(const TEfficiency& rEff) {*this += rEff;}
95  void Browse(TBrowser*){Draw();}
96  TGraphAsymmErrors* CreateGraph(Option_t * opt = "") const;
97  TH2* CreateHistogram(Option_t * opt = "") const;
98  virtual Int_t DistancetoPrimitive(Int_t px, Int_t py);
99  void Draw(Option_t* opt = "");
100  virtual void ExecuteEvent(Int_t event, Int_t px, Int_t py);
101  void Fill(Bool_t bPassed,Double_t x,Double_t y=0,Double_t z=0);
102  void FillWeighted(Bool_t bPassed,Double_t weight,Double_t x,Double_t y=0,Double_t z=0);
103  Int_t FindFixBin(Double_t x,Double_t y=0,Double_t z=0) const;
104  TFitResultPtr Fit(TF1* f1,Option_t* opt="");
105  // use trick of -1 to return global parameters
106  Double_t GetBetaAlpha(Int_t bin = -1) const {return (fBeta_bin_params.size() > (UInt_t)bin) ? fBeta_bin_params[bin].first : fBeta_alpha;}
107  Double_t GetBetaBeta(Int_t bin = -1) const {return (fBeta_bin_params.size() > (UInt_t)bin) ? fBeta_bin_params[bin].second : fBeta_beta;}
108  Double_t GetConfidenceLevel() const {return fConfLevel;}
109  TH1* GetCopyPassedHisto() const;
110  TH1* GetCopyTotalHisto() const;
111  Int_t GetDimension() const;
112  TDirectory* GetDirectory() const {return fDirectory;}
113  Double_t GetEfficiency(Int_t bin) const;
114  Double_t GetEfficiencyErrorLow(Int_t bin) const;
115  Double_t GetEfficiencyErrorUp(Int_t bin) const;
116  Int_t GetGlobalBin(Int_t binx,Int_t biny=0,Int_t binz=0) const;
117  TGraphAsymmErrors* GetPaintedGraph() const { return fPaintGraph; }
118  TH2* GetPaintedHistogram() const { return fPaintHisto; }
119  TList* GetListOfFunctions();
120  const TH1* GetPassedHistogram() const {return fPassedHistogram;}
121  EStatOption GetStatisticOption() const {return fStatisticOption;}
122  const TH1* GetTotalHistogram() const {return fTotalHistogram;}
123  Double_t GetWeight() const {return fWeight;}
124  Long64_t Merge(TCollection* list);
125  TEfficiency& operator+=(const TEfficiency& rhs);
126  TEfficiency& operator=(const TEfficiency& rhs);
127  void Paint(Option_t* opt);
128  void SavePrimitive(std::ostream& out,Option_t* opt="");
129  void SetBetaAlpha(Double_t alpha);
130  void SetBetaBeta(Double_t beta);
131  void SetBetaBinParameters(Int_t bin, Double_t alpha, Double_t beta);
132  void SetConfidenceLevel(Double_t level);
133  void SetDirectory(TDirectory* dir);
134  void SetName(const char* name);
135  Bool_t SetPassedEvents(Int_t bin,Int_t events);
136  Bool_t SetPassedHistogram(const TH1& rPassed,Option_t* opt);
137  void SetPosteriorMode(Bool_t on = true) { SetBit(kPosteriorMode,on); SetShortestInterval(on); }
138  void SetPosteriorAverage(Bool_t on = true) { SetBit(kPosteriorMode,!on); }
139  void SetShortestInterval(Bool_t on = true) { SetBit(kShortestInterval,on); }
140  void SetCentralInterval(Bool_t on = true) { SetBit(kShortestInterval,!on); }
141  void SetStatisticOption(EStatOption option);
142  Bool_t SetBins(Int_t nx, Double_t xmin, Double_t xmax);
143  Bool_t SetBins(Int_t nx, const Double_t *xBins);
144  Bool_t SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax);
145  Bool_t SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins);
146  Bool_t SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax,
147  Int_t nz, Double_t zmin, Double_t zmax);
148  Bool_t SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t * yBins, Int_t nz,
149  const Double_t *zBins);
150 
151  void SetTitle(const char* title);
152  Bool_t SetTotalEvents(Int_t bin,Int_t events);
153  Bool_t SetTotalHistogram(const TH1& rTotal,Option_t* opt);
154  void SetUseWeightedEvents(Bool_t on = kTRUE);
155  void SetWeight(Double_t weight);
156  Bool_t UsesBayesianStat() const {return TestBit(kIsBayesian);}
157  Bool_t UsesPosteriorMode() const {return TestBit(kPosteriorMode) && TestBit(kIsBayesian);}
158  Bool_t UsesShortestInterval() const {return TestBit(kShortestInterval) && TestBit(kIsBayesian);}
159  Bool_t UsesPosteriorAverage() const {return !UsesPosteriorMode();}
160  Bool_t UsesCentralInterval() const {return !UsesShortestInterval();}
161  Bool_t UsesWeights() const {return TestBit(kUseWeights);}
162 
163  static Bool_t CheckBinning(const TH1& pass,const TH1& total);
164  static Bool_t CheckConsistency(const TH1& pass,const TH1& total,Option_t* opt="");
165  static Bool_t CheckEntries(const TH1& pass,const TH1& total,Option_t* opt="");
166  static Bool_t CheckWeights(const TH1& pass,const TH1& total);
167  static Double_t Combine(Double_t& up,Double_t& low,Int_t n,const Int_t* pass,const Int_t* total,
168  Double_t alpha,Double_t beta,Double_t level=0.683,
169  const Double_t* w=0,Option_t* opt="");
170  static TGraphAsymmErrors* Combine(TCollection* pList,Option_t* opt="",Int_t n=0,const Double_t* w=0);
171 
172  //calculating boundaries of confidence intervals
173  static Double_t AgrestiCoull(Double_t total,Double_t passed,Double_t level,Bool_t bUpper);
174  static Double_t ClopperPearson(Double_t total,Double_t passed,Double_t level,Bool_t bUpper);
175  static Double_t Normal(Double_t total,Double_t passed,Double_t level,Bool_t bUpper);
176  static Double_t Wilson(Double_t total,Double_t passed,Double_t level,Bool_t bUpper);
177  static Double_t FeldmanCousins(Double_t total,Double_t passed,Double_t level,Bool_t bUpper);
178  static Bool_t FeldmanCousinsInterval(Double_t total,Double_t passed,Double_t level,Double_t & lower, Double_t & upper);
179  static Double_t MidPInterval(Double_t total,Double_t passed,Double_t level,Bool_t bUpper);
180  // Bayesian functions
181  static Double_t Bayesian(Double_t total,Double_t passed,Double_t level,Double_t alpha,Double_t beta,Bool_t bUpper, Bool_t bShortest = false);
182  // helper functions for Bayesian statistics
183  static Double_t BetaCentralInterval(Double_t level,Double_t alpha,Double_t beta,Bool_t bUpper);
184  static Bool_t BetaShortestInterval(Double_t level,Double_t alpha,Double_t beta,Double_t & lower, Double_t & upper);
185  static Double_t BetaMean(Double_t alpha,Double_t beta);
186  static Double_t BetaMode(Double_t alpha,Double_t beta);
187 
188  ClassDef(TEfficiency,2) //calculating efficiencies
189 };
190 
191 const TEfficiency operator+(const TEfficiency& lhs,const TEfficiency& rhs);
192 
193 #endif