Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist002_RHist_weighted.C
Go to the documentation of this file.
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>
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.
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.
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 << '|';
55 std::cout << "\n";
56 }
57}
58
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.
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;
76 }
77
78 // "Draw" the histograms with ASCII characters.
79 std::cout << "hist1 with expected mean = " << normal.mean() << "\n";
81 std::cout << "\n";
82
83 std::cout << "hist2 with distorted normal distribution\n";
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.
90 for (std::size_t i = 0; i < 25000; i++) {
91 double value = normal(gen);
92 double weight = 0.2 + 0.008 * value * value;
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.
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 << '|';
117 std::cout << "\n";
118 }
119}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
A bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:22
static RBinIndex Overflow()
static RBinIndex Underflow()
A histogram for aggregation of data along multiple dimensions.
Definition RHist.hxx:55
double ComputeMean(std::size_t dim=0) const
Compute the arithmetic mean of unbinned values.
Definition RHist.hxx:113
double ComputeStdDev(std::size_t dim=0) const
Compute the standard deviation of unbinned values.
Definition RHist.hxx:115
const BinContentType & GetBinContent(const std::array< RBinIndex, N > &indices) const
Get the content of a single bin.
Definition RHist.hxx:136
const std::vector< RAxisVariant > & GetAxes() const
Definition RHist.hxx:105
std::uint64_t GetNEntries() const
Definition RHist.hxx:109
A regular axis with equidistant bins in the interval .
A weight for filling histograms.
Definition RWeight.hxx:17