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(ROOT.RooArgSet(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(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
34 "RooPlot with decorations"), ROOT.RooFit.Bins(40))
35data.plotOn(frame)
36gauss.plotOn(frame)
37
38# Add box with pdf parameters
39# -----------------------------------------------------
40
41# Left edge of box starts at 55% of Xaxis)
42gauss.paramOn(frame, ROOT.RooFit.Layout(0.55))
43
44# Add box with data statistics
45# -------------------------------------------------------
46
47# X size of box is from 55% to 99% of Xaxis range, of box is at 80% of
48# Yaxis range)
49data.statOn(frame, ROOT.RooFit.Layout(0.55, 0.99, 0.8))
50
51# Add text and arrow
52# -----------------------------------
53
54# Add text to frame
55txt = ROOT.TText(2, 100, "Signal")
56txt.SetTextSize(0.04)
57txt.SetTextColor(ROOT.kRed)
58frame.addObject(txt)
59
60# Add arrow to frame
61arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
62arrow.SetLineColor(ROOT.kRed)
63arrow.SetFillColor(ROOT.kRed)
64arrow.SetLineWidth(3)
65frame.addObject(arrow)
66
67# Persist frame with all decorations in ROOT file
68# ---------------------------------------------------------------------------------------------
69
70f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE")
71frame.Write()
72f.Close()
73
74# To read back and plot frame with all decorations in clean root session do
75# root> ROOT.TFile f("rf106_plotdecoration.root")
76# root> xframe.Draw()
77
78c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600)
79ROOT.gPad.SetLeftMargin(0.15)
80frame.GetYaxis().SetTitleOffset(1.6)
81frame.Draw()
82
83c.SaveAs("rf106_plotdecoration.png")