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 
15 #include "RooStats/ToyMCSampler.h"
16 #include <vector>
17 
18 namespace RooStats {
19 
21 
23 
24  public:
26  ToyMCSampler()
27  {
28  // Proof constructor. Do not use.
29 
30  fIndexGenDensity = 0;
31  fGenerateFromNull = true;
32  fApplyVeto = true;
33  fReuseNLL = true;
35  }
37  ToyMCSampler(ts, ntoys)
38  {
39  fIndexGenDensity = 0;
40  fGenerateFromNull = true;
41  fApplyVeto = true;
42  fReuseNLL = true;
44  }
45 
46  virtual ~ToyMCImportanceSampler();
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 
70  fIndexGenDensity = i;
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;
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 
189  bool fReuseNLL;
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
n
const Int_t n
Definition: legend1.C:16
RooStats::ToyMCImportanceSampler::SetConditionalObservables
virtual void SetConditionalObservables(const RooArgSet &set)
Definition: ToyMCImportanceSampler.h:150
RooStats::ToyMCImportanceSampler::fToysStrategy
toysStrategies fToysStrategy
Definition: ToyMCImportanceSampler.h:191
RooStats::ToyMCImportanceSampler::fImpNLLs
std::vector< RooAbsReal * > fImpNLLs
Definition: ToyMCImportanceSampler.h:194
RooAbsData
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:47
RooStats::ToyMCImportanceSampler
ToyMCImportanceSampler is an extension of the ToyMCSampler for Importance Sampling.
Definition: ToyMCImportanceSampler.h:22
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooStats::ToyMCImportanceSampler::ToyMCImportanceSampler
ToyMCImportanceSampler(TestStatistic &ts, Int_t ntoys)
Definition: ToyMCImportanceSampler.h:36
oocoutI
#define oocoutI(o, a)
Definition: RooMsgService.h:45
RooStats::ToyMCImportanceSampler::CreateNImpDensitiesForOnePOI
int CreateNImpDensitiesForOnePOI(RooAbsPdf &pdf, const RooArgSet &allPOI, RooRealVar &poi, int n, double poiValueForBackground=0.0)
n is the number of importance densities
Definition: ToyMCImportanceSampler.cxx:457
RooStats::ToyMCImportanceSampler::GetSamplingDistributionsSingleWorker
virtual RooDataSet * GetSamplingDistributionsSingleWorker(RooArgSet &paramPoint)
This is the main function for serial runs.
Definition: ToyMCImportanceSampler.cxx:53
RooStats::ToyMCImportanceSampler::SetPdf
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCImportanceSampler.h:118
RooStats::toysStrategies
toysStrategies
Definition: ToyMCImportanceSampler.h:20
RooStats::ToyMCImportanceSampler::GenerateToyData
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:112
r
ROOT::R::TRInterface & r
Definition: Object.C:4
RooStats::ToyMCImportanceSampler::fApplyVeto
bool fApplyVeto
Definition: ToyMCImportanceSampler.h:177
RooStats::ToyMCImportanceSampler::fNullSnapshots
std::vector< const RooArgSet * > fNullSnapshots
Definition: ToyMCImportanceSampler.h:183
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
RooStats::ToyMCImportanceSampler::fNullDensities
std::vector< RooAbsPdf * > fNullDensities
Definition: ToyMCImportanceSampler.h:182
ToyMCSampler.h
RooStats::TestStatistic
TestStatistic is an interface class to provide a facility for construction test statistics distributi...
Definition: TestStatistic.h:31
RooStats::ToyMCImportanceSampler::fImportanceSnapshots
std::vector< const RooArgSet * > fImportanceSnapshots
Definition: ToyMCImportanceSampler.h:187
RooStats::ToyMCSampler::SetParametersForTestStat
virtual void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCSampler.h:157
RooStats::ToyMCImportanceSampler::AddNullDensity
void AddNullDensity(RooAbsPdf *p, const RooArgSet *s=NULL)
Definition: ToyMCImportanceSampler.h:102
RooStats::ToyMCImportanceSampler::AddImportanceDensity
void AddImportanceDensity(RooAbsPdf *p, const RooArgSet *s)
Definition: ToyMCImportanceSampler.h:81
RooStats::EQUALTOYSPERDENSITY
@ EQUALTOYSPERDENSITY
Definition: ToyMCImportanceSampler.h:20
oocoutE
#define oocoutE(o, a)
Definition: RooMsgService.h:48
RooStats::ToyMCImportanceSampler::fIndexGenDensity
unsigned int fIndexGenDensity
Definition: ToyMCImportanceSampler.h:175
b
#define b(i)
Definition: RSha256.hxx:100
RooStats::ToyMCImportanceSampler::SetDensityToGenerateFromByIndex
void SetDensityToGenerateFromByIndex(unsigned int i, bool fromNull=false)
specifies the pdf to sample from
Definition: ToyMCImportanceSampler.h:61
RooStats::ToyMCImportanceSampler::SetEqualNumToysPerDensity
void SetEqualNumToysPerDensity(void)
Definition: ToyMCImportanceSampler.h:167
RooStats::ToyMCSampler::fPdf
RooAbsPdf * fPdf
Definition: ToyMCSampler.h:251
RooArgSet::snapshot
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:126
RooStats::ToyMCImportanceSampler::CreateImpDensitiesForOnePOIAdaptively
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.
Definition: ToyMCImportanceSampler.cxx:434
RooStats::ToyMCImportanceSampler::fImportanceDensities
std::vector< RooAbsPdf * > fImportanceDensities
Definition: ToyMCImportanceSampler.h:186
RooStats::ToyMCImportanceSampler::SetParametersForTestStat
void SetParametersForTestStat(const RooArgSet &nullpoi)
Definition: ToyMCImportanceSampler.h:128
RooArgSet::add
Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE) override
Add element to non-owning set.
Definition: RooArgSet.cxx:261
RooStats::ToyMCImportanceSampler::SetApplyVeto
void SetApplyVeto(bool b=true)
Definition: ToyMCImportanceSampler.h:143
RooStats::EXPONENTIALTOYDISTRIBUTION
@ EXPONENTIALTOYDISTRIBUTION
Definition: ToyMCImportanceSampler.h:20
RooStats::ToyMCImportanceSampler::fReuseNLL
bool fReuseNLL
Definition: ToyMCImportanceSampler.h:189
RooStats::ToyMCImportanceSampler::fConditionalObs
RooArgSet fConditionalObs
Definition: ToyMCImportanceSampler.h:179
RooStats::ToyMCSampler::GenerateToyData
virtual RooAbsData * GenerateToyData(RooArgSet &paramPoint, RooAbsPdf &pdf) const
Definition: ToyMCSampler.h:112
RooStats::ToyMCSampler::fParametersForTestStat
const RooArgSet * fParametersForTestStat
Definition: ToyMCSampler.h:252
RooStats::ToyMCImportanceSampler::ToyMCImportanceSampler
ToyMCImportanceSampler()
Definition: ToyMCImportanceSampler.h:25
RooStats::ToyMCImportanceSampler::ClearCache
virtual void ClearCache()
clear the cache obtained from the pdf used for speeding the toy and global observables generation nee...
Definition: ToyMCImportanceSampler.cxx:44
RooStats
Namespace for the RooStats classes.
Definition: Asimov.h:19
RooStats::ToyMCImportanceSampler::SetExpIncreasingNumToysPerDensity
void SetExpIncreasingNumToysPerDensity(void)
Definition: ToyMCImportanceSampler.h:168
RooStats::ToyMCSampler
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:73
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
RooStats::ToyMCImportanceSampler::fGenerateFromNull
bool fGenerateFromNull
Definition: ToyMCImportanceSampler.h:176
RooStats::ToyMCImportanceSampler::fNullNLLs
std::vector< RooAbsReal * > fNullNLLs
Definition: ToyMCImportanceSampler.h:193
RooStats::ToyMCImportanceSampler::SetReuseNLL
void SetReuseNLL(bool r=true)
Definition: ToyMCImportanceSampler.h:145
RooDataSet
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:33
RooAbsPdf
Definition: RooAbsPdf.h:43
RooRealVar
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:37
RooAbsCollection::removeAll
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
Definition: RooAbsCollection.cxx:643
RooStats::ToyMCImportanceSampler::~ToyMCImportanceSampler
virtual ~ToyMCImportanceSampler()
Definition: ToyMCImportanceSampler.cxx:37
RooStats::ToyMCSampler::SetPdf
virtual void SetPdf(RooAbsPdf &pdf)
Definition: ToyMCSampler.h:162
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
int