ROOT
master
Reference Guide
Loading...
Searching...
No Matches
hist004_TH1_labels.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_hist
3
/// \notebook
4
/// \preview 1D histograms with alphanumeric labels.
5
///
6
/// A TH1 can have named bins that are filled with the method overload TH1::Fill(const char*, double)
7
///
8
/// \macro_image
9
/// \macro_code
10
///
11
/// \date November 2024
12
/// \author Rene Brun
13
14
void
hist004_TH1_labels()
15
{
16
// Create the histogram
17
const
std::array people{
"Jean"
,
"Pierre"
,
"Marie"
,
"Odile"
,
"Sebastien"
,
"Fons"
,
"Rene"
,
18
"Nicolas"
,
"Xavier"
,
"Greg"
,
"Bjarne"
,
"Anton"
,
"Otto"
,
"Eddy"
,
19
"Peter"
,
"Pasha"
,
"Philippe"
,
"Suzanne"
,
"Jeff"
,
"Valery"
};
20
// Start with an arbitrary amount of bins and an arbitrary range, but this will be extended thanks to SetCanExtend().
21
int
nBins = 3;
22
double
rangeMin = 0.0;
23
double
rangeMax = 3.0;
24
auto
*
h
=
new
TH1D
(
"h"
,
"test"
, nBins, rangeMin, rangeMax);
25
// Disable the default stats box when drawing this histogram
26
h
->SetStats(0);
27
h
->SetFillColor(38);
28
// Allow both axes to extend past the initial range we gave in the constructor
29
h
->SetCanExtend(
TH1::kAllAxes
);
30
// Fill the Y axis with arbitrary values, a random amount per bin
31
TRandom3
rng;
32
for
(
int
i = 0; i < 5000; i++) {
33
int
r
= rng.
Rndm
() * 20;
34
// `Fill()` called with a const char* as the first argument will add a value to the bin with that name,
35
// creating it if it doesn't exist yet.
36
h
->Fill(people[
r
], 1);
37
}
38
// Remove empty bins
39
h
->LabelsDeflate();
40
41
auto
*
c1
=
new
TCanvas
(
"c1"
,
"demo bin labels"
, 10, 10, 900, 500);
42
// Enable the grid in the plot
43
c1
->SetGrid();
44
c1
->SetTopMargin(0.15);
45
46
// Draw the histogram
47
h
->Draw();
48
49
// Draw a boxed text
50
// "brNDC" = coordinates draw bottom-right shadow and pass the coordinates in normalized device coordinates
51
auto
*
pt
=
new
TPaveText
(0.7, 0.85, 0.98, 0.98,
"brNDC"
);
52
pt
->SetFillColor(18);
53
pt
->SetTextAlign(12);
54
pt
->AddText(
"Use the axis Context Menu LabelsOption"
);
55
pt
->AddText(
" \"a\" to sort by alphabetic order"
);
56
pt
->AddText(
" \">\" to sort by decreasing values"
);
57
pt
->AddText(
" \"<\" to sort by increasing values"
);
58
pt
->Draw();
59
}
h
#define h(i)
Definition
RSha256.hxx:106
r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Definition
TGWin32VirtualXProxy.cxx:168
TCanvas
The Canvas class.
Definition
TCanvas.h:23
TH1D
1-D histogram with a double per channel (see TH1 documentation)
Definition
TH1.h:926
TH1::kAllAxes
@ kAllAxes
Definition
TH1.h:126
TPaveText
A Pave (see TPave) with text, lines or/and boxes inside.
Definition
TPaveText.h:21
TRandom3
Random number generator class based on M.
Definition
TRandom3.h:27
TRandom3::Rndm
Double_t Rndm() override
Machine independent random number generator.
Definition
TRandom3.cxx:107
pt
TPaveText * pt
Definition
entrylist_figure1.C:7
c1
return c1
Definition
legend1.C:41
tutorials
hist
hist004_TH1_labels.C
ROOTmaster - Reference Guide Generated on Tue May 19 2026 19:40:21 (GVA Time) using Doxygen 1.13.2