36 ClassImp(RooStats::HypoTestCalculatorGeneric);
38 using namespace RooStats;
47 HypoTestCalculatorGeneric::HypoTestCalculatorGeneric(
48 const RooAbsData &data,
49 const ModelConfig &altModel,
50 const ModelConfig &nullModel,
51 TestStatSampler *sampler
54 fNullModel(&nullModel),
56 fTestStatSampler(sampler),
63 =
new RatioOfProfiledLikelihoodsTestStat(*nullModel.GetPdf(),
65 altModel.GetSnapshot());
67 fDefaultSampler =
new ToyMCSampler(*fDefaultTestStat, 1000);
68 fTestStatSampler = fDefaultSampler;
77 void HypoTestCalculatorGeneric::SetupSampler(
const ModelConfig& model)
const {
78 fNullModel->LoadSnapshot();
79 fTestStatSampler->SetObservables(*fNullModel->GetObservables());
80 fTestStatSampler->SetParametersForTestStat(*fNullModel->GetParametersOfInterest());
84 fTestStatSampler->SetSamplingDistName(model.GetName());
85 fTestStatSampler->SetPdf(*model.GetPdf());
86 fTestStatSampler->SetNuisanceParameters(*model.GetNuisanceParameters());
93 HypoTestCalculatorGeneric::~HypoTestCalculatorGeneric() {
94 if(fDefaultSampler)
delete fDefaultSampler;
95 if(fDefaultTestStat)
delete fDefaultTestStat;
106 HypoTestResult* HypoTestCalculatorGeneric::GetHypoTest()
const {
110 const_cast<ModelConfig*
>(fNullModel)->GuessObsAndNuisance(*fData);
111 const_cast<ModelConfig*
>(fAltModel)->GuessObsAndNuisance(*fData);
113 const RooArgSet * nullSnapshot = fNullModel->GetSnapshot();
114 if(nullSnapshot == NULL) {
115 oocoutE((TObject*)0,Generation) <<
"Null model needs a snapshot. Set using modelconfig->SetSnapshot(poi)." << endl;
120 if(CheckHook() != 0) {
121 oocoutE((TObject*)0,Generation) <<
"There was an error in CheckHook(). Stop." << endl;
125 if (!fTestStatSampler || !fTestStatSampler->GetTestStatistic() ) {
126 oocoutE((TObject*)0,InputArguments) <<
"Test Statistic Sampler or Test Statistics not defined. Stop." << endl;
131 RooArgSet *nullParams = fNullModel->GetPdf()->getParameters(*fData);
132 RooArgSet *altParams = fAltModel->GetPdf()->getParameters(*fData);
134 RooArgSet *bothParams = fNullModel->GetPdf()->getParameters(*fData);
135 bothParams->add(*altParams,
false);
136 RooArgSet *saveAll = (RooArgSet*) bothParams->snapshot();
139 ToyMCSampler* toymcs =
dynamic_cast<ToyMCSampler*
>( fTestStatSampler );
143 RooArgSet nullP(*nullSnapshot);
146 RooArgList* allTS = NULL;
148 allTS = toymcs->EvaluateAllTestStatistics(*const_cast<RooAbsData*>(fData), nullP);
149 if (!allTS)
return 0;
152 RooRealVar* firstTS = (RooRealVar*)allTS->at(0);
153 obsTestStat = firstTS->getVal();
154 if (allTS->getSize()<=1) {
159 obsTestStat = fTestStatSampler->EvaluateTestStatistic(*const_cast<RooAbsData*>(fData), nullP);
161 oocoutP((TObject*)0,Generation) <<
"Test Statistic on data: " << obsTestStat << endl;
166 *bothParams = *saveAll;
171 SetupSampler(*fNullModel);
172 RooArgSet paramPointNull(*fNullModel->GetParametersOfInterest());
173 if(PreNullHook(¶mPointNull, obsTestStat) != 0) {
174 oocoutE((TObject*)0,Generation) <<
"PreNullHook did not return 0." << endl;
176 SamplingDistribution* samp_null = NULL;
177 RooDataSet* detOut_null = NULL;
179 detOut_null = toymcs->GetSamplingDistributions(paramPointNull);
181 samp_null =
new SamplingDistribution( detOut_null->GetName(), detOut_null->GetTitle(), *detOut_null );
182 if (detOut_null->get()->getSize()<=1) {
187 }
else samp_null = fTestStatSampler->GetSamplingDistribution(paramPointNull);
190 *bothParams = *saveAll;
193 SetupSampler(*fAltModel);
194 RooArgSet paramPointAlt(*fAltModel->GetParametersOfInterest());
195 if(PreAltHook(¶mPointAlt, obsTestStat) != 0) {
196 oocoutE((TObject*)0,Generation) <<
"PreAltHook did not return 0." << endl;
198 SamplingDistribution* samp_alt = NULL;
199 RooDataSet* detOut_alt = NULL;
204 unsigned int prevSeed = 0;
205 if (fAltToysSeed > 0) {
206 prevSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1;
207 RooRandom::randomGenerator()->SetSeed(fAltToysSeed);
210 detOut_alt = toymcs->GetSamplingDistributions(paramPointAlt);
212 samp_alt =
new SamplingDistribution( detOut_alt->GetName(), detOut_alt->GetTitle(), *detOut_alt );
213 if (detOut_alt->get()->getSize()<=1) {
221 RooRandom::randomGenerator()->SetSeed(prevSeed);
224 }
else samp_alt = fTestStatSampler->GetSamplingDistribution(paramPointAlt);
228 string resultname =
"HypoTestCalculator_result";
229 HypoTestResult* res =
new HypoTestResult(resultname.c_str());
230 res->SetPValueIsRightTail(fTestStatSampler->GetTestStatistic()->PValueIsRightTail());
231 res->SetTestStatisticData(obsTestStat);
232 res->SetAltDistribution(samp_alt);
233 res->SetNullDistribution(samp_null);
234 res->SetAltDetailedOutput( detOut_alt );
235 res->SetNullDetailedOutput( detOut_null );
236 res->SetAllTestStatisticsData( allTS );
238 const RooArgSet *aset = GetFitInfo();
240 RooDataSet *dset =
new RooDataSet(NULL, NULL, *aset);
242 res->SetFitInfo( dset );
245 *bothParams = *saveAll;
259 void HypoTestCalculatorGeneric::UseSameAltToys() {
260 fAltToysSeed = RooRandom::integer(std::numeric_limits<unsigned int>::max()-1)+1;