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->get());
110 const_cast<ModelConfig*>(fAltModel)->GuessObsAndNuisance(*fData->get());
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 // evaluate test statistic on data
141 double obsTestStat;
142
143 std::unique_ptr<RooArgList> allTS;
144 if( toymcs ) {
145 allTS = std::unique_ptr<RooArgList>{toymcs->EvaluateAllTestStatistics(*const_cast<RooAbsData*>(fData), nullP)};
146 if (!allTS) return nullptr;
147 //oocoutP(nullptr,Generation) << "All Test Statistics on data: " << std::endl;
148 //allTS->Print("v");
149 RooRealVar* firstTS = static_cast<RooRealVar*>(allTS->at(0));
150 obsTestStat = firstTS->getVal();
151 if (allTS->size()<=1) {
152 allTS = nullptr; // don't save
153 }
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 // If the pdf is not extended, the generation of toy datasets will fail if
166 // the number of events requested per toy is not set. In that case, we
167 // generate the same number of events as in the observed data.
169 int nEventsPerToy = toymcs->nEventsPerToy();
170 auto *pdf = modelConfig->GetPdf();
171 if (nEventsPerToy == 0 && (!pdf->canBeExtended() || pdf->expectedEvents(modelConfig->GetObservables()) <= 0)) {
172 toymcs->SetNEventsPerToy(fData->sumEntries());
173 }
174 };
175
176
177 // Generate sampling distribution for null
179 RooArgSet paramPointNull(*fNullModel->GetParametersOfInterest());
181 oocoutE(nullptr,Generation) << "PreNullHook did not return 0." << std::endl;
182 }
184 RooDataSet* detOut_null = nullptr;
185 if(toymcs) {
187
188 detOut_null = toymcs->GetSamplingDistributions(paramPointNull);
189 if( detOut_null ) {
190 samp_null = new SamplingDistribution( detOut_null->GetName(), detOut_null->GetTitle(), *detOut_null );
191 if (detOut_null->get()->size()<=1) {
192 delete detOut_null;
193 detOut_null= nullptr;
194 }
195 }
197
198 // set parameters back
199 bothParams->assign(*saveAll);
200
201 // Generate sampling distribution for alternate
203 RooArgSet paramPointAlt(*fAltModel->GetParametersOfInterest());
205 oocoutE(nullptr,Generation) << "PreAltHook did not return 0." << std::endl;
206 }
208 RooDataSet* detOut_alt = nullptr;
209 if(toymcs) {
210
211 // case of re-using same toys for every points
212 // set a given seed
213 unsigned int prevSeed = 0;
214 if (fAltToysSeed > 0) {
215 prevSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1; // want to avoid zero value
217 }
218
220
221 detOut_alt = toymcs->GetSamplingDistributions(paramPointAlt);
222 if( detOut_alt ) {
223 samp_alt = new SamplingDistribution( detOut_alt->GetName(), detOut_alt->GetTitle(), *detOut_alt );
224 if (detOut_alt->get()->size()<=1) {
225 delete detOut_alt;
226 detOut_alt= nullptr;
227 }
228 }
229
230 // restore the seed
231 if (prevSeed > 0) {
233 }
234
236
237
238 // create result
239 string resultname = "HypoTestCalculator_result";
240 HypoTestResult* res = new HypoTestResult(resultname.c_str());
241 res->SetPValueIsRightTail(fTestStatSampler->GetTestStatistic()->PValueIsRightTail());
243 res->SetAltDistribution(samp_alt); // takes ownership of samp_alt
244 res->SetNullDistribution(samp_null); // takes ownership of samp_null
247 res->SetAllTestStatisticsData( allTS.get() );
248
249 const RooArgSet *aset = GetFitInfo();
250 if (aset != nullptr) {
251 RooDataSet *dset = new RooDataSet("", "", *aset);
252 dset->add(*aset);
253 res->SetFitInfo( dset );
254 }
255
256 bothParams->assign(*saveAll);
257 PostHook();
258 return res;
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// to re-use same toys for alternate hypothesis
263
265 fAltToysSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1;
266}
#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:59