Logo ROOT   6.18/05
Reference Guide
rf107_plotstyles.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Basic functionality: demonstration of various plotting styles of data, functions in a RooPlot
5##
6## \macro_code
7##
8## \date February 2018
9## \author Clemens Lange, Wouter Verkerke (C++ version)
10
11import ROOT
12
13
14# Set up model
15# ---------------------
16
17# Create observables
18x = ROOT.RooRealVar("x", "x", -10, 10)
19
20# Create Gaussian
21sigma = ROOT.RooRealVar("sigma", "sigma", 3, 0.1, 10)
22mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
23gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
24
25# Generate a sample of 100 events with sigma=3
26data = gauss.generate(ROOT.RooArgSet(x), 100)
27
28# Fit pdf to data
29gauss.fitTo(data)
30
31# Make plot frames
32# -------------------------------
33
34# Make four plot frames to demonstrate various plotting features
35frame1 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
36 "Red Curve / SumW2 Histo errors"), ROOT.RooFit.Bins(20))
37frame2 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
38 "Dashed Curve / No XError bars"), ROOT.RooFit.Bins(20))
39frame3 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
40 "Filled Curve / Blue Histo"), ROOT.RooFit.Bins(20))
41frame4 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
42 "Partial Range / Filled Bar chart"), ROOT.RooFit.Bins(20))
43
44# Data plotting styles
45# ---------------------------------------
46
47# Use sqrt(sum(weights^2)) error instead of Poisson errors
48data.plotOn(frame1, ROOT.RooFit.DataError(ROOT.RooAbsData.SumW2))
49
50# Remove horizontal error bars
51data.plotOn(frame2, ROOT.RooFit.XErrorSize(0))
52
53# Blue markers and error bors
54data.plotOn(frame3, ROOT.RooFit.MarkerColor(
55 ROOT.kBlue), ROOT.RooFit.LineColor(ROOT.kBlue))
56
57# Filled bar chart
58data.plotOn(
59 frame4,
60 ROOT.RooFit.DrawOption("B"),
61 ROOT.RooFit.DataError(
62 ROOT.RooAbsData.ErrorType(2)),
63 ROOT.RooFit.XErrorSize(0),
64 ROOT.RooFit.FillColor(
65 ROOT.kGray))
66
67# Function plotting styles
68# -----------------------------------------------
69
70# Change line color to red
71gauss.plotOn(frame1, ROOT.RooFit.LineColor(ROOT.kRed))
72
73# Change line style to dashed
74gauss.plotOn(frame2, ROOT.RooFit.LineStyle(ROOT.kDashed))
75
76# Filled shapes in green color
77gauss.plotOn(frame3, ROOT.RooFit.DrawOption("F"),
78 ROOT.RooFit.FillColor(ROOT.kOrange), ROOT.RooFit.MoveToBack())
79
80#
81gauss.plotOn(frame4, ROOT.RooFit.Range(-8, 3),
82 ROOT.RooFit.LineColor(ROOT.kMagenta))
83
84c = ROOT.TCanvas("rf107_plotstyles", "rf107_plotstyles", 800, 800)
85c.Divide(2, 2)
86c.cd(1)
87ROOT.gPad.SetLeftMargin(0.15)
88frame1.GetYaxis().SetTitleOffset(1.6)
89frame1.Draw()
90c.cd(2)
91ROOT.gPad.SetLeftMargin(0.15)
92frame2.GetYaxis().SetTitleOffset(1.6)
93frame2.Draw()
94c.cd(3)
95ROOT.gPad.SetLeftMargin(0.15)
96frame3.GetYaxis().SetTitleOffset(1.6)
97frame3.Draw()
98c.cd(4)
99ROOT.gPad.SetLeftMargin(0.15)
100frame4.GetYaxis().SetTitleOffset(1.6)
101frame4.Draw()
102
103c.SaveAs("rf107_plotstyles.png")