ToyMCSampler.h

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: ToyMCSampler.h 37084 2010-11-29 21:37:13Z moneta $
00002 // Author: Sven Kreiss and Kyle Cranmer    June 2010
00003 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
00004 // Additions and modifications by Mario Pelliccioni
00005 /*************************************************************************
00006  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00007  * All rights reserved.                                                  *
00008  *                                                                       *
00009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00011  *************************************************************************/
00012 
00013 #ifndef ROOSTATS_ToyMCSampler
00014 #define ROOSTATS_ToyMCSampler
00015 
00016 //_________________________________________________
00017 /*
00018 BEGIN_HTML
00019 <p>
00020 ToyMCSampler is an implementation of the TestStatSampler interface.
00021 It generates Toy Monte Carlo for a given parameter point and evaluates a
00022 TestStatistic.
00023 </p>
00024 
00025 <p>
00026 For parallel runs, ToyMCSampler can be given an instance of ProofConfig
00027 and then run in parallel using proof or proof-lite. Internally, it uses
00028 ToyMCStudy with the RooStudyManager.
00029 </p>
00030 END_HTML
00031 */
00032 //
00033 
00034 #ifndef ROOT_Rtypes
00035 #include "Rtypes.h"
00036 #endif
00037 
00038 #include <vector>
00039 #include <sstream>
00040 
00041 #include "RooStats/TestStatSampler.h"
00042 #include "RooStats/SamplingDistribution.h"
00043 #include "RooStats/TestStatistic.h"
00044 #include "RooStats/ModelConfig.h"
00045 #include "RooStats/ProofConfig.h"
00046 
00047 #include "RooWorkspace.h"
00048 #include "RooMsgService.h"
00049 #include "RooAbsPdf.h"
00050 #include "RooRealVar.h"
00051 
00052 #include "RooDataSet.h"
00053 
00054 namespace RooStats {
00055 
00056 class ToyMCSampler: public TestStatSampler {
00057 
00058    public:
00059       ToyMCSampler() :
00060          fTestStat(NULL), fSamplingDistName("temp"), fNToys(1)
00061       {
00062          // Proof constructor. Do not use.
00063 
00064          fPdf = NULL;
00065          fPriorNuisance = NULL;
00066          fNullPOI = NULL;
00067          fNuisancePars = NULL;
00068          fObservables = NULL;
00069          fGlobalObservables = NULL;
00070 
00071          fSize = 0.05;
00072          fNEvents = 0;
00073          fGenerateBinned = kFALSE;
00074          fExpectedNuisancePar = kFALSE;
00075 
00076          fToysInTails = 0.0;
00077          fMaxToys = RooNumber::infinity();
00078          fAdaptiveLowLimit = -RooNumber::infinity();
00079          fAdaptiveHighLimit = RooNumber::infinity();
00080 
00081          fImportanceDensity = NULL;
00082          fImportanceSnapshot = NULL;
00083          fProtoData = NULL;
00084 
00085          fProofConfig = NULL;
00086       }
00087       ToyMCSampler(TestStatistic &ts, Int_t ntoys) :
00088          fTestStat(&ts), fSamplingDistName(ts.GetVarName()), fNToys(ntoys)
00089       {
00090          fPdf = NULL;
00091          fPriorNuisance = NULL;
00092          fNullPOI = NULL;
00093          fNuisancePars = NULL;
00094          fObservables = NULL;
00095          fGlobalObservables = NULL;
00096 
00097          fSize = 0.05;
00098          fNEvents = 0;
00099          fGenerateBinned = kFALSE;
00100          fExpectedNuisancePar = kFALSE;
00101 
00102          fToysInTails = 0.0;
00103          fMaxToys = RooNumber::infinity();
00104          fAdaptiveLowLimit = -RooNumber::infinity();
00105          fAdaptiveHighLimit = RooNumber::infinity();
00106 
00107          fImportanceDensity = NULL;
00108          fImportanceSnapshot = NULL;
00109          fProtoData = NULL;
00110 
00111          fProofConfig = NULL;
00112       }
00113 
00114       virtual ~ToyMCSampler() {
00115       }
00116 
00117       // main interface
00118       virtual SamplingDistribution* GetSamplingDistribution(RooArgSet& paramPoint);
00119 
00120       virtual SamplingDistribution* GetSamplingDistributionSingleWorker(RooArgSet& paramPoint);
00121 
00122 
00123 
00124       // generates toy data
00125       virtual RooAbsData* GenerateToyData(RooArgSet& /*paramPoint*/) const;
00126 
00127 
00128 
00129       // Extended interface to append to sampling distribution more samples
00130       virtual SamplingDistribution* AppendSamplingDistribution(RooArgSet& allParameters, 
00131                                                                SamplingDistribution* last, 
00132                                                                Int_t additionalMC) {
00133 
00134         Int_t tmp = fNToys;
00135         fNToys = additionalMC;
00136         SamplingDistribution* newSamples = GetSamplingDistribution(allParameters);
00137         fNToys = tmp;
00138         
00139         if(last){
00140           last->Add(newSamples);
00141           delete newSamples;
00142           return last;
00143         }
00144 
00145         return newSamples;
00146       }
00147 
00148 
00149       // Main interface to evaluate the test statistic on a dataset
00150       virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) {
00151          return fTestStat->Evaluate(data, nullPOI);
00152       }
00153 
00154       virtual TestStatistic* GetTestStatistic() const { return fTestStat; }
00155       virtual Double_t ConfidenceLevel() const { return 1. - fSize; }
00156       virtual void Initialize(
00157          RooAbsArg& /*testStatistic*/,
00158          RooArgSet& /*paramsOfInterest*/,
00159          RooArgSet& /*nuisanceParameters*/
00160       ) {}
00161 
00162       virtual Int_t GetNToys(void) { return fNToys; }
00163       virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
00164       virtual void SetNEventsPerToy(const Int_t nevents) {
00165          // Forces n events even for extended PDFs. Set NEvents=0 to
00166          // use the Poisson distributed events from the extended PDF.
00167          fNEvents = nevents;
00168       }
00169 
00170 
00171       // specify the values of parameters used when evaluating test statistic
00172       virtual void SetParametersForTestStat(const RooArgSet& nullpoi) { fNullPOI = (RooArgSet*)nullpoi.snapshot(); }
00173       // Set the Pdf, add to the the workspace if not already there
00174       virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; }
00175       // How to randomize the prior. Set to NULL to deactivate randomization.
00176       virtual void SetPriorNuisance(RooAbsPdf* pdf) { fPriorNuisance = pdf; }
00177       // specify the nuisance parameters (eg. the rest of the parameters)
00178       virtual void SetNuisanceParameters(const RooArgSet& np) { fNuisancePars = &np; }
00179       // specify the observables in the dataset (needed to evaluate the test statistic)
00180       virtual void SetObservables(const RooArgSet& o) { fObservables = &o; }
00181       // specify the conditional observables
00182       virtual void SetGlobalObservables(const RooArgSet& o) { fGlobalObservables = &o; }
00183 
00184 
00185       // set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
00186       virtual void SetTestSize(Double_t size) { fSize = size; }
00187       // set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
00188       virtual void SetConfidenceLevel(Double_t cl) { fSize = 1. - cl; }
00189 
00190       // Set the TestStatistic (want the argument to be a function of the data & parameter points
00191       virtual void SetTestStatistic(TestStatistic *testStatistic) { fTestStat = testStatistic; }
00192 
00193       virtual void SetExpectedNuisancePar(Bool_t i = kTRUE) { fExpectedNuisancePar = i; }
00194       virtual void SetAsimovNuisancePar(Bool_t i = kTRUE) { fExpectedNuisancePar = i; }
00195 
00196       // Checks for sufficient information to do a GetSamplingDistribution(...).
00197       Bool_t CheckConfig(void);
00198 
00199       // control to use bin data generation
00200       void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
00201 
00202       // Set the name of the sampling distribution used for plotting
00203       void SetSamplingDistName(const char* name) { if(name) fSamplingDistName = name; }
00204       string GetSamplingDistName(void) { return fSamplingDistName; }
00205 
00206       // This option forces a maximum number of total toys.
00207       void SetMaxToys(Double_t t) { fMaxToys = t; }
00208 
00209       void SetToysLeftTail(Double_t toys, Double_t threshold) {
00210          fToysInTails = toys;
00211          fAdaptiveLowLimit = threshold;
00212          fAdaptiveHighLimit = RooNumber::infinity();
00213       }
00214       void SetToysRightTail(Double_t toys, Double_t threshold) {
00215          fToysInTails = toys;
00216          fAdaptiveHighLimit = threshold;
00217          fAdaptiveLowLimit = -RooNumber::infinity();
00218       }
00219       void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold) {
00220          fToysInTails = toys;
00221          fAdaptiveHighLimit = high_threshold;
00222          fAdaptiveLowLimit = low_threshold;
00223       }
00224 
00225       // for importance sampling, specifies the pdf to sample from
00226       void SetImportanceDensity(RooAbsPdf *p) { fImportanceDensity = p; }
00227       // for importance sampling, a snapshot of the parameters used in importance density
00228       void SetImportanceSnapshot(const RooArgSet &s) { fImportanceSnapshot = &s; }
00229 
00230       // calling with argument or NULL deactivates proof
00231       void SetProofConfig(ProofConfig *pc = NULL) { fProofConfig = pc; }
00232 
00233       void SetProtoData(const RooDataSet* d) { fProtoData = d; }
00234 
00235    protected:
00236 
00237       // helper for GenerateToyData
00238       RooAbsData* Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const;
00239 
00240 
00241 
00242       TestStatistic *fTestStat; // test statistic that is being sampled
00243       RooAbsPdf *fPdf; // model
00244       string fSamplingDistName; // name of the model
00245       RooAbsPdf *fPriorNuisance; // prior pdf for nuisance parameters
00246       RooArgSet *fNullPOI; // parameters of interest
00247       const RooArgSet *fNuisancePars;
00248       const RooArgSet *fObservables;
00249       const RooArgSet *fGlobalObservables;
00250       Int_t fNToys; // number of toys to generate
00251       Int_t fNEvents; // number of events per toy (may be ignored depending on settings)
00252       Double_t fSize;
00253       Bool_t fExpectedNuisancePar; // whether to use expectation values for nuisance parameters (ie Asimov data set)
00254       Bool_t fGenerateBinned;
00255 
00256       // minimum no of toys in tails for adaptive sampling
00257       // (taking weights into account, therefore double)
00258       // Default: 0.0 which means no adaptive sampling
00259       Double_t fToysInTails;
00260       // maximum no of toys
00261       // (taking weights into account, therefore double)
00262       Double_t fMaxToys;
00263       // tails
00264       Double_t fAdaptiveLowLimit;
00265       Double_t fAdaptiveHighLimit;
00266 
00267       RooAbsPdf *fImportanceDensity; // in dev
00268       const RooArgSet *fImportanceSnapshot; // in dev
00269 
00270       const RooDataSet *fProtoData; // in dev
00271 
00272       ProofConfig *fProofConfig;   //!
00273 
00274    protected:
00275    ClassDef(ToyMCSampler,1) // A simple implementation of the TestStatSampler interface
00276 };
00277 }
00278 
00279 
00280 #endif

Generated on Tue Jul 5 14:26:27 2011 for ROOT_528-00b_version by  doxygen 1.5.1