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
17namespace RooStats {
18
20
22
23 public:
26 {
27 // Proof constructor. Do not use.
28
30 fGenerateFromNull = true;
31 fApplyVeto = true;
32 fReuseNLL = true;
34 }
36 ToyMCSampler(ts, ntoys)
37 {
39 fGenerateFromNull = true;
40 fApplyVeto = true;
41 fReuseNLL = true;
43 }
44
46
47 // overwrite GetSamplingDistributionsSingleWorker(paramPoint) with a version that loops
48 // over nulls and importance densities, but calls the parent
49 // ToyMCSampler::GetSamplingDistributionsSingleWorker(paramPoint).
51
53 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const;
54 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, std::vector<double>& impNLLs, double& nullNLL) const;
55 virtual RooAbsData* GenerateToyData(std::vector<double>& weights) const;
56 virtual RooAbsData* GenerateToyData(std::vector<double>& weights, std::vector<double>& nullNLLs, std::vector<double>& impNLLs) const;
57
58
59 /// specifies the pdf to sample from
60 void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull = false) {
61 if( (fromNull && i >= fNullDensities.size()) ||
62 (!fromNull && i >= fImportanceDensities.size())
63 ) {
64 oocoutE((TObject*)0,InputArguments) << "Index out of range. Requested index: "<<i<<
65 " , but null densities: "<<fNullDensities.size()<<
66 " and importance densities: "<<fImportanceDensities.size() << std::endl;
67 }
68
70 fGenerateFromNull = fromNull;
71
72 ClearCache();
73 }
74
75 // For importance sampling with multiple densities/snapshots:
76 // This is used to check the current Likelihood against Likelihoods from
77 // other importance densities apart from the one given as importance snapshot.
78 // The pdf can be NULL in which case the density from SetImportanceDensity()
79 // is used. The snapshot is also optional.
81 if( p == NULL && s == NULL ) {
82 oocoutI((TObject*)0,InputArguments) << "Neither density nor snapshot given. Doing nothing." << std::endl;
83 return;
84 }
85 if( p == NULL && fPdf == NULL ) {
86 oocoutE((TObject*)0,InputArguments) << "No density given, but snapshot is there. Aborting." << std::endl;
87 return;
88 }
89
90 if( p == NULL ) p = fPdf;
91
92 if( s ) s = (const RooArgSet*)s->snapshot();
93
94 fImportanceDensities.push_back( p );
95 fImportanceSnapshots.push_back( s );
96 fImpNLLs.push_back( NULL );
97 }
98
99 // The pdf can be NULL in which case the density from SetPdf()
100 // is used. The snapshot and TestStatistic is also optional.
101 void AddNullDensity(RooAbsPdf* p, const RooArgSet* s = NULL) {
102 if( p == NULL && s == NULL ) {
103 oocoutI((TObject*)0,InputArguments) << "Neither density nor snapshot nor test statistic given. Doing nothing." << std::endl;
104 return;
105 }
106
107 if( p == NULL && fNullDensities.size() >= 1 ) p = fNullDensities[0];
108 if( s == NULL ) s = fParametersForTestStat.get();
109 if( s ) s = (const RooArgSet*)s->snapshot();
110
111 fNullDensities.push_back( p );
112 fNullSnapshots.push_back( s );
113 fNullNLLs.push_back( NULL );
114 ClearCache();
115 }
116 // overwrite from ToyMCSampler
117 virtual void SetPdf(RooAbsPdf& pdf) {
119
120 if( fNullDensities.size() == 1 ) { fNullDensities[0] = &pdf; }
121 else if( fNullDensities.size() == 0) AddNullDensity( &pdf );
122 else{
123 oocoutE((TObject*)0,InputArguments) << "Cannot use SetPdf() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
124 }
125 }
126 // overwrite from ToyMCSampler
127 void SetParametersForTestStat(const RooArgSet& nullpoi) {
129 if( fNullSnapshots.size() == 0 ) AddNullDensity( NULL, &nullpoi );
130 else if( fNullSnapshots.size() == 1 ) {
131 oocoutI((TObject*)0,InputArguments) << "Overwriting snapshot for the only defined null density." << std::endl;
132 if( fNullSnapshots[0] ) delete fNullSnapshots[0];
133 fNullSnapshots[0] = (const RooArgSet*)nullpoi.snapshot();
134 }else{
135 oocoutE((TObject*)0,InputArguments) << "Cannot use SetParametersForTestStat() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
136 }
137 }
138
139 // When set to true, this sets the weight of all toys to zero that
140 // do not have the largest likelihood under the density it was generated
141 // compared to the other densities.
142 void SetApplyVeto(bool b = true) { fApplyVeto = b; }
143
144 void SetReuseNLL(bool r = true) { fReuseNLL = r; }
145
146 // set the conditional observables which will be used when creating the NLL
147 // so the pdf's will not be normalized on the conditional observables when computing the NLL
148 // Since the class use a NLL we need to set the conditional observables if they exist in the model
150
152 RooAbsPdf& pdf,
153 const RooArgSet& allPOI,
154 RooRealVar& poi,
155 int n,
156 double poiValueForBackground = 0.0
157 );
159 RooAbsPdf& pdf,
160 const RooArgSet& allPOI,
161 RooRealVar& poi,
162 double nStdDevOverlap = 0.5,
163 double poiValueForBackground = 0.0
164 );
165
168
169 protected:
170
171 // helper method for clearing the cache
172 virtual void ClearCache();
173
174 unsigned int fIndexGenDensity;
177
178 RooArgSet fConditionalObs; // set of conditional observables
179
180 // support multiple null densities
181 std::vector<RooAbsPdf*> fNullDensities;
182 mutable std::vector<const RooArgSet*> fNullSnapshots;
183
184 // densities and snapshots to generate from
185 std::vector<RooAbsPdf*> fImportanceDensities;
186 std::vector<const RooArgSet*> fImportanceSnapshots;
187
189
191
192 mutable std::vector<RooAbsReal*> fNullNLLs; //!
193 mutable std::vector<RooAbsReal*> fImpNLLs; //!
194
195 protected:
196 ClassDef(ToyMCImportanceSampler,2) // An implementation of importance sampling
197};
198}
199
200#endif
ROOT::R::TRInterface & r
Definition: Object.C:4
#define b(i)
Definition: RSha256.hxx:100
#define oocoutE(o, a)
Definition: RooMsgService.h:49
#define oocoutI(o, a)
Definition: RooMsgService.h:46
int Int_t
Definition: RtypesCore.h:41
#define ClassDef(name, id)
Definition: Rtypes.h:326
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:39
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:134
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:31
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:35
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)
std::vector< const RooArgSet * > fImportanceSnapshots
void SetParametersForTestStat(const RooArgSet &nullpoi)
std::vector< const RooArgSet * > fNullSnapshots
void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull=false)
specifies the pdf to sample from
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
virtual void SetPdf(RooAbsPdf &pdf)
virtual void SetConditionalObservables(const RooArgSet &set)
ToyMCImportanceSampler(TestStatistic &ts, Int_t ntoys)
std::vector< RooAbsPdf * > fImportanceDensities
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
virtual void ClearCache()
clear the cache obtained from the pdf used for speeding the toy and global observables generation nee...
void AddNullDensity(RooAbsPdf *p, const RooArgSet *s=NULL)
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.
std::vector< RooAbsPdf * > fNullDensities
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:72
std::unique_ptr< const RooArgSet > fParametersForTestStat
Definition: ToyMCSampler.h:250
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:160
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:111
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:156
Mother of all ROOT objects.
Definition: TObject.h:37
const Int_t n
Definition: legend1.C:16
@ InputArguments
Definition: RooGlobalFunc.h:68
Namespace for the RooStats classes.
Definition: Asimov.h:20
static constexpr double s