Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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:
25 /// Proof constructor. Do not use.
27
29
30 ~ToyMCImportanceSampler() override;
31
32 /// overwrite GetSamplingDistributionsSingleWorker(paramPoint) with a version that loops
33 /// over nulls and importance densities, but calls the parent
34 /// ToyMCSampler::GetSamplingDistributionsSingleWorker(paramPoint).
36
38 RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const override;
39 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, std::vector<double>& impNLLs, double& nullNLL) const;
40 virtual RooAbsData* GenerateToyData(std::vector<double>& weights) const;
41 virtual RooAbsData* GenerateToyData(std::vector<double>& weights, std::vector<double>& nullNLLs, std::vector<double>& impNLLs) const;
42
43
44 /// specifies the pdf to sample from
45 void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull = false) {
46 if( (fromNull && i >= fNullDensities.size()) ||
47 (!fromNull && i >= fImportanceDensities.size())
48 ) {
49 oocoutE(nullptr,InputArguments) << "Index out of range. Requested index: "<<i<<
50 " , but null densities: "<<fNullDensities.size()<<
51 " and importance densities: "<<fImportanceDensities.size() << std::endl;
52 }
53
55 fGenerateFromNull = fromNull;
56
57 ClearCache();
58 }
59
60 /// For importance sampling with multiple densities/snapshots:
61 /// This is used to check the current Likelihood against Likelihoods from
62 /// other importance densities apart from the one given as importance snapshot.
63 /// The pdf can be nullptr in which case the density from SetImportanceDensity()
64 /// is used. The snapshot is also optional.
66 if( p == nullptr && s == nullptr ) {
67 oocoutI(nullptr,InputArguments) << "Neither density nor snapshot given. Doing nothing." << std::endl;
68 return;
69 }
70 if( p == nullptr && fPdf == nullptr ) {
71 oocoutE(nullptr,InputArguments) << "No density given, but snapshot is there. Aborting." << std::endl;
72 return;
73 }
74
75 if( p == nullptr ) p = fPdf;
76
77 if( s ) s = (const RooArgSet*)s->snapshot();
78
79 fImportanceDensities.push_back( p );
80 fImportanceSnapshots.push_back( s );
81 fImpNLLs.push_back( nullptr );
82 }
83
84 /// The pdf can be nullptr in which case the density from SetPdf()
85 /// is used. The snapshot and TestStatistic is also optional.
86 void AddNullDensity(RooAbsPdf* p, const RooArgSet* s = nullptr) {
87 if( p == nullptr && s == nullptr ) {
88 oocoutI(nullptr,InputArguments) << "Neither density nor snapshot nor test statistic given. Doing nothing." << std::endl;
89 return;
90 }
91
92 if( p == nullptr && !fNullDensities.empty() ) p = fNullDensities[0];
93 if( s == nullptr ) s = fParametersForTestStat.get();
94 if( s ) s = (const RooArgSet*)s->snapshot();
95
96 fNullDensities.push_back( p );
97 fNullSnapshots.push_back( s );
98 fNullNLLs.emplace_back( nullptr );
99 ClearCache();
100 }
101 /// overwrite from ToyMCSampler
102 void SetPdf(RooAbsPdf& pdf) override {
104
105 if( fNullDensities.size() == 1 ) { fNullDensities[0] = &pdf; }
106 else if( fNullDensities.empty()) AddNullDensity( &pdf );
107 else{
108 oocoutE(nullptr,InputArguments) << "Cannot use SetPdf() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
109 }
110 }
111 /// overwrite from ToyMCSampler
112 void SetParametersForTestStat(const RooArgSet& nullpoi) override {
114 if( fNullSnapshots.empty() ) AddNullDensity( nullptr, &nullpoi );
115 else if( fNullSnapshots.size() == 1 ) {
116 oocoutI(nullptr,InputArguments) << "Overwriting snapshot for the only defined null density." << std::endl;
117 if( fNullSnapshots[0] ) delete fNullSnapshots[0];
118 fNullSnapshots[0] = (const RooArgSet*)nullpoi.snapshot();
119 }else{
120 oocoutE(nullptr,InputArguments) << "Cannot use SetParametersForTestStat() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
121 }
122 }
123
124 /// When set to true, this sets the weight of all toys to zero that
125 /// do not have the largest likelihood under the density it was generated
126 /// compared to the other densities.
127 void SetApplyVeto(bool b = true) { fApplyVeto = b; }
128
129 void SetReuseNLL(bool r = true) { fReuseNLL = r; }
130
131 /// set the conditional observables which will be used when creating the NLL
132 /// so the pdf's will not be normalized on the conditional observables when computing the NLL
133 /// Since the class use a NLL we need to set the conditional observables if they exist in the model
135
137 RooAbsPdf& pdf,
138 const RooArgSet& allPOI,
139 RooRealVar& poi,
140 int n,
141 double poiValueForBackground = 0.0
142 );
144 RooAbsPdf& pdf,
145 const RooArgSet& allPOI,
146 RooRealVar& poi,
147 double nStdDevOverlap = 0.5,
148 double poiValueForBackground = 0.0
149 );
150
153
154 protected:
155
156 /// helper method for clearing the cache
157 void ClearCache() override;
158
159 unsigned int fIndexGenDensity = 0;
160 bool fGenerateFromNull = true;
161 bool fApplyVeto = true;
162
163 RooArgSet fConditionalObs; ///< set of conditional observables
164
165 /// support multiple null densities
166 std::vector<RooAbsPdf*> fNullDensities;
167 mutable std::vector<const RooArgSet*> fNullSnapshots;
168
169 // densities and snapshots to generate from
170 std::vector<RooAbsPdf*> fImportanceDensities;
171 std::vector<const RooArgSet*> fImportanceSnapshots;
172
173 bool fReuseNLL = true;
174
176
177 mutable std::vector<std::unique_ptr<RooAbsReal>> fNullNLLs; ///<!
178 mutable std::vector<std::unique_ptr<RooAbsReal>> fImpNLLs; ///<!
179
180 protected:
181 ClassDefOverride(ToyMCImportanceSampler,2) // An implementation of importance sampling
182};
183}
184
185#endif
#define b(i)
Definition RSha256.hxx:100
#define oocoutE(o, a)
#define oocoutI(o, a)
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
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 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.
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:191
Container class to hold unbinned data.
Definition RooDataSet.h:57
Variable that can be changed from the outside.
Definition RooRealVar.h:37
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
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
std::vector< std::unique_ptr< RooAbsReal > > fImpNLLs
!
ToyMCImportanceSampler()=default
Proof constructor. Do not use.
std::vector< std::unique_ptr< RooAbsReal > > fNullNLLs
!
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
int CreateNImpDensitiesForOnePOI(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, int n, double poiValueForBackground=0.0)
n is the number of importance densities
ToyMCSampler is an implementation of the TestStatSampler interface.
void SetParametersForTestStat(const RooArgSet &nullpoi) override
Set the Pdf, add to the workspace if not already there.
std::unique_ptr< const RooArgSet > fParametersForTestStat
RooAbsPdf * fPdf
densities, snapshots, and test statistics to reweight to
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
generates toy data without weight
void SetPdf(RooAbsPdf &pdf) override
Set the Pdf, add to the workspace if not already there.
const Int_t n
Definition legend1.C:16
Namespace for the RooStats classes.
Definition Asimov.h:19