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
20
21#include "RooWorkspace.h"
22#include "RooMsgService.h"
23#include "RooAbsPdf.h"
24#include "RooRealVar.h"
25#include "RooDataSet.h"
26
27#include <vector>
28#include <list>
29#include <string>
30#include <sstream>
31#include <memory>
32
33namespace RooStats {
34
35 class DetailedOutputAggregator;
36
38
39 public:
40 NuisanceParametersSampler(RooAbsPdf *prior=nullptr, const RooArgSet *parameters=nullptr, Int_t nToys=1000, bool asimov=false) :
41 fPrior(prior),
42 fParams(parameters),
43 fNToys(nToys),
44 fExpected(asimov),
45 fIndex(0)
46 {
47 if(prior) Refresh();
48 }
49 virtual ~NuisanceParametersSampler() = default;
50
51 void NextPoint(RooArgSet& nuisPoint, double& weight);
52
53 protected:
54 void Refresh();
55
56 private:
57 RooAbsPdf *fPrior; // prior for nuisance parameters
58 const RooArgSet *fParams; // nuisance parameters
61
62 std::unique_ptr<RooAbsData> fPoints; // generated nuisance parameter points
63 Int_t fIndex; // current index in fPoints array
64};
65
67
68 public:
69
72 ~ToyMCSampler() override;
73
74 static void SetAlwaysUseMultiGen(bool flag);
75
76 void SetUseMultiGen(bool flag) { fUseMultiGen = flag ; }
77
78 /// main interface
80 virtual RooDataSet* GetSamplingDistributions(RooArgSet& paramPoint);
82
84 RooArgSet& allParameters,
86 Int_t additionalMC
87 );
88
89
90 /// The pdf can be nullptr in which case the density from SetPdf()
91 /// is used. The snapshot and TestStatistic is also optional.
92 virtual void AddTestStatistic(TestStatistic* t = nullptr) {
93 if( t == nullptr ) {
94 oocoutI(nullptr,InputArguments) << "No test statistic given. Doing nothing." << std::endl;
95 return;
96 }
97
98 fTestStatistics.push_back( t );
99 }
100
101 /// generates toy data
102 /// without weight
103 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, RooAbsPdf& pdf) const {
104 if(fExpectedNuisancePar) oocoutE(nullptr,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
105 double weight;
106 return GenerateToyData(paramPoint, weight, pdf);
107 }
108 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint) const { return GenerateToyData(paramPoint,*fPdf); }
109 /// generates toy data
110 /// with weight
111 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
112 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
113
114 /// generate global observables
115 virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
116
117
118 /// Main interface to evaluate the test statistic on a dataset
119 virtual double EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI, int i ) {
120 return fTestStatistics[i]->Evaluate(data, nullPOI);
121 }
122 double EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) override { return EvaluateTestStatistic( data,nullPOI, 0 ); }
124
125
126 virtual TestStatistic* GetTestStatistic(unsigned int i) const {
127 if( fTestStatistics.size() <= i ) return nullptr;
128 return fTestStatistics[i];
129 }
130 TestStatistic* GetTestStatistic(void) const override { return GetTestStatistic(0); }
131
132 double ConfidenceLevel() const override { return 1. - fSize; }
134 RooAbsArg& /*testStatistic*/,
135 RooArgSet& /*paramsOfInterest*/,
136 RooArgSet& /*nuisanceParameters*/
137 ) override {}
138
139 virtual Int_t GetNToys(void) { return fNToys; }
140 virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
141 /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
142 /// use the Poisson-distributed events from the extended PDF.
143 virtual void SetNEventsPerToy(const Int_t nevents) {
144 fNEvents = nevents;
145 }
146
147
148 /// Set the Pdf, add to the workspace if not already there
149 void SetParametersForTestStat(const RooArgSet& nullpoi) override {
150 auto params = std::make_unique<RooArgSet>();
151 nullpoi.snapshot(*params);
152 fParametersForTestStat = std::move(params);
153 }
154
155 void SetPdf(RooAbsPdf& pdf) override { fPdf = &pdf; ClearCache(); }
156
157 /// How to randomize the prior. Set to nullptr to deactivate randomization.
158 void SetPriorNuisance(RooAbsPdf* pdf) override {
159 fPriorNuisance = pdf;
163 }
164 }
165 /// specify the nuisance parameters (eg. the rest of the parameters)
166 void SetNuisanceParameters(const RooArgSet& np) override { fNuisancePars = &np; }
167 /// specify the observables in the dataset (needed to evaluate the test statistic)
168 void SetObservables(const RooArgSet& o) override { fObservables = &o; }
169 /// specify the conditional observables
170 void SetGlobalObservables(const RooArgSet& o) override { 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 void SetTestSize(double size) override { fSize = size; }
175 /// set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
176 void SetConfidenceLevel(double cl) override { 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(nullptr,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 }
190 void SetTestStatistic(TestStatistic *t) override { return SetTestStatistic(t,0); }
191
192 virtual void SetExpectedNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
193 virtual void SetAsimovNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
194
195 /// Checks for sufficient information to do a GetSamplingDistribution(...).
196 bool CheckConfig(void);
197
198 /// control to use bin data generation (=> see RooFit::AllBinned() option)
199 void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
200 /// name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
201 void SetGenerateBinnedTag( const char* binnedTag = "" ) { fGenerateBinnedTag = binnedTag; }
202 /// set auto binned generation (=> see RooFit::AutoBinned() option)
203 void SetGenerateAutoBinned( bool autoBinned = true ) { fGenerateAutoBinned = autoBinned; }
204
205 /// Set the name of the sampling distribution used for plotting
206 void SetSamplingDistName(const char* name) override { if(name) fSamplingDistName = name; }
207 std::string GetSamplingDistName(void) { return fSamplingDistName; }
208
209 /// This option forces a maximum number of total toys.
210 void SetMaxToys(double t) { fMaxToys = t; }
211
212 void SetToysLeftTail(double toys, double threshold) {
213 fToysInTails = toys;
214 fAdaptiveLowLimit = threshold;
216 }
217 void SetToysRightTail(double toys, double threshold) {
218 fToysInTails = toys;
219 fAdaptiveHighLimit = threshold;
221 }
222 void SetToysBothTails(double toys, double low_threshold, double high_threshold) {
223 fToysInTails = toys;
224 fAdaptiveHighLimit = high_threshold;
225 fAdaptiveLowLimit = low_threshold;
226 }
227
229
230 protected:
231
233
234 /// helper for GenerateToyData
235 std::unique_ptr<RooAbsData> Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, int forceEvents=0) const;
236
237 /// helper method for clearing the cache
238 virtual void ClearCache();
239
240
241 /// densities, snapshots, and test statistics to reweight to
242 RooAbsPdf *fPdf = nullptr; ///< model (can be alt or null)
243 std::unique_ptr<const RooArgSet> fParametersForTestStat;
244 std::vector<TestStatistic*> fTestStatistics;
245
246 std::string fSamplingDistName; ///< name of the model
247 RooAbsPdf *fPriorNuisance = nullptr; ///< prior pdf for nuisance parameters
248 const RooArgSet *fNuisancePars = nullptr;
249 const RooArgSet *fObservables = nullptr;
251 Int_t fNToys; ///< number of toys to generate
252 Int_t fNEvents = 0; ///< number of events per toy (may be ignored depending on settings)
253 double fSize = 0.05;
255 false; ///< whether to use expectation values for nuisance parameters (ie Asimov data set)
256 bool fGenerateBinned = false;
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
263 double fToysInTails = 0.0;
264 /// maximum no of toys
265 /// (taking weights into account, therefore double)
266 double fMaxToys;
267 /// tails
270
271 const RooDataSet *fProtoData = nullptr; ///< in dev
272
274
275 // objects below cache information and are mutable and non-persistent
276 mutable std::unique_ptr<RooArgSet> _allVars; ///<!
277 mutable std::vector<RooAbsPdf*> _pdfList; ///<! We don't own those objects
278 mutable std::vector<std::unique_ptr<RooArgSet>> _obsList; ///<!
279 mutable std::vector<std::unique_ptr<RooAbsPdf::GenSpec>> _gsList; ///<!
280 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs1; ///<! GenSpec #1
281 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs2; ///<! GenSpec #2
282 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs3; ///<! GenSpec #3
283 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs4; ///<! GenSpec #4
284
285 static bool fgAlwaysUseMultiGen ; ///< Use PrepareMultiGen always
286 bool fUseMultiGen = false; ///< Use PrepareMultiGen?
287
288 protected:
289 ClassDefOverride(ToyMCSampler, 4) // A simple implementation of the TestStatSampler interface
290};
291}
292
293
294#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:77
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:34
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
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.
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(...).
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 CodegenImpl.h:58