// @(#)root/roostats:$Id$
// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke, Sven Kreiss
/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//_________________________________________________
/*
BEGIN_HTML
<p>
The p-value of the null for a given test statistic is rigorously defined and
this is the starting point for the following conventions.
</p>

<h3>Conventions used in this class</h3>
<p>
The p-value for the null and alternate are on the <b>same side</b> of the
observed value of the test statistic. This is the more standard
convention and avoids confusion when doing inverted tests.
</p>
<p>
For exclusion, we also want the formula
CLs = CLs+b / CLb to hold which therefore defines our conventions
for CLs+b and CLb. CLs was specifically invented for exclusion
and therefore all quantities need be related through the assignments
as they are for exclusion: <b>CLs+b = p_{s+b}; CLb = p_b</b>. This
is derived by considering the scenarios of a powerful and not powerful
inverted test, where for the not so powerful test, CLs must be
close to one.
</p>
<p>
For results of Hypothesis tests,
CLs has no similar direct interpretation as for exclusion and can
be larger than one.
</p>

END_HTML
*/
//


#ifndef ROOSTATS_HypoTestResult
#define ROOSTATS_HypoTestResult

#ifndef ROOT_TNamed
#include "TNamed.h"
#endif

#ifndef ROOSTATS_RooStatsUtils
#include "RooStats/RooStatsUtils.h"
#endif

#ifndef ROOSTATS_SamplingDistribution
#include "RooStats/SamplingDistribution.h"
#endif

namespace RooStats {

   class HypoTestResult : public TNamed {

   public:
      
      // default constructor
      explicit HypoTestResult(const char* name = 0);
      
      // copy constructo
      HypoTestResult(const HypoTestResult& other);

      // constructor from name, null and alternate p values 
      HypoTestResult(const char* name, Double_t nullp, Double_t altp);

      // destructor 
      virtual ~HypoTestResult();

      // assignment operator
      HypoTestResult & operator=(const HypoTestResult& other);

      // add values from another HypoTestResult
      virtual void Append(const HypoTestResult *other);

      // Return p-value for null hypothesis
      virtual Double_t NullPValue() const { return fNullPValue; }

      // Return p-value for alternate hypothesis
      virtual Double_t AlternatePValue() const { return fAlternatePValue; }

      // Convert  NullPValue into a "confidence level"
      virtual Double_t CLb() const { return !fBackgroundIsAlt ? NullPValue() : AlternatePValue(); }

      // Convert  AlternatePValue into a "confidence level"
      virtual Double_t CLsplusb() const { return !fBackgroundIsAlt ? AlternatePValue() : NullPValue(); }

      // CLs is simply CLs+b/CLb (not a method, but a quantity)
      virtual Double_t CLs() const {
         double thisCLb = CLb();
         if (thisCLb == 0) {
            std::cout << "Error: Cannot compute CLs because CLb = 0. Returning CLs = -1\n";
            return -1;
         }
         double thisCLsb = CLsplusb();
         return thisCLsb / thisCLb;
      }

      // familiar name for the Null p-value in terms of 1-sided Gaussian significance
      virtual Double_t Significance() const {return RooStats::PValueToSignificance( NullPValue() ); }

      SamplingDistribution* GetNullDistribution(void) const { return fNullDistr; }
      SamplingDistribution* GetAltDistribution(void) const { return fAltDistr; }
      RooDataSet* GetNullDetailedOutput(void) const { return fNullDetailedOutput; }
      RooDataSet* GetAltDetailedOutput(void) const { return fAltDetailedOutput; }
      RooDataSet* GetFitInfo(void) const { return fFitInfo; }
      Double_t GetTestStatisticData(void) const { return fTestStatisticData; }
      const RooArgList* GetAllTestStatisticsData(void) const { return fAllTestStatisticsData; }
      Bool_t HasTestStatisticData(void) const;

      void SetAltDistribution(SamplingDistribution *alt);
      void SetNullDistribution(SamplingDistribution *null);
      void SetAltDetailedOutput(RooDataSet* d) { fAltDetailedOutput = d; }
      void SetNullDetailedOutput(RooDataSet* d) { fNullDetailedOutput = d; }
      void SetFitInfo(RooDataSet* d) { fFitInfo = d; }
      void SetTestStatisticData(const Double_t tsd);
      void SetAllTestStatisticsData(const RooArgList* tsd);

      void SetPValueIsRightTail(Bool_t pr);
      Bool_t GetPValueIsRightTail(void) const { return fPValueIsRightTail; }

      void SetBackgroundAsAlt(Bool_t l = kTRUE) { fBackgroundIsAlt = l; }
      Bool_t GetBackGroundIsAlt(void) const { return fBackgroundIsAlt; }

      /// The error on the "confidence level" of the null hypothesis
      Double_t CLbError() const;

      /// The error on the "confidence level" of the alternative hypothesis
      Double_t CLsplusbError() const;

      /// The error on the ratio CLs+b/CLb
      Double_t CLsError() const;

      /// The error on the Null p-value
      Double_t NullPValueError() const;

      /// The error on the significance, computed from NullPValueError via error propagation
      Double_t SignificanceError() const;


      void Print(const Option_t* = "") const;

   private:
      void UpdatePValue(const SamplingDistribution* distr, Double_t &pvalue, Double_t &perror,  Bool_t pIsRightTail);


   protected:

      mutable Double_t fNullPValue; // p-value for the null hypothesis (small number means disfavored)
      mutable Double_t fAlternatePValue; // p-value for the alternate hypothesis (small number means disfavored)
      mutable Double_t fNullPValueError; // error of p-value for the null hypothesis (small number means disfavored)
      mutable Double_t fAlternatePValueError; // error of p-value for the alternate hypothesis (small number means disfavored)
      Double_t fTestStatisticData; // result of the test statistic evaluated on data
      const RooArgList* fAllTestStatisticsData; // for the case of multiple test statistics, holds all the results
      SamplingDistribution *fNullDistr;
      SamplingDistribution *fAltDistr;
      RooDataSet* fNullDetailedOutput;
      RooDataSet* fAltDetailedOutput;
      RooDataSet* fFitInfo;
      Bool_t fPValueIsRightTail;
      Bool_t fBackgroundIsAlt;

      ClassDef(HypoTestResult,3)  // Base class to represent results of a hypothesis test

   };
}


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