Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hf001_example.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_histfactory
3## A ROOT script demonstrating an example of writing and fitting a HistFactory model using Python.
4##
5## \macro_image
6## \macro_code
7## \macro_output
8##
9## \author George Lewis
10
11import ROOT
12
13"""
14Create a HistFactory measurement from python
15"""
16
17InputFile = "./data/example.root"
18if ROOT.gSystem.AccessPathName(InputFile):
19 ROOT.Info("example.py", InputFile + " does not exist")
20 ROOT.gROOT.ProcessLine(".! prepareHistFactory .")
21 if ROOT.gSystem.AccessPathName(InputFile):
22 ROOT.Info("example.py", InputFile + " still does not exist. \n EXIT")
23 exit()
24
25# Create the measurement
26meas = ROOT.RooStats.HistFactory.Measurement("meas", "meas")
27
28meas.SetOutputFilePrefix("./results/example_UsingPy")
29meas.SetPOI("SigXsecOverSM")
30meas.AddConstantParam("Lumi")
31meas.AddConstantParam("alpha_syst1")
32
33meas.SetLumi(1.0)
34meas.SetLumiRelErr(0.10)
35
36# Create a channel
37
38chan = ROOT.RooStats.HistFactory.Channel("channel1")
39chan.SetData("data", InputFile)
40chan.SetStatErrorConfig(0.05, "Poisson")
41
42# Now, create some samples
43
44# Create the signal sample
45signal = ROOT.RooStats.HistFactory.Sample("signal", "signal", InputFile)
46signal.AddOverallSys("syst1", 0.95, 1.05)
47signal.AddNormFactor("SigXsecOverSM", 1, 0, 3)
48chan.AddSample(signal)
49
50
51# Background 1
52background1 = ROOT.RooStats.HistFactory.Sample("background1", "background1", InputFile)
53background1.ActivateStatError("background1_statUncert", InputFile)
54background1.AddOverallSys("syst2", 0.95, 1.05)
55chan.AddSample(background1)
56
57
58# Background 1
59background2 = ROOT.RooStats.HistFactory.Sample("background2", "background2", InputFile)
60background2.ActivateStatError()
61background2.AddOverallSys("syst3", 0.95, 1.05)
62chan.AddSample(background2)
63
64
65# Done with this channel
66# Add it to the measurement:
67
68meas.AddChannel(chan)
69
70# Collect the histograms from their files,
71# print some output,
72meas.CollectHistograms()
73meas.PrintTree()
74
75# One can print XML code to an output directory:
76# meas.PrintXML("xmlFromPyCode", meas.GetOutputFilePrefix())
77
78# Now, do the measurement
79ws = ROOT.RooStats.HistFactory.MakeModelAndMeasurementFast(meas)
80
81# Retrieve the ModelConfig
82modelConfig = ws["ModelConfig"]
83
84# Extract the PDF and global observables
85pdf = modelConfig.GetPdf()
86
87# Perform the fit using Minos to get the correct asymmetric uncertainties
88result = pdf.fitTo(
89 ws["obsData"], Save=True, PrintLevel=-1, GlobalObservables=modelConfig.GetGlobalObservables(), Minos=True
90)
91
92# Getting list of Parameters of Interest and getting first from them
93poi = modelConfig.GetParametersOfInterest().first()
94
95nll = pdf.createNLL(ws["obsData"])
96profile = nll.createProfile(poi)
97
98# frame for future plot
99frame = poi.frame()
100
101frame.SetTitle("")
102frame.GetYaxis().SetTitle("-log likelihood")
103frame.GetXaxis().SetTitle(poi.GetTitle())
104
105profileLikelihoodCanvas = ROOT.TCanvas("combined", "", 800, 600)
106
107xmin = poi.getMin()
108xmax = poi.getMax()
109
110line = ROOT.TLine(xmin, 0.5, xmax, 0.5)
111line.SetLineColor(ROOT.kGreen)
112line90 = ROOT.TLine(xmin, 2.71 / 2, xmax, 2.71 / 2)
113line90.SetLineColor(ROOT.kGreen)
114line95 = ROOT.TLine(xmin, 3.84 / 2, xmax, 3.84 / 2)
115line95.SetLineColor(ROOT.kGreen)
116
117frame.addObject(line)
118frame.addObject(line90)
119frame.addObject(line95)
120
121nll.plotOn(frame, ShiftToZero=True, LineColor="r", LineStyle="--")
122profile.plotOn(frame)
123
124frame.SetMinimum(0)
125frame.SetMaximum(2)
126
127frame.Draw()
128
129# Print fit results to console in verbose mode to see asymmetric uncertainties
130result.Print("v")