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