Basic functionality: demonstration of various plotting styles of data, functions in a RooPlot
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 Tuesday, March 19, 2024 at 07:14 PM.
import ROOT
Create observables
x = ROOT.RooRealVar("x", "x", -10, 10)
Create Gaussian
sigma = ROOT.RooRealVar("sigma", "sigma", 3, 0.1, 10)
mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
Generate a sample of 100 events with sigma=3
data = gauss.generate({x}, 100)
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
Make four plot frames to demonstrate various plotting features
frame1 = x.frame(Name="xframe", Title="Red Curve / SumW2 Histo errors", Bins=20)
frame2 = x.frame(Name="xframe", Title="Dashed Curve / No XError bars", Bins=20)
frame3 = x.frame(Name="xframe", Title="Filled Curve / Blue Histo", Bins=20)
frame4 = x.frame(Name="xframe", Title="Partial Range / Filled Bar chart", Bins=20)
Use sqrt(sum(weights^2)) error instead of Poisson errors
data.plotOn(frame1, DataError="SumW2")
<cppyy.gbl.RooPlot object at 0xb422400>
Remove horizontal error bars
data.plotOn(frame2, XErrorSize=0)
<cppyy.gbl.RooPlot object at 0xb38b790>
Blue markers and error bors
data.plotOn(frame3, MarkerColor="b", LineColor="b")
<cppyy.gbl.RooPlot object at 0xb304130>
Filled bar chart
data.plotOn(frame4, DrawOption="B", DataError=None, XErrorSize=0, FillColor="kGray")
<cppyy.gbl.RooPlot object at 0xb5bfc40>
Change line color to red
gauss.plotOn(frame1, LineColor="r")
<cppyy.gbl.RooPlot object at 0xb422400>
Change line style to dashed
gauss.plotOn(frame2, LineStyle="--")
<cppyy.gbl.RooPlot object at 0xb38b790>
Filled shapes in green color
gauss.plotOn(frame3, MoveToBack=True, DrawOption="F", FillColor="kOrange")
<cppyy.gbl.RooPlot object at 0xb304130>
gauss.plotOn(frame4, Range=(-8, 3), LineColor="m")
c = ROOT.TCanvas("rf107_plotstyles", "rf107_plotstyles", 800, 800)
c.Divide(2, 2)
c.cd(1)
ROOT.gPad.SetLeftMargin(0.15)
frame1.GetYaxis().SetTitleOffset(1.6)
frame1.Draw()
c.cd(2)
ROOT.gPad.SetLeftMargin(0.15)
frame2.GetYaxis().SetTitleOffset(1.6)
frame2.Draw()
c.cd(3)
ROOT.gPad.SetLeftMargin(0.15)
frame3.GetYaxis().SetTitleOffset(1.6)
frame3.Draw()
c.cd(4)
ROOT.gPad.SetLeftMargin(0.15)
frame4.GetYaxis().SetTitleOffset(1.6)
frame4.Draw()
c.SaveAs("rf107_plotstyles.png")
[#1] INFO:Plotting -- RooAbsPdf::plotOn(gauss) only plotting range [-8,3], curve is normalized to data in given range [#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'plotRange' created with bounds [-8,3]
Info in <TCanvas::Print>: png file rf107_plotstyles.png has been created
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()