// @(#)root/roostats:$Id$
// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
/*************************************************************************
 * 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.             *
 *************************************************************************/

#ifndef ROOSTATS_NeymanConstruction
#define ROOSTATS_NeymanConstruction


#ifndef ROOT_Rtypes
#include "Rtypes.h"
#endif

#ifndef ROOSTATS_IntervalCalculator
#include "RooStats/IntervalCalculator.h"
#endif

#include "RooStats/TestStatSampler.h"
#include "RooStats/ModelConfig.h"
#include "RooStats/ConfidenceBelt.h"
#include "RooStats/PointSetInterval.h"

#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooArgSet.h"
#include "TList.h"

class RooAbsData; 

namespace RooStats {

   class ConfInterval; 


/**

   \ingroup Roostats

NeymanConstruction is a concrete implementation of the NeymanConstruction interface that, as the name suggests, performs a NeymanConstruction. It produces a RooStats::PointSetInterval, which is a concrete implementation of the ConfInterval interface.

The Neyman Construction is not a uniquely defined statistical technique, it requires that one specify an ordering rule or ordering principle, which is usually incoded by choosing a specific test statistic and limits of integration (corresponding to upper/lower/central limits). As a result, this class must be configured with the corresponding information before it can produce an interval. Common configurations, such as the Feldman-Cousins approach, can be enforced by other light weight classes.

The Neyman Construction considers every point in the parameter space independently, no assumptions are made that the interval is connected or of a particular shape. As a result, the PointSetInterval class is used to represent the result. The user indicate which points in the parameter space to perform the constrution by providing a PointSetInterval instance with the desired points.

This class is fairly light weight, because the choice of parameter points to be considered is factorized and so is the creation of the sampling distribution of the test statistic (which is done by a concrete class implementing the DistributionCreator interface). As a result, this class basically just drives the construction by:

*   using a DistributionCreator to create the SamplingDistribution of a user-defined test statistic for each parameter point of interest,
*   defining the acceptance region in the data by finding the thresholds on the test statistic such that the integral of the sampling distribution is of the appropriate size and consistent with the limits of integration (eg. upper/lower/central limits),
*   and finally updating the PointSetInterval based on whether the value of the test statistic evaluated on the data are in the acceptance region.

*/

   class NeymanConstruction : public IntervalCalculator{

   public:

     ///     NeymanConstruction();
     NeymanConstruction(RooAbsData& data, ModelConfig& model);

     virtual ~NeymanConstruction();
    
      /// Main interface to get a ConfInterval (will be a PointSetInterval)
     virtual PointSetInterval* GetInterval() const;

      /// in addition to interface we also need:
      /// Set the TestStatSampler (eg. ToyMC or FFT, includes choice of TestStatistic)
      void SetTestStatSampler(TestStatSampler& sampler) {fTestStatSampler = &sampler;}
      /// fLeftSideTailFraction*fSize defines lower edge of acceptance region.
      /// Unified limits use 0, central limits use 0.5, 
      /// for upper/lower limits it is 0/1 depends on sign of test statistic w.r.t. parameter
      void SetLeftSideTailFraction(Double_t leftSideFraction = 0.) {fLeftSideFraction = leftSideFraction;} 

      /// User-defined set of points to test
      void SetParameterPointsToTest(RooAbsData& pointsToTest) {
	fPointsToTest = &pointsToTest;
        fConfBelt = new ConfidenceBelt("ConfBelt",pointsToTest);
      }
      /// This class can make regularly spaced scans based on range stored in RooRealVars.
      /// Choose number of steps for a rastor scan (common for each dimension)
      ///      void SetNumSteps(Int_t);
      /// This class can make regularly spaced scans based on range stored in RooRealVars.
      /// Choose number of steps for a rastor scan (specific for each dimension)
      ///      void SetNumSteps(std::map<RooAbsArg, Int_t>)

      /// Get the size of the test (eg. rate of Type I error)
      virtual Double_t Size() const {return fSize;}

      /// Get the Confidence level for the test
      virtual Double_t ConfidenceLevel()  const {return 1.-fSize;}  

      /// Set ModelConfig
      virtual void SetModel(const ModelConfig &model) {fModel = model;}

      /// Set the DataSet 
      virtual void SetData(RooAbsData& data) { fData = data; }

      /// Set the Pdf, add to the the workspace if not already there
      virtual void SetPdf(RooAbsPdf& /*pdf*/) { 
        std::cout << "DEPRECATED, use ModelConfig"<<std::endl;
      }  

      /// specify the parameters of interest in the interval
      virtual void SetParameters(const RooArgSet& /*set*/) {
        std::cout << "DEPRECATED, use ModelConfig"<<std::endl;
      }

      /// specify the nuisance parameters (eg. the rest of the parameters)
      virtual void SetNuisanceParameters(const RooArgSet& /*set*/) {
        std::cout << "DEPRECATED, use ModelConfig"<<std::endl;
      }

      /// set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
      virtual void SetTestSize(Double_t size) {fSize = size;}
      /// set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
      virtual void SetConfidenceLevel(Double_t cl) {fSize = 1.-cl;}

      /// get confidence belt
      ConfidenceBelt* GetConfidenceBelt() {return fConfBelt;}

      /// adaptive sampling algorithm to speed up interval caculation
      void UseAdaptiveSampling(bool flag=true){fAdaptiveSampling=flag;}

      /// give user ability to ask for more toys
      void AdditionalNToysFactor(double fact){fAdditionalNToysFactor = fact;}

      /// save teh confidence belt to a file
      void SaveBeltToFile(bool flag=true){
	fSaveBeltToFile = flag;
	if(flag) fCreateBelt = true;
      }
      /// should create confidence belt
      void CreateConfBelt(bool flag=true){fCreateBelt = flag;}

      /// Returns instance of TestStatSampler. Use to change properties of
      /// TestStatSampler, e.g. GetTestStatSampler.SetTestSize(Double_t size);
      TestStatSampler* GetTestStatSampler(void) { return fTestStatSampler; }

      
   private:

      Double_t fSize; /// size of the test (eg. specified rate of Type I error)
      RooAbsData& fData; /// data set 
      ModelConfig &fModel;
      /*
      RooAbsPdf * fPdf; // common PDF
      mutable RooArgSet fPOI; // RooArgSet specifying  parameters of interest for interval
      RooArgSet fNuisParams;// RooArgSet specifying  nuisance parameters for interval
      */

      TestStatSampler* fTestStatSampler;
      RooAbsData* fPointsToTest;
      Double_t fLeftSideFraction;
      ConfidenceBelt* fConfBelt;
      bool fAdaptiveSampling; // controls use of adaptive sampling algorithm
      Double_t fAdditionalNToysFactor; // give user ability to ask for more toys
      bool fSaveBeltToFile; // controls use if ConfidenceBelt should be saved to a TFile
      bool fCreateBelt; // controls use if ConfidenceBelt should be saved to a TFile

   protected:
      ClassDef(NeymanConstruction,1)   // Interface for tools setting limits (producing confidence intervals)
   };
}


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