Logo ROOT   6.12/07
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 #include "RooStats/ToyMCSampler.h"
16 
17 namespace RooStats {
18 
20 
22 
23  public:
25  ToyMCSampler()
26  {
27  // Proof constructor. Do not use.
28 
29  fIndexGenDensity = 0;
30  fGenerateFromNull = true;
31  fApplyVeto = true;
32  fReuseNLL = true;
34  }
36  ToyMCSampler(ts, ntoys)
37  {
38  fIndexGenDensity = 0;
39  fGenerateFromNull = true;
40  fApplyVeto = true;
41  fReuseNLL = true;
43  }
44 
45  virtual ~ToyMCImportanceSampler();
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 
69  fIndexGenDensity = i;
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;
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 
188  bool fReuseNLL;
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
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:86
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:155
void SetParametersForTestStat(const RooArgSet &nullpoi)
#define oocoutI(o, a)
Definition: RooMsgService.h:44
virtual void ClearCache()
clear the cache obtained from the pdf used for speeding the toy and global observables generation nee...
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, double &weight) const
int Int_t
Definition: RtypesCore.h:41
ToyMCImportanceSampler is an extension of the ToyMCSampler for Importance Sampling.
#define ClassDef(name, id)
Definition: Rtypes.h:320
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
#define oocoutE(o, a)
Definition: RooMsgService.h:47
std::vector< RooAbsReal * > fNullNLLs
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
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
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:71
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
int CreateNImpDensitiesForOnePOI(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, int n, double poiValueForBackground=0.0)
n is the number of importance densities
std::vector< RooAbsPdf * > fNullDensities
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
Namespace for the RooStats classes.
Definition: Asimov.h:20
std::vector< RooAbsReal * > fImpNLLs
static constexpr double s
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:160
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:110
Mother of all ROOT objects.
Definition: TObject.h:37
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:250
you should not use this method at all Int_t Int_t Double_t Double_t Double_t Int_t Double_t Double_t Double_t Double_t b
Definition: TRolke.cxx:630
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=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:31
const Int_t n
Definition: legend1.C:16