Basic functionality: adding boxes with parameters to RooPlots and decorating with arrows, etc...
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:17 AM.
import ROOT
Create observables
x = ROOT.RooRealVar("x", "x", -10, 10)
Create Gaussian
sigma = ROOT.RooRealVar("sigma", "sigma", 1, 0.1, 10)
mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
Generate a sample of 1000 events with sigma=3
data = gauss.generate({x}, 1000)
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
Overlay projection of gauss on data
frame = x.frame(Name="xframe", Title="RooPlot with decorations", Bins=40)
data.plotOn(frame)
gauss.plotOn(frame)
<cppyy.gbl.RooPlot object at 0xa7beaa0>
Left edge of box starts at 55% of Xaxis)
gauss.paramOn(frame, Layout=0.55)
<cppyy.gbl.RooPlot object at 0xa7beaa0>
X size of box is from 55% to 99% of Xaxis range, of box is at 80% of Yaxis range)
data.statOn(frame, Layout=(0.55, 0.99, 0.8))
<cppyy.gbl.RooPlot object at 0xa7beaa0>
Add text to frame
txt = ROOT.TText(2, 100, "Signal")
txt.SetTextSize(0.04)
txt.SetTextColor(ROOT.kRed)
frame.addObject(txt)
Add arrow to frame
arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
arrow.SetLineColor(ROOT.kRed)
arrow.SetFillColor(ROOT.kRed)
arrow.SetLineWidth(3)
frame.addObject(arrow)
f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE")
frame.Write()
f.Close()
To read back and plot frame with all decorations in clean root session do root> ROOT.TFile f("rf106_plotdecoration.root") root> xframe.Draw()
c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.6)
frame.Draw()
c.SaveAs("rf106_plotdecoration.png")
Info in <TCanvas::Print>: png file rf106_plotdecoration.png has been created
Draw all canvases
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()