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