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_code
7##
8## \authors Clemens Lange, Wouter Verkerke (C++ version)
9
10import ROOT
11
12# Set up model
13# ---------------------
14
15# Create observables
16x = ROOT.RooRealVar("x", "x", -10, 10)
17
18# Create Gaussian
19sigma = ROOT.RooRealVar("sigma", "sigma", 1, 0.1, 10)
20mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
21gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
22
23# Generate a sample of 1000 events with sigma=3
24data = gauss.generate({x}, 1000)
25
26# Fit pdf to data
27gauss.fitTo(data)
28
29# Plot pdf and data
30# -------------------------------------
31
32# Overlay projection of gauss on data
33frame = x.frame(Name="xframe", Title="RooPlot with decorations", Bins=40)
34data.plotOn(frame)
35gauss.plotOn(frame)
36
37# Add box with pdf parameters
38# -----------------------------------------------------
39
40# Left edge of box starts at 55% of Xaxis)
41gauss.paramOn(frame, Layout=0.55)
42
43# Add box with data statistics
44# -------------------------------------------------------
45
46# X size of box is from 55% to 99% of Xaxis range, of box is at 80% of
47# Yaxis range)
48data.statOn(frame, Layout=(0.55, 0.99, 0.8))
49
50# Add text and arrow
51# -----------------------------------
52
53# Add text to frame
54txt = ROOT.TText(2, 100, "Signal")
55txt.SetTextSize(0.04)
56txt.SetTextColor(ROOT.kRed)
57frame.addObject(txt)
58
59# Add arrow to frame
60arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
61arrow.SetLineColor(ROOT.kRed)
62arrow.SetFillColor(ROOT.kRed)
63arrow.SetLineWidth(3)
64frame.addObject(arrow)
65
66# Persist frame with all decorations in ROOT file
67# ---------------------------------------------------------------------------------------------
68
69f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE")
70frame.Write()
71f.Close()
72
73# To read back and plot frame with all decorations in clean root session do
74# root> ROOT.TFile f("rf106_plotdecoration.root")
75# root> xframe.Draw()
76
77c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600)
78ROOT.gPad.SetLeftMargin(0.15)
79frame.GetYaxis().SetTitleOffset(1.6)
80frame.Draw()
81
82c.SaveAs("rf106_plotdecoration.png")