//////////////////////////////////////////////////////////////////////////////
//
//  TRolke
//
//  This class computes confidence intervals for the rate of a Poisson
//  in the presence of background and efficiency with a fully frequentist
//  treatment of the uncertainties in the efficiency and background estimate
//  using the profile likelihood method.
//
//      Author: Jan Conrad (CERN) 2004
//      Updated: Johan Lundberg (CERN) 2009
//
//      Copyright CERN 2004,2009           Jan.Conrad@cern.ch,
//                                     Johan.Lundberg@cern.ch
//
//  For information about the statistical meaning of the parameters
//  and the syntax, consult TRolke.cxx
//                  ------------------
//
//  Examples are found in the file Rolke.C
//  --------------------------------------
//
//////////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TRolke
#define ROOT_TRolke

#include "TObject.h"
#include "TMath.h"

// Class definition. This class is not intended to be used as a base class.
class TRolke : public TObject
{

private:
   Double_t fCL;         // confidence level as a fraction [0.9 for 90% ]
   Double_t fUpperLimit; // the calculated upper limit
   Double_t fLowerLimit; // the calculated lower limit
   bool fBounding;       // false for unbounded likelihood
                         // true for bounded likelihood   
   Int_t fNumWarningsDeprecated1;
   Int_t fNumWarningsDeprecated2;

   /* ----------------------------------------------------------------- */
   /* These variables are set by the Set methods for the various models */
   Int_t f_x;
   Int_t f_y;
   Int_t f_z;
   Double_t f_bm;
   Double_t f_em;
   Double_t f_e;
   Int_t f_mid;
   Double_t f_sde;
   Double_t f_sdb;
   Double_t f_tau;
   Double_t f_b;
   Int_t f_m;

   /* ----------------------------------------------------------------- */
   /* Internal helper functions and methods */
   // The Calculator
   Double_t Interval(Int_t x, Int_t y, Int_t z, Double_t bm, Double_t em, Double_t e, Int_t mid, Double_t sde, Double_t sdb, Double_t tau, Double_t b, Int_t m);

   // LIKELIHOOD ROUTINE
   Double_t Likelihood(Double_t mu, Int_t x, Int_t y, Int_t z, Double_t bm, Double_t em, Int_t mid, Double_t sde, Double_t sdb, Double_t tau, Double_t b, Int_t m, Int_t what);

   //MODEL 1
   Double_t EvalLikeMod1(Double_t mu, Int_t x, Int_t y, Int_t z, Double_t tau, Int_t m, Int_t what);
   Double_t LikeMod1(Double_t mu, Double_t b, Double_t e, Int_t x, Int_t y, Int_t z, Double_t tau, Int_t m);
   void     ProfLikeMod1(Double_t mu, Double_t &b, Double_t &e, Int_t x, Int_t y, Int_t z, Double_t tau, Int_t m);
   Double_t LikeGradMod1(Double_t e, Double_t mu, Int_t x, Int_t y, Int_t z, Double_t tau, Int_t m);

   //MODEL 2
   Double_t EvalLikeMod2(Double_t mu, Int_t x, Int_t y, Double_t em, Double_t sde, Double_t tau, Int_t what);

   Double_t LikeMod2(Double_t mu, Double_t b, Double_t e, Int_t x, Int_t y, Double_t em, Double_t tau, Double_t v);

   //MODEL 3
   Double_t EvalLikeMod3(Double_t mu, Int_t x, Double_t bm, Double_t em, Double_t sde, Double_t sdb, Int_t what);
   Double_t LikeMod3(Double_t mu, Double_t b, Double_t e, Int_t x, Double_t bm, Double_t em, Double_t u, Double_t v);

   //MODEL 4
   Double_t EvalLikeMod4(Double_t mu, Int_t x, Int_t y, Double_t tau, Int_t what);
   Double_t LikeMod4(Double_t mu, Double_t b, Int_t x, Int_t y, Double_t tau);

   //MODEL 5
   Double_t EvalLikeMod5(Double_t mu, Int_t x, Double_t bm, Double_t sdb, Int_t what);
   Double_t LikeMod5(Double_t mu, Double_t b, Int_t x, Double_t bm, Double_t u);

   //MODEL 6
   Double_t EvalLikeMod6(Double_t mu, Int_t x, Int_t z, Double_t b, Int_t m, Int_t what);
   Double_t LikeMod6(Double_t mu, Double_t b, Double_t e, Int_t x, Int_t z, Int_t m);

