Skip to content

Commit 8a99fd3

Browse files
hahnjodpiparo
authored andcommitted
[hist] Add tutorial for weighted filling
1 parent f1e7125 commit 8a99fd3

File tree

2 files changed

+120
-0
lines changed

2 files changed

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

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)