122 using namespace RooFit;
123 using namespace RooStats;
126 bool useProof =
false;
131 void TwoSidedFrequentistUpperLimitWithBands(
const char *infile =
"",
const char *workspaceName =
"combined",
132 const char *modelConfigName =
"ModelConfig",
133 const char *dataName =
"obsData")
136 double confidenceLevel = 0.95;
139 double additionalToysFac = 0.5;
140 int nPointsToScan = 20;
146 const char *filename =
"";
147 if (!strcmp(infile,
"")) {
148 filename =
"results/example_combined_GaussExample_model.root";
149 bool fileExist = !gSystem->AccessPathName(filename);
153 cout <<
"HistFactory file cannot be generated on Windows - exit" << endl;
157 cout <<
"will run standard hist2workspace example" << endl;
158 gROOT->ProcessLine(
".! prepareHistFactory .");
159 gROOT->ProcessLine(
".! hist2workspace config/example.xml");
160 cout <<
"\n\n---------------------" << endl;
161 cout <<
"Done creating example input" << endl;
162 cout <<
"---------------------\n\n" << endl;
169 TFile *file = TFile::Open(filename);
173 cout <<
"StandardRooStatsDemoMacro: Input file " << filename <<
" is not found" << endl;
181 RooWorkspace *w = (RooWorkspace *)file->Get(workspaceName);
183 cout <<
"workspace not found" << endl;
188 ModelConfig *mc = (ModelConfig *)w->obj(modelConfigName);
191 RooAbsData *data = w->data(dataName);
196 cout <<
"data or ModelConfig was not found" << endl;
200 cout <<
"Found data and ModelConfig:" << endl;
206 RooRealVar *firstPOI = (RooRealVar *)mc->GetParametersOfInterest()->first();
217 FeldmanCousins fc(*data, *mc);
218 fc.SetConfidenceLevel(confidenceLevel);
219 fc.AdditionalNToysFactor(additionalToysFac);
221 fc.SetNBins(nPointsToScan);
222 fc.CreateConfBelt(
true);
232 ToyMCSampler *toymcsampler = (ToyMCSampler *)fc.GetTestStatSampler();
233 ProfileLikelihoodTestStat *testStat =
dynamic_cast<ProfileLikelihoodTestStat *
>(toymcsampler->GetTestStatistic());
242 if (!mc->GetPdf()->canBeExtended()) {
243 if (data->numEntries() == 1)
244 fc.FluctuateNumDataEntries(
false);
246 cout <<
"Not sure what to do about this model" << endl;
256 ProofConfig pc(*w, nworkers,
"",
false);
257 toymcsampler->SetProofConfig(&pc);
260 if (mc->GetGlobalObservables()) {
261 cout <<
"will use global observables for unconditional ensemble" << endl;
262 mc->GetGlobalObservables()->Print();
263 toymcsampler->SetGlobalObservables(*mc->GetGlobalObservables());
267 PointSetInterval *interval = fc.GetInterval();
268 ConfidenceBelt *belt = fc.GetConfidenceBelt();
271 cout <<
"\n95% interval on " << firstPOI->GetName() <<
" is : [" << interval->LowerLimit(*firstPOI) <<
", "
272 << interval->UpperLimit(*firstPOI) <<
"] " << endl;
275 RooArgSet tmpPOI(*firstPOI);
276 double observedUL = interval->UpperLimit(*firstPOI);
277 firstPOI->setVal(observedUL);
278 double obsTSatObsUL = fc.GetTestStatSampler()->EvaluateTestStatistic(*data, tmpPOI);
281 RooDataSet *parameterScan = (RooDataSet *)fc.GetPointsToScan();
285 TH1F *histOfThresholds =
286 new TH1F(
"histOfThresholds",
"", parameterScan->numEntries(), firstPOI->getMin(), firstPOI->getMax());
287 histOfThresholds->GetXaxis()->SetTitle(firstPOI->GetName());
288 histOfThresholds->GetYaxis()->SetTitle(
"Threshold");
293 for (Int_t i = 0; i < parameterScan->numEntries(); ++i) {
294 tmpPoint = (RooArgSet *)parameterScan->get(i)->clone(
"temp");
296 double arMax = belt->GetAcceptanceRegionMax(*tmpPoint);
297 double poiVal = tmpPoint->getRealValue(firstPOI->GetName());
298 histOfThresholds->Fill(poiVal, arMax);
300 TCanvas *c1 =
new TCanvas();
303 histOfThresholds->SetMinimum(0);
304 histOfThresholds->Draw();
311 RooAbsReal *nll = mc->GetPdf()->createNLL(*data);
312 RooAbsReal *profile = nll->createProfile(*mc->GetParametersOfInterest());
313 firstPOI->setVal(0.);
315 RooArgSet *poiAndNuisance =
new RooArgSet();
316 if (mc->GetNuisanceParameters())
317 poiAndNuisance->add(*mc->GetNuisanceParameters());
318 poiAndNuisance->add(*mc->GetParametersOfInterest());
319 w->saveSnapshot(
"paramsToGenerateData", *poiAndNuisance);
320 RooArgSet *paramsToGenerateData = (RooArgSet *)poiAndNuisance->snapshot();
321 cout <<
"\nWill use these parameter points to generate pseudo data for bkg only" << endl;
322 paramsToGenerateData->Print(
"v");
324 RooArgSet unconditionalObs;
325 unconditionalObs.add(*mc->GetObservables());
326 unconditionalObs.add(*mc->GetGlobalObservables());
329 double CLbinclusive = 0;
332 TH1F *histOfUL =
new TH1F(
"histOfUL",
"", 100, 0, firstPOI->getMax());
333 histOfUL->GetXaxis()->SetTitle(
"Upper Limit (background only)");
334 histOfUL->GetYaxis()->SetTitle(
"Entries");
335 for (
int imc = 0; imc < nToyMC; ++imc) {
339 w->loadSnapshot(
"paramsToGenerateData");
342 RooDataSet *toyData = 0;
344 if (!mc->GetPdf()->canBeExtended()) {
345 if (data->numEntries() == 1)
346 toyData = mc->GetPdf()->generate(*mc->GetObservables(), 1);
348 cout <<
"Not sure what to do about this model" << endl;
351 toyData = mc->GetPdf()->generate(*mc->GetObservables(), Extended());
361 RooSimultaneous *simPdf =
dynamic_cast<RooSimultaneous *
>(mc->GetPdf());
363 RooDataSet *one = mc->GetPdf()->generate(*mc->GetGlobalObservables(), 1);
364 const RooArgSet *values = one->get();
365 RooArgSet *allVars = mc->GetPdf()->getVariables();
370 RooDataSet *one = simPdf->generateSimGlobal(*mc->GetGlobalObservables(), 1);
371 const RooArgSet *values = one->get();
372 RooArgSet *allVars = mc->GetPdf()->getVariables();
379 firstPOI->setVal(observedUL);
380 double toyTSatObsUL = fc.GetTestStatSampler()->EvaluateTestStatistic(*toyData, tmpPOI);
383 if (obsTSatObsUL < toyTSatObsUL)
384 CLb += (1.) / nToyMC;
385 if (obsTSatObsUL <= toyTSatObsUL)
386 CLbinclusive += (1.) / nToyMC;
390 for (Int_t i = 0; i < parameterScan->numEntries(); ++i) {
391 tmpPoint = (RooArgSet *)parameterScan->get(i)->clone(
"temp");
392 double arMax = belt->GetAcceptanceRegionMax(*tmpPoint);
393 firstPOI->setVal(tmpPoint->getRealValue(firstPOI->GetName()));
395 double thisTS = fc.GetTestStatSampler()->EvaluateTestStatistic(*toyData, tmpPOI);
400 if (thisTS <= arMax) {
401 thisUL = firstPOI->getVal();
407 histOfUL->Fill(thisUL);
415 c1->SaveAs(
"two-sided_upper_limit_output.pdf");
430 Double_t *bins = histOfUL->GetIntegral();
431 TH1F *cumulative = (TH1F *)histOfUL->Clone(
"cumulative");
432 cumulative->SetContent(bins);
433 double band2sigDown = 0, band1sigDown = 0, bandMedian = 0, band1sigUp = 0, band2sigUp = 0;
434 for (
int i = 1; i <= cumulative->GetNbinsX(); ++i) {
435 if (bins[i] < RooStats::SignificanceToPValue(2))
436 band2sigDown = cumulative->GetBinCenter(i);
437 if (bins[i] < RooStats::SignificanceToPValue(1))
438 band1sigDown = cumulative->GetBinCenter(i);
440 bandMedian = cumulative->GetBinCenter(i);
441 if (bins[i] < RooStats::SignificanceToPValue(-1))
442 band1sigUp = cumulative->GetBinCenter(i);
443 if (bins[i] < RooStats::SignificanceToPValue(-2))
444 band2sigUp = cumulative->GetBinCenter(i);
446 cout <<
"-2 sigma band " << band2sigDown << endl;
447 cout <<
"-1 sigma band " << band1sigDown <<
" [Power Constraint)]" << endl;
448 cout <<
"median of band " << bandMedian << endl;
449 cout <<
"+1 sigma band " << band1sigUp << endl;
450 cout <<
"+2 sigma band " << band2sigUp << endl;
453 cout <<
"\nobserved 95% upper-limit " << interval->UpperLimit(*firstPOI) << endl;
454 cout <<
"CLb strict [P(toy>obs|0)] for observed 95% upper-limit " << CLb << endl;
455 cout <<
"CLb inclusive [P(toy>=obs|0)] for observed 95% upper-limit " << CLbinclusive << endl;