Logo ROOT   master
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
85  virtual RooDataSet* GetSamplingDistributions(RooArgSet& paramPoint);
87 
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
125  virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI, int i ) {
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  /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
148  /// use the Poisson-distributed events from the extended PDF.
149  virtual void SetNEventsPerToy(const Int_t nevents) {
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) {
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;
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 
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;
220  }
221  void SetToysRightTail(Double_t toys, Double_t threshold) {
222  fToysInTails = toys;
223  fAdaptiveHighLimit = threshold;
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
234 
235  void SetProtoData(const RooDataSet* d) { fProtoData = d; }
236 
237  protected:
238 
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 SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint)
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
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
Use for serial and parallel runs.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
TestStatSampler is an interface class for a tools which produce RooStats SamplingDistributions.
virtual Double_t ConfidenceLevel() const
Definition: ToyMCSampler.h:138
RooAbsData * Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const
This is the generate function to use in the context of the ToyMCSampler instead of the standard RooAb...
#define oocoutI(o, a)
Definition: RooMsgService.h:45
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)
Forces the generation of exactly n events even for extended PDFs.
Definition: ToyMCSampler.h:149
Basic string class.
Definition: TString.h:131
std::string fSamplingDistName
Definition: ToyMCSampler.h:253
NuisanceParametersSampler * fNuisanceParametersSampler
Definition: ToyMCSampler.h:281
virtual void SetConfidenceLevel(Double_t cl)
Definition: ToyMCSampler.h:181
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:322
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:48
virtual SamplingDistribution * AppendSamplingDistribution(RooArgSet &allParameters, SamplingDistribution *last, Int_t additionalMC)
Extended interface to append to sampling distribution more samples.
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Evaluate all test statistics, returning result and any detailed output.
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
Bool_t CheckConfig(void)
only checks, no guessing/determination (do this in calculators, e.g.
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
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
static constexpr double pc
const RooDataSet * fProtoData
Definition: ToyMCSampler.h:277
void SetProofConfig(ProofConfig *pc=NULL)
Definition: ToyMCSampler.h:233
virtual void ClearCache()
clear the cache obtained from the pdf used for speeding the toy and global observables generation nee...
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:44
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:33
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:90
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:134
#define d(i)
Definition: RSha256.hxx:102
Namespace for the RooStats classes.
Definition: Asimov.h:19
RooAbsPdf::GenSpec * _gs4
GenSpec #3.
Definition: ToyMCSampler.h:291
double Double_t
Definition: RtypesCore.h:57
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
ToyMCSampler()
Proof constructor. Do not use.
const RooArgSet * fObservables
Definition: ToyMCSampler.h:256
RooAbsPdf, the base class of all PDFs
Definition: RooAbsPdf.h:40
const RooArgSet * fParametersForTestStat
Definition: ToyMCSampler.h:250
virtual void SetTestSize(Double_t size)
Definition: ToyMCSampler.h:179
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
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:73
virtual void GenerateGlobalObservables(RooAbsPdf &pdf) const
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:89
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
char name[80]
Definition: TGX11.cxx:109
static void SetAlwaysUseMultiGen(Bool_t flag)
virtual TestStatistic * GetTestStatistic(void) const
Definition: ToyMCSampler.h:136
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI)
Definition: ToyMCSampler.h:128