Skip to content

Commit e01ec94

Browse files
committed
[hist] Add tutorial for weighted filling
1 parent 74d4832 commit e01ec94

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/// \file
2+
/// \ingroup tutorial_histv7
3+
///
4+
/// Weighted filling of RHist and RBinWithError bin content type.
5+
///
6+
/// \macro_code
7+
///
8+
/// \date October 2025
9+
/// \author The ROOT Team
10+
11+
#include <ROOT/RBinIndex.hxx>
12+
#include <ROOT/RBinWithError.hxx>
13+
#include <ROOT/RHist.hxx>
14+
#include <ROOT/RRegularAxis.hxx>
15+
#include <ROOT/RWeight.hxx>
16+
17+
#include <cstddef>
18+
#include <iostream>
19+
#include <random>
20+
#include <variant>
21+
22+
// It is currently not possible to directly draw RHist's, so this function implements an output with ASCII characters.
23+
static void DrawHistogram(const ROOT::Experimental::RHist<double> &hist)
24+
{
25+
// Get the axis object from the histogram.
26+
auto &axis = std::get<ROOT::Experimental::RRegularAxis>(hist.GetAxes()[0]);
27+
28+
// Print (some of) the global statistics.
29+
std::cout << "entries = " << hist.GetNEntries();
30+
std::cout << ", mean = " << hist.ComputeMean();
31+
std::cout << ", stddev = " << hist.ComputeStdDev();
32+
std::cout << "\n";
33+
34+
// "Draw" the histogram with ASCII characters. The height is hard-coded to work for this tutorial.
35+
for (int row = 15; row > 0; row--) {
36+
auto print = [&](ROOT::Experimental::RBinIndex bin) {
37+
auto value = hist.GetBinContent(bin);
38+
static constexpr int Scale = 100;
39+
std::cout << (value >= (row * Scale) ? '*' : ' ');
40+
};
41+
42+
// First the underflow bin, separated by a vertical bar.
43+
print(ROOT::Experimental::RBinIndex::Underflow());
44+
std::cout << '|';
45+
46+
// Now iterate the normal bins and print a '*' if the value is sufficiently large.
47+
for (auto bin : axis.GetNormalRange()) {
48+
print(bin);
49+
}
50+
51+
// Finally the overflow bin after a separating vertical bar.
52+
std::cout << '|';
53+
print(ROOT::Experimental::RBinIndex::Overflow());
54+
std::cout << "\n";
55+
}
56+
}
57+
58+
void hist002_RHist_weighted()
59+
{
60+
// Create an axis that can be used for multiple histograms.
61+
ROOT::Experimental::RRegularAxis axis(40, {0, 20});
62+
63+
// Create two histograms, one of which will be filled with weighted entries.
64+
ROOT::Experimental::RHist<double> hist1({axis});
65+
ROOT::Experimental::RHist<double> hist2({axis});
66+
67+
// Create a normal distribution with mean 10.0 and stddev 5.0.
68+
std::mt19937 gen;
69+
std::normal_distribution normal(10.0, 5.0);
70+
for (std::size_t i = 0; i < 25000; i++) {
71+
hist1.Fill(normal(gen));
72+
double value = normal(gen);
73+
double weight = 0.2 + 0.008 * value * value;
74+
hist2.Fill(value, ROOT::Experimental::RWeight(weight));
75+
}
76+
77+
// "Draw" the histograms with ASCII characters.
78+
std::cout << "hist1 with expected mean = " << normal.mean() << "\n";
79+
DrawHistogram(hist1);
80+
std::cout << "\n";
81+
82+
std::cout << "hist2 with distorted normal distribution\n";
83+
DrawHistogram(hist2);
84+
std::cout << "\n";
85+
86+
// Create and fill a third histogram with the special RBinWithError bin content type.
87+
ROOT::Experimental::RHist<ROOT::Experimental::RBinWithError> hist3({axis});
88+
for (std::size_t i = 0; i < 25000; i++) {
89+
double value = normal(gen);
90+
double weight = 0.2 + 0.008 * value * value;
91+
hist3.Fill(value, ROOT::Experimental::RWeight(weight));
92+
}
93+
94+
// This allows to compute the bin errors, visualized with ASCII characters.
95+
std::cout << "bin errors of hist3 (not to scale)\n";
96+
for (int row = 15; row > 0; row--) {
97+
auto print = [&](ROOT::Experimental::RBinIndex bin) {
98+
auto error = std::sqrt(hist3.GetBinContent(bin).fSum2);
99+
static constexpr int Scale = 5;
100+
std::cout << (error >= (row * Scale) ? '*' : ' ');
101+
};
102+
103+
// First the underflow bin, separated by a vertical bar.
104+
print(ROOT::Experimental::RBinIndex::Underflow());
105+
std::cout << '|';
106+
107+
// Now iterate the normal bins and print a '*' if the value is sufficiently large.
108+
for (auto bin : axis.GetNormalRange()) {
109+
print(bin);
110+
}
111+
112+
// Finally the overflow bin after a separating vertical bar.
113+
std::cout << '|';
114+
print(ROOT::Experimental::RBinIndex::Overflow());
115+
std::cout << "\n";
116+
}
117+
}

tutorials/hist/histv7/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Examples demonstrating ROOT's histogram package.
66
| **Tutorial** | **Description** |
77
|---|---|
88
| hist001_RHist_basics.C | Basics of RHist, including filling and adding them. |
9+
| hist002_RHist_weighted.C | Weighted filling of RHist and RBinWithError bin content type. |

0 commit comments

Comments
 (0)