ToyMCSamplerOld.h

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: ToyMCSamplerOld.h 38101 2011-02-16 16:52:00Z moneta $
00002 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
00003 // Additions and modifications by Mario Pelliccioni
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #ifndef ROOSTATS_ToyMCSamplerOld
00013 #define ROOSTATS_ToyMCSamplerOld
00014 
00015 //_________________________________________________
00016 /*
00017 BEGIN_HTML
00018 <p>
00019 ToyMCSamplerOld is a simple implementation of the TestStatSampler interface.
00020 It generates Toy Monte Carlo for a given parameter point, and evaluates a 
00021 test statistic that the user specifies (passed via the RooStats::TestStatistic interface).
00022 
00023 Development notes: We need to provide a nice way for the user to:
00024 <ul>
00025   <li>specify the number of toy experiments (needed to probe a given confidence level)</li>
00026   <li>specify if the number of events per toy experiment should be fixed (conditioning) or floating (unconditional)</li>
00027   <li>specify if any auxiliary observations should be fixed (conditioning) or floating (unconditional)</li>
00028   <li>specify if nuisance paramters should be part of the toy MC: eg: integrated out (Bayesian marginalization)</li>
00029 </ul>
00030 
00031 All of these should be made fairly explicit in the interface.
00032 </p>
00033 END_HTML
00034 */
00035 //
00036 
00037 #ifndef ROOT_Rtypes
00038 #include "Rtypes.h"
00039 #endif
00040 
00041 #include <vector>
00042 #include <string>
00043 #include <sstream>
00044 
00045 #include "RooStats/TestStatSampler.h"
00046 #include "RooStats/SamplingDistribution.h"
00047 #include "RooStats/TestStatistic.h"
00048 #include "RooStats/RooStatsUtils.h"
00049 
00050 #include "RooWorkspace.h"
00051 #include "RooMsgService.h"
00052 #include "RooAbsPdf.h"
00053 #include "TRandom.h"
00054 
00055 #include "RooDataSet.h"
00056 
00057 namespace RooStats {
00058 
00059     class ToyMCSamplerOld : public TestStatSampler {
00060 
00061 
00062   public:
00063     ToyMCSamplerOld(TestStatistic &ts) {
00064       fTestStat = &ts;
00065       fWS = new RooWorkspace();
00066       fOwnsWorkspace = true;
00067       fDataName = "";
00068       fPdfName = "";
00069       fNullPOI = 0;
00070       fNuisParams=0;
00071       fObservables=0;
00072       fExtended = kTRUE;
00073       fRand = new TRandom();
00074       fCounter=0;
00075       fVarName = fTestStat->GetVarName();
00076       fLastDataSet = 0;
00077       fNevents = 0; 
00078       fNtoys = 0; 
00079       fSize = 0.05; 
00080     }
00081 
00082     virtual ~ToyMCSamplerOld() {
00083       if(fOwnsWorkspace) delete fWS;
00084       if(fRand) delete fRand;
00085       if(fLastDataSet) delete fLastDataSet;
00086     }
00087     
00088     // Extended interface to append to sampling distribution more samples
00089     virtual SamplingDistribution* AppendSamplingDistribution(RooArgSet& allParameters, 
00090                                                              SamplingDistribution* last, 
00091                                                              Int_t additionalMC) {
00092 
00093       Int_t tmp = fNtoys;
00094       fNtoys = additionalMC;
00095       SamplingDistribution* newSamples = GetSamplingDistribution(allParameters);
00096       fNtoys = tmp;
00097 
00098       if(last){
00099         last->Add(newSamples);
00100         delete newSamples;
00101         return last;
00102       }
00103 
00104       return newSamples;
00105     }
00106 
00107     // Main interface to get a SamplingDistribution
00108     virtual SamplingDistribution* GetSamplingDistribution(RooArgSet& allParameters) {
00109       std::vector<Double_t> testStatVec;
00110       //       cout << " about to generate sampling dist " << endl;
00111 
00112       RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
00113       RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR) ;
00114 
00115       for(Int_t i=0; i<fNtoys; ++i){
00116         //cout << " on toy number " << i << endl;
00117         //      RooAbsData* toydata = (RooAbsData*)GenerateToyData(allParameters);
00118         //      testStatVec.push_back( fTestStat->Evaluate(*toydata, allParameters) );
00119         //      delete toydata;
00120 
00121         RooDataSet* toydata = (RooDataSet*)GenerateToyData(allParameters);
00122 
00123         // note, evaluation always done at fNullPOI
00124         testStatVec.push_back( fTestStat->Evaluate(*toydata, *fNullPOI) );
00125 
00126         // want to clean up memory, but delete toydata causes problem with 
00127         // nll->setData(data, noclone) because pointer to last data set is no longer valid
00128         //      delete toydata; 
00129 
00130         // instead, delete previous data set
00131         if(fLastDataSet) delete fLastDataSet;
00132         fLastDataSet = toydata;
00133       }
00134      
00135        RooMsgService::instance().setGlobalKillBelow(msglevel);
00136 
00137       //      cout << " generated sampling dist " << endl;
00138       return new SamplingDistribution( "temp",//MakeName(allParameters).c_str(),
00139                                        "Sampling Distribution of Test Statistic", testStatVec, fVarName );
00140     } 
00141 
00142      virtual RooAbsData* GenerateToyData(RooArgSet& allParameters) const {
00143        // This method generates a toy dataset for the given parameter point.
00144 
00145 
00146        //       cout << "fNevents = " << fNevents << endl;
00147        RooAbsPdf* pdf = fWS->pdf(fPdfName);
00148        if(!fObservables){
00149          cout << "Observables not specified in ToyMCSamplerOld, will try to determine.  "
00150               << "Will ignore all constant parameters, parameters of interest, and nuisance parameters." << endl;
00151          RooArgSet* observables = pdf->getVariables();
00152          RemoveConstantParameters(observables); // observables might be set constant, this is just a guess
00153 
00154 
00155          if(fNullPOI) observables->remove(*fNullPOI, kFALSE, kTRUE);
00156          if(fNuisParams) observables->remove(*fNuisParams, kFALSE, kTRUE);
00157          cout << "will use the following as observables when generating data" << endl;
00158          observables->Print();
00159          fObservables=observables;
00160        } /*else {
00161            cout << "obs set: will use the following as observables when generating data" << endl;
00162            fObservables->Print();
00163            }*/
00164 
00165        //fluctuate the number of events if fExtended is on.  
00166        // This is a bit slippery for number counting expts. where entry in data and
00167 
00168        /*
00169        // model is number of events, and so number of entries in data always =1.
00170        Int_t nEvents = fNevents;
00171        if(fExtended) {
00172          if( pdf->expectedEvents(*fObservables) > 0){
00173            // if PDF knows expected events use it instead
00174            nEvents = fRand->Poisson(pdf->expectedEvents(*fObservables));
00175          } else{
00176            nEvents = fRand->Poisson(fNevents);
00177          }
00178        }
00179        */
00180 
00181        // Set the parameters to desired values for generating toys
00182        RooArgSet* parameters = pdf->getParameters(fObservables);
00183        RooStats::SetParameters(&allParameters, parameters);
00184 
00185        /*       
00186          cout << "expected events = " <<  pdf->expectedEvents(*observables) 
00187             << "fExtended = " << fExtended
00188             << "fNevents = " << fNevents << " fNevents " 
00189             << "generating" << nEvents << " events " << endl;
00190        */
00191        
00192        RooFit::MsgLevel level = RooMsgService::instance().globalKillBelow();
00193        RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR) ;
00194 
00195        //       cout << "nEvents = " << nEvents << endl;
00196        RooAbsData* data = NULL;
00197        if(fExtended) {
00198          data = (RooAbsData*)pdf->generate(*fObservables, RooFit::Extended());
00199        } else {
00200          data = (RooAbsData*)pdf->generate(*fObservables, fNevents);
00201      }
00202 
00203        RooMsgService::instance().setGlobalKillBelow(level) ;
00204        delete parameters;
00205        return data;
00206      }
00207 
00208      // helper method to create meaningful names for sampling dist
00209      string MakeName(RooArgSet& /*params*/){
00210        std::ostringstream str;
00211        str<<"SamplingDist_"<< fCounter;
00212        fCounter++;
00213        std::string buf = str.str(); 
00214        return buf ;       
00215      }
00216 
00217       // Main interface to evaluate the test statistic on a dataset
00218      virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& allParameters) {
00219        return fTestStat->Evaluate(data, allParameters);
00220      }
00221 
00222       // Get the TestStatistic
00223       virtual TestStatistic* GetTestStatistic()  const {
00224          return fTestStat;
00225       }  
00226     
00227       // Get the Confidence level for the test
00228       virtual Double_t ConfidenceLevel()  const {return 1.-fSize;}  
00229 
00230       // Common Initialization
00231       virtual void Initialize(RooAbsArg& /*testStatistic*/, 
00232                               RooArgSet& /*paramsOfInterest*/, 
00233                               RooArgSet& /*nuisanceParameters*/) {}
00234 
00235       //set the parameters for the toyMC generation
00236       virtual void SetNToys(const Int_t ntoy) {
00237         fNtoys = ntoy;
00238       }
00239 
00240       virtual void SetNEventsPerToy(const Int_t nevents) {
00241         fNevents = nevents;
00242       }
00243 
00244 
00245       virtual void SetExtended(const Bool_t isExtended) {
00246         fExtended = isExtended;
00247       }
00248 
00249       // Set the DataSet, add to the the workspace if not already there
00250       virtual void SetData(RooAbsData& data) {
00251         if(&data){
00252           fWS->import(data);
00253           fDataName = data.GetName();
00254           fWS->Print();
00255         }
00256       }
00257       // Set the Pdf, add to the the workspace if not already there
00258       virtual void SetPdf(RooAbsPdf& pdf) { 
00259         if(&pdf){
00260           fWS->import(pdf);
00261           fPdfName = pdf.GetName();
00262         }
00263       }
00264 
00265       // specify the name of the dataset in the workspace to be used
00266       virtual void SetData(const char* name) {fDataName = name;}
00267       // specify the name of the PDF in the workspace to be used
00268       virtual void SetPdf(const char* name) {fPdfName = name;}
00269       // How to randomize the prior. Set to NULL to deactivate randomization.
00270       virtual void SetPriorNuisance(RooAbsPdf* /*not supported*/) {}
00271 
00272       // specify the values of parameters used when evaluating test statistic
00273       virtual void SetParametersForTestStat(const RooArgSet& nullpoi) {fNullPOI = (RooArgSet*)nullpoi.snapshot();}
00274       // specify the nuisance parameters (eg. the rest of the parameters)
00275       virtual void SetNuisanceParameters(const RooArgSet& set) {fNuisParams = &set;}
00276       // specify the observables in the dataset (needed to evaluate the test statistic)
00277       virtual void SetObservables(const RooArgSet& set) {fObservables = &set;}
00278       // specify the conditional observables
00279       virtual void SetGlobalObservables(const RooArgSet& ) {}
00280 
00281       // set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
00282       virtual void SetTestSize(Double_t size) {fSize = size;}
00283       // set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
00284       virtual void SetConfidenceLevel(Double_t cl) {fSize = 1.-cl;}
00285 
00286       // Set the TestStatistic (want the argument to be a function of the data & parameter points
00287       virtual void SetTestStatistic(TestStatistic* testStat)   {
00288         fTestStat = testStat;
00289       }  
00290 
00291       // Set the name of the sampling distribution used for plotting
00292       void SetSamplingDistName(const char* name) { if(name) fSamplingDistName = name; }
00293 
00294       
00295    private:
00296       Double_t fSize;
00297       RooWorkspace* fWS; // a workspace that owns all the components to be used by the calculator
00298       Bool_t fOwnsWorkspace; // flag if this object owns its workspace
00299       string fSamplingDistName; // name of the model
00300       const char* fPdfName; // name of  common PDF in workspace
00301       const char* fDataName; // name of data set in workspace
00302       RooArgSet* fNullPOI; // the values of parameters used when evaluating test statistic
00303       const RooArgSet* fNuisParams;// RooArgSet specifying  nuisance parameters for interval
00304       mutable const RooArgSet* fObservables; // RooArgSet specifying the observables in the dataset (needed to evaluate the test statistic)
00305       TestStatistic* fTestStat; // pointer to the test statistic that is being sampled
00306       Int_t fNtoys; // number of toys to generate
00307       Int_t fNevents; // number of events per toy (may be ignored depending on settings)
00308       Bool_t fExtended; // if nEvents should fluctuate
00309       TRandom* fRand; // random generator
00310       TString fVarName; // name of test statistic
00311 
00312       Int_t fCounter; // counter for naming sampling dist objects
00313 
00314       RooDataSet* fLastDataSet; // work around for memory issues in nllvar->setData(data, noclone)
00315 
00316    protected:
00317       ClassDef(ToyMCSamplerOld,1)   // A simple implementation of the TestStatSampler interface
00318         };
00319 }
00320 
00321 
00322 #endif

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