Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist003_TH1_draw.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_hist
3## \notebook
4## Draw a 1D histogram to a canvas.
5##
6## \note When using graphics inside a ROOT macro the objects must be created with `new`.
7##
8## \macro_image
9## \macro_code
10##
11## \date November 2024
12## \author Rene Brun, Giacomo Parolini
13import ROOT
14
15# Create and fill the histogram.
16# See hist002_TH1_fillrandom_userfunc.C for more information about this section.
17form1 = ROOT.TFormula("form1", "abs(sin(x)/x)")
18rangeMin = 0.0
19rangeMax = 10.0
20sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax)
21sqroot.SetLineColor(4)
22sqroot.SetLineWidth(6)
23sqroot.SetParameters(10.0, 4.0, 1.0, 20.0)
24
25nBins = 200
26h1d = ROOT.TH1D("h1d", "Test random numbers", nBins, rangeMin, rangeMax)
27
28h1d.FillRandom("sqroot", 10000)
29
30# Create a canvas and draw the histogram
31topX = 200
32topY = 10
33width = 700
34height = 900
35c1 = ROOT.TCanvas("c1", "The FillRandom example", topX, topY, width, height)
36
37# Split the canvas into two sections to plot both the function and the histogram
38# The TPad's constructor accepts the relative coordinates (0 to 1) of the pad's boundaries
39pad1 = ROOT.TPad("pad1", "The pad with the function", 0.05, 0.50, 0.95, 0.95)
40pad2 = ROOT.TPad("pad2", "The pad with the histogram", 0.05, 0.05, 0.95, 0.45)
41
42# Draw the two pads
43pad1.Draw()
44pad2.Draw()
45
46# Select pad1 to draw the next objects into
47pad1.cd()
48pad1.SetGridx()
49pad1.SetGridy()
50pad1.GetFrame().SetBorderMode(-1)
51pad1.GetFrame().SetBorderSize(5)
52
53# Draw the function in pad1
54sqroot.Draw()
55# Add a label to the function.
56# TPaveLabel's constructor accepts the pixel coordinates and the label string.
57lfunction = ROOT.TPaveLabel(5, 39, 9.8, 46, "The sqroot function")
58lfunction.Draw()
59c1.Update()
60
61# Select pad2 to draw the next objects into
62pad2.cd()
63pad2.GetFrame().SetBorderMode(-1)
64pad2.GetFrame().SetBorderSize(5)
65
66h1d.SetFillColor(45)
67h1d.Draw()
68c1.Update()
c SetBorderSize(2)