Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
ToyMCSampler.h
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Sven Kreiss and Kyle Cranmer June 2010
3 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
4 // Additions and modifications by Mario Pelliccioni
5 /*************************************************************************
6  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
7  * All rights reserved. *
8  * *
9  * For the licensing terms see $ROOTSYS/LICENSE. *
10  * For the list of contributors see $ROOTSYS/README/CREDITS. *
11  *************************************************************************/
12 
13 #ifndef ROOSTATS_ToyMCSampler
14 #define ROOSTATS_ToyMCSampler
15 
16 
17 #include "Rtypes.h"
18 
19 #include <vector>
20 #include <sstream>
21 
24 #include "RooStats/TestStatistic.h"
25 #include "RooStats/ModelConfig.h"
26 #include "RooStats/ProofConfig.h"
27 
28 #include "RooWorkspace.h"
29 #include "RooMsgService.h"
30 #include "RooAbsPdf.h"
31 #include "RooRealVar.h"
32 
33 #include "RooDataSet.h"
34 
35 namespace RooStats {
36 
37  class DetailedOutputAggregator;
38 
39 class NuisanceParametersSampler {
40 
41  public:
42  NuisanceParametersSampler(RooAbsPdf *prior=NULL, const RooArgSet *parameters=NULL, Int_t nToys=1000, Bool_t asimov=kFALSE) :
43  fPrior(prior),
44  fParams(parameters),
45  fNToys(nToys),
46  fExpected(asimov),
47  fPoints(NULL),
48  fIndex(0)
49  {
50  if(prior) Refresh();
51  }
52  virtual ~NuisanceParametersSampler() {
53  if(fPoints) { delete fPoints; fPoints = NULL; }
54  }
55 
56  void NextPoint(RooArgSet& nuisPoint, Double_t& weight);
57 
58  protected:
59  void Refresh();
60 
61  private:
62  RooAbsPdf *fPrior; // prior for nuisance parameters
63  const RooArgSet *fParams; // nuisance parameters
64  Int_t fNToys;
65  Bool_t fExpected;
66 
67  RooAbsData *fPoints; // generated nuisance parameter points
68  Int_t fIndex; // current index in fPoints array
69 };
70 
71 class ToyMCSampler: public TestStatSampler {
72 
73  public:
74 
75  ToyMCSampler();
76  ToyMCSampler(TestStatistic &ts, Int_t ntoys);
77  virtual ~ToyMCSampler();
78 
79  static void SetAlwaysUseMultiGen(Bool_t flag);
80 
81  void SetUseMultiGen(Bool_t flag) { fUseMultiGen = flag ; }
82 
83  // main interface
84  virtual SamplingDistribution* GetSamplingDistribution(RooArgSet& paramPoint);
85  virtual RooDataSet* GetSamplingDistributions(RooArgSet& paramPoint);
86  virtual RooDataSet* GetSamplingDistributionsSingleWorker(RooArgSet& paramPoint);
87 
88  virtual SamplingDistribution* AppendSamplingDistribution(
89  RooArgSet& allParameters,
90  SamplingDistribution* last,
91  Int_t additionalMC
92  );
93 
94 
95  // The pdf can be NULL in which case the density from SetPdf()
96  // is used. The snapshot and TestStatistic is also optional.
97  virtual void AddTestStatistic(TestStatistic* t = NULL) {
98  if( t == NULL ) {
99  oocoutI((TObject*)0,InputArguments) << "No test statistic given. Doing nothing." << std::endl;
100  return;
101  }
102 
103  //if( t == NULL && fTestStatistics.size() >= 1 ) t = fTestStatistics[0];
104 
105  fTestStatistics.push_back( t );
106  }
107 
108  // generates toy data
109  // without weight
110  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, RooAbsPdf& pdf) const {
111  if(fExpectedNuisancePar) oocoutE((TObject*)NULL,InputArguments) << "ToyMCSampler: using expected nuisance parameters but ignoring weight. Use GetSamplingDistribution(paramPoint, weight) instead." << std::endl;
112  double weight;
113  return GenerateToyData(paramPoint, weight, pdf);
114  }
115  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint) const { return GenerateToyData(paramPoint,*fPdf); }
116  // with weight
117  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight, RooAbsPdf& pdf) const;
118  virtual RooAbsData* GenerateToyData(RooArgSet& paramPoint, double& weight) const { return GenerateToyData(paramPoint,weight,*fPdf); }
119 
120  // generate global observables
121  virtual void GenerateGlobalObservables(RooAbsPdf& pdf) const;
122 
123 
124  // Main interface to evaluate the test statistic on a dataset
125  virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI, int i ) {
126  return fTestStatistics[i]->Evaluate(data, nullPOI);
127  }
128  virtual Double_t EvaluateTestStatistic(RooAbsData& data, RooArgSet& nullPOI) { return EvaluateTestStatistic( data,nullPOI, 0 ); }
129  virtual RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi);
130 
131 
132  virtual TestStatistic* GetTestStatistic(unsigned int i) const {
133  if( fTestStatistics.size() <= i ) return NULL;
134  return fTestStatistics[i];
135  }
136  virtual TestStatistic* GetTestStatistic(void) const { return GetTestStatistic(0); }
137 
138  virtual Double_t ConfidenceLevel() const { return 1. - fSize; }
139  virtual void Initialize(
140  RooAbsArg& /*testStatistic*/,
141  RooArgSet& /*paramsOfInterest*/,
142  RooArgSet& /*nuisanceParameters*/
143  ) {}
144 
145  virtual Int_t GetNToys(void) { return fNToys; }
146  virtual void SetNToys(const Int_t ntoy) { fNToys = ntoy; }
147  /// Forces the generation of exactly `n` events even for extended PDFs. Set to 0 to
148  /// use the Poisson-distributed events from the extended PDF.
149  virtual void SetNEventsPerToy(const Int_t nevents) {
150  fNEvents = nevents;
151  }
152 
153 
154  // Set the Pdf, add to the the workspace if not already there
155  virtual void SetParametersForTestStat(const RooArgSet& nullpoi) {
156  if( fParametersForTestStat ) delete fParametersForTestStat;
157  fParametersForTestStat = (const RooArgSet*)nullpoi.snapshot();
158  }
159 
160  virtual void SetPdf(RooAbsPdf& pdf) { fPdf = &pdf; ClearCache(); }
161 
162  // How to randomize the prior. Set to NULL to deactivate randomization.
163  virtual void SetPriorNuisance(RooAbsPdf* pdf) {
164  fPriorNuisance = pdf;
165  if (fNuisanceParametersSampler) {
166  delete fNuisanceParametersSampler;
167  fNuisanceParametersSampler = NULL;
168  }
169  }
170  // specify the nuisance parameters (eg. the rest of the parameters)
171  virtual void SetNuisanceParameters(const RooArgSet& np) { fNuisancePars = &np; }
172  // specify the observables in the dataset (needed to evaluate the test statistic)
173  virtual void SetObservables(const RooArgSet& o) { fObservables = &o; }
174  // specify the conditional observables
175  virtual void SetGlobalObservables(const RooArgSet& o) { fGlobalObservables = &o; }
176 
177 
178  // set the size of the test (rate of Type I error) ( Eg. 0.05 for a 95% Confidence Interval)
179  virtual void SetTestSize(Double_t size) { fSize = size; }
180  // set the confidence level for the interval (eg. 0.95 for a 95% Confidence Interval)
181  virtual void SetConfidenceLevel(Double_t cl) { fSize = 1. - cl; }
182 
183  // Set the TestStatistic (want the argument to be a function of the data & parameter points
184  virtual void SetTestStatistic(TestStatistic *testStatistic, unsigned int i) {
185  if( fTestStatistics.size() < i ) {
186  oocoutE((TObject*)NULL,InputArguments) << "Cannot set test statistic for this index." << std::endl;
187  return;
188  }
189  if( fTestStatistics.size() == i)
190  fTestStatistics.push_back(testStatistic);
191  else
192  fTestStatistics[i] = testStatistic;
193  }
194  virtual void SetTestStatistic(TestStatistic *t) { return SetTestStatistic(t,0); }
195 
196  virtual void SetExpectedNuisancePar(Bool_t i = kTRUE) { fExpectedNuisancePar = i; }
197  virtual void SetAsimovNuisancePar(Bool_t i = kTRUE) { fExpectedNuisancePar = i; }
198 
199  // Checks for sufficient information to do a GetSamplingDistribution(...).
200  Bool_t CheckConfig(void);
201 
202  // control to use bin data generation (=> see RooFit::AllBinned() option)
203  void SetGenerateBinned(bool binned = true) { fGenerateBinned = binned; }
204  // name of the tag for individual components to be generated binned (=> see RooFit::GenBinned() option)
205  void SetGenerateBinnedTag( const char* binnedTag = "" ) { fGenerateBinnedTag = binnedTag; }
206  // set auto binned generation (=> see RooFit::AutoBinned() option)
207  void SetGenerateAutoBinned( Bool_t autoBinned = kTRUE ) { fGenerateAutoBinned = autoBinned; }
208 
209  // Set the name of the sampling distribution used for plotting
210  void SetSamplingDistName(const char* name) { if(name) fSamplingDistName = name; }
211  std::string GetSamplingDistName(void) { return fSamplingDistName; }
212 
213  // This option forces a maximum number of total toys.
214  void SetMaxToys(Double_t t) { fMaxToys = t; }
215 
216  void SetToysLeftTail(Double_t toys, Double_t threshold) {
217  fToysInTails = toys;
218  fAdaptiveLowLimit = threshold;
219  fAdaptiveHighLimit = RooNumber::infinity();
220  }
221  void SetToysRightTail(Double_t toys, Double_t threshold) {
222  fToysInTails = toys;
223  fAdaptiveHighLimit = threshold;
224  fAdaptiveLowLimit = -RooNumber::infinity();
225  }
226  void SetToysBothTails(Double_t toys, Double_t low_threshold, Double_t high_threshold) {
227  fToysInTails = toys;
228  fAdaptiveHighLimit = high_threshold;
229  fAdaptiveLowLimit = low_threshold;
230  }
231 
232  // calling with argument or NULL deactivates proof
233  void SetProofConfig(ProofConfig *pc = NULL) { fProofConfig = pc; }
234 
235  void SetProtoData(const RooDataSet* d) { fProtoData = d; }
236 
237  protected:
238 
239  const RooArgList* EvaluateAllTestStatistics(RooAbsData& data, const RooArgSet& poi, DetailedOutputAggregator& detOutAgg);
240 
241  // helper for GenerateToyData
242  RooAbsData* Generate(RooAbsPdf &pdf, RooArgSet &observables, const RooDataSet *protoData=NULL, int forceEvents=0) const;
243 
244  // helper method for clearing the cache
245  virtual void ClearCache();
246 
247 
248  // densities, snapshots, and test statistics to reweight to
249  RooAbsPdf *fPdf; // model (can be alt or null)
250  const RooArgSet* fParametersForTestStat;
251  std::vector<TestStatistic*> fTestStatistics;
252 
253  std::string fSamplingDistName; // name of the model
254  RooAbsPdf *fPriorNuisance; // prior pdf for nuisance parameters
255  const RooArgSet *fNuisancePars;
256  const RooArgSet *fObservables;
257  const RooArgSet *fGlobalObservables;
258  Int_t fNToys; // number of toys to generate
259  Int_t fNEvents; // number of events per toy (may be ignored depending on settings)
260  Double_t fSize;
261  Bool_t fExpectedNuisancePar; // whether to use expectation values for nuisance parameters (ie Asimov data set)
262  Bool_t fGenerateBinned;
263  TString fGenerateBinnedTag;
264  Bool_t fGenerateAutoBinned;
265 
266  // minimum no of toys in tails for adaptive sampling
267  // (taking weights into account, therefore double)
268  // Default: 0.0 which means no adaptive sampling
269  Double_t fToysInTails;
270  // maximum no of toys
271  // (taking weights into account, therefore double)
272  Double_t fMaxToys;
273  // tails
274  Double_t fAdaptiveLowLimit;
275  Double_t fAdaptiveHighLimit;
276 
277  const RooDataSet *fProtoData; // in dev
278 
279  ProofConfig *fProofConfig; //!
280 
281  mutable NuisanceParametersSampler *fNuisanceParametersSampler; //!
282 
283  // objects below cache information and are mutable and non-persistent
284  mutable RooArgSet* _allVars ; //!
285  mutable std::list<RooAbsPdf*> _pdfList ; //!
286  mutable std::list<RooArgSet*> _obsList ; //!
287  mutable std::list<RooAbsPdf::GenSpec*> _gsList ; //!
288  mutable RooAbsPdf::GenSpec* _gs1 ; //! GenSpec #1
289  mutable RooAbsPdf::GenSpec* _gs2 ; //! GenSpec #2
290  mutable RooAbsPdf::GenSpec* _gs3 ; //! GenSpec #3
291  mutable RooAbsPdf::GenSpec* _gs4 ; //! GenSpec #4
292 
293  static Bool_t fgAlwaysUseMultiGen ; // Use PrepareMultiGen always
294  Bool_t fUseMultiGen ; // Use PrepareMultiGen?
295 
296  protected:
297  ClassDef(ToyMCSampler,3) // A simple implementation of the TestStatSampler interface
298 };
299 }
300 
301 
302 #endif