ROOT logo
#ifndef ROOT_TEfficiency
#define ROOT_TEfficiency

//standard header
#include <vector>
#include <utility>

//ROOT header
#ifndef ROOT_TNamed
#include "TNamed.h"
#endif

#ifndef ROOT_TAttLine
#include "TAttLine.h"
#endif

#ifndef ROOT_TAttFill
#include "TAttFill.h"
#endif

#ifndef ROOT_TAttMarker
#include "TAttMarker.h"
#endif

class TCollection;
class TF1;
class TGraphAsymmErrors;
class TH1;
class TH2;
class TList;

//|TEfficiency
//------------------------

class TEfficiency: public TNamed, public TAttLine, public TAttFill, public TAttMarker
{
public:  
      //enumaration type for different statistic options for calculating confidence intervals
      //kF* ... frequentist methods; kB* ... bayesian methods      
   enum EStatOption {
      kFCP = 0,                         //Clopper-Pearson interval (recommended by PDG)
      kFNormal,                         //normal approximation
      kFWilson,                         //Wilson interval
      kFAC,                             //Agresti-Coull interval
      kFFC,                             //Feldman-Cousins interval
      kBJeffrey,                        //Jeffrey interval (Prior ~ Beta(0.5,0.5)
      kBUniform,                        //Prior ~ Uniform = Beta(1,1)
      kBBayesian                        //user specified Prior ~ Beta(fBeta_alpha,fBeta_beta)
   };

protected:

      Double_t      fBeta_alpha;             //global parameter for prior beta distribution (default = 1)
      Double_t      fBeta_beta;              //global parameter for prior beta distribution (default = 1)
      std::vector<std::pair<Double_t, Double_t> > fBeta_bin_params;  // parameter for prior beta distribution different bin by bin 
                                                                 // (default vector is empty)
      Double_t      (*fBoundary)(Int_t,Int_t,Double_t,Bool_t);               //!pointer to a method calculating the boundaries of confidence intervals
      Double_t      fConfLevel;              //confidence level (default = 0.683, 1 sigma)
      TDirectory*   fDirectory;              //!pointer to directory holding this TEfficiency object
      TList*        fFunctions;              //->pointer to list of functions
      TGraphAsymmErrors* fPaintGraph;        //!temporary graph for painting
      TH2*          fPaintHisto;             //!temporary histogram for painting      
      TH1*          fPassedHistogram;        //histogram for events which passed certain criteria
      EStatOption   fStatisticOption;        //defines how the confidence intervals are determined
      TH1*          fTotalHistogram;         //histogram for total number of events
      Double_t      fWeight;                 //weight for all events (default = 1)

      enum{
         kIsBayesian       = BIT(14),              //bayesian statistics are used
         kPosteriorMode    = BIT(15),              //use posterior mean for best estimate (Bayesian statistics)
         kShortestInterval = BIT(16),              // use shortest interval
         kUseBinPrior      = BIT(17),              // use a different prior for each bin
         kUseWeights       = BIT(18)               // use weights
      };

