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.SetDirectory(ROOT.nullptr)
229histOfThresholds.GetXaxis().SetTitle(firstPOI.GetName())
230histOfThresholds.GetYaxis().SetTitle(
"Threshold")
235for i
in range(parameterScan.numEntries()):
236 tmpPoint = parameterScan.get(i).clone(
"temp")
238 arMax = belt.GetAcceptanceRegionMax(tmpPoint)
239 poiVal = tmpPoint.getRealValue(firstPOI.GetName())
240 histOfThresholds.Fill(poiVal, arMax)
245histOfThresholds.SetMinimum(0)
246histOfThresholds.Draw()
253nll = mc.GetPdf().createNLL(data)
254profile = nll.createProfile(mc.GetParametersOfInterest())
257poiAndNuisance = ROOT.RooArgSet()
258if mc.GetNuisanceParameters():
259 poiAndNuisance.add(mc.GetNuisanceParameters())
260poiAndNuisance.add(mc.GetParametersOfInterest())
261w.saveSnapshot(
"paramsToGenerateData", poiAndNuisance)
262paramsToGenerateData = poiAndNuisance.snapshot()
263print(
"\nWill use these parameter points to generate pseudo data for bkg only")
264paramsToGenerateData.Print(
"v")
266unconditionalObs = ROOT.RooArgSet()
267unconditionalObs.add(mc.GetObservables())
268unconditionalObs.add(mc.GetGlobalObservables())
274histOfUL = ROOT.TH1F(
"histOfUL",
"", 100, 0, firstPOI.getMax())
275histOfUL.SetDirectory(ROOT.nullptr)
276histOfUL.GetXaxis().SetTitle(
"Upper Limit (background only)")
277histOfUL.GetYaxis().SetTitle(
"Entries")
278for imc
in range(nToyMC):
282 w.loadSnapshot(
"paramsToGenerateData")
286 if not mc.GetPdf().canBeExtended():
287 if data.numEntries() == 1:
288 toyData = mc.GetPdf().generate(mc.GetObservables(), 1)
290 print(f
"Not sure what to do about this model")
293 toyData = mc.GetPdf().generate(mc.GetObservables(), Extended=
True)
302 simPdf = ROOT.RooSimultaneous(mc.GetPdf())
304 one = mc.GetPdf().generate(mc.GetGlobalObservables(), 1)
306 allVars = mc.GetPdf().getVariables()
307 allVars.assign(values)
309 one = simPdf.generateSimGlobal(mc.GetGlobalObservables(), 1)
311 allVars = mc.GetPdf().getVariables()
312 allVars.assign(values)
315 firstPOI.setVal(observedUL)
316 toyTSatObsUL = fc.GetTestStatSampler().EvaluateTestStatistic(toyData, tmpPOI)
319 if obsTSatObsUL < toyTSatObsUL:
320 CLb += (1.0) / nToyMC
321 if obsTSatObsUL <= toyTSatObsUL:
322 CLbinclusive += (1.0) / nToyMC
326 for i
in range(parameterScan.numEntries()):
327 tmpPoint = parameterScan.get(i).clone(
"temp")
328 arMax = belt.GetAcceptanceRegionMax(tmpPoint)
329 firstPOI.setVal(tmpPoint.getRealValue(firstPOI.GetName()))
331 thisTS = fc.GetTestStatSampler().EvaluateTestStatistic(toyData, tmpPOI)
336 thisUL = firstPOI.getVal()
340 histOfUL.Fill(thisUL)
350c1.SaveAs(
"two-sided_upper_limit_output.pdf")
364bins = histOfUL.GetIntegral()
365cumulative = histOfUL.Clone(
"cumulative")
366cumulative.SetContent(bins)
372for i
in range(1, cumulative.GetNbinsX() + 1):
373 if bins[i] < ROOT.RooStats.SignificanceToPValue(2):
374 band2sigDown = cumulative.GetBinCenter(i)
375 if bins[i] < ROOT.RooStats.SignificanceToPValue(1):
376 band1sigDown = cumulative.GetBinCenter(i)
378 bandMedian = cumulative.GetBinCenter(i)
379 if bins[i] < ROOT.RooStats.SignificanceToPValue(-1):
380 band1sigUp = cumulative.GetBinCenter(i)
381 if bins[i] < ROOT.RooStats.SignificanceToPValue(-2):
382 band2sigUp = cumulative.GetBinCenter(i)
384print(f
"-2 sigma band {band2sigDown}")
385print(f
"-1 sigma band {band1sigDown} [Power Constraint)]")
386print(f
"median of band {bandMedian}")
387print(f
"+1 sigma band {band1sigUp}")
388print(f
"+2 sigma band {band2sigUp}")
391print(f
"\nobserved 95% upper-limit {interval.UpperLimit(firstPOI)}")
392print(f
"CLb strict [P(toy>obs|0)] for observed 95% upper-limit {CLb}")
393print(f
"CLb inclusive [P(toy>=obs|0)] for observed 95% upper-limit {CLbinclusive}")
void Print(GNN_Data &d, std::string txt="")