Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist001_RHist_basics.C
Go to the documentation of this file.
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.
21static 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.
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 << '|';
52 std::cout << "\n";
53 }
54}
55
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.
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";
74 std::cout << "\n";
75
76 // Create a second histogram and fill with random values of a different distribution.
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";
84 std::cout << "\n";
85
86 // Create a third, merged histogram from the two previous two.
88 hist3.Add(hist1);
89 hist3.Add(hist2);
90 std::cout << "hist3 with expected entries = " << (hist1.GetNEntries() + hist2.GetNEntries()) << "\n";
92}
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 .