      void          Build(const char* name,const char* title);   
      void          FillGraph(TGraphAsymmErrors * graph, Option_t * opt) const;
      void          FillHistogram(TH2 * h2) const;
      
public:
      TEfficiency();   
      TEfficiency(const TH1& passed,const TH1& total);
      TEfficiency(const char* name,const char* title,Int_t nbins,
                  const Double_t* xbins);
      TEfficiency(const char* name,const char* title,Int_t nbins,Double_t xlow,
                  Double_t xup);
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
                  Double_t xlow,Double_t xup,Int_t nbinsy,Double_t ylow,
                  Double_t yup);
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
                  const Double_t* xbins,Int_t nbinsy,const Double_t* ybins);
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
                  Double_t xlow,Double_t xup,Int_t nbinsy,Double_t ylow,
                  Double_t yup,Int_t nbinsz,Double_t zlow,Double_t zup);
      TEfficiency(const char* name,const char* title,Int_t nbinsx,
                  const Double_t* xbins,Int_t nbinsy,const Double_t* ybins,
                  Int_t nbinsz,const Double_t* zbins);
      TEfficiency(const TEfficiency& heff);
      ~TEfficiency();
      
      void          Add(const TEfficiency& rEff) {*this += rEff;}
      TGraphAsymmErrors*   CreateGraph(Option_t * opt = "") const;
      TH2*          CreateHistogram(Option_t * opt = "") const;
      virtual Int_t DistancetoPrimitive(Int_t px, Int_t py);
      void          Draw(Option_t* opt = "");
      virtual void  ExecuteEvent(Int_t event, Int_t px, Int_t py);
      void          Fill(Bool_t bPassed,Double_t x,Double_t y=0,Double_t z=0);
      void          FillWeighted(Bool_t bPassed,Double_t weight,Double_t x,Double_t y=0,Double_t z=0);
      Int_t         FindFixBin(Double_t x,Double_t y=0,Double_t z=0) const;
      Int_t         Fit(TF1* f1,Option_t* opt="");
      // use trick of -1 to return global parameters
      Double_t      GetBetaAlpha(Int_t bin = -1) const {return (fBeta_bin_params.size() > (UInt_t)bin) ? fBeta_bin_params[bin].first : fBeta_alpha;}
      Double_t      GetBetaBeta(Int_t bin =  -1) const {return (fBeta_bin_params.size() > (UInt_t)bin) ? fBeta_bin_params[bin].second : fBeta_beta;}  
      Double_t      GetConfidenceLevel() const {return fConfLevel;}
      TH1*          GetCopyPassedHisto() const;
      TH1*          GetCopyTotalHisto() const;
      Int_t         GetDimension() const;
      TDirectory*   GetDirectory() const {return fDirectory;}
      Double_t      GetEfficiency(Int_t bin) const;
      Double_t      GetEfficiencyErrorLow(Int_t bin) const;
      Double_t      GetEfficiencyErrorUp(Int_t bin) const;
      Int_t         GetGlobalBin(Int_t binx,Int_t biny=0,Int_t binz=0) const;
      TGraphAsymmErrors*   GetPaintedGraph() const { return fPaintGraph; }     
      TH2*          GetPaintedHistogram() const { return fPaintHisto; }     
      TList*        GetListOfFunctions();
      const TH1*    GetPassedHistogram() const {return fPassedHistogram;}
      EStatOption   GetStatisticOption() const {return fStatisticOption;}
      const TH1*    GetTotalHistogram() const {return fTotalHistogram;}
      Double_t      GetWeight() const {return fWeight;}
      Long64_t      Merge(TCollection* list);      
      TEfficiency&  operator+=(const TEfficiency& rhs);
      TEfficiency&  operator=(const TEfficiency& rhs);
      void          Paint(Option_t* opt);
      void          SavePrimitive(ostream& out,Option_t* opt="");
      void          SetBetaAlpha(Double_t alpha);
      void          SetBetaBeta(Double_t beta);    
      void          SetBetaBinParameters(Int_t bin, Double_t alpha, Double_t beta);
      void          SetConfidenceLevel(Double_t level);
      void          SetDirectory(TDirectory* dir);
      void          SetName(const char* name);
      Bool_t        SetPassedEvents(Int_t bin,Int_t events);
      Bool_t        SetPassedHistogram(const TH1& rPassed,Option_t* opt);
      void          SetPosteriorMode(Bool_t on = true) { SetBit(kPosteriorMode,on); SetShortestInterval(on); } 
      void          SetPosteriorAverage(Bool_t on = true) { SetBit(kPosteriorMode,!on); } 
      void          SetShortestInterval(Bool_t on = true) { SetBit(kShortestInterval,on); } 
      void          SetCentralInterval(Bool_t on = true) { SetBit(kShortestInterval,!on); } 
      void          SetStatisticOption(EStatOption option);
      Bool_t        SetBins(Int_t nx, Double_t xmin, Double_t xmax);
      Bool_t        SetBins(Int_t nx, const Double_t *xBins);
      Bool_t        SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax);
      Bool_t        SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t *yBins);
      Bool_t        SetBins(Int_t nx, Double_t xmin, Double_t xmax, Int_t ny, Double_t ymin, Double_t ymax,
                            Int_t nz, Double_t zmin, Double_t zmax);
      Bool_t        SetBins(Int_t nx, const Double_t *xBins, Int_t ny, const Double_t * yBins, Int_t nz,
                            const Double_t *zBins);

      void          SetTitle(const char* title);
      Bool_t        SetTotalEvents(Int_t bin,Int_t events);
      Bool_t        SetTotalHistogram(const TH1& rTotal,Option_t* opt);
      void          SetUseWeightedEvents();
      void          SetWeight(Double_t weight);
      Bool_t        UsesBayesianStat() const {return TestBit(kIsBayesian);}
      Bool_t        UsesPosteriorMode() const   {return TestBit(kPosteriorMode) && TestBit(kIsBayesian);} 
      Bool_t        UsesShortestInterval() const   {return TestBit(kShortestInterval) && TestBit(kIsBayesian);} 
      Bool_t        UsesPosteriorAverage() const   {return !UsesPosteriorMode();} 
      Bool_t        UsesCentralInterval() const   {return !UsesShortestInterval();}
      Bool_t        UsesWeights() const {return TestBit(kUseWeights);}

      static Bool_t CheckBinning(const TH1& pass,const TH1& total);
      static Bool_t CheckConsistency(const TH1& pass,const TH1& total,Option_t* opt="");
      static Bool_t CheckEntries(const TH1& pass,const TH1& total,Option_t* opt="");
      static Double_t Combine(Double_t& up,Double_t& low,Int_t n,const Int_t* pass,const Int_t* total,
                              Double_t alpha,Double_t beta,Double_t level=0.683,
                              const Double_t* w=0,Option_t* opt="");
      static TGraphAsymmErrors* Combine(TCollection* pList,Option_t* opt="",Int_t n=0,const Double_t* w=0);
      
      //calculating boundaries of confidence intervals
      static Double_t AgrestiCoull(Int_t total,Int_t passed,Double_t level,Bool_t bUpper);
      static Double_t ClopperPearson(Int_t total,Int_t passed,Double_t level,Bool_t bUpper);
      static Double_t Normal(Int_t total,Int_t passed,Double_t level,Bool_t bUpper);
      static Double_t Wilson(Int_t total,Int_t passed,Double_t level,Bool_t bUpper);
      static Double_t FeldmanCousins(Int_t total,Int_t passed,Double_t level,Bool_t bUpper);
      static Bool_t FeldmanCousinsInterval(Int_t total,Int_t passed,Double_t level,Double_t & lower, Double_t & upper);
      // Bayesian functions 
      static Double_t Bayesian(Int_t total,Int_t passed,Double_t level,Double_t alpha,Double_t beta,Bool_t bUpper, Bool_t bShortest = false);
      // helper functions for Bayesian statistics  
      static Double_t BetaCentralInterval(Double_t level,Double_t alpha,Double_t beta,Bool_t bUpper);
      static Bool_t   BetaShortestInterval(Double_t level,Double_t alpha,Double_t beta,Double_t & lower, Double_t & upper);
      static Double_t BetaMean(Double_t alpha,Double_t beta);
      static Double_t BetaMode(Double_t alpha,Double_t beta);
      
      ClassDef(TEfficiency,2)     //calculating efficiencies
};

