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=nullptr, const RooArgSet *parameters=nullptr, Int_t nToys=1000, bool asimov=false) :
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& 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 flag);
76
77 void SetUseMultiGen(bool 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 nullptr in which case the density from SetPdf()
92 /// is used. The snapshot and TestStatistic is also optional.
93 virtual void AddTestStatistic(TestStatistic* t = nullptr) {
94 if( t == nullptr ) {
95 oocoutI(nullptr,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(nullptr,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
120 virtual double EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI, int i ) {
121 return fTestStatistics[i]->Evaluate(data, nullPOI);
122 }
123 double 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 nullptr;
129 return fTestStatistics[i];
130 }
131 TestStatistic* GetTestStatistic(void) const override { return GetTestStatistic(0); }
132
133 double 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 nullptr 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 size) override { fSize = size; }
174 /// set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
175 void SetConfidenceLevel(double 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(nullptr,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
190 virtual void SetExpectedNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
191 virtual void SetAsimovNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
192
193 /// Checks for sufficient information to do a GetSamplingDistribution(...).
194 bool 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 autoBinned = true ) { 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) { fMaxToys = t; }
209
210 void SetToysLeftTail(double toys, double threshold) {
211 fToysInTails = toys;
212 fAdaptiveLowLimit = threshold;
214 }
215 void SetToysRightTail(double toys, double threshold) {
216 fToysInTails = toys;
217 fAdaptiveHighLimit = threshold;
219 }
220 void SetToysBothTails(double toys, double low_threshold, double high_threshold) {
221 fToysInTails = toys;
222 fAdaptiveHighLimit = high_threshold;
223 fAdaptiveLowLimit = low_threshold;
224 }
225
226 /// calling with argument or nullptr deactivates proof
228
230
231 protected:
232
234
235 /// helper for GenerateToyData
236 RooAbsData* Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, 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)
254 double fSize;
255 bool 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)
266 double fMaxToys;
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 fgAlwaysUseMultiGen ; ///< Use PrepareMultiGen always
288 bool 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:52
#define oocoutI(o, a)
Definition: RooMsgService.h:49
#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:71
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:62
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:56
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:179
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
static double 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.
NuisanceParametersSampler(RooAbsPdf *prior=nullptr, const RooArgSet *parameters=nullptr, Int_t nToys=1000, bool asimov=false)
Definition: ToyMCSampler.h:41
void NextPoint(RooArgSet &nuisPoint, double &weight)
Assigns new nuisance parameter point to members of nuisPoint.
std::unique_ptr< RooAbsData > fPoints
Definition: ToyMCSampler.h:63
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
double ConfidenceLevel() const override
Get the Confidence level for the test.
Definition: ToyMCSampler.h:133
void SetProofConfig(ProofConfig *pc=nullptr)
calling with argument or nullptr deactivates proof
Definition: ToyMCSampler.h:227
virtual void SetNToys(const Int_t ntoy)
Definition: ToyMCSampler.h:141
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 nullptr 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
RooAbsData * Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, int forceEvents=0) const
helper for GenerateToyData
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
std::string GetSamplingDistName(void)
Definition: ToyMCSampler.h:205
std::unique_ptr< const RooArgSet > fParametersForTestStat
Definition: ToyMCSampler.h:244
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
void SetConfidenceLevel(double cl) override
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
Definition: ToyMCSampler.h:175
double fToysInTails
minimum no of toys in tails for adaptive sampling (taking weights into account, therefore double) Def...
Definition: ToyMCSampler.h:263
virtual TestStatistic * GetTestStatistic(unsigned int i) const
Definition: ToyMCSampler.h:127
void SetToysBothTails(double toys, double low_threshold, double high_threshold)
Definition: ToyMCSampler.h:220
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
void SetToysRightTail(double toys, double threshold)
Definition: ToyMCSampler.h:215
SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint) override
main interface
void SetMaxToys(double t)
This option forces a maximum number of total toys.
Definition: ToyMCSampler.h:208
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
virtual void AddTestStatistic(TestStatistic *t=nullptr)
The pdf can be nullptr in which case the density from SetPdf() is used.
Definition: ToyMCSampler.h:93
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
generates toy data without weight
Definition: ToyMCSampler.h:104
bool fExpectedNuisancePar
whether to use expectation values for nuisance parameters (ie Asimov data set)
Definition: ToyMCSampler.h:255
bool fUseMultiGen
Use PrepareMultiGen?
Definition: ToyMCSampler.h:288
std::unique_ptr< RooAbsPdf::GenSpec > _gs2
! GenSpec #2
Definition: ToyMCSampler.h:283
const RooArgSet * fNuisancePars
Definition: ToyMCSampler.h:249
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 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.
double fAdaptiveLowLimit
tails
Definition: ToyMCSampler.h:268
std::unique_ptr< RooArgSet > _allVars
!
Definition: ToyMCSampler.h:278
static void SetAlwaysUseMultiGen(bool flag)
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
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
Int_t fNToys
number of toys to generate
Definition: ToyMCSampler.h:252
void SetToysLeftTail(double toys, double threshold)
Definition: ToyMCSampler.h:210
virtual void SetExpectedNuisancePar(bool i=true)
Definition: ToyMCSampler.h:190
void SetGlobalObservables(const RooArgSet &o) override
specify the conditional observables
Definition: ToyMCSampler.h:169
virtual double EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI, int i)
Main interface to evaluate the test statistic on a dataset.
Definition: ToyMCSampler.h:120
virtual void ClearCache()
helper method for clearing the cache
double EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI) override
Main interface to evaluate the test statistic on a dataset.
Definition: ToyMCSampler.h:123
void SetGenerateAutoBinned(bool autoBinned=true)
set auto binned generation (=> see RooFit::AutoBinned() option)
Definition: ToyMCSampler.h:201
bool CheckConfig(void)
Checks for sufficient information to do a GetSamplingDistribution(...).
ProofConfig * fProofConfig
!
Definition: ToyMCSampler.h:273
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
static bool fgAlwaysUseMultiGen
Use PrepareMultiGen always.
Definition: ToyMCSampler.h:287
double fMaxToys
maximum no of toys (taking weights into account, therefore double)
Definition: ToyMCSampler.h:266
void SetUseMultiGen(bool flag)
Definition: ToyMCSampler.h:77
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
RooAbsPdf * fPriorNuisance
prior pdf for nuisance parameters
Definition: ToyMCSampler.h:248
void SetTestSize(double 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 void SetAsimovNuisancePar(bool i=true)
Definition: ToyMCSampler.h:191
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