126 using namespace RooFit;
127 using namespace RooStats;
129 bool useProof =
false;
135 void OneSidedFrequentistUpperLimitWithBands(
const char *infile =
"",
const char *workspaceName =
"combined",
136 const char *modelConfigName =
"ModelConfig",
137 const char *dataName =
"obsData")
140 double confidenceLevel = 0.95;
141 int nPointsToScan = 12;
147 const char *filename =
"";
148 if (!strcmp(infile,
"")) {
149 filename =
"results/example_combined_GaussExample_model.root";
150 bool fileExist = !gSystem->AccessPathName(filename);
154 cout <<
"HistFactory file cannot be generated on Windows - exit" << endl;
158 cout <<
"will run standard hist2workspace example" << endl;
159 gROOT->ProcessLine(
".! prepareHistFactory .");
160 gROOT->ProcessLine(
".! hist2workspace config/example.xml");
161 cout <<
"\n\n---------------------" << endl;
162 cout <<
"Done creating example input" << endl;
163 cout <<
"---------------------\n\n" << endl;
170 TFile *file = TFile::Open(filename);
174 cout <<
"StandardRooStatsDemoMacro: Input file " << filename <<
" is not found" << endl;
182 RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
184 cout <<
"workspace not found" << endl;
189 ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
192 RooAbsData *data = w->data(dataName);
197 cout <<
"data or ModelConfig was not found" << endl;
205 RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();
216 FeldmanCousins fc(*data, *mc);
217 fc.SetConfidenceLevel(confidenceLevel);
218 fc.AdditionalNToysFactor(
221 fc.SetNBins(nPointsToScan);
222 fc.CreateConfBelt(
true);
233 ToyMCSampler *toymcsampler = (ToyMCSampler *)fc.GetTestStatSampler();
234 ProfileLikelihoodTestStat *testStat =
dynamic_cast<ProfileLikelihoodTestStat *
>(toymcsampler->GetTestStatistic());
235 testStat->SetOneSided(
true);
244 if (!mc->GetPdf()->canBeExtended()) {
245 if (data->numEntries() == 1)
246 fc.FluctuateNumDataEntries(
false);
248 cout <<
"Not sure what to do about this model" << endl;
258 ProofConfig pc(*w, nworkers,
"",
false);
259 toymcsampler->SetProofConfig(&pc);
262 if (mc->GetGlobalObservables()) {
263 cout <<
"will use global observables for unconditional ensemble" << endl;
264 mc->GetGlobalObservables()->Print();
265 toymcsampler->SetGlobalObservables(*mc->GetGlobalObservables());
269 PointSetInterval *interval = fc.GetInterval();
270 ConfidenceBelt *belt = fc.GetConfidenceBelt();
273 cout <<
"\n95% interval on " << firstPOI->GetName() <<
" is : [" << interval->LowerLimit(*firstPOI) <<
", "
274 << interval->UpperLimit(*firstPOI) <<
"] " << endl;
277 RooArgSet tmpPOI(*firstPOI);
278 double observedUL = interval->UpperLimit(*firstPOI);
279 firstPOI->setVal(observedUL);
280 double obsTSatObsUL = fc.GetTestStatSampler()->EvaluateTestStatistic(*data, tmpPOI);
283 RooDataSet *parameterScan = (RooDataSet *)fc.GetPointsToScan();
287 TH1F *histOfThresholds =
288 new TH1F(
"histOfThresholds",
"", parameterScan->numEntries(), firstPOI->getMin(), firstPOI->getMax());
289 histOfThresholds->GetXaxis()->SetTitle(firstPOI->GetName());
290 histOfThresholds->GetYaxis()->SetTitle(
"Threshold");
295 for (Int_t i = 0; i < parameterScan->numEntries(); ++i) {
296 tmpPoint = (RooArgSet *)parameterScan->get(i)->clone(
"temp");
298 double arMax = belt->GetAcceptanceRegionMax(*tmpPoint);
299 double poiVal = tmpPoint->getRealValue(firstPOI->GetName());
300 histOfThresholds->Fill(poiVal, arMax);
302 TCanvas *c1 =
new TCanvas();
305 histOfThresholds->SetMinimum(0);
306 histOfThresholds->Draw();
313 RooAbsReal *nll = mc->GetPdf()->createNLL(*data);
314 RooAbsReal *profile = nll->createProfile(*mc->GetParametersOfInterest());
315 firstPOI->setVal(0.);
317 RooArgSet *poiAndNuisance =
new RooArgSet();
318 if (mc->GetNuisanceParameters())
319 poiAndNuisance->add(*mc->GetNuisanceParameters());
320 poiAndNuisance->add(*mc->GetParametersOfInterest());
321 w->saveSnapshot(
"paramsToGenerateData", *poiAndNuisance);
322 RooArgSet *paramsToGenerateData = (RooArgSet *)poiAndNuisance->snapshot();
323 cout <<
"\nWill use these parameter points to generate pseudo data for bkg only" << endl;
324 paramsToGenerateData->Print(
"v");
326 RooArgSet unconditionalObs;
327 unconditionalObs.add(*mc->GetObservables());
328 unconditionalObs.add(*mc->GetGlobalObservables());
331 double CLbinclusive = 0;
334 TH1F *histOfUL =
new TH1F(
"histOfUL",
"", 100, 0, firstPOI->getMax());
335 histOfUL->GetXaxis()->SetTitle(
"Upper Limit (background only)");
336 histOfUL->GetYaxis()->SetTitle(
"Entries");
337 for (
int imc = 0; imc < nToyMC; ++imc) {
341 w->loadSnapshot(
"paramsToGenerateData");
344 RooDataSet *toyData = 0;
346 if (!mc->GetPdf()->canBeExtended()) {
347 if (data->numEntries() == 1)
348 toyData = mc->GetPdf()->generate(*mc->GetObservables(), 1);
350 cout <<
"Not sure what to do about this model" << endl;
353 toyData = mc->GetPdf()->generate(*mc->GetObservables(), Extended());
360 RooSimultaneous *simPdf =
dynamic_cast<RooSimultaneous *
>(mc->GetPdf());
362 RooDataSet *one = mc->GetPdf()->generate(*mc->GetGlobalObservables(), 1);
363 const RooArgSet *values = one->get();
364 RooArgSet *allVars = mc->GetPdf()->getVariables();
372 TIterator *iter = simPdf->indexCat().typeIterator();
373 RooCatType *tt = NULL;
374 while ((tt = (RooCatType *)iter->Next())) {
377 RooAbsPdf *pdftmp = simPdf->getPdf(tt->GetName());
380 RooArgSet *globtmp = pdftmp->getObservables(*mc->GetGlobalObservables());
381 RooDataSet *tmp = pdftmp->generate(*globtmp, 1);
384 *globtmp = *tmp->get(0);
400 firstPOI->setVal(observedUL);
401 double toyTSatObsUL = fc.GetTestStatSampler()->EvaluateTestStatistic(*toyData, tmpPOI);
404 if (obsTSatObsUL < toyTSatObsUL)
405 CLb += (1.) / nToyMC;
406 if (obsTSatObsUL <= toyTSatObsUL)
407 CLbinclusive += (1.) / nToyMC;
411 for (Int_t i = 0; i < parameterScan->numEntries(); ++i) {
412 tmpPoint = (RooArgSet *)parameterScan->get(i)->clone(
"temp");
413 double arMax = belt->GetAcceptanceRegionMax(*tmpPoint);
414 firstPOI->setVal(tmpPoint->getRealValue(firstPOI->GetName()));
416 double thisTS = fc.GetTestStatSampler()->EvaluateTestStatistic(*toyData, tmpPOI);
421 if (thisTS <= arMax) {
422 thisUL = firstPOI->getVal();
458 histOfUL->Fill(thisUL);
466 c1->SaveAs(
"one-sided_upper_limit_output.pdf");
481 Double_t *bins = histOfUL->GetIntegral();
482 TH1F *cumulative = (TH1F *)histOfUL->Clone(
"cumulative");
483 cumulative->SetContent(bins);
484 double band2sigDown, band1sigDown, bandMedian, band1sigUp, band2sigUp;
485 for (
int i = 1; i <= cumulative->GetNbinsX(); ++i) {
486 if (bins[i] < RooStats::SignificanceToPValue(2))
487 band2sigDown = cumulative->GetBinCenter(i);
488 if (bins[i] < RooStats::SignificanceToPValue(1))
489 band1sigDown = cumulative->GetBinCenter(i);
491 bandMedian = cumulative->GetBinCenter(i);
492 if (bins[i] < RooStats::SignificanceToPValue(-1))
493 band1sigUp = cumulative->GetBinCenter(i);
494 if (bins[i] < RooStats::SignificanceToPValue(-2))
495 band2sigUp = cumulative->GetBinCenter(i);
497 cout <<
"-2 sigma band " << band2sigDown << endl;
498 cout <<
"-1 sigma band " << band1sigDown <<
" [Power Constraint)]" << endl;
499 cout <<
"median of band " << bandMedian << endl;
500 cout <<
"+1 sigma band " << band1sigUp << endl;
501 cout <<
"+2 sigma band " << band2sigUp << endl;
504 cout <<
"\nobserved 95% upper-limit " << interval->UpperLimit(*firstPOI) << endl;
505 cout <<
"CLb strict [P(toy>obs|0)] for observed 95% upper-limit " << CLb << endl;
506 cout <<
"CLb inclusive [P(toy>=obs|0)] for observed 95% upper-limit " << CLbinclusive << endl;