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=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 workspace if not already there
150 void SetParametersForTestStat(const RooArgSet& nullpoi) override {
151 auto params = std::make_unique<RooArgSet>();
152 nullpoi.snapshot(*params);
153 fParametersForTestStat = std::move(params);
154 }
155
156 void SetPdf(RooAbsPdf& pdf) override { fPdf = &pdf; ClearCache(); }
157
158 /// How to randomize the prior. Set to nullptr to deactivate randomization.
159 void SetPriorNuisance(RooAbsPdf* pdf) override {
160 fPriorNuisance = pdf;
164 }
165 }
166 /// specify the nuisance parameters (eg. the rest of the parameters)
167 void SetNuisanceParameters(const RooArgSet& np) override { fNuisancePars = &np; }
168 /// specify the observables in the dataset (needed to evaluate the test statistic)
169 void SetObservables(const RooArgSet& o) override { fObservables = &o; }
170 /// specify the conditional observables
171 void SetGlobalObservables(const RooArgSet& o) override { fGlobalObservables = &o; }
172
173
174 /// set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
175 void SetTestSize(double size) override { fSize = size; }
176 /// set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
177 void SetConfidenceLevel(double cl) override { fSize = 1. - cl; }
178
179 /// Set the TestStatistic (want the argument to be a function of the data & parameter points
180 virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i) {
181 if( fTestStatistics.size() < i ) {
182 oocoutE(nullptr,InputArguments) << "Cannot set test statistic for this index." << std::endl;
183 return;
184 }
185 if (fTestStatistics.size() == i) {
186 fTestStatistics.push_back(testStatistic);
187 } else {
188 fTestStatistics[i] = testStatistic;
189 }
190 }
191 void SetTestStatistic(TestStatistic *t) override { return SetTestStatistic(t,0); }
192
193 virtual void SetExpectedNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
194 virtual void SetAsimovNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
195
196 /// Checks for sufficient information to do a GetSamplingDistribution(...).
197 bool CheckConfig(void);
198
199 /// control to use bin data generation (=> see RooFit::AllBinned() option)
200 void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
201 /// name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
202 void SetGenerateBinnedTag( const char* binnedTag = "" ) { fGenerateBinnedTag = binnedTag; }
203 /// set auto binned generation (=> see RooFit::AutoBinned() option)
204 void SetGenerateAutoBinned( bool autoBinned = true ) { fGenerateAutoBinned = autoBinned; }
205
206 /// Set the name of the sampling distribution used for plotting
207 void SetSamplingDistName(const char* name) override { if(name) fSamplingDistName = name; }
208 std::string GetSamplingDistName(void) { return fSamplingDistName; }
209
210 /// This option forces a maximum number of total toys.
211 void SetMaxToys(double t) { fMaxToys = t; }
212
213 void SetToysLeftTail(double toys, double threshold) {
214 fToysInTails = toys;
215 fAdaptiveLowLimit = threshold;
217 }
218 void SetToysRightTail(double toys, double threshold) {
219 fToysInTails = toys;
220 fAdaptiveHighLimit = threshold;
222 }
223 void SetToysBothTails(double toys, double low_threshold, double high_threshold) {
224 fToysInTails = toys;
225 fAdaptiveHighLimit = high_threshold;
226 fAdaptiveLowLimit = low_threshold;
227 }
228
229 /// calling with argument or nullptr deactivates proof
230 void SetProofConfig(ProofConfig *pc = nullptr) { fProofConfig = pc; }
231
233
234 protected:
235
237
238 /// helper for GenerateToyData
239 std::unique_ptr<RooAbsData> Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, int forceEvents=0) const;
240
241 /// helper method for clearing the cache
242 virtual void ClearCache();
243
244
245 /// densities, snapshots, and test statistics to reweight to
246 RooAbsPdf *fPdf = nullptr; ///< model (can be alt or null)
247 std::unique_ptr<const RooArgSet> fParametersForTestStat;
248 std::vector<TestStatistic*> fTestStatistics;
249
250 std::string fSamplingDistName; ///< name of the model
251 RooAbsPdf *fPriorNuisance = nullptr; ///< prior pdf for nuisance parameters
252 const RooArgSet *fNuisancePars = nullptr;
253 const RooArgSet *fObservables = nullptr;
255 Int_t fNToys; ///< number of toys to generate
256 Int_t fNEvents = 0; ///< number of events per toy (may be ignored depending on settings)
257 double fSize = 0.05;
259 false; ///< whether to use expectation values for nuisance parameters (ie Asimov data set)
260 bool fGenerateBinned = false;
263
264 /// minimum no of toys in tails for adaptive sampling
265 /// (taking weights into account, therefore double)
266 /// Default: 0.0 which means no adaptive sampling
267 double fToysInTails = 0.0;
268 /// maximum no of toys
269 /// (taking weights into account, therefore double)
270 double fMaxToys;
271 /// tails
274
275 const RooDataSet *fProtoData = nullptr; ///< in dev
276
277 ProofConfig *fProofConfig = nullptr; ///<!
278
280
281 // objects below cache information and are mutable and non-persistent
282 mutable std::unique_ptr<RooArgSet> _allVars; ///<!
283 mutable std::vector<RooAbsPdf*> _pdfList; ///<! We don't own those objects
284 mutable std::vector<std::unique_ptr<RooArgSet>> _obsList; ///<!
285 mutable std::vector<std::unique_ptr<RooAbsPdf::GenSpec>> _gsList; ///<!
286 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs1; ///<! GenSpec #1
287 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs2; ///<! GenSpec #2
288 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs3; ///<! GenSpec #3
289 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs4; ///<! GenSpec #4
290
291 static bool fgAlwaysUseMultiGen ; ///< Use PrepareMultiGen always
292 bool fUseMultiGen = false; ///< Use PrepareMultiGen?
293
294 protected:
295 ClassDefOverride(ToyMCSampler, 4) // A simple implementation of the TestStatSampler interface
296};
297}
298
299
300#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)
#define ClassDefOverride(name, id)
Definition Rtypes.h:346
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
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
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:24
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:154
Container class to hold unbinned data.
Definition RooDataSet.h:33
static constexpr double infinity()
Return internal infinity representation.
Definition RooNumber.h:25
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.
NuisanceParametersSampler(RooAbsPdf *prior=nullptr, const RooArgSet *parameters=nullptr, Int_t nToys=1000, bool asimov=false)
void NextPoint(RooArgSet &nuisPoint, double &weight)
Assigns new nuisance parameter point to members of nuisPoint.
std::unique_ptr< RooAbsData > fPoints
Holds configuration options for proof and proof-lite.
Definition ProofConfig.h:45
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
double ConfidenceLevel() const override
Get the Confidence level for the test.
void SetProofConfig(ProofConfig *pc=nullptr)
calling with argument or nullptr deactivates proof
virtual void SetNToys(const Int_t ntoy)
void SetParametersForTestStat(const RooArgSet &nullpoi) override
Set the Pdf, add to the workspace if not already there.
void SetProtoData(const RooDataSet *d)
void SetPriorNuisance(RooAbsPdf *pdf) override
How to randomize the prior. Set to nullptr to deactivate randomization.
std::string fSamplingDistName
name of the model
virtual void GenerateGlobalObservables(RooAbsPdf &pdf) const
generate global observables
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
std::string GetSamplingDistName(void)
std::unique_ptr< const RooArgSet > fParametersForTestStat
virtual SamplingDistribution * AppendSamplingDistribution(RooArgSet &allParameters, SamplingDistribution *last, Int_t additionalMC)
Extended interface to append to sampling distribution more samples.
NuisanceParametersSampler * fNuisanceParametersSampler
!
void SetObservables(const RooArgSet &o) override
specify the observables in the dataset (needed to evaluate the test statistic)
void Initialize(RooAbsArg &, RooArgSet &, RooArgSet &) override
Common Initialization.
std::unique_ptr< RooAbsPdf::GenSpec > _gs4
! GenSpec #4
const RooArgSet * fObservables
void SetConfidenceLevel(double cl) override
set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
double fToysInTails
minimum no of toys in tails for adaptive sampling (taking weights into account, therefore double) Def...
virtual TestStatistic * GetTestStatistic(unsigned int i) const
void SetToysBothTails(double toys, double low_threshold, double high_threshold)
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
void SetToysRightTail(double toys, double threshold)
SamplingDistribution * GetSamplingDistribution(RooArgSet &paramPoint) override
main interface
void SetMaxToys(double t)
This option forces a maximum number of total toys.
std::unique_ptr< RooAbsPdf::GenSpec > _gs1
! GenSpec #1
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)
virtual void AddTestStatistic(TestStatistic *t=nullptr)
The pdf can be nullptr in which case the density from SetPdf() is used.
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
generates toy data without weight
bool fExpectedNuisancePar
whether to use expectation values for nuisance parameters (ie Asimov data set)
bool fUseMultiGen
Use PrepareMultiGen?
std::unique_ptr< RooAbsPdf::GenSpec > _gs2
! GenSpec #2
const RooArgSet * fNuisancePars
void SetTestStatistic(TestStatistic *t) override
Set the TestStatistic (want the argument to be a function of the data & parameter points.
virtual Int_t GetNToys(void)
void SetNuisanceParameters(const RooArgSet &np) override
specify the nuisance parameters (eg. the rest of the parameters)
std::unique_ptr< RooAbsPdf::GenSpec > _gs3
! GenSpec #3
std::vector< TestStatistic * > fTestStatistics
virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i)
Set the TestStatistic (want the argument to be a function of the data & parameter points.
Int_t fNEvents
number of events per toy (may be ignored depending on settings)
ToyMCSampler()
Proof constructor. Do not use.
double fAdaptiveLowLimit
tails
std::unique_ptr< RooArgSet > _allVars
!
std::unique_ptr< RooAbsData > Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, int forceEvents=0) const
helper for GenerateToyData
static void SetAlwaysUseMultiGen(bool flag)
void SetPdf(RooAbsPdf &pdf) override
Set the Pdf, add to the workspace if not already there.
std::vector< RooAbsPdf * > _pdfList
! We don't own those objects
void SetGenerateBinned(bool binned=true)
control to use bin data generation (=> see RooFit::AllBinned() option)
TestStatistic * GetTestStatistic(void) const override
Get the TestStatistic.
Int_t fNToys
number of toys to generate
void SetToysLeftTail(double toys, double threshold)
virtual void SetExpectedNuisancePar(bool i=true)
void SetGlobalObservables(const RooArgSet &o) override
specify the conditional observables
virtual double EvaluateTestStatistic(RooAbsData &data, RooArgSet &nullPOI, int i)
Main interface to evaluate the test statistic on a dataset.
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.
void SetGenerateAutoBinned(bool autoBinned=true)
set auto binned generation (=> see RooFit::AutoBinned() option)
bool CheckConfig(void)
Checks for sufficient information to do a GetSamplingDistribution(...).
ProofConfig * fProofConfig
!
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
!
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
static bool fgAlwaysUseMultiGen
Use PrepareMultiGen always.
double fMaxToys
maximum no of toys (taking weights into account, therefore double)
void SetUseMultiGen(bool flag)
const RooDataSet * fProtoData
in dev
void SetSamplingDistName(const char *name) override
Set the name of the sampling distribution used for plotting.
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint) const
std::vector< std::unique_ptr< RooAbsPdf::GenSpec > > _gsList
!
RooAbsPdf * fPriorNuisance
prior pdf for nuisance parameters
void SetTestSize(double size) override
set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
virtual void SetAsimovNuisancePar(bool i=true)
Basic string class.
Definition TString.h:139
Namespace for the RooStats classes.
Definition Asimov.h:19