Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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) :
42 fParams(parameters),
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
71 ~ToyMCSampler() override;
72
73 static void SetAlwaysUseMultiGen(bool flag);
74
76
77 /// main interface
81
86 );
87
88
89 /// The pdf can be nullptr in which case the density from SetPdf()
90 /// is used. The snapshot and TestStatistic is also optional.
91 virtual void AddTestStatistic(TestStatistic* t = nullptr) {
92 if( t == nullptr ) {
93 oocoutI(nullptr,InputArguments) << "No test statistic given. Doing nothing." << std::endl;
94 return;
95 }
96
97 fTestStatistics.push_back( t );
98 }
99
100 /// generates toy data
101 /// without weight
103 if(fExpectedNuisancePar) oocoutE(nullptr,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
104 double weight;
105 return GenerateToyData(paramPoint, weight, pdf);
106 }
108 /// generates toy data
109 /// with weight
110 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
111 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
112
113 /// generate global observables
114 virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
115
116
117 /// Main interface to evaluate the test statistic on a dataset
119 return fTestStatistics[i]->Evaluate(data, nullPOI);
120 }
123
124
125 virtual TestStatistic* GetTestStatistic(unsigned int i) const {
126 if( fTestStatistics.size() <= i ) return nullptr;
127 return fTestStatistics[i];
128 }
129 TestStatistic* GetTestStatistic(void) const override { return GetTestStatistic(0); }
130
131 double ConfidenceLevel() const override { return 1. - fSize; }
133 RooAbsArg& /*testStatistic*/,
134 RooArgSet& /*paramsOfInterest*/,
135 RooArgSet& /*nuisanceParameters*/
136 ) override {}
137
138 virtual Int_t GetNToys(void) { return fNToys; }
139 virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
140 /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
141 /// use the Poisson-distributed events from the extended PDF.
142 virtual void SetNEventsPerToy(const Int_t nevents) {
144 }
145
146
147 /// Set the Pdf, add to the workspace if not already there
149 auto params = std::make_unique<RooArgSet>();
150 nullpoi.snapshot(*params);
151 fParametersForTestStat = std::move(params);
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) {
185 } else {
187 }
188 }
189 void SetTestStatistic(TestStatistic *t) override { return SetTestStatistic(t,0); }
190
191 virtual void SetExpectedNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
192 virtual void SetAsimovNuisancePar(bool i = true) { fExpectedNuisancePar = i; }
193
194 /// Checks for sufficient information to do a GetSamplingDistribution(...).
195 bool 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)
201 /// set auto binned generation (=> see RooFit::AutoBinned() option)
203
204 /// Set the name of the sampling distribution used for plotting
205 void SetSamplingDistName(const char* name) override { if(name) fSamplingDistName = name; }
206 std::string GetSamplingDistName(void) { return fSamplingDistName; }
207
208 /// This option forces a maximum number of total toys.
209 void SetMaxToys(double t) { fMaxToys = t; }
210
226
228
229 protected:
230
232
233 /// helper for GenerateToyData
234 std::unique_ptr<RooAbsData> Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=nullptr, int forceEvents=0) const;
235
236 /// helper method for clearing the cache
237 virtual void ClearCache();
238
239
240 /// densities, snapshots, and test statistics to reweight to
241 RooAbsPdf *fPdf = nullptr; ///< model (can be alt or null)
242 std::unique_ptr<const RooArgSet> fParametersForTestStat;
243 std::vector<TestStatistic*> fTestStatistics;
244
245 std::string fSamplingDistName; ///< name of the model
246 RooAbsPdf *fPriorNuisance = nullptr; ///< prior pdf for nuisance parameters
247 const RooArgSet *fNuisancePars = nullptr;
248 const RooArgSet *fObservables = nullptr;
250 Int_t fNToys; ///< number of toys to generate
251 Int_t fNEvents = 0; ///< number of events per toy (may be ignored depending on settings)
252 double fSize = 0.05;
254 false; ///< whether to use expectation values for nuisance parameters (ie Asimov data set)
255 bool fGenerateBinned = false;
258
259 /// minimum no of toys in tails for adaptive sampling
260 /// (taking weights into account, therefore double)
261 /// Default: 0.0 which means no adaptive sampling
262 double fToysInTails = 0.0;
263 /// maximum no of toys
264 /// (taking weights into account, therefore double)
265 double fMaxToys;
266 /// tails
269
270 const RooDataSet *fProtoData = nullptr; ///< in dev
271
273
274 // objects below cache information and are mutable and non-persistent
275 mutable std::unique_ptr<RooArgSet> _allVars; ///<!
276 mutable std::vector<RooAbsPdf*> _pdfList; ///<! We don't own those objects
277 mutable std::vector<std::unique_ptr<RooArgSet>> _obsList; ///<!
278 mutable std::vector<std::unique_ptr<RooAbsPdf::GenSpec>> _gsList; ///<!
279 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs1; ///<! GenSpec #1
280 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs2; ///<! GenSpec #2
281 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs3; ///<! GenSpec #3
282 mutable std::unique_ptr<RooAbsPdf::GenSpec> _gs4; ///<! GenSpec #4
283
284 static bool fgAlwaysUseMultiGen ; ///< Use PrepareMultiGen always
285 bool fUseMultiGen = false; ///< Use PrepareMultiGen?
286
287 ClassDefOverride(ToyMCSampler, 0) // A simple implementation of the TestStatSampler interface
288};
289}
290
291
292#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
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.
ToyMCSampler(TestStatistic &ts, Int_t ntoys)
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)
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