Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf106_plotdecoration.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit
3## \notebook
4## Basic functionality: adding boxes with parameters to RooPlots and decorating with arrows, etc...
5##
6## \macro_image
7## \macro_code
8## \macro_output
9##
10## \authors Clemens Lange, Wouter Verkerke (C++ version)
11
12import ROOT
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", 1, 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 1000 events with sigma=3
26data = gauss.generate({x}, 1000)
27
28# Fit pdf to data
29gauss.fitTo(data, PrintLevel=-1)
30
31# Plot pdf and data
32# -------------------------------------
33
34# Overlay projection of gauss on data
35frame = x.frame(Name="xframe", Title="RooPlot with decorations", 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, 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, Layout=(0.55, 0.99, 0.8))
51
52# Add text and arrow
53# -----------------------------------
54
55# Add text to frame
56txt = ROOT.TText(2, 100, "Signal")
57txt.SetTextSize(0.04)
58txt.SetTextColor(ROOT.kRed)
59frame.addObject(txt)
60
61# Add arrow to frame
62arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
63arrow.SetLineColor(ROOT.kRed)
64arrow.SetFillColor(ROOT.kRed)
65arrow.SetLineWidth(3)
66frame.addObject(arrow)
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)
80ROOT.gPad.SetLeftMargin(0.15)
81frame.GetYaxis().SetTitleOffset(1.6)
82frame.Draw()
83
84c.SaveAs("rf106_plotdecoration.png")