Skip to content

Commit f692e72

Browse files
committed
[hist] Add first basic tutorial
1 parent 2d85462 commit f692e72

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

tutorials/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ else()
410410
if(MSVC AND NOT win_broken_tests)
411411
list(APPEND root7_veto analysis/dataframe/df013_InspectAnalysis.C)
412412
endif()
413-
file(GLOB v7_veto_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/ visualisation/webgui/browserv7/*.cxx visualisation/webgui/fitpanelv7/*.cxx visualisation/rcanvas/*.py visualisation/rcanvas/*.cxx)
413+
file(GLOB v7_veto_files RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/ hist/histv7/*.C visualisation/webgui/browserv7/*.cxx visualisation/webgui/fitpanelv7/*.cxx visualisation/rcanvas/*.py visualisation/rcanvas/*.cxx)
414414
list(APPEND root7_veto ${v7_veto_files})
415415
# This depends on RCanvas
416416
list(APPEND root7_veto io/ntuple/ntpl011_global_temperatures.C)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/// \file
2+
/// \ingroup tutorial_histv7
3+
///
4+
/// Basics of RHist, including filling and adding them.
5+
///
6+
/// \macro_code
7+
///
8+
/// \date September 2025
9+
/// \author The ROOT Team
10+
11+
#include <ROOT/RBinIndex.hxx>
12+
#include <ROOT/RHist.hxx>
13+
#include <ROOT/RRegularAxis.hxx>
14+
15+
#include <cstddef>
16+
#include <iostream>
17+
#include <random>
18+
#include <variant>
19+
20+
// It is currently not possible to directly draw RHist's, so this function implements an output with ASCII characters.
21+
static void DrawHistogram(const ROOT::Experimental::RHist<int> &hist)
22+
{
23+
// Get the axis object from the histogram.
24+
auto &axis = std::get<ROOT::Experimental::RRegularAxis>(hist.GetAxes()[0]);
25+
26+
// Print (some of) the global statistics.
27+
std::cout << "entries = " << hist.GetNEntries();
28+
std::cout << ", mean = " << hist.ComputeMean();
29+
std::cout << ", stddev = " << hist.ComputeStdDev();
30+
std::cout << "\n";
31+
32+
// "Draw" the histogram with ASCII characters. The height is hard-coded to work for this tutorial.
33+
for (int row = 15; row > 0; row--) {
34+
auto print = [&](ROOT::Experimental::RBinIndex bin) {
35+
auto value = hist.GetBinContent(bin);
36+
static constexpr int Scale = 10;
37+
std::cout << (value >= (row * Scale) ? '*' : ' ');
38+
};
39+
40+
// First the underflow bin, separated by a vertical bar.
41+
print(ROOT::Experimental::RBinIndex::Underflow());
42+
std::cout << '|';
43+
44+
// Now iterate the normal bins and print a '*' if the value is sufficiently large.
45+
for (auto bin : axis.GetNormalRange()) {
46+
print(bin);
47+
}
48+
49+
// Finally the overflow bin after a separating vertical bar.
50+
std::cout << '|';
51+
print(ROOT::Experimental::RBinIndex::Overflow());
52+
std::cout << "\n";
53+
}
54+
}
55+
56+
void hist001_RHist_basics()
57+
{
58+
// Create an axis that can be used for multiple histograms.
59+
ROOT::Experimental::RRegularAxis axis(40, 0, 20);
60+
61+
// Create a first histograms and fill with random values.
62+
ROOT::Experimental::RHist<int> hist1({axis});
63+
64+
// Create a normal distribution with mean 5.0 and stddev 2.0.
65+
std::mt19937 gen;
66+
std::normal_distribution normal1(5.0, 2.0);
67+
for (std::size_t i = 0; i < 1000; i++) {
68+
hist1.Fill(normal1(gen));
69+
}
70+
71+
// "Draw" the histogram with ASCII characters.
72+
std::cout << "hist1 with expected mean = " << normal1.mean() << "\n";
73+
DrawHistogram(hist1);
74+
std::cout << "\n";
75+
76+
// Create a second histogram and fill with random values of a different distribution.
77+
ROOT::Experimental::RHist<int> hist2({axis});
78+
std::normal_distribution normal2(13.0, 4.0);
79+
for (std::size_t i = 0; i < 1500; i++) {
80+
hist2.Fill(normal2(gen));
81+
}
82+
std::cout << "hist2 with expected mean = " << normal2.mean() << "\n";
83+
DrawHistogram(hist2);
84+
std::cout << "\n";
85+
86+
// Create a third, merged histogram from the two previous two.
87+
ROOT::Experimental::RHist<int> hist3({axis});
88+
hist3.Add(hist1);
89+
hist3.Add(hist2);
90+
std::cout << "hist3 with expected entries = " << (hist1.GetNEntries() + hist2.GetNEntries()) << "\n";
91+
DrawHistogram(hist3);
92+
}

tutorials/hist/histv7/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
\defgroup tutorial_histv7 Histogram tutorials
2+
\ingroup tutorial_hist
3+
4+
Examples demonstrating ROOT's histogram package.
5+
6+
| **Tutorial** | **Description** |
7+
|---|---|
8+
| hist001_RHist_basics.C | Basics of RHist, including filling and adding them. |

0 commit comments

Comments
 (0)