ROOT   Reference Guide
rf106_plotdecoration.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4##
5## Basic functionality: adding boxes with parameters to RooPlots and decorating with arrows, etc...
6##
7## \macro_code
8##
9## \authors Clemens Lange, Wouter Verkerke (C++ version)
10
11import ROOT
12
13# Set up model
14# ---------------------
15
16# Create observables
17x = ROOT.RooRealVar("x", "x", -10, 10)
18
19# Create Gaussian
20sigma = ROOT.RooRealVar("sigma", "sigma", 1, 0.1, 10)
21mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
22gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
23
24# Generate a sample of 1000 events with sigma=3
25data = gauss.generate(ROOT.RooArgSet(x), 1000)
26
27# Fit pdf to data
28gauss.fitTo(data)
29
30# Plot p.d.f. and data
31# -------------------------------------
32
33# Overlay projection of gauss on data
34frame = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
35 "RooPlot with decorations"), ROOT.RooFit.Bins(40))
36data.plotOn(frame)
37gauss.plotOn(frame)
38
39# Add box with pdf parameters
40# -----------------------------------------------------
41
42# Left edge of box starts at 55% of Xaxis)
43gauss.paramOn(frame, ROOT.RooFit.Layout(0.55))
44
45# Add box with data statistics
46# -------------------------------------------------------
47
48# X size of box is from 55% to 99% of Xaxis range, of box is at 80% of
49# Yaxis range)
50data.statOn(frame, ROOT.RooFit.Layout(0.55, 0.99, 0.8))
51
53# -----------------------------------
54
56txt = ROOT.TText(2, 100, "Signal")
57txt.SetTextSize(0.04)
58txt.SetTextColor(ROOT.kRed)
60
62arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
63arrow.SetLineColor(ROOT.kRed)
64arrow.SetFillColor(ROOT.kRed)
65arrow.SetLineWidth(3)
67
68# Persist frame with all decorations in ROOT file
69# ---------------------------------------------------------------------------------------------
70
71f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE")
72frame.Write()
73f.Close()
74
75# To read back and plot frame with all decorations in clean root session do
76# root> ROOT.TFile f("rf106_plotdecoration.root")
77# root> xframe.Draw()
78
79c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600)