109workspaceName =
"combined"
110modelConfigName =
"ModelConfig"
114confidenceLevel = 0.95
117additionalToysFac = 0.5
126 filename =
"results/example_combined_GaussExample_model.root"
127 fileExist =
not ROOT.gSystem.AccessPathName(filename)
131 print(f
"will run standard hist2workspace example")
132 ROOT.gROOT.ProcessLine(
".! prepareHistFactory .")
133 ROOT.gROOT.ProcessLine(
".! hist2workspace config/example.xml")
134 print(
"\n\n---------------------")
135 print(
"Done creating example input")
136 print(
"---------------------\n\n")
141inputFile = ROOT.TFile.Open(filename)
147w = inputFile.Get(workspaceName)
150mc = w.obj(modelConfigName)
153data = w.data(dataName)
155print(f
"Found data and ModelConfig:")
161firstPOI = mc.GetParametersOfInterest().first()
172fc = ROOT.RooStats.FeldmanCousins(data, mc)
173fc.SetConfidenceLevel(confidenceLevel)
174fc.AdditionalNToysFactor(additionalToysFac)
176fc.SetNBins(nPointsToScan)
177fc.CreateConfBelt(
True)
187toymcsampler = fc.GetTestStatSampler()
188testStat = toymcsampler.GetTestStatistic()
197if not mc.GetPdf().canBeExtended():
198 if data.numEntries() == 1:
199 fc.FluctuateNumDataEntries(
False)
201 print(f
"Not sure what to do about this model")
203if mc.GetGlobalObservables():
204 print(f
"will use global observables for unconditional ensemble")
205 mc.GetGlobalObservables().
Print()
206 toymcsampler.SetGlobalObservables(mc.GetGlobalObservables())
209interval = fc.GetInterval()
210belt = fc.GetConfidenceBelt()
214 f
"\n95% interval on {firstPOI.GetName()} is : [{interval.LowerLimit(firstPOI)}, {interval.UpperLimit(firstPOI)}] "
218tmpPOI = ROOT.RooArgSet(firstPOI)
219observedUL = interval.UpperLimit(firstPOI)
220firstPOI.setVal(observedUL)
221obsTSatObsUL = fc.GetTestStatSampler().EvaluateTestStatistic(data, tmpPOI)
224parameterScan = fc.GetPointsToScan()
227histOfThresholds = ROOT.TH1F(
"histOfThresholds",
"", parameterScan.numEntries(), firstPOI.getMin(), firstPOI.getMax())
228histOfThresholds.GetXaxis().SetTitle(firstPOI.GetName())
229histOfThresholds.GetYaxis().SetTitle(
"Threshold")
234for i
in range(parameterScan.numEntries()):
235 tmpPoint = parameterScan.get(i).
clone(
"temp")
237 arMax = belt.GetAcceptanceRegionMax(tmpPoint)
238 poiVal = tmpPoint.getRealValue(firstPOI.GetName())
239 histOfThresholds.Fill(poiVal, arMax)
244histOfThresholds.SetMinimum(0)
245histOfThresholds.Draw()
252nll = mc.GetPdf().createNLL(data)
253profile = nll.createProfile(mc.GetParametersOfInterest())
256poiAndNuisance = ROOT.RooArgSet()
257if mc.GetNuisanceParameters():
258 poiAndNuisance.add(mc.GetNuisanceParameters())
259poiAndNuisance.add(mc.GetParametersOfInterest())
260w.saveSnapshot(
"paramsToGenerateData", poiAndNuisance)
261paramsToGenerateData = poiAndNuisance.snapshot()
262print(
"\nWill use these parameter points to generate pseudo data for bkg only")
263paramsToGenerateData.Print(
"v")
265unconditionalObs = ROOT.RooArgSet()
266unconditionalObs.add(mc.GetObservables())
267unconditionalObs.add(mc.GetGlobalObservables())
273histOfUL = ROOT.TH1F(
"histOfUL",
"", 100, 0, firstPOI.getMax())
274histOfUL.GetXaxis().SetTitle(
"Upper Limit (background only)")
275histOfUL.GetYaxis().SetTitle(
"Entries")
276for imc
in range(nToyMC):
280 w.loadSnapshot(
"paramsToGenerateData")
284 if not mc.GetPdf().canBeExtended():
285 if data.numEntries() == 1:
286 toyData = mc.GetPdf().generate(mc.GetObservables(), 1)
288 print(f
"Not sure what to do about this model")
291 toyData = mc.GetPdf().generate(mc.GetObservables(), Extended=
True)
300 simPdf = ROOT.RooSimultaneous(mc.GetPdf())
302 one = mc.GetPdf().generate(mc.GetGlobalObservables(), 1)
304 allVars = mc.GetPdf().getVariables()
305 allVars.assign(values)
307 one = simPdf.generateSimGlobal(mc.GetGlobalObservables(), 1)
309 allVars = mc.GetPdf().getVariables()
310 allVars.assign(values)
313 firstPOI.setVal(observedUL)
314 toyTSatObsUL = fc.GetTestStatSampler().EvaluateTestStatistic(toyData, tmpPOI)
317 if obsTSatObsUL < toyTSatObsUL:
318 CLb += (1.0) / nToyMC
319 if obsTSatObsUL <= toyTSatObsUL:
320 CLbinclusive += (1.0) / nToyMC
324 for i
in range(parameterScan.numEntries()):
325 tmpPoint = parameterScan.get(i).
clone(
"temp")
326 arMax = belt.GetAcceptanceRegionMax(tmpPoint)
327 firstPOI.setVal(tmpPoint.getRealValue(firstPOI.GetName()))
329 thisTS = fc.GetTestStatSampler().EvaluateTestStatistic(toyData, tmpPOI)
334 thisUL = firstPOI.getVal()
338 histOfUL.Fill(thisUL)
344c1.SaveAs(
"two-sided_upper_limit_output.pdf")
358bins = histOfUL.GetIntegral()
359cumulative = ROOT.TH1F()
360cumulative = histOfUL.Clone(
"cumulative")
361cumulative.SetContent(bins)
367for i
in range(1, cumulative.GetNbinsX() + 1):
368 if bins[i] < ROOT.RooStats.SignificanceToPValue(2):
369 band2sigDown = cumulative.GetBinCenter(i)
370 if bins[i] < ROOT.RooStats.SignificanceToPValue(1):
371 band1sigDown = cumulative.GetBinCenter(i)
373 bandMedian = cumulative.GetBinCenter(i)
374 if bins[i] < ROOT.RooStats.SignificanceToPValue(-1):
375 band1sigUp = cumulative.GetBinCenter(i)
376 if bins[i] < ROOT.RooStats.SignificanceToPValue(-2):
377 band2sigUp = cumulative.GetBinCenter(i)
379print(f
"-2 sigma band {band2sigDown}")
380print(f
"-1 sigma band {band1sigDown} [Power Constraint)]")
381print(f
"median of band {bandMedian}")
382print(f
"+1 sigma band {band1sigUp}")
383print(f
"+2 sigma band {band2sigUp}")
386print(f
"\nobserved 95% upper-limit {interval.UpperLimit(firstPOI)}")
387print(f
"CLb strict [P(toy>obs|0)] for observed 95% upper-limit {CLb}")
388print(f
"CLb inclusive [P(toy>=obs|0)] for observed 95% upper-limit {CLbinclusive}")
TObject * clone(const char *newname) const override
void Print(GNN_Data &d, std::string txt="")