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

Detailed Description

Weighted filling of RHist and RBinWithError bin content type.

#include <ROOT/RHist.hxx>
#include <ROOT/RWeight.hxx>
#include <cstddef>
#include <iostream>
#include <random>
#include <variant>
// It is currently not possible to directly draw an RHist, 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 = 100;
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.
ROOT::Experimental::RRegularAxis axis(40, {0.0, 20.0});
// Create two histograms, one of which will be filled with weighted entries.
// Create a normal distribution with mean 10.0 and stddev 5.0.
std::mt19937 gen;
std::normal_distribution normal(10.0, 5.0);
for (std::size_t i = 0; i < 25000; i++) {
hist1.Fill(normal(gen));
double value = normal(gen);
double weight = 0.2 + 0.008 * value * value;
}
// "Draw" the histograms with ASCII characters.
std::cout << "hist1 with expected mean = " << normal.mean() << "\n";
std::cout << "\n";
std::cout << "hist2 with distorted normal distribution\n";
std::cout << "\n";
// Create and fill a third histogram with the special RBinWithError bin content type.
// In addition to the sum of weights, it tracks the sum of weights squared to compute the bin errors.
for (std::size_t i = 0; i < 25000; i++) {
double value = normal(gen);
double weight = 0.2 + 0.008 * value * value;
}
// Visualize the computed bin errors with ASCII characters.
std::cout << "bin errors of hist3 (not to scale)\n";
for (int row = 15; row > 0; row--) {
auto print = [&](ROOT::Experimental::RBinIndex bin) {
auto error = std::sqrt(hist3.GetBinContent(bin).fSum2);
static constexpr int Scale = 5;
std::cout << (error >= (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";
}
}
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
hist1 with expected mean = 10
entries = 25000, mean = 9.99652, stddev = 4.99767
| |
| |
| |
| |
| |
| * |
| ******** |
| ************ |
| ***************** |
| ******************** |
*| ************************ |*
*| *************************** |*
*| ******************************** |*
*| *********************************** |*
*|****************************************|*
hist2 with distorted normal distribution
entries = 25000, mean = 13.2862, stddev = 4.69151
| |*
| |*
| * |*
| ******* |*
| ********** |*
| ************** |*
| *************** |*
| ***************** |*
| ******************** |*
| ************************ |*
| **************************|*
| ***************************|*
| *****************************|*
| *******************************|*
*| **********************************|*
bin errors of hist3 (not to scale)
| |*
| |*
| |*
| |*
| |*
| * * |*
| ************ * |*
| *************** |*
| *******************|*
| *********************|*
| ***********************|*
| ************************|*
| ***************************|*
| ******************************|*
*| **********************************|*
Date
October 2025
Author
The ROOT Team

Definition in file hist002_RHist_weighted.C.