Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist003_RHist_multi.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_histv7
3///
4/// Multidimensional RHist with different axis types.
5///
6/// \macro_code
7/// \macro_output
8///
9/// \date January 2026
10/// \author The ROOT Team
11
12#include <ROOT/RBinIndex.hxx>
13#include <ROOT/RHist.hxx>
14#include <ROOT/RRegularAxis.hxx>
16
17#include <cmath>
18#include <cstddef>
19#include <iostream>
20#include <random>
21#include <vector>
22
24{
25 // Define the bin edges and variable bin axis for the eta dimension.
26 std::vector<double> etaEdges = {-5.0, -3.0, -2.4, -1.5, 0.0, 1.5, 2.4, 3.0, 5.0};
28
29 // Define the regular axis for the phi dimension (in radians), disabling under- and overflow bins.
30 static constexpr double Pi = 3.14159;
31 ROOT::Experimental::RRegularAxis phiAxis(20, {-Pi / 2, Pi / 2}, false);
32
33 // Define the logarithmic axis for the energy (in GeV).
35
36 // Create the multidimensional histogram.
38
39 // Print some information about the histogram.
40 std::cout << "Created a histogram with " << hist.GetNDimensions() << " dimensions and " << hist.GetTotalNBins()
41 << "\n";
42
43 // Generate angles and energies to fill the histogram.
44 std::mt19937 gen;
45 std::uniform_real_distribution dist(0.0, 1.0);
46 static constexpr double maxE = 1050;
47 for (std::size_t i = 0; i < 100000; i++) {
48 double delta = 2 * Pi * dist(gen);
49 double eta = -std::log(std::tan(delta / 2));
50 double phi = std::acos(2 * dist(gen) - 1) - Pi / 2;
51 double E = maxE * dist(gen);
52 hist.Fill(eta, phi, E);
53 }
54 std::cout << " ... filled with " << hist.GetNEntries() << " entries.\n\n";
55
56 // Get the content for specific bins:
57 std::cout << "eta in [1.5, 2.4), phi in [0, 0.157), E between 10 GeV and 100 GeV\n";
58 auto content = hist.GetBinContent(5, 10, 1);
59 std::cout << " content = " << content << "\n";
60
61 std::cout << "eta in [-1.5, 0), phi in [-0.785, -0.628), E between 1 GeV and 10 GeV\n";
62 content = hist.GetBinContent(3, 5, 0);
63 std::cout << " content = " << content << "\n";
64}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
A histogram for aggregation of data along multiple dimensions.
Definition RHist.hxx:64
A regular axis with equidistant bins in the interval .
An axis with variable bins defined by their edges.
double Pi()
Mathematical constants.
Definition Math.h:61
double dist(Rotation3D const &r1, Rotation3D const &r2)
constexpr Double_t E()
Base of natural log: .
Definition TMath.h:96