ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HypoTestCalculatorGeneric.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Kyle Cranmer, Sven Kreiss 23/05/10
3 /*************************************************************************
4  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 /**
12 Same purpose as HybridCalculatorOriginal, but different implementation.
13 
14 This is the "generic" version that works with any TestStatSampler. The
15 HybridCalculator derives from this class but explicitly uses the
16 ToyMCSampler as its TestStatSampler.
17 */
18 
20 #include "RooStats/ToyMCSampler.h"
24 
25 #include "RooAddPdf.h"
26 
27 #include "RooRandom.h"
28 
29 
31 
32 using namespace RooStats;
33 using namespace std;
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Constructor. When test stat sampler is not provided
37 /// uses ToyMCSampler and RatioOfProfiledLikelihoodsTestStat
38 /// and nToys = 1000.
39 /// User can : GetTestStatSampler()->SetNToys( # )
40 
42  const RooAbsData &data,
43  const ModelConfig &altModel,
44  const ModelConfig &nullModel,
45  TestStatSampler *sampler
46  ) :
47  fAltModel(&altModel),
48  fNullModel(&nullModel),
49  fData(&data),
50  fTestStatSampler(sampler),
51  fDefaultSampler(0),
52  fDefaultTestStat(0),
53  fAltToysSeed(0)
54 {
55  if(!sampler){
56  fDefaultTestStat
57  = new RatioOfProfiledLikelihoodsTestStat(*nullModel.GetPdf(),
58  *altModel.GetPdf(),
59  altModel.GetSnapshot());
60 
61  fDefaultSampler = new ToyMCSampler(*fDefaultTestStat, 1000);
62  fTestStatSampler = fDefaultSampler;
63  }
64 
65 
66 }
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// common setup for both models
70 
71 void HypoTestCalculatorGeneric::SetupSampler(const ModelConfig& model) const {
72  fNullModel->LoadSnapshot();
73  fTestStatSampler->SetObservables(*fNullModel->GetObservables());
74  fTestStatSampler->SetParametersForTestStat(*fNullModel->GetParametersOfInterest());
75 
76  // for this model
77  model.LoadSnapshot();
78  fTestStatSampler->SetSamplingDistName(model.GetName());
79  fTestStatSampler->SetPdf(*model.GetPdf());
80  fTestStatSampler->SetNuisanceParameters(*model.GetNuisanceParameters());
81  // global observables or nuisanance pdf will be set by the derived classes
82  // (e.g. Frequentist or HybridCalculator)
83 }
84 
85 ////////////////////////////////////////////////////////////////////////////////
86 
87 HypoTestCalculatorGeneric::~HypoTestCalculatorGeneric() {
88  if(fDefaultSampler) delete fDefaultSampler;
89  if(fDefaultTestStat) delete fDefaultTestStat;
90 }
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 
94 HypoTestResult* HypoTestCalculatorGeneric::GetHypoTest() const {
95  // several possibilities:
96  // no prior nuisance given and no nuisance parameters: ok
97  // no prior nuisance given but nuisance parameters: error
98  // prior nuisance given for some nuisance parameters:
99  // - nuisance parameters are constant, so they don't float in test statistic
100  // - nuisance parameters are floating, so they do float in test statistic
101 
102  // initial setup
103  PreHook();
104  const_cast<ModelConfig*>(fNullModel)->GuessObsAndNuisance(*fData);
105  const_cast<ModelConfig*>(fAltModel)->GuessObsAndNuisance(*fData);
106 
107  const RooArgSet * nullSnapshot = fNullModel->GetSnapshot();
108  if(nullSnapshot == NULL) {
109  oocoutE((TObject*)0,Generation) << "Null model needs a snapshot. Set using modelconfig->SetSnapshot(poi)." << endl;
110  return 0;
111  }
112 
113  // CheckHook
114  if(CheckHook() != 0) {
115  oocoutE((TObject*)0,Generation) << "There was an error in CheckHook(). Stop." << endl;
116  return 0;
117  }
118 
119  if (!fTestStatSampler || !fTestStatSampler->GetTestStatistic() ) {
120  oocoutE((TObject*)0,InputArguments) << "Test Statistic Sampler or Test Statistics not defined. Stop." << endl;
121  return 0;
122  }
123 
124  // get a big list of all variables for convenient switching
125  RooArgSet *nullParams = fNullModel->GetPdf()->getParameters(*fData);
126  RooArgSet *altParams = fAltModel->GetPdf()->getParameters(*fData);
127  // save all parameters so we can set them back to what they were
128  RooArgSet *bothParams = fNullModel->GetPdf()->getParameters(*fData);
129  bothParams->add(*altParams,false);
130  RooArgSet *saveAll = (RooArgSet*) bothParams->snapshot();
131 
132  // check whether we have a ToyMCSampler and if so, keep a pointer to it
133  ToyMCSampler* toymcs = dynamic_cast<ToyMCSampler*>( fTestStatSampler );
134 
135 
136  // evaluate test statistic on data
137  RooArgSet nullP(*nullSnapshot);
138  double obsTestStat;
139 
140  RooArgList* allTS = NULL;
141  if( toymcs ) {
142  allTS = toymcs->EvaluateAllTestStatistics(*const_cast<RooAbsData*>(fData), nullP);
143  if (!allTS) return 0;
144  //oocoutP((TObject*)0,Generation) << "All Test Statistics on data: " << endl;
145  //allTS->Print("v");
146  RooRealVar* firstTS = (RooRealVar*)allTS->at(0);
147  obsTestStat = firstTS->getVal();
148  if (allTS->getSize()<=1) {
149  delete allTS;
150  allTS= 0; // don't save
151  }
152  }else{
153  obsTestStat = fTestStatSampler->EvaluateTestStatistic(*const_cast<RooAbsData*>(fData), nullP);
154  }
155  oocoutP((TObject*)0,Generation) << "Test Statistic on data: " << obsTestStat << endl;
156 
157  // set parameters back ... in case the evaluation of the test statistic
158  // modified something (e.g. a nuisance parameter that is not randomized
159  // must be set here)
160  *bothParams = *saveAll;
161 
162 
163 
164  // Generate sampling distribution for null
165  SetupSampler(*fNullModel);
166  RooArgSet paramPointNull(*fNullModel->GetParametersOfInterest());
167  if(PreNullHook(&paramPointNull, obsTestStat) != 0) {
168  oocoutE((TObject*)0,Generation) << "PreNullHook did not return 0." << endl;
169  }
170  SamplingDistribution* samp_null = NULL;
171  RooDataSet* detOut_null = NULL;
172  if(toymcs) {
173  detOut_null = toymcs->GetSamplingDistributions(paramPointNull);
174  if( detOut_null ) {
175  samp_null = new SamplingDistribution( detOut_null->GetName(), detOut_null->GetTitle(), *detOut_null );
176  if (detOut_null->get()->getSize()<=1) {
177  delete detOut_null;
178  detOut_null= 0;
179  }
180  }
181  }else samp_null = fTestStatSampler->GetSamplingDistribution(paramPointNull);
182 
183  // set parameters back
184  *bothParams = *saveAll;
185 
186  // Generate sampling distribution for alternate
187  SetupSampler(*fAltModel);
188  RooArgSet paramPointAlt(*fAltModel->GetParametersOfInterest());
189  if(PreAltHook(&paramPointAlt, obsTestStat) != 0) {
190  oocoutE((TObject*)0,Generation) << "PreAltHook did not return 0." << endl;
191  }
192  SamplingDistribution* samp_alt = NULL;
193  RooDataSet* detOut_alt = NULL;
194  if(toymcs) {
195 
196  // case of re-using same toys for every points
197  // set a given seed
198  unsigned int prevSeed = 0;
199  if (fAltToysSeed > 0) {
200  prevSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1; // want to avoid zero value
201  RooRandom::randomGenerator()->SetSeed(fAltToysSeed);
202  }
203 
204  detOut_alt = toymcs->GetSamplingDistributions(paramPointAlt);
205  if( detOut_alt ) {
206  samp_alt = new SamplingDistribution( detOut_alt->GetName(), detOut_alt->GetTitle(), *detOut_alt );
207  if (detOut_alt->get()->getSize()<=1) {
208  delete detOut_alt;
209  detOut_alt= 0;
210  }
211  }
212 
213  // restore the seed
214  if (prevSeed > 0) {
215  RooRandom::randomGenerator()->SetSeed(prevSeed);
216  }
217 
218  }else samp_alt = fTestStatSampler->GetSamplingDistribution(paramPointAlt);
219 
220 
221  // create result
222  string resultname = "HypoTestCalculator_result";
223  HypoTestResult* res = new HypoTestResult(resultname.c_str());
224  res->SetPValueIsRightTail(fTestStatSampler->GetTestStatistic()->PValueIsRightTail());
225  res->SetTestStatisticData(obsTestStat);
226  res->SetAltDistribution(samp_alt);
227  res->SetNullDistribution(samp_null);
228  res->SetAltDetailedOutput( detOut_alt );
229  res->SetNullDetailedOutput( detOut_null );
230  res->SetAllTestStatisticsData( allTS );
231 
232  const RooArgSet *aset = GetFitInfo();
233  if (aset != NULL) {
234  RooDataSet *dset = new RooDataSet(NULL, NULL, *aset);
235  dset->add(*aset);
236  res->SetFitInfo( dset );
237  }
238 
239  *bothParams = *saveAll;
240  delete allTS;
241  delete bothParams;
242  delete saveAll;
243  delete altParams;
244  delete nullParams;
245  delete nullSnapshot;
246  PostHook();
247  return res;
248 }
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// to re-use same toys for alternate hypothesis
252 
253 void HypoTestCalculatorGeneric::UseSameAltToys() {
255 }
256 
257 
258 
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
void SetAltDistribution(SamplingDistribution *alt)
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition: ModelConfig.h:52
RooAbsPdf * GetPdf() const
get model PDF (return NULL if pdf has not been specified or does not exist)
Definition: ModelConfig.h:244
void SetFitInfo(RooDataSet *d)
TestStatSampler is an interface class for a tools which produce RooStats SamplingDistributions.
HypoTestResult is a base class for results from hypothesis tests.
static UInt_t integer(UInt_t max, TRandom *generator=randomGenerator())
Return an integer uniformly distributed from [0,n-1].
Definition: RooRandom.cxx:102
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
const std::vector< Double_t > & GetSamplingDistribution() const
Get test statistics values.
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return NULL if not existing)
Definition: ModelConfig.h:250
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Common base class for the Hypothesis Test Calculators.
#define oocoutP(o, a)
Definition: RooMsgService.h:46
#define oocoutE(o, a)
Definition: RooMsgService.h:48
void LoadSnapshot() const
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition: RooRandom.cxx:54
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:37
void SetAllTestStatisticsData(const RooArgList *tsd)
void SetAltDetailedOutput(RooDataSet *d)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
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:99
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:29
void SetTestStatisticData(const Double_t tsd)
This class simply holds a sampling distribution of some test statistic.
TestStatistic that returns the ratio of profiled likelihoods.
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the 'data' argset, to the data set...
void SetPValueIsRightTail(Bool_t pr)
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
static Vc_ALWAYS_INLINE int_v max(const int_v &x, const int_v &y)
Definition: vector.h:440
ClassImp(RooStats::HypoTestCalculatorGeneric) using namespace RooStats
Same purpose as HybridCalculatorOriginal, but different implementation.
Mother of all ROOT objects.
Definition: TObject.h:58
void SetNullDetailedOutput(RooDataSet *d)
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
#define NULL
Definition: Rtypes.h:82
Int_t getSize() const
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add element to non-owning set.
Definition: RooArgSet.cxx:448
void SetNullDistribution(SamplingDistribution *null)