Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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/** \class RooStats::HypoTestCalculatorGeneric
12 \ingroup Roostats
13
14Common base class for the Hypothesis Test Calculators.
15It is not designed to use directly but via its derived classes
16
17Same purpose as HybridCalculatorOriginal, but different implementation.
18
19This is the "generic" version that works with any TestStatSampler. The
20HybridCalculator derives from this class but explicitly uses the
21ToyMCSampler as its TestStatSampler.
22
23*/
24
30
31#include "RooAddPdf.h"
32
33#include "RooRandom.h"
34
35
36
37using namespace RooStats;
38using std::endl, std::string;
39
40////////////////////////////////////////////////////////////////////////////////
41/// Constructor. When test stat sampler is not provided
42/// uses ToyMCSampler and RatioOfProfiledLikelihoodsTestStat
43/// and nToys = 1000.
44/// User can : GetTestStatSampler()->SetNToys( # )
45
47 const RooAbsData &data,
48 const ModelConfig &altModel,
51 ) :
52 fAltModel(&altModel),
53 fNullModel(&nullModel),
54 fData(&data),
55 fTestStatSampler(sampler),
56 fDefaultSampler(nullptr),
57 fDefaultTestStat(nullptr),
58 fAltToysSeed(0)
59{
60 if(!sampler){
63 *altModel.GetPdf(),
64 altModel.GetSnapshot());
65
68 }
69
70
71}
72
73////////////////////////////////////////////////////////////////////////////////
74/// common setup for both models
75
77 fNullModel->LoadSnapshot();
78 fTestStatSampler->SetObservables(*fNullModel->GetObservables());
79 fTestStatSampler->SetParametersForTestStat(*fNullModel->GetParametersOfInterest());
80
81 // for this model
82 model.LoadSnapshot();
86 // global observables or nuisance pdf will be set by the derived classes
87 // (e.g. Frequentist or HybridCalculator)
88}
89
90////////////////////////////////////////////////////////////////////////////////
91
96
97////////////////////////////////////////////////////////////////////////////////
98/// several possibilities:
99/// no prior nuisance given and no nuisance parameters: ok
100/// no prior nuisance given but nuisance parameters: error
101/// prior nuisance given for some nuisance parameters:
102/// - nuisance parameters are constant, so they don't float in test statistic
103/// - nuisance parameters are floating, so they do float in test statistic
104
106
107 // initial setup
108 PreHook();
109 const_cast<ModelConfig*>(fNullModel)->GuessObsAndNuisance(*fData);
110 const_cast<ModelConfig*>(fAltModel)->GuessObsAndNuisance(*fData);
111
112 std::unique_ptr<const RooArgSet> nullSnapshot {fNullModel->GetSnapshot()};
113 if(nullSnapshot == nullptr) {
114 oocoutE(nullptr,Generation) << "Null model needs a snapshot. Set using modelconfig->SetSnapshot(poi)." << std::endl;
115 return nullptr;
116 }
117
118 // CheckHook
119 if(CheckHook() != 0) {
120 oocoutE(nullptr,Generation) << "There was an error in CheckHook(). Stop." << std::endl;
121 return nullptr;
122 }
123
125 oocoutE(nullptr,InputArguments) << "Test Statistic Sampler or Test Statistics not defined. Stop." << std::endl;
126 return nullptr;
127 }
128
129 // get a big list of all variables for convenient switching
130 std::unique_ptr<RooArgSet> altParams{fAltModel->GetPdf()->getParameters(*fData)};
131 // save all parameters so we can set them back to what they were
132 std::unique_ptr<RooArgSet> bothParams{fNullModel->GetPdf()->getParameters(*fData)};
133 bothParams->add(*altParams,false);
134 std::unique_ptr<RooArgSet> saveAll {(RooArgSet*) bothParams->snapshot()};
135
136 // check whether we have a ToyMCSampler and if so, keep a pointer to it
138
139
140 // evaluate test statistic on data
142 double obsTestStat;
143
144 std::unique_ptr<RooArgList> allTS;
145 if( toymcs ) {
146 allTS = std::unique_ptr<RooArgList>{toymcs->EvaluateAllTestStatistics(*const_cast<RooAbsData*>(fData), nullP)};
147 if (!allTS) return nullptr;
148 //oocoutP(nullptr,Generation) << "All Test Statistics on data: " << std::endl;
149 //allTS->Print("v");
150 RooRealVar* firstTS = static_cast<RooRealVar*>(allTS->at(0));
151 obsTestStat = firstTS->getVal();
152 if (allTS->size()<=1) {
153 allTS = nullptr; // don't save
154 }
155 }else{
157 }
158 oocoutP(nullptr,Generation) << "Test Statistic on data: " << obsTestStat << std::endl;
159
160 // set parameters back ... in case the evaluation of the test statistic
161 // modified something (e.g. a nuisance parameter that is not randomized
162 // must be set here)
163 bothParams->assign(*saveAll);
164
165
166
167 // Generate sampling distribution for null
169 RooArgSet paramPointNull(*fNullModel->GetParametersOfInterest());
171 oocoutE(nullptr,Generation) << "PreNullHook did not return 0." << std::endl;
172 }
174 RooDataSet* detOut_null = nullptr;
175 if(toymcs) {
176 detOut_null = toymcs->GetSamplingDistributions(paramPointNull);
177 if( detOut_null ) {
178 samp_null = new SamplingDistribution( detOut_null->GetName(), detOut_null->GetTitle(), *detOut_null );
179 if (detOut_null->get()->size()<=1) {
180 delete detOut_null;
181 detOut_null= nullptr;
182 }
183 }
185
186 // set parameters back
187 bothParams->assign(*saveAll);
188
189 // Generate sampling distribution for alternate
191 RooArgSet paramPointAlt(*fAltModel->GetParametersOfInterest());
193 oocoutE(nullptr,Generation) << "PreAltHook did not return 0." << std::endl;
194 }
196 RooDataSet* detOut_alt = nullptr;
197 if(toymcs) {
198
199 // case of re-using same toys for every points
200 // set a given seed
201 unsigned int prevSeed = 0;
202 if (fAltToysSeed > 0) {
203 prevSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1; // want to avoid zero value
205 }
206
207 detOut_alt = toymcs->GetSamplingDistributions(paramPointAlt);
208 if( detOut_alt ) {
209 samp_alt = new SamplingDistribution( detOut_alt->GetName(), detOut_alt->GetTitle(), *detOut_alt );
210 if (detOut_alt->get()->size()<=1) {
211 delete detOut_alt;
212 detOut_alt= nullptr;
213 }
214 }
215
216 // restore the seed
217 if (prevSeed > 0) {
219 }
220
222
223
224 // create result
225 string resultname = "HypoTestCalculator_result";
226 HypoTestResult* res = new HypoTestResult(resultname.c_str());
227 res->SetPValueIsRightTail(fTestStatSampler->GetTestStatistic()->PValueIsRightTail());
229 res->SetAltDistribution(samp_alt); // takes ownership of samp_alt
230 res->SetNullDistribution(samp_null); // takes ownership of samp_null
233 res->SetAllTestStatisticsData( allTS.get() );
234
235 const RooArgSet *aset = GetFitInfo();
236 if (aset != nullptr) {
237 RooDataSet *dset = new RooDataSet("", "", *aset);
238 dset->add(*aset);
239 res->SetFitInfo( dset );
240 }
241
242 bothParams->assign(*saveAll);
243 PostHook();
244 return res;
245}
246
247////////////////////////////////////////////////////////////////////////////////
248/// to re-use same toys for alternate hypothesis
249
251 fAltToysSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1;
252}
#define oocoutE(o, a)
#define oocoutP(o, a)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Container class to hold unbinned data.
Definition RooDataSet.h:34
static UInt_t integer(UInt_t max, TRandom *generator=randomGenerator())
Return an integer uniformly distributed from [0,n-1].
Definition RooRandom.cxx:95
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:47
Variable that can be changed from the outside.
Definition RooRealVar.h:37
HypoTestResult * GetHypoTest() const override
inherited methods from HypoTestCalculator interface
virtual int PreNullHook(RooArgSet *, double) const
void SetupSampler(const ModelConfig &model) const
common setup for both models
virtual int PreAltHook(RooArgSet *, double) const
virtual const RooArgSet * GetFitInfo() const
void UseSameAltToys()
Set this for re-using always the same toys for alternate hypothesis in case of calls at different nul...
HypoTestCalculatorGeneric(const RooAbsData &data, const ModelConfig &altModel, const ModelConfig &nullModel, TestStatSampler *sampler=nullptr)
Constructor.
HypoTestResult is a base class for results from hypothesis tests.
void SetAltDetailedOutput(RooDataSet *d)
void SetNullDetailedOutput(RooDataSet *d)
void SetAllTestStatisticsData(const RooArgList *tsd)
void SetNullDistribution(SamplingDistribution *null)
void SetTestStatisticData(const double tsd)
void SetFitInfo(RooDataSet *d)
void SetAltDistribution(SamplingDistribution *alt)
ModelConfig is a simple class that holds configuration information specifying how a model should be u...
Definition ModelConfig.h:35
const RooArgSet * GetNuisanceParameters() const
get RooArgSet containing the nuisance parameters (return nullptr if not existing)
void LoadSnapshot() const
load the snapshot from ws if it exists
RooAbsPdf * GetPdf() const
get model PDF (return nullptr if pdf has not been specified or does not exist)
TestStatistic that returns the ratio of profiled likelihoods.
This class simply holds a sampling distribution of some test statistic.
TestStatSampler is an interface class for a tools which produce RooStats SamplingDistributions.
virtual void SetObservables(const RooArgSet &)=0
specify the observables in the dataset (needed to evaluate the test statistic)
virtual TestStatistic * GetTestStatistic() const =0
Get the TestStatistic.
virtual void SetParametersForTestStat(const RooArgSet &)=0
specify the values of parameters used when evaluating test statistic
virtual double EvaluateTestStatistic(RooAbsData &data, RooArgSet &paramsOfInterest)=0
Main interface to evaluate the test statistic on a dataset.
virtual void SetNuisanceParameters(const RooArgSet &)=0
specify the nuisance parameters (eg. the rest of the parameters)
virtual SamplingDistribution * GetSamplingDistribution(RooArgSet &paramsOfInterest)=0
Main interface to get a ConfInterval, pure virtual.
virtual void SetSamplingDistName(const char *name)=0
Set the name of the sampling distribution used for plotting.
virtual void SetPdf(RooAbsPdf &)=0
Set the Pdf, add to the workspace if not already there.
ToyMCSampler is an implementation of the TestStatSampler interface.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Namespace for the RooStats classes.
Definition CodegenImpl.h:58