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