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
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 ~ToyMCSampler() override;
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 fTestStatistics.push_back( t );
100 }
101
102 /// generates toy data
103 /// without weight
104 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, RooAbsPdf& pdf) const {
105 if(fExpectedNuisancePar) oocoutE((TObject*)NULL,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
106 double weight;
107 return GenerateToyData(paramPoint, weight, pdf);
108 }
109 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint) const { return GenerateToyData(paramPoint,*fPdf); }
110 /// generates toy data
111 /// with weight
112 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
113 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
114
115 /// generate global observables
116 virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
117
118
119 /// Main interface to evaluate the test statistic on a dataset
121 return fTestStatistics[i]->Evaluate(data, nullPOI);
122 }
123 Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) override { return EvaluateTestStatistic( data,nullPOI, 0 ); }
125
126
127 virtual TestStatistic* GetTestStatistic(unsigned int i) const {
128 if( fTestStatistics.size() <= i ) return NULL;
129 return fTestStatistics[i];
130 }
131 TestStatistic* GetTestStatistic(void) const override { return GetTestStatistic(0); }
132
133 Double_t ConfidenceLevel() const override { return 1. - fSize; }
135 RooAbsArg& /*testStatistic*/,
136 RooArgSet& /*paramsOfInterest*/,
137 RooArgSet& /*nuisanceParameters*/
138 ) override {}
139
140 virtual Int_t GetNToys(void) { return fNToys; }
141 virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
142 /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
143 /// use the Poisson-distributed events from the extended PDF.
144 virtual void SetNEventsPerToy(const Int_t nevents) {
145 fNEvents = nevents;
146 }
147
148
149 /// Set the Pdf, add to the the workspace if not already there
150 void SetParametersForTestStat(const RooArgSet& nullpoi) override {
151 fParametersForTestStat.reset( nullpoi.snapshot() );
152 }
153
154 void SetPdf(RooAbsPdf& pdf) override { fPdf = &pdf; ClearCache(); }
155
156 /// How to randomize the prior. Set to NULL to deactivate randomization.
157 void SetPriorNuisance(RooAbsPdf* pdf) override {
158 fPriorNuisance = pdf;
162 }
163 }
164 /// specify the nuisance parameters (eg. the rest of the parameters)
165 void SetNuisanceParameters(const RooArgSet& np) override { fNuisancePars = &np; }
166 /// specify the observables in the dataset (needed to evaluate the test statistic)
167 void SetObservables(const RooArgSet& o) override { fObservables = &o; }
168 /// specify the conditional observables
169 void SetGlobalObservables(const RooArgSet& o) override { fGlobalObservables = &o; }
170
171
172 /// set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
173 void SetTestSize(Double_t size) override { fSize = size; }
174 /// set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
175 void SetConfidenceLevel(Double_t cl) override { fSize = 1. - cl; }
176
177 /// Set the TestStatistic (want the argument to be a function of the data & parameter points
178 virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i) {
179 if( fTestStatistics.size() < i ) {
180 oocoutE((TObject*)NULL,InputArguments) << "Cannot set test statistic for this index." << std::endl;
181 return;
182 }
183 if( fTestStatistics.size() == i)
184 fTestStatistics.push_back(testStatistic);
185 else
186 fTestStatistics[i] = testStatistic;
187 }
188 void SetTestStatistic(TestStatistic *t) override { return SetTestStatistic(t,0); }
189
192
193 /// Checks for sufficient information to do a GetSamplingDistribution(...).
194 Bool_t CheckConfig(void);
195
196 /// control to use bin data generation (=> see RooFit::AllBinned() option)
197 void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
198 /// name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
199 void SetGenerateBinnedTag( const char* binnedTag = "" ) { fGenerateBinnedTag = binnedTag; }
200 /// set auto binned generation (=> see RooFit::AutoBinned() option)
201 void SetGenerateAutoBinned( Bool_t autoBinned = kTRUE ) { fGenerateAutoBinned = autoBinned; }
202
203 /// Set the name of the sampling distribution used for plotting
204 void SetSamplingDistName(const char* name) override { if(name) fSamplingDistName = name; }
205 std::string GetSamplingDistName(void) { return fSamplingDistName; }
206
207 /// This option forces a maximum number of total toys.
208 void SetMaxToys(Double_t t) { fMaxToys = t; }
209
210 void SetToysLeftTail(Double_t toys, Double_t threshold) {
211 fToysInTails = toys;
212 fAdaptiveLowLimit = threshold;
214 }
215 void SetToysRightTail(Double_t toys, Double_t threshold) {
216 fToysInTails = toys;
217 fAdaptiveHighLimit = threshold;
219 }
220 void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold) {
221 fToysInTails = toys;
222 fAdaptiveHighLimit = high_threshold;
223 fAdaptiveLowLimit = low_threshold;
224 }
225
226 /// calling with argument or NULL deactivates proof
228
230
231 protected:
232
234
235 /// helper for GenerateToyData
236 RooAbsData* Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const;
237
238 /// helper method for clearing the cache
239 virtual void ClearCache();
240
241
242 /// densities, snapshots, and test statistics to reweight to
243 RooAbsPdf *fPdf; ///< model (can be alt or null)
244 std::unique_ptr<const RooArgSet> fParametersForTestStat;
245 std::vector<TestStatistic*> fTestStatistics;
246
247 std::string fSamplingDistName; ///< name of the model
248 RooAbsPdf *fPriorNuisance; ///< prior pdf for nuisance parameters
252 Int_t fNToys; ///< number of toys to generate
253 Int_t fNEvents; ///< number of events per toy (may be ignored depending on settings)
255 Bool_t fExpectedNuisancePar; ///< whether to use expectation values for nuisance parameters (ie Asimov data set)
259
260 /// minimum no of toys in tails for adaptive sampling
261 /// (taking weights into account, therefore double)
262 /// Default: 0.0 which means no adaptive sampling
264 /// maximum no of toys
265 /// (taking weights into account, therefore double)
267 /// tails
270
271 const RooDataSet *fProtoData; ///< in dev
272
274
276
277 // objects below cache information and are mutable and non-persistent
278 mutable std::unique_ptr<RooArgSet> _allVars; ///<!
279 mutable std::vector<RooAbsPdf*> _pdfList; ///<! We don't own those objects
280 mutable std::vector<std::unique_ptr<RooArgSet>> _obsList; ///<!
281 mutable std::vector<std::unique_ptr<RooAbsPdf::GenSpec>> _gsList; ///<!
282 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs1; ///<! GenSpec #1
283 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs2; ///<! GenSpec #2
284 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs3; ///<! GenSpec #3
285 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs4; ///<! GenSpec #4
286
287 static Bool_t fgAlwaysUseMultiGen ; ///< Use PrepareMultiGen always
288 Bool_t fUseMultiGen ; ///< Use PrepareMultiGen?
289
290 protected:
291 ClassDefOverride(ToyMCSampler, 4) // A simple implementation of the TestStatSampler interface
292};
293}
294
295
296#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)
Definition: RooMsgService.h:48
#define oocoutI(o, a)
Definition: RooMsgService.h:45
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t np
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:78
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:61
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:57
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:180
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
static Double_t infinity()
Return internal infinity representation.
Definition: RooNumber.cxx:48
This class is designed to aid in the construction of RooDataSets and RooArgSets, particularly those n...
Helper class for ToyMCSampler.
Definition: ToyMCSampler.h:38
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
Definition: ToyMCSampler.h:63
NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE)
Definition: ToyMCSampler.h:41
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:67
const RooArgSet * fGlobalObservables
Definition: ToyMCSampler.h:251
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:141
void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold)
Definition: ToyMCSampler.h:220
void SetParametersForTestStat(const RooArgSet &nullpoi) override
Set the Pdf, add to the the workspace if not already there.
Definition: ToyMCSampler.h:150
void SetProtoData(const RooDataSet *d)
Definition: ToyMCSampler.h:229
void SetPriorNuisance(RooAbsPdf *pdf) override
How to randomize the prior. Set to NULL to deactivate randomization.
Definition: ToyMCSampler.h:157
std::string fSamplingDistName
name of the model
Definition: ToyMCSampler.h:247
virtual void GenerateGlobalObservables(RooAbsPdf &pdf) const
generate global observables
Double_t fAdaptiveLowLimit
tails
Definition: ToyMCSampler.h:268
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
std::string GetSamplingDistName(void)
Definition: ToyMCSampler.h:205
void SetProofConfig(ProofConfig *pc=NULL)
calling with argument or NULL deactivates proof
Definition: ToyMCSampler.h:227
RooAbsData * Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const
helper for GenerateToyData
std::unique_ptr< const RooArgSet > fParametersForTestStat
Definition: ToyMCSampler.h:244
virtual Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI, int i)
Main interface to evaluate the test statistic on a dataset.
Definition: ToyMCSampler.h:120
Double_t fToysInTails
minimum no of toys in tails for adaptive sampling (taking weights into account, therefore double) Def...
Definition: ToyMCSampler.h:263
virtual SamplingDistribution * AppendSamplingDistribution(RooArgSet &allParameters, SamplingDistribution *last, Int_t additionalMC)
Extended interface to append to sampling distribution more samples.
NuisanceParametersSampler * fNuisanceParametersSampler
!
Definition: ToyMCSampler.h:275
void SetObservables(const RooArgSet &o) override
specify the observables in the dataset (needed to evaluate the test statistic)
Definition: ToyMCSampler.h:167
void Initialize(RooAbsArg &, RooArgSet &, RooArgSet &) override
Common Initialization.
Definition: ToyMCSampler.h:134
std::unique_ptr< RooAbsPdf::GenSpec > _gs4
! GenSpec #4
Definition: ToyMCSampler.h:285
const RooArgSet * fObservables
Definition: ToyMCSampler.h:250
Double_t fMaxToys
maximum no of toys (taking weights into account, therefore double)
Definition: ToyMCSampler.h:266
void SetGenerateAutoBinned(Bool_t autoBinned=kTRUE)
set auto binned generation (=> see RooFit::AutoBinned() option)
Definition: ToyMCSampler.h:201
virtual TestStatistic * GetTestStatistic(unsigned int i) const
Definition: ToyMCSampler.h:127
void SetToysLeftTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:210
Bool_t fUseMultiGen
Use PrepareMultiGen?
Definition: ToyMCSampler.h:288
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Evaluate all test statistics, returning result and any detailed output.
RooAbsPdf * fPdf
densities, snapshots, and test statistics to reweight to
Definition: ToyMCSampler.h:243
SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint) override
main interface
std::unique_ptr< RooAbsPdf::GenSpec > _gs1
! GenSpec #1
Definition: ToyMCSampler.h:282
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
Use for serial and parallel runs.
void SetGenerateBinnedTag(const char *binnedTag="")
name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
Definition: ToyMCSampler.h:199
Bool_t CheckConfig(void)
Checks for sufficient information to do a GetSamplingDistribution(...).
static Bool_t fgAlwaysUseMultiGen
Use PrepareMultiGen always.
Definition: ToyMCSampler.h:287
void SetTestSize(Double_t size) override
set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
Definition: ToyMCSampler.h:173
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
generates toy data without weight
Definition: ToyMCSampler.h:104
std::unique_ptr< RooAbsPdf::GenSpec > _gs2
! GenSpec #2
Definition: ToyMCSampler.h:283
virtual void AddTestStatistic(TestStatistic *t=NULL)
The pdf can be NULL in which case the density from SetPdf() is used.
Definition: ToyMCSampler.h:93
const RooArgSet * fNuisancePars
Definition: ToyMCSampler.h:249
static void SetAlwaysUseMultiGen(Bool_t flag)
Bool_t fExpectedNuisancePar
whether to use expectation values for nuisance parameters (ie Asimov data set)
Definition: ToyMCSampler.h:255
void SetTestStatistic(TestStatistic *t) override
Set the TestStatistic (want the argument to be a function of the data & parameter points.
Definition: ToyMCSampler.h:188
virtual Int_t GetNToys(void)
Definition: ToyMCSampler.h:140
void SetConfidenceLevel(Double_t cl) override
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
Definition: ToyMCSampler.h:175
void SetNuisanceParameters(const RooArgSet &np) override
specify the nuisance parameters (eg. the rest of the parameters)
Definition: ToyMCSampler.h:165
std::unique_ptr< RooAbsPdf::GenSpec > _gs3
! GenSpec #3
Definition: ToyMCSampler.h:284
std::vector< TestStatistic * > fTestStatistics
Definition: ToyMCSampler.h:245
virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i)
Set the TestStatistic (want the argument to be a function of the data & parameter points.
Definition: ToyMCSampler.h:178
Int_t fNEvents
number of events per toy (may be ignored depending on settings)
Definition: ToyMCSampler.h:253
ToyMCSampler()
Proof constructor. Do not use.
std::unique_ptr< RooArgSet > _allVars
!
Definition: ToyMCSampler.h:278
void SetPdf(RooAbsPdf &pdf) override
Set the Pdf, add to the the workspace if not already there.
Definition: ToyMCSampler.h:154
std::vector< RooAbsPdf * > _pdfList
! We don't own those objects
Definition: ToyMCSampler.h:279
virtual void SetAsimovNuisancePar(Bool_t i=kTRUE)
Definition: ToyMCSampler.h:191
void SetGenerateBinned(bool binned=true)
control to use bin data generation (=> see RooFit::AllBinned() option)
Definition: ToyMCSampler.h:197
TestStatistic * GetTestStatistic(void) const override
Get the TestStatistic.
Definition: ToyMCSampler.h:131
virtual void SetExpectedNuisancePar(Bool_t i=kTRUE)
Definition: ToyMCSampler.h:190
Int_t fNToys
number of toys to generate
Definition: ToyMCSampler.h:252
void SetGlobalObservables(const RooArgSet &o) override
specify the conditional observables
Definition: ToyMCSampler.h:169
void SetUseMultiGen(Bool_t flag)
Definition: ToyMCSampler.h:77
virtual void ClearCache()
helper method for clearing the cache
Double_t EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI) override
Main interface to evaluate the test statistic on a dataset.
Definition: ToyMCSampler.h:123
void SetToysRightTail(Double_t toys, Double_t threshold)
Definition: ToyMCSampler.h:215
ProofConfig * fProofConfig
!
Definition: ToyMCSampler.h:273
void SetMaxToys(Double_t t)
This option forces a maximum number of total toys.
Definition: ToyMCSampler.h:208
virtual void SetNEventsPerToy(const Int_t nevents)
Forces the generation of exactly n events even for extended PDFs.
Definition: ToyMCSampler.h:144
std::vector< std::unique_ptr< RooArgSet > > _obsList
!
Definition: ToyMCSampler.h:280
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
Definition: ToyMCSampler.h:113
const RooDataSet * fProtoData
in dev
Definition: ToyMCSampler.h:271
void SetSamplingDistName(const char *name) override
Set the name of the sampling distribution used for plotting.
Definition: ToyMCSampler.h:204
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint) const
Definition: ToyMCSampler.h:109
std::vector< std::unique_ptr< RooAbsPdf::GenSpec > > _gsList
!
Definition: ToyMCSampler.h:281
Double_t ConfidenceLevel() const override
Get the Confidence level for the test.
Definition: ToyMCSampler.h:133
RooAbsPdf * fPriorNuisance
prior pdf for nuisance parameters
Definition: ToyMCSampler.h:248
Mother of all ROOT objects.
Definition: TObject.h:37
Basic string class.
Definition: TString.h:136
@ InputArguments
Definition: RooGlobalFunc.h:64
Namespace for the RooStats classes.
Definition: Asimov.h:19
static constexpr double pc