Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <list>
21#include <string>
22#include <sstream>
23
29
30#include "RooWorkspace.h"
31#include "RooMsgService.h"
32#include "RooAbsPdf.h"
33#include "RooRealVar.h"
34#include "RooDataSet.h"
35
36#include <memory>
37
38namespace RooStats {
39
40 class DetailedOutputAggregator;
41
43
44 public:
45 NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE) :
46 fPrior(prior),
47 fParams(parameters),
48 fNToys(nToys),
49 fExpected(asimov),
50 fPoints(NULL),
51 fIndex(0)
52 {
53 if(prior) Refresh();
54 }
56 if(fPoints) { delete fPoints; fPoints = NULL; }
57 }
58
59 void NextPoint(RooArgSet& nuisPoint, Double_t& weight);
60
61 protected:
62 void Refresh();
63
64 private:
65 RooAbsPdf *fPrior; // prior for nuisance parameters
66 const RooArgSet *fParams; // nuisance parameters
69
70 RooAbsData *fPoints; // generated nuisance parameter points
71 Int_t fIndex; // current index in fPoints array
72};
73
75
76 public:
77
80 virtual ~ToyMCSampler();
81
82 static void SetAlwaysUseMultiGen(Bool_t flag);
83
84 void SetUseMultiGen(Bool_t flag) { fUseMultiGen = flag ; }
85
86 // main interface
88 virtual RooDataSet* GetSamplingDistributions(RooArgSet& paramPoint);
90
92 RooArgSet& allParameters,
94 Int_t additionalMC
95 );
96
97
98 // The pdf can be NULL in which case the density from SetPdf()
99 // is used. The snapshot and TestStatistic is also optional.
100 virtual void AddTestStatistic(TestStatistic* t = NULL) {
101 if( t == NULL ) {
102 oocoutI((TObject*)0,InputArguments) << "No test statistic given. Doing nothing." << std::endl;
103 return;
104 }
105
106 //if( t == NULL && fTestStatistics.size() >= 1 ) t = fTestStatistics[0];
107
108 fTestStatistics.push_back( t );
109 }
110
111 // generates toy data
112 // without weight
113 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, RooAbsPdf& pdf) const {
114 if(fExpectedNuisancePar) oocoutE((TObject*)NULL,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
115 double weight;
116 return GenerateToyData(paramPoint, weight, pdf);
117 }
118 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint) const { return GenerateToyData(paramPoint,*fPdf); }
119 // with weight
120 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
121 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
122
123 // generate global observables
124 virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
125
126
127 // Main interface to evaluate the test statistic on a dataset
128 virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI, int i ) {
129 return fTestStatistics[i]->Evaluate(data, nullPOI);
130 }
131 virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) { return EvaluateTestStatistic( data,nullPOI, 0 ); }
132 virtual RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi);
133
134
135 virtual TestStatistic* GetTestStatistic(unsigned int i) const {
136 if( fTestStatistics.size() <= i ) return NULL;
137 return fTestStatistics[i];
138 }
139 virtual TestStatistic* GetTestStatistic(void) const { return GetTestStatistic(0); }
140
141 virtual Double_t ConfidenceLevel() const { return 1. - fSize; }
142 virtual void Initialize(
143 RooAbsArg& /*testStatistic*/,
144 RooArgSet& /*paramsOfInterest*/,
145 RooArgSet& /*nuisanceParameters*/
146 ) {}
147
148 virtual Int_t GetNToys(void) { return fNToys; }
149 virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
150 /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
151 /// use the Poisson-distributed events from the extended PDF.
152 virtual void SetNEventsPerToy(const Int_t nevents) {
153 fNEvents = nevents;
154 }
155
156
157 // Set the Pdf, add to the the workspace if not already there
158 virtual void SetParametersForTestStat(const RooArgSet& nullpoi) {
159 fParametersForTestStat.reset( nullpoi.snapshot() );
160 }
161
162 virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; ClearCache(); }
163
164 // How to randomize the prior. Set to NULL to deactivate randomization.
165 virtual void SetPriorNuisance(RooAbsPdf* pdf) {
166 fPriorNuisance = pdf;
170 }
171 }
172 // specify the nuisance parameters (eg. the rest of the parameters)
173 virtual void SetNuisanceParameters(const RooArgSet& np) { fNuisancePars = &np; }
174 // specify the observables in the dataset (needed to evaluate the test statistic)
175 virtual void SetObservables(const RooArgSet& o) { fObservables = &o; }
176 // specify the conditional observables
177 virtual void SetGlobalObservables(const RooArgSet& o) { fGlobalObservables = &o; }
178
179
180 // set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
181 virtual void SetTestSize(Double_t size) { fSize = size; }
182 // set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
183 virtual void SetConfidenceLevel(Double_t cl) { fSize = 1. - cl; }
184
185 // Set the TestStatistic (want the argument to be a function of the data & parameter points
186 virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i) {
187 if( fTestStatistics.size() < i ) {
188 oocoutE((TObject*)NULL,InputArguments) << "Cannot set test statistic for this index." << std::endl;
189 return;
190 }
191 if( fTestStatistics.size() == i)
192 fTestStatistics.push_back(testStatistic);
193 else
194 fTestStatistics[i] = testStatistic;
195 }
196 virtual void SetTestStatistic(TestStatistic *t) { return SetTestStatistic(t,0); }
197
200
201 // Checks for sufficient information to do a GetSamplingDistribution(...).
202 Bool_t CheckConfig(void);
203
204 // control to use bin data generation (=> see RooFit::AllBinned() option)
205 void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
206 // name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
207 void SetGenerateBinnedTag( const char* binnedTag = "" ) { fGenerateBinnedTag = binnedTag; }
208 // set auto binned generation (=> see RooFit::AutoBinned() option)
209 void SetGenerateAutoBinned( Bool_t autoBinned = kTRUE ) { fGenerateAutoBinned = autoBinned; }
210
211 // Set the name of the sampling distribution used for plotting
213 std::string GetSamplingDistName(void) { return fSamplingDistName; }
214
215 // This option forces a maximum number of total toys.
216 void SetMaxToys(Double_t t) { fMaxToys = t; }
217
218 void SetToysLeftTail(Double_t toys, Double_t threshold) {
219 fToysInTails = toys;
220 fAdaptiveLowLimit = threshold;
222 }
223 void SetToysRightTail(Double_t toys, Double_t threshold) {
224 fToysInTails = toys;
225 fAdaptiveHighLimit = threshold;
227 }
228 void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold) {
229 fToysInTails = toys;
230 fAdaptiveHighLimit = high_threshold;
231 fAdaptiveLowLimit = low_threshold;
232 }
233
234 // calling with argument or NULL deactivates proof
235 void SetProofConfig(ProofConfig *pc = NULL) { fProofConfig = pc; }
236
238
239 protected:
240
242
243 // helper for GenerateToyData
244 RooAbsData* Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const;
245
246 // helper method for clearing the cache
247 virtual void ClearCache();
248
249
250 // densities, snapshots, and test statistics to reweight to
251 RooAbsPdf *fPdf; // model (can be alt or null)
252 std::unique_ptr<const RooArgSet> fParametersForTestStat;
253 std::vector<TestStatistic*> fTestStatistics;
254
255 std::string fSamplingDistName; // name of the model
256 RooAbsPdf *fPriorNuisance; // prior pdf for nuisance parameters
260 Int_t fNToys; // number of toys to generate
261 Int_t fNEvents; // number of events per toy (may be ignored depending on settings)
263 Bool_t fExpectedNuisancePar; // whether to use expectation values for nuisance parameters (ie Asimov data set)
267
268 // minimum no of toys in tails for adaptive sampling
269 // (taking weights into account, therefore double)
270 // Default: 0.0 which means no adaptive sampling
272 // maximum no of toys
273 // (taking weights into account, therefore double)
275 // tails
278
279 const RooDataSet *fProtoData; // in dev
280
282
284
285 // objects below cache information and are mutable and non-persistent
286 mutable RooArgSet* _allVars ; //!
287 mutable std::list<RooAbsPdf*> _pdfList ; //!
288 mutable std::list<RooArgSet*> _obsList ; //!
289 mutable std::list<RooAbsPdf::GenSpec*> _gsList ; //!
290 mutable RooAbsPdf::GenSpec* _gs1 ; //! GenSpec #1
291 mutable RooAbsPdf::GenSpec* _gs2 ; //! GenSpec #2
292 mutable RooAbsPdf::GenSpec* _gs3 ; //! GenSpec #3
293 mutable RooAbsPdf::GenSpec* _gs4 ; //! GenSpec #4
294
295 static Bool_t fgAlwaysUseMultiGen ; // Use PrepareMultiGen always
296 Bool_t fUseMultiGen ; // Use PrepareMultiGen?
297
298 protected:
299 ClassDef(ToyMCSampler, 4) // A simple implementation of the TestStatSampler interface
300};
301}
302
303
304#endif
#define d(i)
Definition RSha256.hxx:102
#define oocoutE(o, a)
#define oocoutI(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:92
double Double_t
Definition RtypesCore.h:59
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassDef(name, id)
Definition Rtypes.h:325
char name[80]
Definition TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:72
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:49
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:29
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:118
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.
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)
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...
ToyMCSampler is an implementation of the TestStatSampler interface.
const RooArgSet * fGlobalObservables
RooAbsPdf::GenSpec * _gs3
GenSpec #2.
virtual void SetNToys(const Int_t ntoy)
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
std::list< RooAbsPdf::GenSpec * > _gsList
void SetProtoData(const RooDataSet *d)
std::string fSamplingDistName
virtual void GenerateGlobalObservables(RooAbsPdf &pdf) const
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
std::string GetSamplingDistName(void)
void SetProofConfig(ProofConfig *pc=NULL)
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
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI, int i)
RooAbsPdf::GenSpec * _gs2
GenSpec #1.
virtual SamplingDistribution * AppendSamplingDistribution(RooArgSet &allParameters, SamplingDistribution *last, Int_t additionalMC)
Extended interface to append to sampling distribution more samples.
NuisanceParametersSampler * fNuisanceParametersSampler
virtual void Initialize(RooAbsArg &, RooArgSet &, RooArgSet &)
const RooArgSet * fObservables
virtual void SetObservables(const RooArgSet &o)
void SetGenerateAutoBinned(Bool_t autoBinned=kTRUE)
virtual TestStatistic * GetTestStatistic(unsigned int i) const
void SetSamplingDistName(const char *name)
void SetToysLeftTail(Double_t toys, Double_t threshold)
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Evaluate all test statistics, returning result and any detailed output.
virtual void SetPdf(RooAbsPdf &pdf)
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
Use for serial and parallel runs.
void SetGenerateBinnedTag(const char *binnedTag="")
Bool_t CheckConfig(void)
only checks, no guessing/determination (do this in calculators, e.g.
virtual void SetPriorNuisance(RooAbsPdf *pdf)
virtual TestStatistic * GetTestStatistic(void) const
static Bool_t fgAlwaysUseMultiGen
GenSpec #4.
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
virtual void AddTestStatistic(TestStatistic *t=NULL)
const RooArgSet * fNuisancePars
std::list< RooArgSet * > _obsList
static void SetAlwaysUseMultiGen(Bool_t flag)
virtual Int_t GetNToys(void)
std::vector< TestStatistic * > fTestStatistics
virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i)
ToyMCSampler()
Proof constructor. Do not use.
virtual void SetGlobalObservables(const RooArgSet &o)
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI)
virtual void SetAsimovNuisancePar(Bool_t i=kTRUE)
void SetGenerateBinned(bool binned=true)
virtual void SetExpectedNuisancePar(Bool_t i=kTRUE)
virtual Double_t ConfidenceLevel() const
virtual void SetTestSize(Double_t size)
virtual void SetTestStatistic(TestStatistic *t)
void SetUseMultiGen(Bool_t flag)
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)
RooAbsPdf::GenSpec * _gs4
GenSpec #3.
ProofConfig * fProofConfig
void SetMaxToys(Double_t t)
virtual void SetNEventsPerToy(const Int_t nevents)
Forces the generation of exactly n events even for extended PDFs.
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
const RooDataSet * fProtoData
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint) const
virtual SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint)
std::list< RooAbsPdf * > _pdfList
virtual void SetNuisanceParameters(const RooArgSet &np)
RooAbsPdf::GenSpec * _gs1
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
virtual void SetConfidenceLevel(Double_t cl)
Mother of all ROOT objects.
Definition TObject.h:37
Basic string class.
Definition TString.h:136
Namespace for the RooStats classes.
Definition Asimov.h:19