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