Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist001_RHist_basics.C File Reference

Detailed Description

Basics of RHist, including filling and adding them.

#include <ROOT/RHist.hxx>
#include <cstddef>
#include <iostream>
#include <random>
#include <variant>
// It is currently not possible to directly draw RHist's, so this function implements an output with ASCII characters.
{
// Get the axis object from the histogram.
auto &axis = std::get<ROOT::Experimental::RRegularAxis>(hist.GetAxes()[0]);
// Print (some of) the global statistics.
std::cout << "entries = " << hist.GetNEntries();
std::cout << ", mean = " << hist.ComputeMean();
std::cout << ", stddev = " << hist.ComputeStdDev();
std::cout << "\n";
// "Draw" the histogram with ASCII characters. The height is hard-coded to work for this tutorial.
for (int row = 15; row > 0; row--) {
auto print = [&](ROOT::Experimental::RBinIndex bin) {
auto value = hist.GetBinContent(bin);
static constexpr int Scale = 10;
std::cout << (value >= (row * Scale) ? '*' : ' ');
};
// First the underflow bin, separated by a vertical bar.
std::cout << '|';
// Now iterate the normal bins and print a '*' if the value is sufficiently large.
for (auto bin : axis.GetNormalRange()) {
print(bin);
}
// Finally the overflow bin after a separating vertical bar.
std::cout << '|';
std::cout << "\n";
}
}
{
// Create an axis that can be used for multiple histograms.
// Create a first histograms and fill with random values.
// Create a normal distribution with mean 5.0 and stddev 2.0.
std::mt19937 gen;
std::normal_distribution normal1(5.0, 2.0);
for (std::size_t i = 0; i < 1000; i++) {
hist1.Fill(normal1(gen));
}
// "Draw" the histogram with ASCII characters.
std::cout << "hist1 with expected mean = " << normal1.mean() << "\n";
std::cout << "\n";
// Create a second histogram and fill with random values of a different distribution.
std::normal_distribution normal2(13.0, 4.0);
for (std::size_t i = 0; i < 1500; i++) {
hist2.Fill(normal2(gen));
}
std::cout << "hist2 with expected mean = " << normal2.mean() << "\n";
std::cout << "\n";
// Create a third, merged histogram from the two previous two.
hist3.Add(hist1);
hist3.Add(hist2);
std::cout << "hist3 with expected entries = " << (hist1.GetNEntries() + hist2.GetNEntries()) << "\n";
}
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 .
Date
September 2025
Author
The ROOT Team

Definition in file hist001_RHist_basics.C.