ROOT
v6-32
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
29
import
ROOT
30
31
workspaceName =
"combined"
32
modelConfigName =
"ModelConfig"
33
dataName =
"obsData"
34
confLevel = 0.95
35
nScanPoints = 50
36
plotAsTF1 =
False
37
poiXMin = 1
38
poiXMax = 0
39
doHypoTest =
False
40
nullParamValue = 0
41
filename =
"results/example_combined_GaussExample_model.root"
42
# if file does not exists generate with histfactory
43
if
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
52
file =
ROOT.TFile.Open
(filename)
53
54
# -------------------------------------------------------
55
# Tutorial starts here
56
# -------------------------------------------------------
57
58
# get the workspace out of the file
59
60
w =
file.Get
(workspaceName)
61
62
# get the modelConfig out of the file
63
mc = w[modelConfigName]
64
65
# get the modelConfig out of the file
66
data = 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
74
pl =
ROOT.RooStats.ProfileLikelihoodCalculator
(data, mc)
75
pl.SetConfidenceLevel
(confLevel)
76
interval =
pl.GetInterval
()
77
78
# print out the interval on the first Parameter of Interest
79
firstPOI =
mc.GetParametersOfInterest
().first()
80
limit_lower, limit_upper =
interval.LowerLimit
(firstPOI),
interval.UpperLimit
(firstPOI)
81
print(f
"\n>>>> RESULT : {confLevel * 100}% interval on {firstPOI.GetName()} is : [{limit_lower}, {limit_upper}]\n"
)
82
83
# make a plot
84
85
print(
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
)
89
plot =
ROOT.RooStats.LikelihoodIntervalPlot
(interval)
90
plot.SetNPoints
(nScanPoints)
# do not use too many points, it could become very slow for some models
91
if
poiXMin < poiXMax:
92
plot.SetRange
(poiXMin, poiXMax)
93
opt =
""
94
if
plotAsTF1:
95
opt +=
"tf1"
96
plot.Draw
(opt)
# use option TF1 if too slow (plot.Draw("tf1")
97
98
# if requested perform also an hypothesis test for the significance
99
if
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
()
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
tutorials
roostats
StandardProfileLikelihoodDemo.py
ROOT v6-32 - Reference Guide Generated on Mon Feb 17 2025 14:17:26 (GVA Time) using Doxygen 1.10.0