Logo ROOT  
Reference Guide
ToyMCImportanceSampler.h
Go to the documentation of this file.
1// @(#)root/roostats:$Id$
2// Author: Sven Kreiss and Kyle Cranmer January 2012
3// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
4/*************************************************************************
5 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOSTATS_ToyMCImportanceSampler
13#define ROOSTATS_ToyMCImportanceSampler
14
16#include <vector>
17
18namespace RooStats {
19
21
23
24 public:
27 {
28 // Proof constructor. Do not use.
29
31 fGenerateFromNull = true;
32 fApplyVeto = true;
33 fReuseNLL = true;
35 }
37 ToyMCSampler(ts, ntoys)
38 {
40 fGenerateFromNull = true;
41 fApplyVeto = true;
42 fReuseNLL = true;
44 }
45
46 ~ToyMCImportanceSampler() override;
47
48 /// overwrite GetSamplingDistributionsSingleWorker(paramPoint) with a version that loops
49 /// over nulls and importance densities, but calls the parent
50 /// ToyMCSampler::GetSamplingDistributionsSingleWorker(paramPoint).
52
54 RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const override;
55 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, std::vector<double>& impNLLs, double& nullNLL) const;
56 virtual RooAbsData* GenerateToyData(std::vector<double>& weights) const;
57 virtual RooAbsData* GenerateToyData(std::vector<double>& weights, std::vector<double>& nullNLLs, std::vector<double>& impNLLs) const;
58
59
60 /// specifies the pdf to sample from
61 void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull = false) {
62 if( (fromNull && i >= fNullDensities.size()) ||
63 (!fromNull && i >= fImportanceDensities.size())
64 ) {
65 oocoutE(nullptr,InputArguments) << "Index out of range. Requested index: "<<i<<
66 " , but null densities: "<<fNullDensities.size()<<
67 " and importance densities: "<<fImportanceDensities.size() << std::endl;
68 }
69
71 fGenerateFromNull = fromNull;
72
73 ClearCache();
74 }
75
76 /// For importance sampling with multiple densities/snapshots:
77 /// This is used to check the current Likelihood against Likelihoods from
78 /// other importance densities apart from the one given as importance snapshot.
79 /// The pdf can be nullptr in which case the density from SetImportanceDensity()
80 /// is used. The snapshot is also optional.
82 if( p == nullptr && s == nullptr ) {
83 oocoutI(nullptr,InputArguments) << "Neither density nor snapshot given. Doing nothing." << std::endl;
84 return;
85 }
86 if( p == nullptr && fPdf == nullptr ) {
87 oocoutE(nullptr,InputArguments) << "No density given, but snapshot is there. Aborting." << std::endl;
88 return;
89 }
90
91 if( p == nullptr ) p = fPdf;
92
93 if( s ) s = (const RooArgSet*)s->snapshot();
94
95 fImportanceDensities.push_back( p );
96 fImportanceSnapshots.push_back( s );
97 fImpNLLs.push_back( nullptr );
98 }
99
100 /// The pdf can be nullptr in which case the density from SetPdf()
101 /// is used. The snapshot and TestStatistic is also optional.
102 void AddNullDensity(RooAbsPdf* p, const RooArgSet* s = nullptr) {
103 if( p == nullptr && s == nullptr ) {
104 oocoutI(nullptr,InputArguments) << "Neither density nor snapshot nor test statistic given. Doing nothing." << std::endl;
105 return;
106 }
107
108 if( p == nullptr && fNullDensities.size() >= 1 ) p = fNullDensities[0];
109 if( s == nullptr ) s = fParametersForTestStat.get();
110 if( s ) s = (const RooArgSet*)s->snapshot();
111
112 fNullDensities.push_back( p );
113 fNullSnapshots.push_back( s );
114 fNullNLLs.push_back( nullptr );
115 ClearCache();
116 }
117 /// overwrite from ToyMCSampler
118 void SetPdf(RooAbsPdf& pdf) override {
120
121 if( fNullDensities.size() == 1 ) { fNullDensities[0] = &pdf; }
122 else if( fNullDensities.empty()) AddNullDensity( &pdf );
123 else{
124 oocoutE(nullptr,InputArguments) << "Cannot use SetPdf() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
125 }
126 }
127 /// overwrite from ToyMCSampler
128 void SetParametersForTestStat(const RooArgSet& nullpoi) override {
130 if( fNullSnapshots.empty() ) AddNullDensity( nullptr, &nullpoi );
131 else if( fNullSnapshots.size() == 1 ) {
132 oocoutI(nullptr,InputArguments) << "Overwriting snapshot for the only defined null density." << std::endl;
133 if( fNullSnapshots[0] ) delete fNullSnapshots[0];
134 fNullSnapshots[0] = (const RooArgSet*)nullpoi.snapshot();
135 }else{
136 oocoutE(nullptr,InputArguments) << "Cannot use SetParametersForTestStat() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
137 }
138 }
139
140 /// When set to true, this sets the weight of all toys to zero that
141 /// do not have the largest likelihood under the density it was generated
142 /// compared to the other densities.
143 void SetApplyVeto(bool b = true) { fApplyVeto = b; }
144
145 void SetReuseNLL(bool r = true) { fReuseNLL = r; }
146
147 /// set the conditional observables which will be used when creating the NLL
148 /// so the pdf's will not be normalized on the conditional observables when computing the NLL
149 /// Since the class use a NLL we need to set the conditional observables if they exist in the model
151
153 RooAbsPdf& pdf,
154 const RooArgSet& allPOI,
155 RooRealVar& poi,
156 int n,
157 double poiValueForBackground = 0.0
158 );
160 RooAbsPdf& pdf,
161 const RooArgSet& allPOI,
162 RooRealVar& poi,
163 double nStdDevOverlap = 0.5,
164 double poiValueForBackground = 0.0
165 );
166
169
170 protected:
171
172 /// helper method for clearing the cache
173 void ClearCache() override;
174
175 unsigned int fIndexGenDensity;
178
179 RooArgSet fConditionalObs; ///< set of conditional observables
180
181 /// support multiple null densities
182 std::vector<RooAbsPdf*> fNullDensities;
183 mutable std::vector<const RooArgSet*> fNullSnapshots;
184
185 // densities and snapshots to generate from
186 std::vector<RooAbsPdf*> fImportanceDensities;
187 std::vector<const RooArgSet*> fImportanceSnapshots;
188
190
192
193 mutable std::vector<RooAbsReal*> fNullNLLs; ///<!
194 mutable std::vector<RooAbsReal*> fImpNLLs; ///<!
195
196 protected:
197 ClassDefOverride(ToyMCImportanceSampler,2) // An implementation of importance sampling
198};
199}
200
201#endif
#define oocoutE(o, a)
Definition: RooMsgService.h:52
#define oocoutI(o, a)
Definition: RooMsgService.h:49
#define ClassDefOverride(name, id)
Definition: Rtypes.h:339
winID h TVirtualViewer3D TVirtualGLPainter p
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 b
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 r
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:62
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
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
Definition: TestStatistic.h:31
ToyMCImportanceSampler is an extension of the ToyMCSampler for Importance Sampling.
void AddImportanceDensity(RooAbsPdf *p, const RooArgSet *s)
For importance sampling with multiple densities/snapshots: This is used to check the current Likeliho...
std::vector< const RooArgSet * > fImportanceSnapshots
RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint) override
overwrite GetSamplingDistributionsSingleWorker(paramPoint) with a version that loops over nulls and i...
RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const override
std::vector< const RooArgSet * > fNullSnapshots
void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull=false)
specifies the pdf to sample from
virtual void SetConditionalObservables(const RooArgSet &set)
set the conditional observables which will be used when creating the NLL so the pdf's will not be nor...
ToyMCImportanceSampler(TestStatistic &ts, Int_t ntoys)
std::vector< RooAbsPdf * > fImportanceDensities
void SetPdf(RooAbsPdf &pdf) override
overwrite from ToyMCSampler
void SetApplyVeto(bool b=true)
When set to true, this sets the weight of all toys to zero that do not have the largest likelihood un...
void SetParametersForTestStat(const RooArgSet &nullpoi) override
overwrite from ToyMCSampler
int CreateImpDensitiesForOnePOIAdaptively(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, double nStdDevOverlap=0.5, double poiValueForBackground=0.0)
poi has to be fitted beforehand. This function expects this to be the muhat value.
void AddNullDensity(RooAbsPdf *p, const RooArgSet *s=nullptr)
The pdf can be nullptr in which case the density from SetPdf() is used.
void ClearCache() override
helper method for clearing the cache
RooArgSet fConditionalObs
set of conditional observables
std::vector< RooAbsPdf * > fNullDensities
support multiple null densities
std::vector< RooAbsReal * > fImpNLLs
!
int CreateNImpDensitiesForOnePOI(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, int n, double poiValueForBackground=0.0)
n is the number of importance densities
std::vector< RooAbsReal * > fNullNLLs
!
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:67
void SetParametersForTestStat(const RooArgSet &nullpoi) override
Set the Pdf, add to the workspace if not already there.
Definition: ToyMCSampler.h:150
std::unique_ptr< const RooArgSet > fParametersForTestStat
Definition: ToyMCSampler.h:244
RooAbsPdf * fPdf
densities, snapshots, and test statistics to reweight to
Definition: ToyMCSampler.h:243
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
generates toy data without weight
Definition: ToyMCSampler.h:104
void SetPdf(RooAbsPdf &pdf) override
Set the Pdf, add to the workspace if not already there.
Definition: ToyMCSampler.h:154
const Int_t n
Definition: legend1.C:16
@ InputArguments
Definition: RooGlobalFunc.h:64
Namespace for the RooStats classes.
Definition: Asimov.h:19
static constexpr double s