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