'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #503
Reading and using a workspace
The input file for self macro is generated by rf502_wspaceread.py
Author: Clemens Lange, Wouter Verkerke (C version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, July 16, 2025 at 03:56 PM.
import ROOT
Open input file with workspace (generated by rf503_wspacewrite)
f = ROOT.TFile("rf502_workspace_py.root")
Retrieve workspace from file
w = f.Get("w")
Retrieve x, and data from workspace
x = w["x"]
model = w["model"]
data = w["modelData"]
Print structure of composite p.d.f.
model.Print("t")
0x56172f8d8860 RooAddPdf::model = 1/1 [Auto,Clean] 0x56172fa605f0/V- RooChebychev::bkg = 1 [Auto,Dirty] 0x56172a9e9fa0/V- RooRealVar::x = 5 0x5617272320e0/V- RooRealVar::a0 = 0.5 0x561727397900/V- RooRealVar::a1 = 0 0x56172fafe0f0/V- RooRealVar::bkgfrac = 0.5 0x56172faf0ed0/V- RooAddPdf::sig = 1/1 [Auto,Clean] 0x56172fa772e0/V- RooGaussian::sig1 = 1 [Auto,Dirty] 0x56172a9e9fa0/V- RooRealVar::x = 5 0x56172a955300/V- RooRealVar::mean = 5 0x561728659600/V- RooRealVar::sigma1 = 0.5 0x561728f2f6d0/V- RooRealVar::sig1frac = 0.8 0x56172fa84c90/V- RooGaussian::sig2 = 1 [Auto,Dirty] 0x56172a9e9fa0/V- RooRealVar::x = 5 0x56172a955300/V- RooRealVar::mean = 5 0x56172f08dcc0/V- RooRealVar::sigma2 = 1
Fit model to data
model.fitTo(data, PrintLevel=-1)
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Fitting -- RooAbsPdf::fitTo(model) fixing normalization set for coefficient determination to observables in data [#1] INFO:Fitting -- using generic CPU library compiled with no vectorizations [#1] INFO:Fitting -- Creation of NLL object took 10.7748 ms [#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_model_modelData) Summation contains a RooNLLVar, using its error level [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
Plot data and PDF overlaid
xframe = x.frame(Title="Model and data read from workspace")
data.plotOn(xframe)
model.plotOn(xframe)
<cppyy.gbl.RooPlot object at 0x5617304f7140>
Overlay the background component of model with a dashed line
model.plotOn(xframe, Components="bkg", LineStyle="--")
<cppyy.gbl.RooPlot object at 0x5617304f7140>
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) directly selected PDF components: (bkg) [#1] INFO:Plotting -- RooAbsPdf::plotOn(model) indirectly selected PDF components: ()
Overlay the background+sig2 components of model with a dotted line
model.plotOn(xframe, Components="bkg,sig2", LineStyle=":")
<cppyy.gbl.RooPlot object at 0x5617304f7140>
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) directly selected PDF components: (bkg,sig2) [#1] INFO:Plotting -- RooAbsPdf::plotOn(model) indirectly selected PDF components: (sig)
Draw the frame on the canvas
c = ROOT.TCanvas("rf503_wspaceread", "rf503_wspaceread", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
xframe.GetYaxis().SetTitleOffset(1.4)
xframe.Draw()
c.SaveAs("rf503_wspaceread.png")
Info in <TCanvas::Print>: png file rf503_wspaceread.png has been created
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()