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
37
38using namespace RooStats;
39using std::endl, std::string;
40
41////////////////////////////////////////////////////////////////////////////////
42/// Constructor. When test stat sampler is not provided
43/// uses ToyMCSampler and RatioOfProfiledLikelihoodsTestStat
44/// and nToys = 1000.
45/// User can : GetTestStatSampler()->SetNToys( # )
46
48 const RooAbsData &data,
49 const ModelConfig &altModel,
50 const ModelConfig &nullModel,
51 TestStatSampler *sampler
52 ) :
53 fAltModel(&altModel),
54 fNullModel(&nullModel),
55 fData(&data),
56 fTestStatSampler(sampler),
57 fDefaultSampler(nullptr),
58 fDefaultTestStat(nullptr),
59 fAltToysSeed(0)
60{
61 if(!sampler){
64 *altModel.GetPdf(),
65 altModel.GetSnapshot());
66
69 }
70
71
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// common setup for both models
76
81
82 // for this model
83 model.LoadSnapshot();
87 // global observables or nuisance pdf will be set by the derived classes
88 // (e.g. Frequentist or HybridCalculator)
89}
90
91////////////////////////////////////////////////////////////////////////////////
92
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// several possibilities:
100/// no prior nuisance given and no nuisance parameters: ok
101/// no prior nuisance given but nuisance parameters: error
102/// prior nuisance given for some nuisance parameters:
103/// - nuisance parameters are constant, so they don't float in test statistic
104/// - nuisance parameters are floating, so they do float in test statistic
105
107
108 // initial setup
109 PreHook();
110 const_cast<ModelConfig*>(fNullModel)->GuessObsAndNuisance(*fData);
111 const_cast<ModelConfig*>(fAltModel)->GuessObsAndNuisance(*fData);
112
113 std::unique_ptr<const RooArgSet> nullSnapshot {fNullModel->GetSnapshot()};
114 if(nullSnapshot == nullptr) {
115 oocoutE(nullptr,Generation) << "Null model needs a snapshot. Set using modelconfig->SetSnapshot(poi)." << endl;
116 return nullptr;
117 }
118
119 // CheckHook
120 if(CheckHook() != 0) {
121 oocoutE(nullptr,Generation) << "There was an error in CheckHook(). Stop." << endl;
122 return nullptr;
123 }
124
126 oocoutE(nullptr,InputArguments) << "Test Statistic Sampler or Test Statistics not defined. Stop." << endl;
127 return nullptr;
128 }
129
130 // get a big list of all variables for convenient switching
131 std::unique_ptr<RooArgSet> altParams{fAltModel->GetPdf()->getParameters(*fData)};
132 // save all parameters so we can set them back to what they were
133 std::unique_ptr<RooArgSet> bothParams{fNullModel->GetPdf()->getParameters(*fData)};
134 bothParams->add(*altParams,false);
135 std::unique_ptr<RooArgSet> saveAll {(RooArgSet*) bothParams->snapshot()};
136
137 // check whether we have a ToyMCSampler and if so, keep a pointer to it
138 ToyMCSampler* toymcs = dynamic_cast<ToyMCSampler*>(fTestStatSampler );
139
140
141 // evaluate test statistic on data
142 RooArgSet nullP(*nullSnapshot);
143 double obsTestStat;
144
145 std::unique_ptr<RooArgList> allTS;
146 if( toymcs ) {
147 allTS = std::unique_ptr<RooArgList>{toymcs->EvaluateAllTestStatistics(*const_cast<RooAbsData*>(fData), nullP)};
148 if (!allTS) return nullptr;
149 //oocoutP(nullptr,Generation) << "All Test Statistics on data: " << endl;
150 //allTS->Print("v");
151 RooRealVar* firstTS = static_cast<RooRealVar*>(allTS->at(0));
152 obsTestStat = firstTS->getVal();
153 if (allTS->size()<=1) {
154 allTS = nullptr; // don't save
155 }
156 }else{
157 obsTestStat = fTestStatSampler->EvaluateTestStatistic(*const_cast<RooAbsData*>(fData), nullP);
158 }
159 oocoutP(nullptr,Generation) << "Test Statistic on data: " << obsTestStat << endl;
160
161 // set parameters back ... in case the evaluation of the test statistic
162 // modified something (e.g. a nuisance parameter that is not randomized
163 // must be set here)
164 bothParams->assign(*saveAll);
165
166
167
168 // Generate sampling distribution for null
170 RooArgSet paramPointNull(*fNullModel->GetParametersOfInterest());
171 if(PreNullHook(&paramPointNull, obsTestStat) != 0) {
172 oocoutE(nullptr,Generation) << "PreNullHook did not return 0." << endl;
173 }
174 SamplingDistribution* samp_null = nullptr;
175 RooDataSet* detOut_null = nullptr;
176 if(toymcs) {
177 detOut_null = toymcs->GetSamplingDistributions(paramPointNull);
178 if( detOut_null ) {
179 samp_null = new SamplingDistribution( detOut_null->GetName(), detOut_null->GetTitle(), *detOut_null );
180 if (detOut_null->get()->size()<=1) {
181 delete detOut_null;
182 detOut_null= nullptr;
183 }
184 }
185 }else samp_null = fTestStatSampler->GetSamplingDistribution(paramPointNull);
186
187 // set parameters back
188 bothParams->assign(*saveAll);
189
190 // Generate sampling distribution for alternate
193 if(PreAltHook(&paramPointAlt, obsTestStat) != 0) {
194 oocoutE(nullptr,Generation) << "PreAltHook did not return 0." << endl;
195 }
196 SamplingDistribution* samp_alt = nullptr;
197 RooDataSet* detOut_alt = nullptr;
198 if(toymcs) {
199
200 // case of re-using same toys for every points
201 // set a given seed
202 unsigned int prevSeed = 0;
203 if (fAltToysSeed > 0) {
204 prevSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1; // want to avoid zero value
206 }
207
208 detOut_alt = toymcs->GetSamplingDistributions(paramPointAlt);
209 if( detOut_alt ) {
210 samp_alt = new SamplingDistribution( detOut_alt->GetName(), detOut_alt->GetTitle(), *detOut_alt );
211 if (detOut_alt->get()->size()<=1) {
212 delete detOut_alt;
213 detOut_alt= nullptr;
214 }
215 }
216
217 // restore the seed
218 if (prevSeed > 0) {
220 }
221
222 }else samp_alt = fTestStatSampler->GetSamplingDistribution(paramPointAlt);
223
224
225 // create result
226 string resultname = "HypoTestCalculator_result";
227 HypoTestResult* res = new HypoTestResult(resultname.c_str());
229 res->SetTestStatisticData(obsTestStat);
230 res->SetAltDistribution(samp_alt); // takes ownership of samp_alt
231 res->SetNullDistribution(samp_null); // takes ownership of samp_null
232 res->SetAltDetailedOutput( detOut_alt );
233 res->SetNullDetailedOutput( detOut_null );
234 res->SetAllTestStatisticsData( allTS.get() );
235
236 const RooArgSet *aset = GetFitInfo();
237 if (aset != nullptr) {
238 RooDataSet *dset = new RooDataSet("", "", *aset);
239 dset->add(*aset);
240 res->SetFitInfo( dset );
241 }
242
243 bothParams->assign(*saveAll);
244 PostHook();
245 return res;
246}
247
248////////////////////////////////////////////////////////////////////////////////
249/// to re-use same toys for alternate hypothesis
250
252 fAltToysSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1;
253}
#define oocoutE(o, a)
#define oocoutP(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
RooFit::OwningPtr< RooArgSet > getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
Storage_t::size_type size() const
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:57
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:103
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Container class to hold unbinned data.
Definition RooDataSet.h:57
const RooArgSet * get(Int_t index) const override
Return RooArgSet with coordinates of event 'index'.
void add(const RooArgSet &row, double weight, double weightError)
Add one ore more rows of data.
static UInt_t integer(UInt_t max, TRandom *generator=randomGenerator())
Return an integer uniformly distributed from [0,n-1].
Definition RooRandom.cxx:96
static TRandom * randomGenerator()
Return a pointer to a singleton random-number generator implementation.
Definition RooRandom.cxx:48
Variable that can be changed from the outside.
Definition RooRealVar.h:37
Common base class for the Hypothesis Test Calculators.
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 * GetParametersOfInterest() const
get RooArgSet containing the parameter of interest (return nullptr if not existing)
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
const RooArgSet * GetObservables() const
get RooArgSet for observables (return nullptr if not existing)
const RooArgSet * GetSnapshot() const
get RooArgSet for parameters for a particular hypothesis (return nullptr if not existing)
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.
virtual bool PValueIsRightTail(void) const
Defines the sign convention of the test statistic. Overwrite function if necessary.
ToyMCSampler is an implementation of the TestStatSampler interface.
virtual RooArgList * EvaluateAllTestStatistics(RooAbsData &data, const RooArgSet &poi)
Evaluate all test statistics, returning result and any detailed output.
virtual RooDataSet * GetSamplingDistributions(RooArgSet &paramPoint)
Use for serial and parallel runs.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:48
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:615
Namespace for the RooStats classes.
Definition Asimov.h:19