|
| 1 | +## \file |
| 2 | +## \ingroup tutorial_roostats |
| 3 | +## \notebook |
| 4 | +## Standard demo of the Profile Likelihood calculator |
| 5 | +## StandardProfileLikelihoodDemo |
| 6 | +## |
| 7 | +## This is a standard demo that can be used with any ROOT file |
| 8 | +## prepared in the standard way. You specify: |
| 9 | +## - name for input ROOT file |
| 10 | +## - name of workspace inside ROOT file that holds model and data |
| 11 | +## - name of ModelConfig that specifies details for calculator tools |
| 12 | +## - name of dataset |
| 13 | +## With the values provided below the macro will attempt to run the |
| 14 | +## standard hist2workspace example and read the ROOT file |
| 15 | +## that it produces. |
| 16 | +## |
| 17 | +## The actual heart of the demo is only about 10 lines long. |
| 18 | +## |
| 19 | +## The ProfileLikelihoodCalculator is based on Wilks's theorem |
| 20 | +## and the asymptotic properties of the profile likelihood ratio |
| 21 | +## (eg. that it is chi-square distributed for the true value). |
| 22 | +## |
| 23 | +## \macro_image |
| 24 | +## \macro_output |
| 25 | +## \macro_code |
| 26 | +## |
| 27 | +## \authors Akeem Hart, Kyle Cranmer (C++ Version) |
| 28 | + |
| 29 | +import ROOT |
| 30 | +import sys |
| 31 | + |
| 32 | +workspaceName = "combined" |
| 33 | +modelConfigName = "ModelConfig" |
| 34 | +dataName = "obsData" |
| 35 | +confLevel = 0.95 |
| 36 | +nScanPoints = 50 |
| 37 | +plotAsTF1 = False |
| 38 | +poiXMin = 1 |
| 39 | +poiXMax = 0 |
| 40 | +doHypoTest = False |
| 41 | +nullParamValue = 0 |
| 42 | +filename = "results/example_combined_GaussExample_model.root" |
| 43 | +# if file does not exists generate with histfactory |
| 44 | +if ROOT.gSystem.AccessPathName(filename): |
| 45 | + # Normally this would be run on the command line |
| 46 | + print("will run standard hist2workspace example") |
| 47 | + ROOT.gROOT.ProcessLine(".! prepareHistFactory .") |
| 48 | + ROOT.gROOT.ProcessLine(".! hist2workspace config/example.xml") |
| 49 | + print("\n\n---------------------") |
| 50 | + print("Done creating example input") |
| 51 | + print("---------------------\n\n") |
| 52 | + |
| 53 | +# Try to open the file |
| 54 | +try: |
| 55 | + file = ROOT.TFile.Open(filename) |
| 56 | +except: |
| 57 | + # if input file was specified but not found, quit |
| 58 | + print("StandardRooStatsDemoMacro: Input file %s is not found" % filename) |
| 59 | + sys.exit() |
| 60 | + |
| 61 | +# ------------------------------------------------------- |
| 62 | +# Tutorial starts here |
| 63 | +# ------------------------------------------------------- |
| 64 | + |
| 65 | +# get the workspace out of the file |
| 66 | + |
| 67 | +w = file.Get(workspaceName) |
| 68 | +if not w: |
| 69 | + print("Workspace not found") |
| 70 | + sys.exit() |
| 71 | + |
| 72 | +# get the modelConfig out of the file |
| 73 | +mc = w.obj(modelConfigName) |
| 74 | + |
| 75 | +# get the modelConfig out of the file |
| 76 | +data = w.data(dataName) |
| 77 | + |
| 78 | +# make sure ingredients are found |
| 79 | +if not data or not mc: |
| 80 | + w.Print() |
| 81 | + print("data or ModelConfig was not found") |
| 82 | + sys.exit() |
| 83 | + |
| 84 | +# --------------------------------------------- |
| 85 | +# create and use the ProfileLikelihoodCalculator |
| 86 | +# to find and plot the 95% confidence interval |
| 87 | +# on the parameter of interest as specified |
| 88 | +# in the model config |
| 89 | + |
| 90 | +pl = ROOT.RooStats.ProfileLikelihoodCalculator(data, mc) |
| 91 | +pl.SetConfidenceLevel(confLevel) |
| 92 | +interval = pl.GetInterval() |
| 93 | + |
| 94 | +# print out the interval on the first Parameter of Interest |
| 95 | +firstPOI = mc.GetParametersOfInterest().first() |
| 96 | +print( |
| 97 | + "\n>>>> RESULT : " |
| 98 | + + str(confLevel * 100) |
| 99 | + + "% interval on " |
| 100 | + + firstPOI.GetName() |
| 101 | + + " is : [" |
| 102 | + + str(interval.LowerLimit(firstPOI)) |
| 103 | + + ", " |
| 104 | + + str(interval.UpperLimit(firstPOI)) |
| 105 | + + "]\n\n" |
| 106 | +) |
| 107 | + |
| 108 | +# make a plot |
| 109 | + |
| 110 | +print( |
| 111 | + "making a plot of the profile likelihood function ....(if it is taking a lot of time use less points or the " |
| 112 | + "TF1 drawing option)\n" |
| 113 | +) |
| 114 | +plot = ROOT.RooStats.LikelihoodIntervalPlot(interval) |
| 115 | +plot.SetNPoints(nScanPoints) # do not use too many points, it could become very slow for some models |
| 116 | +if poiXMin < poiXMax: |
| 117 | + plot.SetRange(poiXMin, poiXMax) |
| 118 | +opt = ROOT.TString("") |
| 119 | +if plotAsTF1: |
| 120 | + opt += ROOT.TString("tf1") |
| 121 | +plot.Draw(opt.Data()) # use option TF1 if too slow (plot.Draw("tf1") |
| 122 | + |
| 123 | +# if requested perform also an hypothesis test for the significance |
| 124 | +if doHypoTest: |
| 125 | + nullparams = ROOT.RooArgSet("nullparams") |
| 126 | + nullparams.addClone(firstPOI) |
| 127 | + nullparams.setRealValue(firstPOI.GetName(), nullParamValue) |
| 128 | + pl.SetNullParameters(nullparams) |
| 129 | + print("Perform Test of Hypothesis : null Hypothesis is " + firstPOI.GetName() + str(nullParamValue)) |
| 130 | + result = pl.GetHypoTest() |
| 131 | + print("\n>>>> Hypotheis Test Result ") |
| 132 | + result.Print() |
0 commit comments