ROOT  6.06/09
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 
15 
16 #include "RooStats/ToyMCSampler.h"
17 
18 
19 
20 namespace RooStats {
21 
22 
24 
25 
26 /**
27 
28 ToyMCImportanceSampler is an extension of the ToyMCSampler for Importance Sampling.
29 
30 Implementation based on a work by Cranmer, Kreiss, Read (in Preparation)
31 
32 \ingroup Roostats
33 
34 */
35 
36 
38 
39  public:
41  ToyMCSampler()
42  {
43  // Proof constructor. Do not use.
44 
45  fIndexGenDensity = 0;
46  fGenerateFromNull = true;
47  fApplyVeto = true;
48  fReuseNLL = true;
50  }
52  ToyMCSampler(ts, ntoys)
53  {
54  fIndexGenDensity = 0;
55  fGenerateFromNull = true;
56  fApplyVeto = true;
57  fReuseNLL = true;
59  }
60 
61 
62  virtual ~ToyMCImportanceSampler();
63 
64 
65  // overwrite GetSamplingDistributionsSingleWorker(paramPoint) with a version that loops
66  // over nulls and importance densities, but calls the parent
67  // ToyMCSampler::GetSamplingDistributionsSingleWorker(paramPoint).
69 
70 
71 
72 
73 
74 
76  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const;
77  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, std::vector<double>& impNLLs, double& nullNLL) const;
78  virtual RooAbsData* GenerateToyData(std::vector<double>& weights) const;
79  virtual RooAbsData* GenerateToyData(std::vector<double>& weights, std::vector<double>& nullNLLs, std::vector<double>& impNLLs) const;
80 
81 
82  /// specifies the pdf to sample from
83  void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull = false) {
84  if( (fromNull && i >= fNullDensities.size()) ||
85  (!fromNull && i >= fImportanceDensities.size())
86  ) {
87  oocoutE((TObject*)0,InputArguments) << "Index out of range. Requested index: "<<i<<
88  " , but null densities: "<<fNullDensities.size()<<
89  " and importance densities: "<<fImportanceDensities.size() << std::endl;
90  }
91 
92  fIndexGenDensity = i;
93  fGenerateFromNull = fromNull;
94 
95  ClearCache();
96  }
97 
98  // For importance sampling with multiple desnities/snapshots:
99  // This is used to check the current Likelihood against Likelihoods from
100  // other importance densities apart from the one given as importance snapshot.
101  // The pdf can be NULL in which case the density from SetImportanceDensity()
102  // is used. The snapshot is also optional.
104  if( p == NULL && s == NULL ) {
105  oocoutI((TObject*)0,InputArguments) << "Neither density nor snapshot given. Doing nothing." << std::endl;
106  return;
107  }
108  if( p == NULL && fPdf == NULL ) {
109  oocoutE((TObject*)0,InputArguments) << "No density given, but snapshot is there. Aborting." << std::endl;
110  return;
111  }
112 
113  if( p == NULL ) p = fPdf;
114 
115  if( s ) s = (const RooArgSet*)s->snapshot();
116 
117  fImportanceDensities.push_back( p );
118  fImportanceSnapshots.push_back( s );
119  fImpNLLs.push_back( NULL );
120  }
121 
122  // The pdf can be NULL in which case the density from SetPdf()
123  // is used. The snapshot and TestStatistic is also optional.
124  void AddNullDensity(RooAbsPdf* p, const RooArgSet* s = NULL) {
125  if( p == NULL && s == NULL ) {
126  oocoutI((TObject*)0,InputArguments) << "Neither density nor snapshot nor test statistic given. Doing nothing." << std::endl;
127  return;
128  }
129 
130  if( p == NULL && fNullDensities.size() >= 1 ) p = fNullDensities[0];
131  if( s == NULL ) s = fParametersForTestStat;
132  if( s ) s = (const RooArgSet*)s->snapshot();
133 
134  fNullDensities.push_back( p );
135  fNullSnapshots.push_back( s );
136  fNullNLLs.push_back( NULL );
137  ClearCache();
138  }
139  // overwrite from ToyMCSampler
140  virtual void SetPdf(RooAbsPdf& pdf) {
142 
143  if( fNullDensities.size() == 1 ) { fNullDensities[0] = &pdf; }
144  else if( fNullDensities.size() == 0) AddNullDensity( &pdf );
145  else{
146  oocoutE((TObject*)0,InputArguments) << "Cannot use SetPdf() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
147  }
148  }
149  // overwrite from ToyMCSampler
150  void SetParametersForTestStat(const RooArgSet& nullpoi) {
152  if( fNullSnapshots.size() == 0 ) AddNullDensity( NULL, &nullpoi );
153  else if( fNullSnapshots.size() == 1 ) {
154  oocoutI((TObject*)0,InputArguments) << "Overwriting snapshot for the only defined null density." << std::endl;
155  if( fNullSnapshots[0] ) delete fNullSnapshots[0];
156  fNullSnapshots[0] = (const RooArgSet*)nullpoi.snapshot();
157  }else{
158  oocoutE((TObject*)0,InputArguments) << "Cannot use SetParametersForTestStat() when already multiple null densities are specified. Please use AddNullDensity()." << std::endl;
159  }
160  }
161 
162 
163  // When set to true, this sets the weight of all toys to zero that
164  // do not have the largest likelihood under the density it was generated
165  // compared to the other densities.
166  void SetApplyVeto(bool b = true) { fApplyVeto = b; }
167 
168  void SetReuseNLL(bool r = true) { fReuseNLL = r; }
169 
170 
171  // set the conditional observables which will be used when creating the NLL
172  // so the pdf's will not be normalized on the conditional observables when computing the NLL
173  // Since the class use a NLL we need to set the ocnditional onservables if they exist in the model
175 
176 
178  RooAbsPdf& pdf,
179  const RooArgSet& allPOI,
180  RooRealVar& poi,
181  int n,
182  double poiValueForBackground = 0.0
183  );
185  RooAbsPdf& pdf,
186  const RooArgSet& allPOI,
187  RooRealVar& poi,
188  double nStdDevOverlap = 0.5,
189  double poiValueForBackground = 0.0
190  );
191 
194 
195  protected:
196 
197  // helper method for clearing the cache
198  virtual void ClearCache();
199 
200  unsigned int fIndexGenDensity;
203 
204  RooArgSet fConditionalObs; // set of conditional observables
205 
206  // support multiple null densities
207  std::vector<RooAbsPdf*> fNullDensities;
208  mutable std::vector<const RooArgSet*> fNullSnapshots;
209 
210  // densities and snapshots to generate from
211  std::vector<RooAbsPdf*> fImportanceDensities;
212  std::vector<const RooArgSet*> fImportanceSnapshots;
213 
214  bool fReuseNLL;
215 
217 
218  mutable std::vector<RooAbsReal*> fNullNLLs; //!
219  mutable std::vector<RooAbsReal*> fImpNLLs; //!
220 
221 
222  protected:
223  ClassDef(ToyMCImportanceSampler,2) // An implementation of importance sampling
224 };
225 }
226 
227 
228 #endif
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:185
void SetParametersForTestStat(const RooArgSet &nullpoi)
#define oocoutI(o, a)
Definition: RooMsgService.h:45
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:140
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
int Int_t
Definition: RtypesCore.h:41
ToyMCImportanceSampler is an extension of the ToyMCSampler for Importance Sampling.
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
#define oocoutE(o, a)
Definition: RooMsgService.h:48
std::vector< RooAbsReal * > fNullNLLs
ROOT::R::TRInterface & r
Definition: Object.C:4
std::vector< const RooArgSet * > fNullSnapshots
ToyMCImportanceSampler(TestStatistic &ts, Int_t ntoys)
virtual void SetConditionalObservables(const RooArgSet &set)
virtual void SetPdf(RooAbsPdf &pdf)
std::vector< const RooArgSet * > fImportanceSnapshots
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:99
int CreateNImpDensitiesForOnePOI(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, int n, double poiValueForBackground=0.0)
std::vector< RooAbsPdf * > fNullDensities
Namespace for the RooStats classes.
Definition: Asimov.h:20
std::vector< RooAbsReal * > fImpNLLs
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:190
Mother of all ROOT objects.
Definition: TObject.h:58
void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull=false)
specifies the pdf to sample from
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
std::vector< RooAbsPdf * > fImportanceDensities
const RooArgSet * fParametersForTestStat
Definition: ToyMCSampler.h:280
#define NULL
Definition: Rtypes.h:82
int CreateImpDensitiesForOnePOIAdaptively(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, double nStdDevOverlap=0.5, double poiValueForBackground=0.0)
void AddNullDensity(RooAbsPdf *p, const RooArgSet *s=NULL)
void AddImportanceDensity(RooAbsPdf *p, const RooArgSet *s)
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
Definition: TestStatistic.h:33
const Int_t n
Definition: legend1.C:16
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448