ROOT  6.06/09
Reference Guide
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"
22 
23 #include "RooAddPdf.h"
24 
25 #include "RooRandom.h"
26 
27 
29 
30 using namespace RooStats;
31 using namespace std;
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// Constructor. When test stat sampler is not provided
35 /// uses ToyMCSampler and RatioOfProfiledLikelihoodsTestStat
36 /// and nToys = 1000.
37 /// User can : GetTestStatSampler()->SetNToys( # )
38 
40  const RooAbsData &data,
41  const ModelConfig &altModel,
42  const ModelConfig &nullModel,
43  TestStatSampler *sampler
44  ) :
45  fAltModel(&altModel),
46  fNullModel(&nullModel),
47  fData(&data),
48  fTestStatSampler(sampler),
49  fDefaultSampler(0),
50  fDefaultTestStat(0),
51  fAltToysSeed(0)
52 {
53  if(!sampler){
54  fDefaultTestStat
55  = new RatioOfProfiledLikelihoodsTestStat(*nullModel.GetPdf(),
56  *altModel.GetPdf(),
57  altModel.GetSnapshot());
58 
59  fDefaultSampler = new ToyMCSampler(*fDefaultTestStat, 1000);
60  fTestStatSampler = fDefaultSampler;
61  }
62 
63 
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// common setup for both models
68 
69 void HypoTestCalculatorGeneric::SetupSampler(const ModelConfig& model) const {
70  fNullModel->LoadSnapshot();
71  fTestStatSampler->SetObservables(*fNullModel->GetObservables());
72  fTestStatSampler->SetParametersForTestStat(*fNullModel->GetParametersOfInterest());
73 
74  // for this model
75  model.LoadSnapshot();
76  fTestStatSampler->SetSamplingDistName(model.GetName());
77  fTestStatSampler->SetPdf(*model.GetPdf());
78  fTestStatSampler->SetNuisanceParameters(*model.GetNuisanceParameters());
79  // global observables or nuisanance pdf will be set by the derived classes
80  // (e.g. Frequentist or HybridCalculator)
81 }
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 
85 HypoTestCalculatorGeneric::~HypoTestCalculatorGeneric() {
86  if(fDefaultSampler) delete fDefaultSampler;
87  if(fDefaultTestStat) delete fDefaultTestStat;
88 }
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 
92 HypoTestResult* HypoTestCalculatorGeneric::GetHypoTest() const {
93  // several possibilities:
94  // no prior nuisance given and no nuisance parameters: ok
95  // no prior nuisance given but nuisance parameters: error
96  // prior nuisance given for some nuisance parameters:
97  // - nuisance parameters are constant, so they don't float in test statistic
98  // - nuisance parameters are floating, so they do float in test statistic
99 
100  // initial setup
101  PreHook();
102  const_cast<ModelConfig*>(fNullModel)->GuessObsAndNuisance(*fData);
103  const_cast<ModelConfig*>(fAltModel)->GuessObsAndNuisance(*fData);
104 
105  const RooArgSet * nullSnapshot = fNullModel->GetSnapshot();
106  if(nullSnapshot == NULL) {
107  oocoutE((TObject*)0,Generation) << "Null model needs a snapshot. Set using modelconfig->SetSnapshot(poi)." << endl;
108  return 0;
109  }
110 
111  // CheckHook
112  if(CheckHook() != 0) {
113  oocoutE((TObject*)0,Generation) << "There was an error in CheckHook(). Stop." << endl;
114  return 0;
115  }
116 
117  if (!fTestStatSampler || !fTestStatSampler->GetTestStatistic() ) {
118  oocoutE((TObject*)0,InputArguments) << "Test Statistic Sampler or Test Statistics not defined. Stop." << endl;
119  return 0;
120  }
121 
122  // get a big list of all variables for convenient switching
123  RooArgSet *nullParams = fNullModel->GetPdf()->getParameters(*fData);
124  RooArgSet *altParams = fAltModel->GetPdf()->getParameters(*fData);
125  // save all parameters so we can set them back to what they were
126  RooArgSet *bothParams = fNullModel->GetPdf()->getParameters(*fData);
127  bothParams->add(*altParams,false);
128  RooArgSet *saveAll = (RooArgSet*) bothParams->snapshot();
129 
130  // check whether we have a ToyMCSampler and if so, keep a pointer to it
131  ToyMCSampler* toymcs = dynamic_cast<ToyMCSampler*>( fTestStatSampler );
132 
133 
134  // evaluate test statistic on data
135  RooArgSet nullP(*nullSnapshot);
136  double obsTestStat;
137 
138  RooArgList* allTS = NULL;
139  if( toymcs ) {
140  allTS = toymcs->EvaluateAllTestStatistics(*const_cast<RooAbsData*>(fData), nullP);
141  if (!allTS) return 0;
142  //oocoutP((TObject*)0,Generation) << "All Test Statistics on data: " << endl;
143  //allTS->Print("v");
144  RooRealVar* firstTS = (RooRealVar*)allTS->at(0);
145  obsTestStat = firstTS->getVal();
146  if (allTS->getSize()<=1) {
147  delete allTS;
148  allTS= 0; // don't save
149  }
150  }else{
151  obsTestStat = fTestStatSampler->EvaluateTestStatistic(*const_cast<RooAbsData*>(fData), nullP);
152  }
153  oocoutP((TObject*)0,Generation) << "Test Statistic on data: " << obsTestStat << endl;
154 
155  // set parameters back ... in case the evaluation of the test statistic
156  // modified something (e.g. a nuisance parameter that is not randomized
157  // must be set here)
158  *bothParams = *saveAll;
159 
160 
161 
162  // Generate sampling distribution for null
163  SetupSampler(*fNullModel);
164  RooArgSet paramPointNull(*fNullModel->GetParametersOfInterest());
165  if(PreNullHook(&paramPointNull, obsTestStat) != 0) {
166  oocoutE((TObject*)0,Generation) << "PreNullHook did not return 0." << endl;
167  }
168  SamplingDistribution* samp_null = NULL;
169  RooDataSet* detOut_null = NULL;
170  if(toymcs) {
171  detOut_null = toymcs->GetSamplingDistributions(paramPointNull);
172  if( detOut_null ) {
173  samp_null = new SamplingDistribution( detOut_null->GetName(), detOut_null->GetTitle(), *detOut_null );
174  if (detOut_null->get()->getSize()<=1) {
175  delete detOut_null;
176  detOut_null= 0;
177  }
178  }
179  }else samp_null = fTestStatSampler->GetSamplingDistribution(paramPointNull);
180 
181  // set parameters back
182  *bothParams = *saveAll;
183 
184  // Generate sampling distribution for alternate
185  SetupSampler(*fAltModel);
186  RooArgSet paramPointAlt(*fAltModel->GetParametersOfInterest());
187  if(PreAltHook(&paramPointAlt, obsTestStat) != 0) {
188  oocoutE((TObject*)0,Generation) << "PreAltHook did not return 0." << endl;
189  }
190  SamplingDistribution* samp_alt = NULL;
191  RooDataSet* detOut_alt = NULL;
192  if(toymcs) {
193 
194  // case of re-using same toys for every points
195  // set a given seed
196  unsigned int prevSeed = 0;
197  if (fAltToysSeed > 0) {
198  prevSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1; // want to avoid zero value
199  RooRandom::randomGenerator()->SetSeed(fAltToysSeed);
200  }
201 
202  detOut_alt = toymcs->GetSamplingDistributions(paramPointAlt);
203  if( detOut_alt ) {
204  samp_alt = new SamplingDistribution( detOut_alt->GetName(), detOut_alt->GetTitle(), *detOut_alt );
205  if (detOut_alt->get()->getSize()<=1) {
206  delete detOut_alt;
207  detOut_alt= 0;
208  }
209  }
210 
211  // restore the seed
212  if (prevSeed > 0) {
213  RooRandom::randomGenerator()->SetSeed(prevSeed);
214  }
215 
216  }else samp_alt = fTestStatSampler->GetSamplingDistribution(paramPointAlt);
217 
218 
219  // create result
220  string resultname = "HypoTestCalculator_result";
221  HypoTestResult* res = new HypoTestResult(resultname.c_str());
222  res->SetPValueIsRightTail(fTestStatSampler->GetTestStatistic()->PValueIsRightTail());
223  res->SetTestStatisticData(obsTestStat);
224  res->SetAltDistribution(samp_alt);
225  res->SetNullDistribution(samp_null);
226  res->SetAltDetailedOutput( detOut_alt );
227  res->SetNullDetailedOutput( detOut_null );
228  res->SetAllTestStatisticsData( allTS );
229 
230  const RooArgSet *aset = GetFitInfo();
231  if (aset != NULL) {
232  RooDataSet *dset = new RooDataSet(NULL, NULL, *aset);
233  dset->add(*aset);
234  res->SetFitInfo( dset );
235  }
236 
237  *bothParams = *saveAll;
238  delete allTS;
239  delete bothParams;
240  delete saveAll;
241  delete altParams;
242  delete nullParams;
243  delete nullSnapshot;
244  PostHook();
245  return res;
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 /// to re-use same toys for alternate hypothesis
250 
251 void HypoTestCalculatorGeneric::UseSameAltToys() {
253 }
254 
255 
256 
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:101
STL namespace.
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:53
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
void SetAllTestStatisticsData(const RooArgList *tsd)
void SetAltDetailedOutput(RooDataSet *d)
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
ToyMCSampler is an implementation of the TestStatSampler interface.
Definition: ToyMCSampler.h:99
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)
Namespace for the RooStats classes.
Definition: Asimov.h:20
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)