102workspaceName =
"combined"
103modelConfigName =
"ModelConfig"
107confidenceLevel = 0.95
110additionalToysFac = 0.5
119 filename =
"results/example_combined_GaussExample_model.root"
120 fileExist =
not ROOT.gSystem.AccessPathName(filename)
124 print(f
"will run standard hist2workspace example")
125 ROOT.gROOT.ProcessLine(
".! prepareHistFactory .")
126 ROOT.gROOT.ProcessLine(
".! hist2workspace config/example.xml")
127 print(
"\n\n---------------------")
128 print(
"Done creating example input")
129 print(
"---------------------\n\n")
134inputFile = ROOT.TFile.Open(filename)
140w = inputFile.Get(workspaceName)
143mc = w.obj(modelConfigName)
146data = w.data(dataName)
148print(f
"Found data and ModelConfig:")
154firstPOI = mc.GetParametersOfInterest().first()
165fc = ROOT.RooStats.FeldmanCousins(data, mc)
166fc.SetConfidenceLevel(confidenceLevel)
167fc.AdditionalNToysFactor(additionalToysFac)
169fc.SetNBins(nPointsToScan)
170fc.CreateConfBelt(
True)
180toymcsampler = fc.GetTestStatSampler()
181testStat = toymcsampler.GetTestStatistic()
190if not mc.GetPdf().canBeExtended():
191 if data.numEntries() == 1:
192 fc.FluctuateNumDataEntries(
False)
194 print(f
"Not sure what to do about this model")
196if mc.GetGlobalObservables():
197 print(f
"will use global observables for unconditional ensemble")
198 mc.GetGlobalObservables().Print()
199 toymcsampler.SetGlobalObservables(mc.GetGlobalObservables())
202interval = fc.GetInterval()
203belt = fc.GetConfidenceBelt()
207 f
"\n95% interval on {firstPOI.GetName()} is : [{interval.LowerLimit(firstPOI)}, {interval.UpperLimit(firstPOI)}] "
211tmpPOI = ROOT.RooArgSet(firstPOI)
212observedUL = interval.UpperLimit(firstPOI)
213firstPOI.setVal(observedUL)
214obsTSatObsUL = fc.GetTestStatSampler().EvaluateTestStatistic(data, tmpPOI)
217parameterScan = fc.GetPointsToScan()
220histOfThresholds = ROOT.TH1F(
"histOfThresholds",
"", parameterScan.numEntries(), firstPOI.getMin(), firstPOI.getMax())
221histOfThresholds.SetDirectory(ROOT.nullptr)
222histOfThresholds.GetXaxis().SetTitle(firstPOI.GetName())
223histOfThresholds.GetYaxis().SetTitle(
"Threshold")
228for i
in range(parameterScan.numEntries()):
229 tmpPoint = parameterScan.get(i).clone(
"temp")
231 arMax = belt.GetAcceptanceRegionMax(tmpPoint)
232 poiVal = tmpPoint.getRealValue(firstPOI.GetName())
233 histOfThresholds.Fill(poiVal, arMax)
238histOfThresholds.SetMinimum(0)
239histOfThresholds.Draw()
246nll = mc.GetPdf().createNLL(data)
247profile = nll.createProfile(mc.GetParametersOfInterest())
250poiAndNuisance = ROOT.RooArgSet()
251if mc.GetNuisanceParameters():
252 poiAndNuisance.add(mc.GetNuisanceParameters())
253poiAndNuisance.add(mc.GetParametersOfInterest())
254w.saveSnapshot(
"paramsToGenerateData", poiAndNuisance)
255paramsToGenerateData = poiAndNuisance.snapshot()
256print(
"\nWill use these parameter points to generate pseudo data for bkg only")
257paramsToGenerateData.Print(
"v")
259unconditionalObs = ROOT.RooArgSet()
260unconditionalObs.add(mc.GetObservables())
261unconditionalObs.add(mc.GetGlobalObservables())
267histOfUL = ROOT.TH1F(
"histOfUL",
"", 100, 0, firstPOI.getMax())
268histOfUL.SetDirectory(ROOT.nullptr)
269histOfUL.GetXaxis().SetTitle(
"Upper Limit (background only)")
270histOfUL.GetYaxis().SetTitle(
"Entries")
271for imc
in range(nToyMC):
275 w.loadSnapshot(
"paramsToGenerateData")
279 if not mc.GetPdf().canBeExtended():
280 if data.numEntries() == 1:
281 toyData = mc.GetPdf().generate(mc.GetObservables(), 1)
283 print(f
"Not sure what to do about this model")
286 toyData = mc.GetPdf().generate(mc.GetObservables(), Extended=
True)
289 one = mc.GetPdf().generateSimGlobal(mc.GetGlobalObservables(), 1)
291 allVars = mc.GetPdf().getVariables()
292 allVars.assign(values)
295 firstPOI.setVal(observedUL)
296 toyTSatObsUL = fc.GetTestStatSampler().EvaluateTestStatistic(toyData, tmpPOI)
299 if obsTSatObsUL < toyTSatObsUL:
300 CLb += (1.0) / nToyMC
301 if obsTSatObsUL <= toyTSatObsUL:
302 CLbinclusive += (1.0) / nToyMC
306 for i
in range(parameterScan.numEntries()):
307 tmpPoint = parameterScan.get(i).clone(
"temp")
308 arMax = belt.GetAcceptanceRegionMax(tmpPoint)
309 firstPOI.setVal(tmpPoint.getRealValue(firstPOI.GetName()))
311 thisTS = fc.GetTestStatSampler().EvaluateTestStatistic(toyData, tmpPOI)
316 thisUL = firstPOI.getVal()
320 histOfUL.Fill(thisUL)
330c1.SaveAs(
"two-sided_upper_limit_output.pdf")
344bins = histOfUL.GetIntegral()
345cumulative = histOfUL.Clone(
"cumulative")
346cumulative.SetContent(bins)
352for i
in range(1, cumulative.GetNbinsX() + 1):
353 if bins[i] < ROOT.RooStats.SignificanceToPValue(2):
354 band2sigDown = cumulative.GetBinCenter(i)
355 if bins[i] < ROOT.RooStats.SignificanceToPValue(1):
356 band1sigDown = cumulative.GetBinCenter(i)
358 bandMedian = cumulative.GetBinCenter(i)
359 if bins[i] < ROOT.RooStats.SignificanceToPValue(-1):
360 band1sigUp = cumulative.GetBinCenter(i)
361 if bins[i] < ROOT.RooStats.SignificanceToPValue(-2):
362 band2sigUp = cumulative.GetBinCenter(i)
364print(f
"-2 sigma band {band2sigDown}")
365print(f
"-1 sigma band {band1sigDown} [Power Constraint)]")
366print(f
"median of band {bandMedian}")
367print(f
"+1 sigma band {band1sigUp}")
368print(f
"+2 sigma band {band2sigUp}")
371print(f
"\nobserved 95% upper-limit {interval.UpperLimit(firstPOI)}")
372print(f
"CLb strict [P(toy>obs|0)] for observed 95% upper-limit {CLb}")
373print(f
"CLb inclusive [P(toy>=obs|0)] for observed 95% upper-limit {CLbinclusive}")