# Rf 1 0 7_Plotstyles¶

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 Saturday, November 28, 2020 at 10:37 AM.

In [1]:
import ROOT

Welcome to JupyROOT 6.23/01


## Set up model¶

Create observables

In [2]:
x = ROOT.RooRealVar("x", "x", -10, 10)

RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby
Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University



Create Gaussian

In [3]:
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

In [4]:
data = gauss.generate(ROOT.RooArgSet(x), 100)


Fit pdf to data

In [5]:
gauss.fitTo(data)

Out[5]:
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
**********
**    1 **SET PRINT           1
**********
**********
**    2 **SET NOGRAD
**********
PARAMETER DEFINITIONS:
NO.   NAME         VALUE      STEP SIZE      LIMITS
1 mean        -3.00000e+00  2.00000e+00   -1.00000e+01  1.00000e+01
2 sigma        3.00000e+00  9.90000e-01    1.00000e-01  1.00000e+01
**********
**    3 **SET ERR         0.5
**********
**********
**    4 **SET PRINT           1
**********
**********
**    5 **SET STR           1
**********
NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
**********
**    6 **MIGRAD        1000           1
**********
FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
FCN=244.778 FROM MIGRAD    STATUS=INITIATE        6 CALLS           7 TOTAL
EDM= unknown      STRATEGY= 1      NO ERROR MATRIX
EXT PARAMETER               CURRENT GUESS       STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  mean        -3.00000e+00   2.00000e+00   2.11716e-01   7.88402e+00
2  sigma        3.00000e+00   9.90000e-01   2.22742e-01   8.68850e+00
ERR DEF= 0.5
MIGRAD MINIMIZATION HAS CONVERGED.
MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=244.648 FROM MIGRAD    STATUS=CONVERGED      27 CALLS          28 TOTAL
EDM=6.12289e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                   STEP         FIRST
NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE
1  mean        -3.06106e+00   3.00167e-01   3.38614e-04  -1.01280e-02
2  sigma        2.89572e+00   2.28664e-01   5.51106e-04   1.31676e-02
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
9.013e-02 -8.498e-03
-8.498e-03  5.233e-02
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.12374   1.000 -0.124
2  0.12374  -0.124  1.000
**********
**    7 **SET ERR         0.5
**********
**********
**    8 **SET PRINT           1
**********
**********
**    9 **HESSE        1000
**********
COVARIANCE MATRIX CALCULATED SUCCESSFULLY
FCN=244.648 FROM HESSE     STATUS=OK             10 CALLS          38 TOTAL
EDM=6.13161e-07    STRATEGY= 1      ERROR MATRIX ACCURATE
EXT PARAMETER                                INTERNAL      INTERNAL
NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE
1  mean        -3.06106e+00   3.00196e-01   6.77227e-05  -3.11100e-01
2  sigma        2.89572e+00   2.28685e-01   1.10221e-04  -4.50268e-01
ERR DEF= 0.5
EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
9.015e-02 -8.552e-03
-8.552e-03  5.234e-02
PARAMETER  CORRELATION COEFFICIENTS
NO.  GLOBAL      1      2
1  0.12449   1.000 -0.124
2  0.12449  -0.124  1.000
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization


## Make plot frames¶

Make four plot frames to demonstrate various plotting features

In [6]:
frame1 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
"Red Curve / SumW2 Histo errors"), ROOT.RooFit.Bins(20))
frame2 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
"Dashed Curve / No XError bars"), ROOT.RooFit.Bins(20))
frame3 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
"Filled Curve / Blue Histo"), ROOT.RooFit.Bins(20))
frame4 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
"Partial Range / Filled Bar chart"), ROOT.RooFit.Bins(20))


## Data plotting styles¶

Use sqrt(sum(weights^2)) error instead of Poisson errors

In [7]:
data.plotOn(frame1, ROOT.RooFit.DataError(ROOT.RooAbsData.SumW2))

Out[7]:
<cppyy.gbl.RooPlot object at 0x812e080>

Remove horizontal error bars

In [8]:
data.plotOn(frame2, ROOT.RooFit.XErrorSize(0))

Out[8]:
<cppyy.gbl.RooPlot object at 0x81f2d30>

Blue markers and error bors

In [9]:
data.plotOn(frame3, ROOT.RooFit.MarkerColor(
ROOT.kBlue), ROOT.RooFit.LineColor(ROOT.kBlue))

Out[9]:
<cppyy.gbl.RooPlot object at 0x81f31c0>

Filled bar chart

In [10]:
data.plotOn(
frame4,
ROOT.RooFit.DrawOption("B"),
ROOT.RooFit.DataError(
ROOT.RooAbsData.ErrorType(2)),
ROOT.RooFit.XErrorSize(0),
ROOT.RooFit.FillColor(
ROOT.kGray))

Out[10]:
<cppyy.gbl.RooPlot object at 0x69247c0>

## Function plotting styles¶

Change line color to red

In [11]:
gauss.plotOn(frame1, ROOT.RooFit.LineColor(ROOT.kRed))

Out[11]:
<cppyy.gbl.RooPlot object at 0x812e080>

Change line style to dashed

In [12]:
gauss.plotOn(frame2, ROOT.RooFit.LineStyle(ROOT.kDashed))

Out[12]:
<cppyy.gbl.RooPlot object at 0x81f2d30>

Filled shapes in green color

In [13]:
gauss.plotOn(frame3, ROOT.RooFit.DrawOption("F"),
ROOT.RooFit.FillColor(ROOT.kOrange), ROOT.RooFit.MoveToBack())

Out[13]:
<cppyy.gbl.RooPlot object at 0x81f31c0>
In [14]:
gauss.plotOn(frame4, ROOT.RooFit.Range(-8, 3),
ROOT.RooFit.LineColor(ROOT.kMagenta))

c = ROOT.TCanvas("rf107_plotstyles", "rf107_plotstyles", 800, 800)
c.Divide(2, 2)
c.cd(1)
frame1.GetYaxis().SetTitleOffset(1.6)
frame1.Draw()
c.cd(2)
frame2.GetYaxis().SetTitleOffset(1.6)
frame2.Draw()
c.cd(3)
frame3.GetYaxis().SetTitleOffset(1.6)
frame3.Draw()
c.cd(4)
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

In [15]:
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()