// @(#)root/roostats:$Id$

/*************************************************************************
 * Project: RooStats                                                     *
 * Package: RooFit/RooStats                                              *
 * Authors:                                                              *
 *   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_SamplingDistribution
#define ROOSTATS_SamplingDistribution

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

#include "Rtypes.h"
#include "RooDataSet.h"


#include <vector>


namespace RooStats {

 class SamplingDistribution : public TNamed {

   public:

    // Constructor for SamplingDistribution
    SamplingDistribution(const char *name,const char *title, std::vector<Double_t>& samplingDist, const char * varName = 0);
    SamplingDistribution(const char *name,const char *title,
			 std::vector<Double_t>& samplingDist, std::vector<Double_t>& sampleWeights, const char * varName = 0);


    SamplingDistribution(const char *name,const char *title, const char * varName = 0);

    SamplingDistribution(const char *name,const char *title, RooDataSet& dataSet, const char * columnName = 0, const char * varName = 0);

    // Default constructor for SamplingDistribution
    SamplingDistribution();
    
    // Destructor of SamplingDistribution
    virtual ~SamplingDistribution();
    
    // get the inverse of the Cumulative distribution function
    Double_t InverseCDF(Double_t pvalue);

    // get the inverse of the Cumulative distribution function
    Double_t InverseCDFInterpolate(Double_t pvalue);

    // get the inverse of the Cumulative distribution function
    // together with the inverse based on sampling variation
    Double_t InverseCDF(Double_t pvalue, Double_t sigmaVariaton, Double_t& inverseVariation);
  
    // merge two sampling distributions
    void Add(const SamplingDistribution* other);
    
    // size of samples
    Int_t GetSize() const{return fSamplingDist.size();}

    // Get test statistics values
    const std::vector<Double_t> & GetSamplingDistribution() const {return fSamplingDist;}
    // Get the sampling weights 
    const std::vector<Double_t> & GetSampleWeights() const {return fSampleWeights;}

    const TString GetVarName() const {return fVarName;}
    
    // numerical integral in these limits
    Double_t Integral(Double_t low, Double_t high, Bool_t normalize = kTRUE, Bool_t lowClosed = kTRUE, Bool_t highClosed = kFALSE) const;

    // numerical integral in these limits including error estimation
    Double_t IntegralAndError(Double_t & error, Double_t low, Double_t high, Bool_t normalize = kTRUE, 
                              Bool_t lowClosed = kTRUE, Bool_t highClosed = kFALSE) const;

    // calculate CDF as a special case of Integral(...) with lower limit equal to -inf
    Double_t CDF(Double_t x) const;

  private:

    mutable std::vector<Double_t> fSamplingDist; // vector of points for the sampling distribution
    mutable std::vector<Double_t> fSampleWeights; // vector of weights for the samples
    // store a RooRealVar that this distribution corresponds to?

    TString fVarName;

    mutable std::vector<Double_t> fSumW;   //! Chached vector with sum of the weight used to compute integral 
    mutable std::vector<Double_t> fSumW2;  //! Chached vector with sum of the weight used to compute integral error 
    
  protected:

    // internal function to sort values
    void SortValues() const;
    
    ClassDef(SamplingDistribution,2)  // Class containing the results of the HybridCalculator
  };
}

#endif
 SamplingDistribution.h:1
 SamplingDistribution.h:2
 SamplingDistribution.h:3
 SamplingDistribution.h:4
 SamplingDistribution.h:5
 SamplingDistribution.h:6
 SamplingDistribution.h:7
 SamplingDistribution.h:8
 SamplingDistribution.h:9
 SamplingDistribution.h:10
 SamplingDistribution.h:11
 SamplingDistribution.h:12
 SamplingDistribution.h:13
 SamplingDistribution.h:14
 SamplingDistribution.h:15
 SamplingDistribution.h:16
 SamplingDistribution.h:17
 SamplingDistribution.h:18
 SamplingDistribution.h:19
 SamplingDistribution.h:20
 SamplingDistribution.h:21
 SamplingDistribution.h:22
 SamplingDistribution.h:23
 SamplingDistribution.h:24
 SamplingDistribution.h:25
 SamplingDistribution.h:26
 SamplingDistribution.h:27
 SamplingDistribution.h:28
 SamplingDistribution.h:29
 SamplingDistribution.h:30
 SamplingDistribution.h:31
 SamplingDistribution.h:32
 SamplingDistribution.h:33
 SamplingDistribution.h:34
 SamplingDistribution.h:35
 SamplingDistribution.h:36
 SamplingDistribution.h:37
 SamplingDistribution.h:38
 SamplingDistribution.h:39
 SamplingDistribution.h:40
 SamplingDistribution.h:41
 SamplingDistribution.h:42
 SamplingDistribution.h:43
 SamplingDistribution.h:44
 SamplingDistribution.h:45
 SamplingDistribution.h:46
 SamplingDistribution.h:47
 SamplingDistribution.h:48
 SamplingDistribution.h:49
 SamplingDistribution.h:50
 SamplingDistribution.h:51
 SamplingDistribution.h:52
 SamplingDistribution.h:53
 SamplingDistribution.h:54
 SamplingDistribution.h:55
 SamplingDistribution.h:56
 SamplingDistribution.h:57
 SamplingDistribution.h:58
 SamplingDistribution.h:59
 SamplingDistribution.h:60
 SamplingDistribution.h:61
 SamplingDistribution.h:62
 SamplingDistribution.h:63
 SamplingDistribution.h:64
 SamplingDistribution.h:65
 SamplingDistribution.h:66
 SamplingDistribution.h:67
 SamplingDistribution.h:68
 SamplingDistribution.h:69
 SamplingDistribution.h:70
 SamplingDistribution.h:71
 SamplingDistribution.h:72
 SamplingDistribution.h:73
 SamplingDistribution.h:74
 SamplingDistribution.h:75
 SamplingDistribution.h:76
 SamplingDistribution.h:77
 SamplingDistribution.h:78
 SamplingDistribution.h:79
 SamplingDistribution.h:80
 SamplingDistribution.h:81
 SamplingDistribution.h:82
 SamplingDistribution.h:83
 SamplingDistribution.h:84
 SamplingDistribution.h:85
 SamplingDistribution.h:86
 SamplingDistribution.h:87
 SamplingDistribution.h:88
 SamplingDistribution.h:89
 SamplingDistribution.h:90
 SamplingDistribution.h:91
 SamplingDistribution.h:92
 SamplingDistribution.h:93
 SamplingDistribution.h:94
 SamplingDistribution.h:95
 SamplingDistribution.h:96
 SamplingDistribution.h:97
 SamplingDistribution.h:98
 SamplingDistribution.h:99
 SamplingDistribution.h:100
 SamplingDistribution.h:101
 SamplingDistribution.h:102
 SamplingDistribution.h:103
 SamplingDistribution.h:104
 SamplingDistribution.h:105