|
| 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. |
| 21 | +static 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. |
| 41 | + print(ROOT::Experimental::RBinIndex::Underflow()); |
| 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 << '|'; |
| 51 | + print(ROOT::Experimental::RBinIndex::Overflow()); |
| 52 | + std::cout << "\n"; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void hist001_RHist_basics() |
| 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. |
| 62 | + ROOT::Experimental::RHist<int> hist1({axis}); |
| 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"; |
| 73 | + DrawHistogram(hist1); |
| 74 | + std::cout << "\n"; |
| 75 | + |
| 76 | + // Create a second histogram and fill with random values of a different distribution. |
| 77 | + ROOT::Experimental::RHist<int> hist2({axis}); |
| 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"; |
| 83 | + DrawHistogram(hist2); |
| 84 | + std::cout << "\n"; |
| 85 | + |
| 86 | + // Create a third, merged histogram from the two previous two. |
| 87 | + ROOT::Experimental::RHist<int> hist3({axis}); |
| 88 | + hist3.Add(hist1); |
| 89 | + hist3.Add(hist2); |
| 90 | + std::cout << "hist3 with expected entries = " << (hist1.GetNEntries() + hist2.GetNEntries()) << "\n"; |
| 91 | + DrawHistogram(hist3); |
| 92 | +} |
0 commit comments