const TEfficiency operator+(const TEfficiency& lhs,const TEfficiency& rhs);

#endif
 TEfficiency.h:1
 TEfficiency.h:2
 TEfficiency.h:3
 TEfficiency.h:4
 TEfficiency.h:5
 TEfficiency.h:6
 TEfficiency.h:7
 TEfficiency.h:8
 TEfficiency.h:9
 TEfficiency.h:10
 TEfficiency.h:11
 TEfficiency.h:12
 TEfficiency.h:13
 TEfficiency.h:14
 TEfficiency.h:15
 TEfficiency.h:16
 TEfficiency.h:17
 TEfficiency.h:18
 TEfficiency.h:19
 TEfficiency.h:20
 TEfficiency.h:21
 TEfficiency.h:22
 TEfficiency.h:23
 TEfficiency.h:24
 TEfficiency.h:25
 TEfficiency.h:26
 TEfficiency.h:27
 TEfficiency.h:28
 TEfficiency.h:29
 TEfficiency.h:30
 TEfficiency.h:31
 TEfficiency.h:32
 TEfficiency.h:33
 TEfficiency.h:34
 TEfficiency.h:35
 TEfficiency.h:36
 TEfficiency.h:37
 TEfficiency.h:38
 TEfficiency.h:39
 TEfficiency.h:40
 TEfficiency.h:41
 TEfficiency.h:42
 TEfficiency.h:43
 TEfficiency.h:44
 TEfficiency.h:45
 TEfficiency.h:46
 TEfficiency.h:47
 TEfficiency.h:48
 TEfficiency.h:49
 TEfficiency.h:50
 TEfficiency.h:51
 TEfficiency.h:52
 TEfficiency.h:53
 TEfficiency.h:54
 TEfficiency.h:55
 TEfficiency.h:56
 TEfficiency.h:57
 TEfficiency.h:58
 TEfficiency.h:59
 TEfficiency.h:60
 TEfficiency.h:61
 TEfficiency.h:62
 TEfficiency.h:63
 TEfficiency.h:64
 TEfficiency.h:65
 TEfficiency.h:66
 TEfficiency.h:67
 TEfficiency.h:68
 TEfficiency.h:69
 TEfficiency.h:70
 TEfficiency.h:71
 TEfficiency.h:72
 TEfficiency.h:73
 TEfficiency.h:74
 TEfficiency.h:75
 TEfficiency.h:76
 TEfficiency.h:77
 TEfficiency.h:78
 TEfficiency.h:79
 TEfficiency.h:80
 TEfficiency.h:81
 TEfficiency.h:82
 TEfficiency.h:83
 TEfficiency.h:84
 TEfficiency.h:85
 TEfficiency.h:86
 TEfficiency.h:87
 TEfficiency.h:88
 TEfficiency.h:89
 TEfficiency.h:90
 TEfficiency.h:91
 TEfficiency.h:92
 TEfficiency.h:93
 TEfficiency.h:94
 TEfficiency.h:95
 TEfficiency.h:96
 TEfficiency.h:97
 TEfficiency.h:98
 TEfficiency.h:99
 TEfficiency.h:100
 TEfficiency.h:101
 TEfficiency.h:102
 TEfficiency.h:103
 TEfficiency.h:104
 TEfficiency.h:105
 TEfficiency.h:106
 TEfficiency.h:107
 TEfficiency.h:108
 TEfficiency.h:109
 TEfficiency.h:110
 TEfficiency.h:111
 TEfficiency.h:112
 TEfficiency.h:113
 TEfficiency.h:114
 TEfficiency.h:115
 TEfficiency.h:116
 TEfficiency.h:117
 TEfficiency.h:118
 TEfficiency.h:119
 TEfficiency.h:120
 TEfficiency.h:121
 TEfficiency.h:122
 TEfficiency.h:123
 TEfficiency.h:124
 TEfficiency.h:125
 TEfficiency.h:126
 TEfficiency.h:127
 TEfficiency.h:128
 TEfficiency.h:129
 TEfficiency.h:130
 TEfficiency.h:131
 TEfficiency.h:132
 TEfficiency.h:133
 TEfficiency.h:134
 TEfficiency.h:135
 TEfficiency.h:136
 TEfficiency.h:137
 TEfficiency.h:138
 TEfficiency.h:139
 TEfficiency.h:140
 TEfficiency.h:141
 TEfficiency.h:142
 TEfficiency.h:143
 TEfficiency.h:144
 TEfficiency.h:145
 TEfficiency.h:146
 TEfficiency.h:147
 TEfficiency.h:148
 TEfficiency.h:149
 TEfficiency.h:150
 TEfficiency.h:151
 TEfficiency.h:152
 TEfficiency.h:153
 TEfficiency.h:154
 TEfficiency.h:155
 TEfficiency.h:156
 TEfficiency.h:157
 TEfficiency.h:158
 TEfficiency.h:159
 TEfficiency.h:160
 TEfficiency.h:161
 TEfficiency.h:162
 TEfficiency.h:163
 TEfficiency.h:164
 TEfficiency.h:165
 TEfficiency.h:166
 TEfficiency.h:167
 TEfficiency.h:168
 TEfficiency.h:169
 TEfficiency.h:170
 TEfficiency.h:171
 TEfficiency.h:172
 TEfficiency.h:173
 TEfficiency.h:174
 TEfficiency.h:175
 TEfficiency.h:176
 TEfficiency.h:177
 TEfficiency.h:178
 TEfficiency.h:179
 TEfficiency.h:180
 TEfficiency.h:181
 TEfficiency.h:182
 TEfficiency.h:183
 TEfficiency.h:184
 TEfficiency.h:185
 TEfficiency.h:186
 TEfficiency.h:187
 TEfficiency.h:188
 TEfficiency.h:189
 TEfficiency.h:190
 TEfficiency.h:191
 TEfficiency.h:192
 TEfficiency.h:193
 TEfficiency.h:194
 TEfficiency.h:195
 TEfficiency.h:196
 TEfficiency.h:197