This tutorial illustrates the basic features of RooFit.
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, April 17, 2024 at 11:16 AM.
import ROOT
Declare variables x,mean,sigma with associated name, title, initial value and allowed range
x = ROOT.RooRealVar("x", "x", -10, 10)
mean = ROOT.RooRealVar("mean", "mean of gaussian", 1, -10, 10)
sigma = ROOT.RooRealVar("sigma", "width of gaussian", 1, 0.1, 10)
Build gaussian pdf in terms of x,mean and sigma
gauss = ROOT.RooGaussian("gauss", "gaussian PDF", x, mean, sigma)
Construct plot frame in 'x'
xframe = x.frame(Title="Gaussian pdf") # RooPlot
Plot gauss in frame (i.e. in x)
gauss.plotOn(xframe)
<cppyy.gbl.RooPlot object at 0xadba6b0>
Change the value of sigma to 3
sigma.setVal(3)
Plot gauss in frame (i.e. in x) and draw frame on canvas
gauss.plotOn(xframe, LineColor="r")
<cppyy.gbl.RooPlot object at 0xadba6b0>
Generate a dataset of 1000 events in x from gauss
data = gauss.generate({x}, 10000) # ROOT.RooDataSet
Make a second plot frame in x and draw both the data and the pdf in the frame
xframe2 = x.frame(Title="Gaussian pdf with data") # RooPlot
data.plotOn(xframe2)
gauss.plotOn(xframe2)
<cppyy.gbl.RooPlot object at 0xaf444d0>
Fit pdf to data
gauss.fitTo(data, PrintLevel=-1)
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Fitting -- RooAbsPdf::fitTo(gauss_over_gauss_Int[x]) fixing normalization set for coefficient determination to observables in data [#1] INFO:Fitting -- using CPU computation library compiled with -mavx2 [#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_gauss_over_gauss_Int[x]_gaussData) Summation contains a RooNLLVar, using its error level [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization [#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
Print values of mean and sigma (that now reflect fitted values and errors)
mean.Print()
sigma.Print()
RooRealVar::mean = 1.01746 +/- 0.0300144 L(-10 - 10) RooRealVar::sigma = 2.9787 +/- 0.0219217 L(0.1 - 10)
Draw all frames on a canvas
c = ROOT.TCanvas("rf101_basics", "rf101_basics", 800, 400)
c.Divide(2)
c.cd(1)
ROOT.gPad.SetLeftMargin(0.15)
xframe.GetYaxis().SetTitleOffset(1.6)
xframe.Draw()
c.cd(2)
ROOT.gPad.SetLeftMargin(0.15)
xframe2.GetYaxis().SetTitleOffset(1.6)
xframe2.Draw()
c.SaveAs("rf101_basics.png")
Info in <TCanvas::Print>: png file rf101_basics.png has been created
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()