Logo ROOT  
Reference Guide
rf107_plotstyles.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_roofit
3 ## \notebook
4 ## Basic functionality: demonstration of various plotting styles of data, functions in a RooPlot
5 ##
6 ## \macro_code
7 ##
8 ## \date February 2018
9 ## \authors Clemens Lange, Wouter Verkerke (C++ version)
10 
11 import ROOT
12 
13 
14 # Set up model
15 # ---------------------
16 
17 # Create observables
18 x = ROOT.RooRealVar("x", "x", -10, 10)
19 
20 # Create Gaussian
21 sigma = ROOT.RooRealVar("sigma", "sigma", 3, 0.1, 10)
22 mean = ROOT.RooRealVar("mean", "mean", -3, -10, 10)
23 gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
24 
25 # Generate a sample of 100 events with sigma=3
26 data = gauss.generate(ROOT.RooArgSet(x), 100)
27 
28 # Fit pdf to data
29 gauss.fitTo(data)
30 
31 # Make plot frames
32 # -------------------------------
33 
34 # Make four plot frames to demonstrate various plotting features
35 frame1 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
36  "Red Curve / SumW2 Histo errors"), ROOT.RooFit.Bins(20))
37 frame2 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
38  "Dashed Curve / No XError bars"), ROOT.RooFit.Bins(20))
39 frame3 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
40  "Filled Curve / Blue Histo"), ROOT.RooFit.Bins(20))
41 frame4 = x.frame(ROOT.RooFit.Name("xframe"), ROOT.RooFit.Title(
42  "Partial Range / Filled Bar chart"), ROOT.RooFit.Bins(20))
43 
44 # Data plotting styles
45 # ---------------------------------------
46 
47 # Use sqrt(sum(weights^2)) error instead of Poisson errors
48 data.plotOn(frame1, ROOT.RooFit.DataError(ROOT.RooAbsData.SumW2))
49 
50 # Remove horizontal error bars
51 data.plotOn(frame2, ROOT.RooFit.XErrorSize(0))
52 
53 # Blue markers and error bors
54 data.plotOn(frame3, ROOT.RooFit.MarkerColor(
55  ROOT.kBlue), ROOT.RooFit.LineColor(ROOT.kBlue))
56 
57 # Filled bar chart
58 data.plotOn(
59  frame4,
60  ROOT.RooFit.DrawOption("B"),
61  ROOT.RooFit.DataError(
62  ROOT.RooAbsData.ErrorType(2)),
63  ROOT.RooFit.XErrorSize(0),
64  ROOT.RooFit.FillColor(
65  ROOT.kGray))
66 
67 # Function plotting styles
68 # -----------------------------------------------
69 
70 # Change line color to red
71 gauss.plotOn(frame1, ROOT.RooFit.LineColor(ROOT.kRed))
72 
73 # Change line style to dashed
74 gauss.plotOn(frame2, ROOT.RooFit.LineStyle(ROOT.kDashed))
75 
76 # Filled shapes in green color
77 gauss.plotOn(frame3, ROOT.RooFit.DrawOption("F"),
78  ROOT.RooFit.FillColor(ROOT.kOrange), ROOT.RooFit.MoveToBack())
79 
80 #
81 gauss.plotOn(frame4, ROOT.RooFit.Range(-8, 3),
82  ROOT.RooFit.LineColor(ROOT.kMagenta))
83 
84 c = ROOT.TCanvas("rf107_plotstyles", "rf107_plotstyles", 800, 800)
85 c.Divide(2, 2)
86 c.cd(1)
87 ROOT.gPad.SetLeftMargin(0.15)
88 frame1.GetYaxis().SetTitleOffset(1.6)
89 frame1.Draw()
90 c.cd(2)
91 ROOT.gPad.SetLeftMargin(0.15)
92 frame2.GetYaxis().SetTitleOffset(1.6)
93 frame2.Draw()
94 c.cd(3)
95 ROOT.gPad.SetLeftMargin(0.15)
96 frame3.GetYaxis().SetTitleOffset(1.6)
97 frame3.Draw()
98 c.cd(4)
99 ROOT.gPad.SetLeftMargin(0.15)
100 frame4.GetYaxis().SetTitleOffset(1.6)
101 frame4.Draw()
102 
103 c.SaveAs("rf107_plotstyles.png")