Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist003_TH1_draw.C
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
13
15{
16 // Create and fill the histogram.
17 // See hist002_TH1_fillrandom_userfunc.C for more information about this section.
18 auto *form1 = new TFormula("form1", "abs(sin(x)/x)");
19 double rangeMin = 0.0;
20 double rangeMax = 10.0;
21 auto *sqroot = new TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax);
22 sqroot->SetLineColor(4);
23 sqroot->SetLineWidth(6);
24 sqroot->SetParameters(10.0, 4.0, 1.0, 20.0);
25
26 int nBins = 200;
27 auto *h1d = new TH1D("h1d", "Test random numbers", nBins, rangeMin, rangeMax);
28
29 h1d->FillRandom("sqroot", 10000);
30
31 // Create a canvas and draw the histogram
32 int topX = 200;
33 int topY = 10;
34 int width = 700;
35 int height = 900;
36 auto *c1 = new TCanvas("c1", "The FillRandom example", topX, topY, width, height);
37
38 // Split the canvas into two sections to plot both the function and the histogram
39 // The TPad's constructor accepts the relative coordinates (0 to 1) of the pad's boundaries
40 auto *pad1 = new TPad("pad1", "The pad with the function", 0.05, 0.50, 0.95, 0.95);
41 auto *pad2 = new TPad("pad2", "The pad with the histogram", 0.05, 0.05, 0.95, 0.45);
42
43 // Draw the two pads
44 pad1->Draw();
45 pad2->Draw();
46
47 // Select pad1 to draw the next objects into
48 pad1->cd();
49 pad1->SetGridx();
50 pad1->SetGridy();
51 pad1->GetFrame()->SetBorderMode(-1);
52 pad1->GetFrame()->SetBorderSize(5);
53
54 // Draw the function in pad1
55 sqroot->Draw();
56 // Add a label to the function.
57 // TPaveLabel's constructor accepts the pixel coordinates and the label string.
58 auto *lfunction = new TPaveLabel(5, 39, 9.8, 46, "The sqroot function");
59 lfunction->Draw();
60 c1->Update();
61
62 // Select pad2 to draw the next objects into
63 pad2->cd();
64 pad2->GetFrame()->SetBorderMode(-1);
65 pad2->GetFrame()->SetBorderSize(5);
66
67 h1d->SetFillColor(45);
68 h1d->Draw();
69 c1->Update();
70}
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
The Canvas class.
Definition TCanvas.h:23
1-Dim function class
Definition TF1.h:233
The Formula class.
Definition TFormula.h:89
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
The most important graphics class in the ROOT system.
Definition TPad.h:28
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
return c1
Definition legend1.C:41