Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
StandardProfileLikelihoodDemo.py
Go to the documentation of this file.
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
29import ROOT
30
31workspaceName = "combined"
32modelConfigName = "ModelConfig"
33dataName = "obsData"
34confLevel = 0.95
35nScanPoints = 50
36plotAsTF1 = False
37poiXMin = 1
38poiXMax = 0
39doHypoTest = False
40nullParamValue = 0
41filename = "results/example_combined_GaussExample_model.root"
42# if file does not exists generate with histfactory
43if ROOT.gSystem.AccessPathName(filename):
44 # Normally this would be run on the command line
45 print("will run standard hist2workspace example")
46 ROOT.gROOT.ProcessLine(".! prepareHistFactory .")
47 ROOT.gROOT.ProcessLine(".! hist2workspace config/example.xml")
48 print("\n\n---------------------")
49 print("Done creating example input")
50 print("---------------------\n\n")
51
52file = ROOT.TFile.Open(filename)
53
54# -------------------------------------------------------
55# Tutorial starts here
56# -------------------------------------------------------
57
58# get the workspace out of the file
59
60w = file.Get(workspaceName)
61
62# get the modelConfig out of the file
63mc = w[modelConfigName]
64
65# get the modelConfig out of the file
66data = w[dataName]
67
68# ---------------------------------------------
69# create and use the ProfileLikelihoodCalculator
70# to find and plot the 95% confidence interval
71# on the parameter of interest as specified
72# in the model config
73
74pl = ROOT.RooStats.ProfileLikelihoodCalculator(data, mc)
75pl.SetConfidenceLevel(confLevel)
76interval = pl.GetInterval()
77
78# print out the interval on the first Parameter of Interest
79firstPOI = mc.GetParametersOfInterest().first()
80limit_lower, limit_upper = interval.LowerLimit(firstPOI), interval.UpperLimit(firstPOI)
81print(f"\n>>>> RESULT : {confLevel * 100}% interval on {firstPOI.GetName()} is : [{limit_lower}, {limit_upper}]\n")
82
83# make a plot
84
85print(
86 "making a plot of the profile likelihood function ....(if it is taking a lot of time use less points or the "
87 "TF1 drawing option)\n"
88)
89plot = ROOT.RooStats.LikelihoodIntervalPlot(interval)
90plot.SetNPoints(nScanPoints) # do not use too many points, it could become very slow for some models
91if poiXMin < poiXMax:
92 plot.SetRange(poiXMin, poiXMax)
93opt = ""
94if plotAsTF1:
95 opt += "tf1"
96plot.Draw(opt) # use option TF1 if too slow (plot.Draw("tf1")
97
98# if requested perform also an hypothesis test for the significance
99if doHypoTest:
100 nullparams = ROOT.RooArgSet("nullparams")
101 nullparams.addClone(firstPOI)
102 nullparams.setRealValue(firstPOI.GetName(), nullParamValue)
103 pl.SetNullParameters(nullparams)
104 print("Perform Test of Hypothesis : null Hypothesis is " + firstPOI.GetName() + str(nullParamValue))
105 result = pl.GetHypoTest()
106 print("\n>>>> Hypotheis Test Result ")
107 result.Print()