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