Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
19 enum toysStrategies { EQUALTOYSPERDENSITY, EXPONENTIALTOYDISTRIBUTION };
20 
21 class ToyMCImportanceSampler: public ToyMCSampler {
22 
23  public:
24  ToyMCImportanceSampler() :
25  ToyMCSampler()
26  {
27  // Proof constructor. Do not use.
28 
29  fIndexGenDensity = 0;
30  fGenerateFromNull = true;
31  fApplyVeto = true;
32  fReuseNLL = true;
33  fToysStrategy = EQUALTOYSPERDENSITY;
34  }
35  ToyMCImportanceSampler(TestStatistic &ts, Int_t ntoys) :
36  ToyMCSampler(ts, ntoys)
37  {
38  fIndexGenDensity = 0;
39  fGenerateFromNull = true;
40  fApplyVeto = true;
41  fReuseNLL = true;
42  fToysStrategy = EQUALTOYSPERDENSITY;
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).
50  virtual RooDataSet* GetSamplingDistributionsSingleWorker(RooArgSet& paramPoint);
51 
52  using ToyMCSampler::GenerateToyData;
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.
80  void AddImportanceDensity(RooAbsPdf* p, const RooArgSet* s) {
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) {
118  ToyMCSampler::SetPdf(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) {
128  ToyMCSampler::SetParametersForTestStat(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
149  virtual void SetConditionalObservables(const RooArgSet& set) {fConditionalObs.removeAll(); fConditionalObs.add(set);}
150 
151  int CreateNImpDensitiesForOnePOI(
152  RooAbsPdf& pdf,
153  const RooArgSet& allPOI,
154  RooRealVar& poi,
155  int n,
156  double poiValueForBackground = 0.0
157  );
158  int CreateImpDensitiesForOnePOIAdaptively(
159  RooAbsPdf& pdf,
160  const RooArgSet& allPOI,
161  RooRealVar& poi,
162  double nStdDevOverlap = 0.5,
163  double poiValueForBackground = 0.0
164  );
165 
166  void SetEqualNumToysPerDensity( void ) { fToysStrategy = EQUALTOYSPERDENSITY; }
167  void SetExpIncreasingNumToysPerDensity( void ) { fToysStrategy = EXPONENTIALTOYDISTRIBUTION; }
168 
169  protected:
170 
171  // helper method for clearing the cache
172  virtual void ClearCache();
173 
174  unsigned int fIndexGenDensity;
175  bool fGenerateFromNull;
176  bool fApplyVeto;
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 
190  toysStrategies fToysStrategy;
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