Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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## \authors 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({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(Name="xframe", Title="Red Curve / SumW2 Histo errors", Bins=20)
36frame2 = x.frame(Name="xframe", Title="Dashed Curve / No XError bars", Bins=20)
37frame3 = x.frame(Name="xframe", Title="Filled Curve / Blue Histo", Bins=20)
38frame4 = x.frame(Name="xframe", Title="Partial Range / Filled Bar chart", Bins=20)
39
40# Data plotting styles
41# ---------------------------------------
42
43# Use sqrt(sum(weights^2)) error instead of Poisson errors
44data.plotOn(frame1, DataError="SumW2")
45
46# Remove horizontal error bars
47data.plotOn(frame2, XErrorSize=0)
48
49# Blue markers and error bors
50data.plotOn(frame3, MarkerColor="b", LineColor="b")
51
52# Filled bar chart
53data.plotOn(frame4, DrawOption="B", DataError=None, XErrorSize=0, FillColor="kGray")
54
55# Function plotting styles
56# -----------------------------------------------
57
58# Change line color to red
59gauss.plotOn(frame1, LineColor="r")
60
61# Change line style to dashed
62gauss.plotOn(frame2, LineStyle="--")
63
64# Filled shapes in green color
65gauss.plotOn(frame3, MoveToBack=True, DrawOption="F", FillColor="kOrange")
66
67#
68gauss.plotOn(frame4, Range=(-8, 3), LineColor="m")
69
70c = ROOT.TCanvas("rf107_plotstyles", "rf107_plotstyles", 800, 800)
71c.Divide(2, 2)
72c.cd(1)
73ROOT.gPad.SetLeftMargin(0.15)
74frame1.GetYaxis().SetTitleOffset(1.6)
75frame1.Draw()
76c.cd(2)
77ROOT.gPad.SetLeftMargin(0.15)
78frame2.GetYaxis().SetTitleOffset(1.6)
79frame2.Draw()
80c.cd(3)
81ROOT.gPad.SetLeftMargin(0.15)
82frame3.GetYaxis().SetTitleOffset(1.6)
83frame3.Draw()
84c.cd(4)
85ROOT.gPad.SetLeftMargin(0.15)
86frame4.GetYaxis().SetTitleOffset(1.6)
87frame4.Draw()
88
89c.SaveAs("rf107_plotstyles.png")