   //MODEL 7
   Double_t EvalLikeMod7(Double_t mu, Int_t x, Double_t em, Double_t sde, Double_t b, Int_t what);
   Double_t LikeMod7(Double_t mu, Double_t b, Double_t e, Int_t x, Double_t em, Double_t v);

   //MISC
   static Double_t EvalPolynomial(Double_t x, const Int_t coef[], Int_t N);
   static Double_t EvalMonomial(Double_t x, const Int_t coef[], Int_t N);
   Double_t LogFactorial(Int_t n);

   Double_t ComputeInterval(Int_t x, Int_t y, Int_t z, Double_t bm, Double_t em, Double_t e, Int_t mid, Double_t sde, Double_t sdb, Double_t tau, Double_t b, Int_t m);

   void SetModelParameters(Int_t x, Int_t y, Int_t z, Double_t bm, Double_t em, Double_t e, Int_t mid, Double_t sde, Double_t sdb, Double_t tau, Double_t b, Int_t m);

   void SetModelParameters();

   Double_t GetBackground();

public:

   /* Constructor */
   TRolke(Double_t CL = 0.9, Option_t *option = "");

   /* Destructor */
   virtual ~TRolke();

   /* Get and set the Confidence Level */
   Double_t GetCL() const         {
      return fCL;
   }
   void     SetCL(Double_t CL)  {
      fCL = CL;
   }

   /* Set the Confidence Level in terms of Sigmas. */
   void SetCLSigmas(Double_t CLsigmas) {
      fCL = TMath::Erf(CLsigmas / TMath::Sqrt(2.0)) ;
   }
   
   // The Set methods for the different models are described in Rolke.cxx
   // model 1
   void SetPoissonBkgBinomEff(Int_t x, Int_t y, Int_t z, Double_t tau, Int_t m);

   // model 2
   void SetPoissonBkgGaussEff(Int_t x, Int_t y, Double_t em, Double_t tau, Double_t sde);

   // model 3
   void SetGaussBkgGaussEff(Int_t x, Double_t bm, Double_t em, Double_t sde, Double_t sdb);

   // model 4
   void SetPoissonBkgKnownEff(Int_t x, Int_t y, Double_t tau, Double_t e);

   // model 5
   void SetGaussBkgKnownEff(Int_t x, Double_t bm, Double_t sdb, Double_t e);

   // model 6
   void SetKnownBkgBinomEff(Int_t x, Int_t z, Int_t m, Double_t b);

   // model 7
   void SetKnownBkgGaussEff(Int_t x, Double_t em, Double_t sde, Double_t b);

   /* Deprecated interface method (read Rolke.cxx). May be removed from future releases */
   Double_t CalculateInterval(Int_t x, Int_t y, Int_t z, Double_t bm, Double_t em, Double_t e, Int_t mid, Double_t sde, Double_t sdb, Double_t tau, Double_t b, Int_t m);

   // get the upper and lower limits based on the specified model
   bool GetLimits(Double_t& low, Double_t& high);
   Double_t GetUpperLimit();
   Double_t GetLowerLimit();

   // get the upper and lower average limits
   bool GetSensitivity(Double_t& low, Double_t& high, Double_t pPrecision = 0.00001);

   // get the upper and lower limits for the outcome corresponding to
   // a given quantile.
   bool GetLimitsQuantile(Double_t& low, Double_t& high, Int_t& out_x, Double_t integral = 0.5);

   // get the upper and lower limits for the most likely outcome.
   bool GetLimitsML(Double_t& low, Double_t& high, Int_t& out_x);

   // get the value of x corresponding to rejection of the null hypothesis.
   bool GetCriticalNumber(Int_t& ncrit,Int_t maxtry=-1);

   /* Get the bounding mode flag. True activates bounded mode. Read
      TRolke.cxx and the references therein for details. */
   bool GetBounding() const {
      return fBounding;
   }

   /* Get the bounding mode flag. True activates bounded mode. Read
      TRolke.cxx and the references therein for details. */
   void SetBounding(const bool bnd) {
      fBounding = bnd;
   }

   /* Deprecated name for SetBounding. */
   void SetSwitch(bool bnd) ;

   /* Dump internals. Option is not used */
   void Print(Option_t*) const;

   ClassDef(TRolke, 2)
};

//calculate confidence limits using the Rolke method
#endif

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