Logo ROOT   6.10/09
Reference Guide
ToyMCSampler.h
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Sven Kreiss and Kyle Cranmer June 2010
3 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
4 // Additions and modifications by Mario Pelliccioni
5 /*************************************************************************
6  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 
13 #ifndef ROOSTATS_ToyMCSampler
14 #define ROOSTATS_ToyMCSampler
15 
16 
17 #include "Rtypes.h"
18 
19 #include <vector>
20 #include <sstream>
21 
24 #include "RooStats/TestStatistic.h"
25 #include "RooStats/ModelConfig.h"
26 #include "RooStats/ProofConfig.h"
27 
28 #include "RooWorkspace.h"
29 #include "RooMsgService.h"
30 #include "RooAbsPdf.h"
31 #include "RooRealVar.h"
32 
33 #include "RooDataSet.h"
34 
35 namespace RooStats {
36 
37  class DetailedOutputAggregator;
38 
40 
41  public:
42  NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE) :
43  fPrior(prior),
44  fParams(parameters),
45  fNToys(nToys),
46  fExpected(asimov),
47  fPoints(NULL),
48  fIndex(0)
49  {
50  if(prior) Refresh();
51  }
53  if(fPoints) { delete fPoints; fPoints = NULL; }
54  }
55 
56  void NextPoint(RooArgSet& nuisPoint, Double_t& weight);
57 
58  protected:
59  void Refresh();
60 
61  private:
62  RooAbsPdf *fPrior; // prior for nuisance parameters
63  const RooArgSet *fParams; // nuisance parameters
66 
67  RooAbsData *fPoints; // generated nuisance parameter points
68  Int_t fIndex; // current index in fPoints array
69 };
70 
72 
73  public:
74 
75  ToyMCSampler();
76  ToyMCSampler(TestStatistic &ts, Int_t ntoys);
77  virtual ~ToyMCSampler();
78 
79  static void SetAlwaysUseMultiGen(Bool_t flag);
80 
81  void SetUseMultiGen(Bool_t flag) { fUseMultiGen = flag ; }
82 
83  // main interface
84  virtual SamplingDistribution* GetSamplingDistribution(RooArgSet& paramPoint);
85  virtual RooDataSet* GetSamplingDistributions(RooArgSet& paramPoint);
86  virtual RooDataSet* GetSamplingDistributionsSingleWorker(RooArgSet& paramPoint);
87 
88  virtual SamplingDistribution* AppendSamplingDistribution(
89  RooArgSet& allParameters,
91  Int_t additionalMC
92  );
93 
94 
95  // The pdf can be NULL in which case the density from SetPdf()
96  // is used. The snapshot and TestStatistic is also optional.
97  virtual void AddTestStatistic(TestStatistic* t = NULL) {
98  if( t == NULL ) {
99  oocoutI((TObject*)0,InputArguments) << "No test statistic given. Doing nothing." << std::endl;
100  return;
101  }
102 
103  //if( t == NULL && fTestStatistics.size() >= 1 ) t = fTestStatistics[0];
104 
105  fTestStatistics.push_back( t );
106  }
107 
108  // generates toy data
109  // without weight
110  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, RooAbsPdf& pdf) const {
111  if(fExpectedNuisancePar) oocoutE((TObject*)NULL,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
112  double weight;
113  return GenerateToyData(paramPoint, weight, pdf);
114  }
115  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint) const { return GenerateToyData(paramPoint,*fPdf); }
116  // with weight
117  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
118  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
119 
120  // generate global observables
121  virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
122 
123 
124  // Main interface to evaluate the test statistic on a dataset
126  return fTestStatistics[i]->Evaluate(data, nullPOI);
127  }
128  virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) { return EvaluateTestStatistic( data,nullPOI, 0 ); }
129  virtual RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi);
130 
131 
132  virtual TestStatistic* GetTestStatistic(unsigned int i) const {
133  if( fTestStatistics.size() <= i ) return NULL;
134  return fTestStatistics[i];
135  }
136  virtual TestStatistic* GetTestStatistic(void) const { return GetTestStatistic(0); }
137 
138  virtual Double_t ConfidenceLevel() const { return 1. - fSize; }
139  virtual void Initialize(
140  RooAbsArg& /*testStatistic*/,
141  RooArgSet& /*paramsOfInterest*/,
142  RooArgSet& /*nuisanceParameters*/
143  ) {}
144 
145  virtual Int_t GetNToys(void) { return fNToys; }
146  virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
147  virtual void SetNEventsPerToy(const Int_t nevents) {
148  // Forces n events even for extended PDFs. Set NEvents=0 to
149  // use the Poisson distributed events from the extended PDF.
150  fNEvents = nevents;
151  }
152 
153 
154  // Set the Pdf, add to the the workspace if not already there
155  virtual void SetParametersForTestStat(const RooArgSet& nullpoi) {
156  if( fParametersForTestStat ) delete fParametersForTestStat;
157  fParametersForTestStat = (const RooArgSet*)nullpoi.snapshot();
158  }
159 
160  virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; ClearCache(); }
161 
162  // How to randomize the prior. Set to NULL to deactivate randomization.
163  virtual void SetPriorNuisance(RooAbsPdf* pdf) {
164  fPriorNuisance = pdf;
165  if (fNuisanceParametersSampler) {
166  delete fNuisanceParametersSampler;
167  fNuisanceParametersSampler = NULL;
168  }
169  }
170  // specify the nuisance parameters (eg. the rest of the parameters)
171  virtual void SetNuisanceParameters(const RooArgSet& np) { fNuisancePars = &np; }
172  // specify the observables in the dataset (needed to evaluate the test statistic)
173  virtual void SetObservables(const RooArgSet& o) { fObservables = &o; }
174  // specify the conditional observables
175  virtual void SetGlobalObservables(const RooArgSet& o) { fGlobalObservables = &o; }
176 
177 
178  // set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
179  virtual void SetTestSize(Double_t size) { fSize = size; }
180  // set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
181  virtual void SetConfidenceLevel(Double_t cl) { fSize = 1. - cl; }
182 
183  // Set the TestStatistic (want the argument to be a function of the data & parameter points
184  virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i) {
185  if( fTestStatistics.size() < i ) {
186  oocoutE((TObject*)NULL,InputArguments) << "Cannot set test statistic for this index." << std::endl;
187  return;
188  }
189  if( fTestStatistics.size() == i)
190  fTestStatistics.push_back(testStatistic);
191  else
192  fTestStatistics[i] = testStatistic;
193  }
194  virtual void SetTestStatistic(TestStatistic *t) { return SetTestStatistic(t,0); }
195 
196  virtual void SetExpectedNuisancePar(Bool_t i = kTRUE) { fExpectedNuisancePar = i; }
197  virtual void SetAsimovNuisancePar(Bool_t i = kTRUE) { fExpectedNuisancePar = i; }
198 
199  // Checks for sufficient information to do a GetSamplingDistribution(...).
200  Bool_t CheckConfig(void);
201 
202  // control to use bin data generation (=> see RooFit::AllBinned() option)
203  void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
204  // name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
205  void SetGenerateBinnedTag( const char* binnedTag = "" ) { fGenerateBinnedTag = binnedTag; }
206  // set auto binned generation (=> see RooFit::AutoBinned() option)
207  void SetGenerateAutoBinned( Bool_t autoBinned = kTRUE ) { fGenerateAutoBinned = autoBinned; }
208 
209  // Set the name of the sampling distribution used for plotting
210  void SetSamplingDistName(const char* name) { if(name) fSamplingDistName = name; }
211  std::string GetSamplingDistName(void) { return fSamplingDistName; }
212 
213  // This option forces a maximum number of total toys.
214  void SetMaxToys(Double_t t) { fMaxToys = t; }
215 
216  void SetToysLeftTail(Double_t toys, Double_t threshold) {
217  fToysInTails = toys;
218  fAdaptiveLowLimit = threshold;
219  fAdaptiveHighLimit = RooNumber::infinity();
220  }
221  void SetToysRightTail(Double_t toys, Double_t threshold) {
222  fToysInTails = toys;
223  fAdaptiveHighLimit = threshold;
224  fAdaptiveLowLimit = -RooNumber::infinity();
225  }
226  void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold) {
227  fToysInTails = toys;
228  fAdaptiveHighLimit = high_threshold;
229  fAdaptiveLowLimit = low_threshold;
230  }
231 
232  // calling with argument or NULL deactivates proof
233  void SetProofConfig(ProofConfig *pc = NULL) { fProofConfig = pc; }
234 
235  void SetProtoData(const RooDataSet* d) { fProtoData = d; }
236 
237  protected:
238 
239  const RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi, DetailedOutputAggregator& detOutAgg);
240 
241  // helper for GenerateToyData
242  RooAbsData* Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const;
243 
244  // helper method for clearing the cache
245  virtual void ClearCache();
246 
247 
248  // densities, snapshots, and test statistics to reweight to
249  RooAbsPdf *fPdf; // model (can be alt or null)
251  std::vector<TestStatistic*> fTestStatistics;
252 
253  std::string fSamplingDistName; // name of the model
254  RooAbsPdf *fPriorNuisance; // prior pdf for nuisance parameters
258  Int_t fNToys; // number of toys to generate
259  Int_t fNEvents; // number of events per toy (may be ignored depending on settings)
261  Bool_t fExpectedNuisancePar; // whether to use expectation values for nuisance parameters (ie Asimov data set)
265 
266  // minimum no of toys in tails for adaptive sampling
267  // (taking weights into account, therefore double)
268  // Default: 0.0 which means no adaptive sampling
270  // maximum no of toys
271  // (taking weights into account, therefore double)
273  // tails
276 
277  const RooDataSet *fProtoData; // in dev
278 
280 
282 
283  // objects below cache information and are mutable and non-persistent
284  mutable RooArgSet* _allVars ; //!
285  mutable std::list<RooAbsPdf*> _pdfList ; //!
286  mutable std::list<RooArgSet*> _obsList ; //!
287  mutable std::list<RooAbsPdf::GenSpec*> _gsList ; //!
288  mutable RooAbsPdf::GenSpec* _gs1 ; //! GenSpec #1
289  mutable RooAbsPdf::GenSpec* _gs2 ; //! GenSpec #2
290  mutable RooAbsPdf::GenSpec* _gs3 ; //! GenSpec #3
291  mutable RooAbsPdf::GenSpec* _gs4 ; //! GenSpec #4
292 
293  static Bool_t fgAlwaysUseMultiGen ; // Use PrepareMultiGen always
294  Bool_t fUseMultiGen ; // Use PrepareMultiGen?
295 
296  protected:
297  ClassDef(ToyMCSampler,3) // A simple implementation of the TestStatSampler interface
298 };
299 }
300 
301 
302 #endif
Holds configuration options for proof and proof-lite.
Definition: ProofConfig.h:46
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:155
virtual void SetTestStatistic(TestStatistic *t)
Definition: ToyMCSampler.h:194
void SetToysLeftTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:216
virtual void SetGlobalObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:175
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI, int i)
Definition: ToyMCSampler.h:125
TestStatSampler is an interface class for a tools which produce RooStats SamplingDistributions.
virtual Double_t ConfidenceLevel() const
Definition: ToyMCSampler.h:138
#define oocoutI(o, a)
Definition: RooMsgService.h:44
RooAbsPdf::GenSpec * _gs1
Definition: ToyMCSampler.h:288
virtual void SetExpectedNuisancePar(Bool_t i=kTRUE)
Definition: ToyMCSampler.h:196
virtual void SetNEventsPerToy(const Int_t nevents)
Definition: ToyMCSampler.h:147
Basic string class.
Definition: TString.h:129
std::string fSamplingDistName
Definition: ToyMCSampler.h:253
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
NuisanceParametersSampler * fNuisanceParametersSampler
Definition: ToyMCSampler.h:281
virtual void SetConfidenceLevel(Double_t cl)
Definition: ToyMCSampler.h:181
#define NULL
Definition: RtypesCore.h:88
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint) const
Definition: ToyMCSampler.h:115
void Refresh()
Creates the initial set of nuisance parameter points.
virtual TestStatistic * GetTestStatistic(unsigned int i) const
Definition: ToyMCSampler.h:132
This class is designed to aid in the construction of RooDataSets and RooArgSets, particularly those n...
virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i)
Definition: ToyMCSampler.h:184
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
Definition: ToyMCSampler.h:118
void NextPoint(RooArgSet &nuisPoint, Double_t &weight)
Assigns new nuisance parameter point to members of nuisPoint.
#define ClassDef(name, id)
Definition: Rtypes.h:297
void SetToysRightTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:221
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
Definition: ToyMCSampler.h:226
#define oocoutE(o, a)
Definition: RooMsgService.h:47
void SetGenerateAutoBinned(Bool_t autoBinned=kTRUE)
Definition: ToyMCSampler.h:207
NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE)
Definition: ToyMCSampler.h:42
std::list< RooArgSet * > _obsList
Definition: ToyMCSampler.h:286
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:146
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
void SetGenerateBinnedTag(const char *binnedTag="")
Definition: ToyMCSampler.h:205
virtual void Initialize(RooAbsArg &, RooArgSet &, RooArgSet &)
Definition: ToyMCSampler.h:139
virtual Int_t GetNToys(void)
Definition: ToyMCSampler.h:145
const RooDataSet * fProtoData
Definition: ToyMCSampler.h:277
void SetProofConfig(ProofConfig *pc=NULL)
Definition: ToyMCSampler.h:233
ProofConfig * fProofConfig
Definition: ToyMCSampler.h:279
static Double_t infinity()
Return internal infinity representation.
Definition: RooNumber.cxx:49
std::vector< TestStatistic * > fTestStatistics
Definition: ToyMCSampler.h:251
const RooArgSet * fGlobalObservables
Definition: ToyMCSampler.h:257
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:71
void SetSamplingDistName(const char *name)
Definition: ToyMCSampler.h:210
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
This class simply holds a sampling distribution of some test statistic.
virtual void SetAsimovNuisancePar(Bool_t i=kTRUE)
Definition: ToyMCSampler.h:197
const Bool_t kFALSE
Definition: RtypesCore.h:92
Namespace for the RooStats classes.
Definition: Asimov.h:20
RooAbsPdf::GenSpec * _gs4
GenSpec #3.
Definition: ToyMCSampler.h:291
double Double_t
Definition: RtypesCore.h:55
virtual void SetObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:173
void SetMaxToys(Double_t t)
Definition: ToyMCSampler.h:214
void SetProtoData(const RooDataSet *d)
Definition: ToyMCSampler.h:235
std::list< RooAbsPdf::GenSpec * > _gsList
Definition: ToyMCSampler.h:287
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:160
static Bool_t fgAlwaysUseMultiGen
GenSpec #4.
Definition: ToyMCSampler.h:293
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:110
virtual void SetPriorNuisance(RooAbsPdf *pdf)
Definition: ToyMCSampler.h:163
Mother of all ROOT objects.
Definition: TObject.h:37
const RooArgSet * fObservables
Definition: ToyMCSampler.h:256
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
const RooArgSet * fParametersForTestStat
Definition: ToyMCSampler.h:250
virtual void SetTestSize(Double_t size)
Definition: ToyMCSampler.h:179
void SetUseMultiGen(Bool_t flag)
Definition: ToyMCSampler.h:81
RooAbsPdf::GenSpec * _gs3
GenSpec #2.
Definition: ToyMCSampler.h:290
void SetGenerateBinned(bool binned=true)
Definition: ToyMCSampler.h:203
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
RooAbsPdf::GenSpec * _gs2
GenSpec #1.
Definition: ToyMCSampler.h:289
std::string GetSamplingDistName(void)
Definition: ToyMCSampler.h:211
const RooArgSet * fNuisancePars
Definition: ToyMCSampler.h:255
virtual void SetNuisanceParameters(const RooArgSet &np)
Definition: ToyMCSampler.h:171
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
Definition: TestStatistic.h:31
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual void AddTestStatistic(TestStatistic *t=NULL)
Definition: ToyMCSampler.h:97
Helper class for ToyMCSampler.
Definition: ToyMCSampler.h:39
RooAbsPdf * fPriorNuisance
Definition: ToyMCSampler.h:254
std::list< RooAbsPdf * > _pdfList
Definition: ToyMCSampler.h:285
virtual TestStatistic * GetTestStatistic(void) const
Definition: ToyMCSampler.h:136
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI)
Definition: ToyMCSampler.h:128