Logo ROOT  
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
27
28#include "RooWorkspace.h"
29#include "RooMsgService.h"
30#include "RooAbsPdf.h"
31#include "RooRealVar.h"
32#include "RooDataSet.h"
33
34#include <memory>
35
36namespace RooStats {
37
38 class DetailedOutputAggregator;
39
41
42 public:
43 NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE) :
44 fPrior(prior),
45 fParams(parameters),
46 fNToys(nToys),
47 fExpected(asimov),
48 fPoints(NULL),
49 fIndex(0)
50 {
51 if(prior) Refresh();
52 }
54 if(fPoints) { delete fPoints; fPoints = NULL; }
55 }
56
57 void NextPoint(RooArgSet& nuisPoint, Double_t& weight);
58
59 protected:
60 void Refresh();
61
62 private:
63 RooAbsPdf *fPrior; // prior for nuisance parameters
64 const RooArgSet *fParams; // nuisance parameters
67
68 RooAbsData *fPoints; // generated nuisance parameter points
69 Int_t fIndex; // current index in fPoints array
70};
71
73
74 public:
75
78 virtual ~ToyMCSampler();
79
80 static void SetAlwaysUseMultiGen(Bool_t flag);
81
82 void SetUseMultiGen(Bool_t flag) { fUseMultiGen = flag ; }
83
84 // main interface
86 virtual RooDataSet* GetSamplingDistributions(RooArgSet& paramPoint);
88
90 RooArgSet& allParameters,
92 Int_t additionalMC
93 );
94
95
96 // The pdf can be NULL in which case the density from SetPdf()
97 // is used. The snapshot and TestStatistic is also optional.
98 virtual void AddTestStatistic(TestStatistic* t = NULL) {
99 if( t == NULL ) {
100 oocoutI((TObject*)0,InputArguments) << "No test statistic given. Doing nothing." << std::endl;
101 return;
102 }
103
104 //if( t == NULL && fTestStatistics.size() >= 1 ) t = fTestStatistics[0];
105
106 fTestStatistics.push_back( t );
107 }
108
109 // generates toy data
110 // without weight
111 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, RooAbsPdf& pdf) const {
112 if(fExpectedNuisancePar) oocoutE((TObject*)NULL,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
113 double weight;
114 return GenerateToyData(paramPoint, weight, pdf);
115 }
116 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint) const { return GenerateToyData(paramPoint,*fPdf); }
117 // with weight
118 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
119 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
120
121 // generate global observables
122 virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
123
124
125 // Main interface to evaluate the test statistic on a dataset
126 virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI, int i ) {
127 return fTestStatistics[i]->Evaluate(data, nullPOI);
128 }
129 virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) { return EvaluateTestStatistic( data,nullPOI, 0 ); }
130 virtual RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi);
131
132
133 virtual TestStatistic* GetTestStatistic(unsigned int i) const {
134 if( fTestStatistics.size() <= i ) return NULL;
135 return fTestStatistics[i];
136 }
137 virtual TestStatistic* GetTestStatistic(void) const { return GetTestStatistic(0); }
138
139 virtual Double_t ConfidenceLevel() const { return 1. - fSize; }
140 virtual void Initialize(
141 RooAbsArg& /*testStatistic*/,
142 RooArgSet& /*paramsOfInterest*/,
143 RooArgSet& /*nuisanceParameters*/
144 ) {}
145
146 virtual Int_t GetNToys(void) { return fNToys; }
147 virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
148 /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
149 /// use the Poisson-distributed events from the extended PDF.
150 virtual void SetNEventsPerToy(const Int_t nevents) {
151 fNEvents = nevents;
152 }
153
154
155 // Set the Pdf, add to the the workspace if not already there
156 virtual void SetParametersForTestStat(const RooArgSet& nullpoi) {
157 fParametersForTestStat.reset( 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
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
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)
250 std::unique_ptr<const RooArgSet> fParametersForTestStat;
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, 4) // A simple implementation of the TestStatSampler interface
298};
299}
300
301
302#endif
#define d(i)
Definition: RSha256.hxx:102
#define oocoutE(o, a)
Definition: RooMsgService.h:48
#define oocoutI(o, a)
Definition: RooMsgService.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:90
double Double_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassDef(name, id)
Definition: Rtypes.h:322
char name[80]
Definition: TGX11.cxx:109
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:44
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:126
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
static Double_t infinity()
Return internal infinity representation.
Definition: RooNumber.cxx:49
This class is designed to aid in the construction of RooDataSets and RooArgSets, particularly those n...
Helper class for ToyMCSampler.
Definition: ToyMCSampler.h:40
void Refresh()
Creates the initial set of nuisance parameter points.
void NextPoint(RooArgSet &nuisPoint, Double_t &weight)
Assigns new nuisance parameter point to members of nuisPoint.
NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE)
Definition: ToyMCSampler.h:43
Holds configuration options for proof and proof-lite.
Definition: ProofConfig.h:46
This class simply holds a sampling distribution of some test statistic.
TestStatSampler is an interface class for a tools which produce RooStats SamplingDistributions.
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
Definition: TestStatistic.h:31
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:72
const RooArgSet * fGlobalObservables
Definition: ToyMCSampler.h:257
RooAbsPdf::GenSpec * _gs3
GenSpec #2.
Definition: ToyMCSampler.h:290
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:147
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
Definition: ToyMCSampler.h:226
std::list< RooAbsPdf::GenSpec * > _gsList
Definition: ToyMCSampler.h:287
void SetProtoData(const RooDataSet *d)
Definition: ToyMCSampler.h:235
std::string fSamplingDistName
Definition: ToyMCSampler.h:253
virtual void GenerateGlobalObservables(RooAbsPdf &pdf) const
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
std::string GetSamplingDistName(void)
Definition: ToyMCSampler.h:211
void SetProofConfig(ProofConfig *pc=NULL)
Definition: ToyMCSampler.h:233
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...
std::unique_ptr< const RooArgSet > fParametersForTestStat
Definition: ToyMCSampler.h:250
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI, int i)
Definition: ToyMCSampler.h:126
RooAbsPdf::GenSpec * _gs2
GenSpec #1.
Definition: ToyMCSampler.h:289
virtual SamplingDistribution * AppendSamplingDistribution(RooArgSet &allParameters, SamplingDistribution *last, Int_t additionalMC)
Extended interface to append to sampling distribution more samples.
NuisanceParametersSampler * fNuisanceParametersSampler
Definition: ToyMCSampler.h:281
virtual void Initialize(RooAbsArg &, RooArgSet &, RooArgSet &)
Definition: ToyMCSampler.h:140
const RooArgSet * fObservables
Definition: ToyMCSampler.h:256
virtual void SetObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:173
void SetGenerateAutoBinned(Bool_t autoBinned=kTRUE)
Definition: ToyMCSampler.h:207
virtual TestStatistic * GetTestStatistic(unsigned int i) const
Definition: ToyMCSampler.h:133
void SetSamplingDistName(const char *name)
Definition: ToyMCSampler.h:210
void SetToysLeftTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:216
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Evaluate all test statistics, returning result and any detailed output.
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:160
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
Use for serial and parallel runs.
void SetGenerateBinnedTag(const char *binnedTag="")
Definition: ToyMCSampler.h:205
Bool_t CheckConfig(void)
only checks, no guessing/determination (do this in calculators, e.g.
virtual void SetPriorNuisance(RooAbsPdf *pdf)
Definition: ToyMCSampler.h:163
virtual TestStatistic * GetTestStatistic(void) const
Definition: ToyMCSampler.h:137
static Bool_t fgAlwaysUseMultiGen
GenSpec #4.
Definition: ToyMCSampler.h:293
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:111
virtual void AddTestStatistic(TestStatistic *t=NULL)
Definition: ToyMCSampler.h:98
const RooArgSet * fNuisancePars
Definition: ToyMCSampler.h:255
std::list< RooArgSet * > _obsList
Definition: ToyMCSampler.h:286
static void SetAlwaysUseMultiGen(Bool_t flag)
virtual Int_t GetNToys(void)
Definition: ToyMCSampler.h:146
std::vector< TestStatistic * > fTestStatistics
Definition: ToyMCSampler.h:251
virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i)
Definition: ToyMCSampler.h:184
ToyMCSampler()
Proof constructor. Do not use.
virtual void SetGlobalObservables(const RooArgSet &o)
Definition: ToyMCSampler.h:175
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI)
Definition: ToyMCSampler.h:129
virtual void SetAsimovNuisancePar(Bool_t i=kTRUE)
Definition: ToyMCSampler.h:197
void SetGenerateBinned(bool binned=true)
Definition: ToyMCSampler.h:203
virtual void SetExpectedNuisancePar(Bool_t i=kTRUE)
Definition: ToyMCSampler.h:196
virtual Double_t ConfidenceLevel() const
Definition: ToyMCSampler.h:139
virtual void SetTestSize(Double_t size)
Definition: ToyMCSampler.h:179
virtual void SetTestStatistic(TestStatistic *t)
Definition: ToyMCSampler.h:194
void SetUseMultiGen(Bool_t flag)
Definition: ToyMCSampler.h:82
virtual void ClearCache()
clear the cache obtained from the pdf used for speeding the toy and global observables generation nee...
void SetToysRightTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:221
RooAbsPdf::GenSpec * _gs4
GenSpec #3.
Definition: ToyMCSampler.h:291
ProofConfig * fProofConfig
Definition: ToyMCSampler.h:279
void SetMaxToys(Double_t t)
Definition: ToyMCSampler.h:214
virtual void SetNEventsPerToy(const Int_t nevents)
Forces the generation of exactly n events even for extended PDFs.
Definition: ToyMCSampler.h:150
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
Definition: ToyMCSampler.h:119
const RooDataSet * fProtoData
Definition: ToyMCSampler.h:277
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint) const
Definition: ToyMCSampler.h:116
virtual SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint)
std::list< RooAbsPdf * > _pdfList
Definition: ToyMCSampler.h:285
virtual void SetNuisanceParameters(const RooArgSet &np)
Definition: ToyMCSampler.h:171
RooAbsPdf::GenSpec * _gs1
Definition: ToyMCSampler.h:288
RooAbsPdf * fPriorNuisance
Definition: ToyMCSampler.h:254
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:156
virtual void SetConfidenceLevel(Double_t cl)
Definition: ToyMCSampler.h:181
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:131
@ InputArguments
Definition: RooGlobalFunc.h:68
Namespace for the RooStats classes.
Definition: Asimov.h:19
static constexpr double pc