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