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