Logo ROOT   6.16/01
Reference Guide
rf106_plotdecoration.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## 'BASIC FUNCTIONALITY' RooFit tutorial macro #106
5## Adding boxes with parameters, to RooPlots.
6## Decorating RooPlots with arrows, etc...
7##
8## \macro_code
9##
10## \date February 2018
11## \author Clemens Lange
12## \author Wouter Verkerke (C version)
13
14import ROOT
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", 1, 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 1000 events with sigma=3
28data = gauss.generate(ROOT.RooArgSet(x), 1000)
29
30# Fit pdf to data
31gauss.fitTo(data)
32
33# Plot p.d.f. and data
34# -------------------------------------
35
36# Overlay projection of gauss on data
37frame = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
38 "RooPlot with decorations"), ROOT.RooFit.Bins(40))
39data.plotOn(frame)
40gauss.plotOn(frame)
41
42# Add box with pdf parameters
43# -----------------------------------------------------
44
45# Left edge of box starts at 55% of Xaxis)
46gauss.paramOn(frame, ROOT.RooFit.Layout(0.55))
47
48# Add box with data statistics
49# -------------------------------------------------------
50
51# X size of box is from 55% to 99% of Xaxis range, of box is at 80% of
52# Yaxis range)
53data.statOn(frame, ROOT.RooFit.Layout(0.55, 0.99, 0.8))
54
55# Add text and arrow
56# -----------------------------------
57
58# Add text to frame
59txt = ROOT.TText(2, 100, "Signal")
60txt.SetTextSize(0.04)
61txt.SetTextColor(ROOT.kRed)
62frame.addObject(txt)
63
64# Add arrow to frame
65arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
66arrow.SetLineColor(ROOT.kRed)
67arrow.SetFillColor(ROOT.kRed)
68arrow.SetLineWidth(3)
69frame.addObject(arrow)
70
71# Persist frame with all decorations in ROOT file
72# ---------------------------------------------------------------------------------------------
73
74f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE")
75frame.Write()
76f.Close()
77
78# To read back and plot frame with all decorations in clean root session do
79# root> ROOT.TFile f("rf106_plotdecoration.root")
80# root> xframe.Draw()
81
82c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600)
83ROOT.gPad.SetLeftMargin(0.15)
84frame.GetYaxis().SetTitleOffset(1.6)
85frame.Draw()
86
87c.SaveAs("rf106_plotdecoration.png")