Logo 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 
11 import ROOT
12 
13 # Set up model
14 # ---------------------
15 
16 # Create observables
17 x = ROOT.RooRealVar("x", "x", -10, 10)
18 
19 # Create Gaussian
20 sigma = ROOT.RooRealVar("sigma", "sigma", 1, 0.1, 10)
21 mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
22 gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
23 
24 # Generate a sample of 1000 events with sigma=3
25 data = gauss.generate(ROOT.RooArgSet(x), 1000)
26 
27 # Fit pdf to data
28 gauss.fitTo(data)
29 
30 # Plot p.d.f. and data
31 # -------------------------------------
32 
33 # Overlay projection of gauss on data
34 frame = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
35  "RooPlot with decorations"), ROOT.RooFit.Bins(40))
36 data.plotOn(frame)
37 gauss.plotOn(frame)
38 
39 # Add box with pdf parameters
40 # -----------------------------------------------------
41 
42 # Left edge of box starts at 55% of Xaxis)
43 gauss.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)
50 data.statOn(frame, ROOT.RooFit.Layout(0.55, 0.99, 0.8))
51 
52 # Add text and arrow
53 # -----------------------------------
54 
55 # Add text to frame
56 txt = ROOT.TText(2, 100, "Signal")
57 txt.SetTextSize(0.04)
58 txt.SetTextColor(ROOT.kRed)
59 frame.addObject(txt)
60 
61 # Add arrow to frame
62 arrow = ROOT.TArrow(2, 100, -1, 50, 0.01, "|>")
63 arrow.SetLineColor(ROOT.kRed)
64 arrow.SetFillColor(ROOT.kRed)
65 arrow.SetLineWidth(3)
66 frame.addObject(arrow)
67 
68 # Persist frame with all decorations in ROOT file
69 # ---------------------------------------------------------------------------------------------
70 
71 f = ROOT.TFile("rf106_plotdecoration.root", "RECREATE")
72 frame.Write()
73 f.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 
79 c = ROOT.TCanvas("rf106_plotdecoration", "rf106_plotdecoration", 600, 600)
80 ROOT.gPad.SetLeftMargin(0.15)
81 frame.GetYaxis().SetTitleOffset(1.6)
82 frame.Draw()
83 
84 c.SaveAs("rf106_plotdecoration.png")