Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RHist.hxx
Go to the documentation of this file.
1/// \file
2/// \warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
3/// Feedback is welcome!
4
5#ifndef ROOT_RHist
6#define ROOT_RHist
7
8#include "RAxes.hxx" // for RAxisVariant
9#include "RBinIndex.hxx"
10#include "RHistEngine.hxx"
11#include "RHistStats.hxx"
12#include "RRegularAxis.hxx"
13#include "RWeight.hxx"
14
15#include <array>
16#include <cstddef>
17#include <cstdint>
18#include <stdexcept>
19#include <tuple>
20#include <utility>
21#include <vector>
22
23class TBuffer;
24
25namespace ROOT {
26namespace Experimental {
27
28// forward declaration for friend declaration
29template <typename BinContentType>
30class RHistFillContext;
31
32/**
33A histogram for aggregation of data along multiple dimensions.
34
35Every call to \ref Fill(const A &... args) "Fill" increments the bin content and is reflected in global statistics:
36\code
37ROOT::Experimental::RHist<int> hist(10, {5, 15});
38hist.Fill(8.5);
39// hist.GetBinContent(ROOT::Experimental::RBinIndex(3)) will return 1
40\endcode
41
42The class is templated on the bin content type. For counting, as in the example above, it may be an integral type such
43as `int` or `long`. Narrower types such as `unsigned char` or `short` are supported, but may overflow due to their
44limited range and must be used with care. For weighted filling, the bin content type must not be an integral type, but
45a floating-point type such as `float` or `double`, or the special type RBinWithError. Note that `float` has a limited
46significand precision of 24 bits.
47
48An object can have arbitrary dimensionality determined at run-time. The axis configuration is passed as a vector of
49RAxisVariant:
50\code
51std::vector<ROOT::Experimental::RAxisVariant> axes;
52axes.push_back(ROOT::Experimental::RRegularAxis(10, 5, 15));
53axes.push_back(ROOT::Experimental::RVariableBinAxis({1, 10, 100, 1000}));
54ROOT::Experimental::RHist<int> hist(axes);
55// hist.GetNDimensions() will return 2
56\endcode
57
58\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
59Feedback is welcome!
60*/
61template <typename BinContentType>
62class RHist final {
63 friend class RHistFillContext<BinContentType>;
64
65 /// The histogram engine including the bin contents.
67 /// The global histogram statistics.
69
70 /// Private constructor based off an engine.
72
73public:
74 /// Construct a histogram.
75 ///
76 /// \param[in] axes the axis objects, must have size > 0
77 explicit RHist(std::vector<RAxisVariant> axes) : fEngine(std::move(axes)), fStats(fEngine.GetNDimensions()) {}
78
79 /// Construct a one-dimensional histogram engine with a regular axis.
80 ///
81 /// \param[in] nNormalBins the number of normal bins, must be > 0
82 /// \param[in] interval the axis interval (lower end inclusive, upper end exclusive)
83 /// \par See also
84 /// the \ref RRegularAxis::RRegularAxis(std::uint64_t nNormalBins, std::pair<double, double> interval, bool
85 /// enableFlowBins) "constructor of RRegularAxis"
86 RHist(std::uint64_t nNormalBins, std::pair<double, double> interval) : RHist({RRegularAxis(nNormalBins, interval)})
87 {
88 }
89
90 /// The copy constructor is deleted.
91 ///
92 /// Copying all bin contents can be an expensive operation, depending on the number of bins. If required, users can
93 /// explicitly call Clone().
94 RHist(const RHist &) = delete;
95 /// Efficiently move construct a histogram.
96 ///
97 /// After this operation, the moved-from object is invalid.
98 RHist(RHist &&) = default;
99
100 /// The copy assignment operator is deleted.
101 ///
102 /// Copying all bin contents can be an expensive operation, depending on the number of bins. If required, users can
103 /// explicitly call Clone().
104 RHist &operator=(const RHist &) = delete;
105 /// Efficiently move a histogram.
106 ///
107 /// After this operation, the moved-from object is invalid.
108 RHist &operator=(RHist &&) = default;
109
110 ~RHist() = default;
111
113 const RHistStats &GetStats() const { return fStats; }
114
115 const std::vector<RAxisVariant> &GetAxes() const { return fEngine.GetAxes(); }
116 std::size_t GetNDimensions() const { return fEngine.GetNDimensions(); }
117 std::uint64_t GetTotalNBins() const { return fEngine.GetTotalNBins(); }
118
119 std::uint64_t GetNEntries() const { return fStats.GetNEntries(); }
120 /// \copydoc RHistStats::ComputeNEffectiveEntries()
122 /// \copydoc RHistStats::ComputeMean()
123 double ComputeMean(std::size_t dim = 0) const { return fStats.ComputeMean(dim); }
124 /// \copydoc RHistStats::ComputeStdDev()
125 double ComputeStdDev(std::size_t dim = 0) const { return fStats.ComputeStdDev(dim); }
126
127 /// Get the content of a single bin.
128 ///
129 /// \code
130 /// ROOT::Experimental::RHist<int> hist({/* two dimensions */});
131 /// std::array<ROOT::Experimental::RBinIndex, 2> indices = {3, 5};
132 /// int content = hist.GetBinContent(indices);
133 /// \endcode
134 ///
135 /// \note Compared to TH1 conventions, the first normal bin has index 0 and underflow and overflow bins are special
136 /// values. See also the class documentation of RBinIndex.
137 ///
138 /// Throws an exception if the number of indices does not match the axis configuration or the bin is not found.
139 ///
140 /// \param[in] indices the array of indices for each axis
141 /// \return the bin content
142 /// \par See also
143 /// the \ref GetBinContent(const A &... args) const "variadic function template overload" accepting arguments
144 /// directly
145 template <std::size_t N>
146 const BinContentType &GetBinContent(const std::array<RBinIndex, N> &indices) const
147 {
148 return fEngine.GetBinContent(indices);
149 }
150
151 /// Get the content of a single bin.
152 ///
153 /// \code
154 /// ROOT::Experimental::RHist<int> hist({/* two dimensions */});
155 /// int content = hist.GetBinContent(ROOT::Experimental::RBinIndex(3), ROOT::Experimental::RBinIndex(5));
156 /// // ... or construct the RBinIndex arguments implicitly from integers:
157 /// content = hist.GetBinContent(3, 5);
158 /// \endcode
159 ///
160 /// \note Compared to TH1 conventions, the first normal bin has index 0 and underflow and overflow bins are special
161 /// values. See also the class documentation of RBinIndex.
162 ///
163 /// Throws an exception if the number of arguments does not match the axis configuration or the bin is not found.
164 ///
165 /// \param[in] args the arguments for each axis
166 /// \return the bin content
167 /// \par See also
168 /// the \ref GetBinContent(const std::array<RBinIndex, N> &indices) const "function overload" accepting
169 /// `std::array`
170 template <typename... A>
171 const BinContentType &GetBinContent(const A &...args) const
172 {
173 return fEngine.GetBinContent(args...);
174 }
175
176 /// Add all bin contents and statistics of another histogram.
177 ///
178 /// Throws an exception if the axes configurations are not identical.
179 ///
180 /// \param[in] other another histogram
181 void Add(const RHist &other)
182 {
183 fEngine.Add(other.fEngine);
184 fStats.Add(other.fStats);
185 }
186
187 /// Add all bin contents and statistics of another histogram using atomic instructions.
188 ///
189 /// Throws an exception if the axes configurations are not identical.
190 ///
191 /// \param[in] other another histogram that must not be modified during the operation
192 void AddAtomic(const RHist &other)
193 {
194 fEngine.AddAtomic(other.fEngine);
195 fStats.AddAtomic(other.fStats);
196 }
197
198 /// Clear all bin contents and statistics.
199 void Clear()
200 {
201 fEngine.Clear();
202 fStats.Clear();
203 }
204
205 /// Clone this histogram.
206 ///
207 /// Copying all bin contents can be an expensive operation, depending on the number of bins.
208 ///
209 /// \return the cloned object
210 RHist Clone() const
211 {
212 RHist h(fEngine.Clone());
213 h.fStats = fStats;
214 return h;
215 }
216
217 /// Fill an entry into the histogram.
218 ///
219 /// \code
220 /// ROOT::Experimental::RHist<int> hist({/* two dimensions */});
221 /// auto args = std::make_tuple(8.5, 10.5);
222 /// hist.Fill(args);
223 /// \endcode
224 ///
225 /// If one of the arguments is outside the corresponding axis and flow bins are disabled, the entry will be silently
226 /// discarded.
227 ///
228 /// Throws an exception if the number of arguments does not match the axis configuration, or if an argument cannot be
229 /// converted for the axis type at run-time.
230 ///
231 /// \param[in] args the arguments for each axis
232 /// \par See also
233 /// the \ref Fill(const A &... args) "variadic function template overload" accepting arguments directly and the
234 /// \ref Fill(const std::tuple<A...> &args, RWeight weight) "overload for weighted filling"
235 template <typename... A>
236 void Fill(const std::tuple<A...> &args)
237 {
238 fEngine.Fill(args);
239 fStats.Fill(args);
240 }
241
242 /// Fill an entry into the histogram with a weight.
243 ///
244 /// This overload is not available for integral bin content types (see \ref RHistEngine::SupportsWeightedFilling).
245 ///
246 /// \code
247 /// ROOT::Experimental::RHist<float> hist({/* two dimensions */});
248 /// auto args = std::make_tuple(8.5, 10.5);
249 /// hist.Fill(args, ROOT::Experimental::RWeight(0.8));
250 /// \endcode
251 ///
252 /// If one of the arguments is outside the corresponding axis and flow bins are disabled, the entry will be silently
253 /// discarded.
254 ///
255 /// Throws an exception if the number of arguments does not match the axis configuration, or if an argument cannot be
256 /// converted for the axis type at run-time.
257 ///
258 /// \param[in] args the arguments for each axis
259 /// \param[in] weight the weight for this entry
260 /// \par See also
261 /// the \ref Fill(const A &... args) "variadic function template overload" accepting arguments directly and the
262 /// \ref Fill(const std::tuple<A...> &args) "overload for unweighted filling"
263 template <typename... A>
264 void Fill(const std::tuple<A...> &args, RWeight weight)
265 {
266 fEngine.Fill(args, weight);
267 fStats.Fill(args, weight);
268 }
269
270 /// Fill an entry into the histogram.
271 ///
272 /// \code
273 /// ROOT::Experimental::RHist<int> hist({/* two dimensions */});
274 /// hist.Fill(8.5, 10.5);
275 /// \endcode
276 ///
277 /// For weighted filling, pass an RWeight as the last argument:
278 /// \code
279 /// ROOT::Experimental::RHist<float> hist({/* two dimensions */});
280 /// hist.Fill(8.5, 10.5, ROOT::Experimental::RWeight(0.8));
281 /// \endcode
282 /// This is not available for integral bin content types (see \ref RHistEngine::SupportsWeightedFilling).
283 ///
284 /// If one of the arguments is outside the corresponding axis and flow bins are disabled, the entry will be silently
285 /// discarded.
286 ///
287 /// Throws an exception if the number of arguments does not match the axis configuration, or if an argument cannot be
288 /// converted for the axis type at run-time.
289 ///
290 /// \param[in] args the arguments for each axis
291 /// \par See also
292 /// the function overloads accepting `std::tuple` \ref Fill(const std::tuple<A...> &args) "for unweighted filling"
293 /// and \ref Fill(const std::tuple<A...> &args, RWeight) "for weighted filling"
294 template <typename... A>
295 void Fill(const A &...args)
296 {
297 fEngine.Fill(args...);
298 fStats.Fill(args...);
299 }
300
301 /// Scale all histogram bin contents and statistics.
302 ///
303 /// This method is not available for integral bin content types.
304 ///
305 /// \param[in] factor the scale factor
306 void Scale(double factor)
307 {
308 fEngine.Scale(factor);
309 fStats.Scale(factor);
310 }
311
312 /// %ROOT Streamer function to throw when trying to store an object of this class.
313 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RHist"); }
314};
315
316} // namespace Experimental
317} // namespace ROOT
318
319#endif
#define h(i)
Definition RSha256.hxx:106
A context to concurrently fill an RHist.
Histogram statistics of unbinned values.
void Add(const RHistStats &other)
Add all entries from another statistics object.
void Clear()
Clear this statistics object.
void AddAtomic(const RHistStats &other)
Add all entries from another statistics object using atomic instructions.
void Scale(double factor)
Scale the histogram statistics.
void Fill(const std::tuple< A... > &args)
Fill an entry into this statistics object.
double ComputeNEffectiveEntries() const
Compute the number of effective entries.
std::uint64_t GetNEntries() const
double ComputeMean(std::size_t dim=0) const
Compute the arithmetic mean of unbinned values.
double ComputeStdDev(std::size_t dim=0) const
Compute the standard deviation of unbinned values.
A histogram for aggregation of data along multiple dimensions.
Definition RHist.hxx:62
RHist(const RHist &)=delete
The copy constructor is deleted.
double ComputeMean(std::size_t dim=0) const
Compute the arithmetic mean of unbinned values.
Definition RHist.hxx:123
RHist(std::uint64_t nNormalBins, std::pair< double, double > interval)
Construct a one-dimensional histogram engine with a regular axis.
Definition RHist.hxx:86
std::size_t GetNDimensions() const
Definition RHist.hxx:116
RHist Clone() const
Clone this histogram.
Definition RHist.hxx:210
RHist & operator=(RHist &&)=default
Efficiently move a histogram.
void Scale(double factor)
Scale all histogram bin contents and statistics.
Definition RHist.hxx:306
void Fill(const std::tuple< A... > &args)
Fill an entry into the histogram.
Definition RHist.hxx:236
RHist & operator=(const RHist &)=delete
The copy assignment operator is deleted.
RHistStats fStats
The global histogram statistics.
Definition RHist.hxx:68
void AddAtomic(const RHist &other)
Add all bin contents and statistics of another histogram using atomic instructions.
Definition RHist.hxx:192
void Fill(const std::tuple< A... > &args, RWeight weight)
Fill an entry into the histogram with a weight.
Definition RHist.hxx:264
RHistEngine< BinContentType > fEngine
The histogram engine including the bin contents.
Definition RHist.hxx:66
double ComputeNEffectiveEntries() const
Compute the number of effective entries.
Definition RHist.hxx:121
std::uint64_t GetTotalNBins() const
Definition RHist.hxx:117
RHist(std::vector< RAxisVariant > axes)
Construct a histogram.
Definition RHist.hxx:77
const RHistStats & GetStats() const
Definition RHist.hxx:113
const RHistEngine< BinContentType > & GetEngine() const
Definition RHist.hxx:112
void Streamer(TBuffer &)
ROOT Streamer function to throw when trying to store an object of this class.
Definition RHist.hxx:313
void Fill(const A &...args)
Fill an entry into the histogram.
Definition RHist.hxx:295
double ComputeStdDev(std::size_t dim=0) const
Compute the standard deviation of unbinned values.
Definition RHist.hxx:125
RHist(RHist &&)=default
Efficiently move construct a histogram.
const BinContentType & GetBinContent(const std::array< RBinIndex, N > &indices) const
Get the content of a single bin.
Definition RHist.hxx:146
const std::vector< RAxisVariant > & GetAxes() const
Definition RHist.hxx:115
void Add(const RHist &other)
Add all bin contents and statistics of another histogram.
Definition RHist.hxx:181
RHist(RHistEngine< BinContentType > engine)
Private constructor based off an engine.
Definition RHist.hxx:71
const BinContentType & GetBinContent(const A &...args) const
Get the content of a single bin.
Definition RHist.hxx:171
void Clear()
Clear all bin contents and statistics.
Definition RHist.hxx:199
std::uint64_t GetNEntries() const
Definition RHist.hxx:119
A regular axis with equidistant bins in the interval .
Buffer base class used for serializing objects.
Definition TBuffer.h:43
A weight for filling histograms.
Definition RWeight.hxx:17