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:
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
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 virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const;
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((TObject*)0,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 NULL in which case the density from SetImportanceDensity()
80 // is used. The snapshot is also optional.
82 if( p == NULL && s == NULL ) {
83 oocoutI((TObject*)0,InputArguments) << "Neither density nor snapshot given. Doing nothing." << std::endl;
84 return;
85 }
86 if( p == NULL && fPdf == NULL ) {
87 oocoutE((TObject*)0,InputArguments) << "No density given, but snapshot is there. Aborting." << std::endl;
88 return;
89 }
90
91 if( p == NULL ) 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( NULL );
98 }
99
100 // The pdf can be NULL 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 = NULL) {
103 if( p == NULL && s == NULL ) {
104 oocoutI((TObject*)0,InputArguments) << "Neither density nor snapshot nor test statistic given. Doing nothing." << std::endl;
105 return;
106 }
107
108 if( p == NULL && fNullDensities.size() >= 1 ) p = fNullDensities[0];
109 if( s == NULL ) 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( NULL );
115 ClearCache();
116 }
117 // overwrite from ToyMCSampler
118 virtual void SetPdf(RooAbsPdf& pdf) {
120
121 if( fNullDensities.size() == 1 ) { fNullDensities[0] = &pdf; }
122 else if( fNullDensities.size() == 0) AddNullDensity( &pdf );
123 else{
124 oocoutE((TObject*)0,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) {
130 if( fNullSnapshots.size() == 0 ) AddNullDensity( NULL, &nullpoi );
131 else if( fNullSnapshots.size() == 1 ) {
132 oocoutI((TObject*)0,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((TObject*)0,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 virtual void ClearCache();
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 ClassDef(ToyMCImportanceSampler,2) // An implementation of importance sampling
198};
199}
200
201#endif
ROOT::R::TRInterface & r
Definition Object.C:4
#define b(i)
Definition RSha256.hxx:100
#define oocoutE(o, a)
#define oocoutI(o, a)
#define ClassDef(name, id)
Definition Rtypes.h:325
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:82
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:158
RooDataSet is a container class to hold unbinned data.
Definition RooDataSet.h:36
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:39
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)
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.
std::unique_ptr< const RooArgSet > fParametersForTestStat
virtual void SetPdf(RooAbsPdf &pdf)
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Mother of all ROOT objects.
Definition TObject.h:41
const Int_t n
Definition legend1.C:16
Namespace for the RooStats classes.
Definition Asimov.h:19