Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RHistEngine.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_RHistEngine
6#define ROOT_RHistEngine
7
8#include "RAxes.hxx"
9#include "RBinIndex.hxx"
10#include "RHistUtils.hxx"
11#include "RLinearizedIndex.hxx"
12#include "RRegularAxis.hxx"
13#include "RWeight.hxx"
14
15#include <array>
16#include <cassert>
17#include <stdexcept>
18#include <tuple>
19#include <type_traits>
20#include <utility>
21#include <vector>
22
23class TBuffer;
24
25namespace ROOT {
26namespace Experimental {
27
28// forward declarations for friend declaration
29template <typename BinContentType>
30class RHistEngine;
31namespace Internal {
32template <typename T, std::size_t N>
33static void SetBinContent(RHistEngine<T> &hist, const std::array<RBinIndex, N> &indices, const T &value);
34} // namespace Internal
35
36/**
37A histogram data structure to bin data along multiple dimensions.
38
39Every call to \ref Fill(const A &... args) "Fill" bins the data according to the axis configuration and increments the
40bin content:
41\code
42ROOT::Experimental::RHistEngine<int> hist(10, {5, 15});
43hist.Fill(8.5);
44// hist.GetBinContent(ROOT::Experimental::RBinIndex(3)) will return 1
45\endcode
46
47The class is templated on the bin content type. For counting, as in the example above, it may be an integral type such
48as `int` or `long`. Narrower types such as `unsigned char` or `short` are supported, but may overflow due to their
49limited range and must be used with care. For weighted filling, the bin content type must not be an integral type, but
50a floating-point type such as `float` or `double`, or the special type RBinWithError. Note that `float` has a limited
51significand precision of 24 bits.
52
53An object can have arbitrary dimensionality determined at run-time. The axis configuration is passed as a vector of
54RAxisVariant:
55\code
56std::vector<ROOT::Experimental::RAxisVariant> axes;
57axes.push_back(ROOT::Experimental::RRegularAxis(10, 5, 15));
58axes.push_back(ROOT::Experimental::RVariableBinAxis({1, 10, 100, 1000}));
59ROOT::Experimental::RHistEngine<int> hist(axes);
60// hist.GetNDimensions() will return 2
61\endcode
62
63\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
64Feedback is welcome!
65*/
66template <typename BinContentType>
68 template <typename T, std::size_t N>
69 friend void Internal::SetBinContent(RHistEngine<T> &, const std::array<RBinIndex, N> &, const T &);
70
71 /// The axis configuration for this histogram. Relevant methods are forwarded from the public interface.
73 /// The bin contents for this histogram
74 std::vector<BinContentType> fBinContents;
75
76public:
77 /// Construct a histogram engine.
78 ///
79 /// \param[in] axes the axis objects, must have size > 0
80 explicit RHistEngine(std::vector<RAxisVariant> axes) : fAxes(std::move(axes))
81 {
83 }
84
85 /// Construct a one-dimensional histogram engine with a regular axis.
86 ///
87 /// \param[in] nNormalBins the number of normal bins, must be > 0
88 /// \param[in] interval the axis interval (lower end inclusive, upper end exclusive)
89 /// \par See also
90 /// the
91 /// \ref RRegularAxis::RRegularAxis(std::size_t nNormalBins, std::pair<double, double> interval, bool enableFlowBins)
92 /// "constructor of RRegularAxis"
93 RHistEngine(std::size_t nNormalBins, std::pair<double, double> interval)
95 {
96 }
97
98 /// The copy constructor is deleted.
99 ///
100 /// Copying all bin contents can be an expensive operation, depending on the number of bins. If required, users can
101 /// explicitly call Clone().
103 /// Efficiently move construct a histogram engine.
104 ///
105 /// After this operation, the moved-from object is invalid.
107
108 /// The copy assignment operator is deleted.
109 ///
110 /// Copying all bin contents can be an expensive operation, depending on the number of bins. If required, users can
111 /// explicitly call Clone().
113 /// Efficiently move a histogram engine.
114 ///
115 /// After this operation, the moved-from object is invalid.
117
118 ~RHistEngine() = default;
119
120 const std::vector<RAxisVariant> &GetAxes() const { return fAxes.Get(); }
121 std::size_t GetNDimensions() const { return fAxes.GetNDimensions(); }
122 std::size_t GetTotalNBins() const { return fBinContents.size(); }
123
124 /// Get the content of a single bin.
125 ///
126 /// \code
127 /// ROOT::Experimental::RHistEngine<int> hist({/* two dimensions */});
128 /// std::array<ROOT::Experimental::RBinIndex, 2> indices = {3, 5};
129 /// int content = hist.GetBinContent(indices);
130 /// \endcode
131 ///
132 /// \note Compared to TH1 conventions, the first normal bin has index 0 and underflow and overflow bins are special
133 /// values. See also the class documentation of RBinIndex.
134 ///
135 /// Throws an exception if the number of indices does not match the axis configuration or the bin is not found.
136 ///
137 /// \param[in] indices the array of indices for each axis
138 /// \return the bin content
139 /// \par See also
140 /// the \ref GetBinContent(const A &... args) const "variadic function template overload" accepting arguments
141 /// directly
142 template <std::size_t N>
143 const BinContentType &GetBinContent(const std::array<RBinIndex, N> &indices) const
144 {
145 // We could rely on RAxes::ComputeGlobalIndex to check the number of arguments, but its exception message might
146 // be confusing for users.
147 if (N != GetNDimensions()) {
148 throw std::invalid_argument("invalid number of indices passed to GetBinContent");
149 }
151 if (!index.fValid) {
152 throw std::invalid_argument("bin not found in GetBinContent");
153 }
154 assert(index.fIndex < fBinContents.size());
155 return fBinContents[index.fIndex];
156 }
157
158 /// Get the content of a single bin.
159 ///
160 /// \code
161 /// ROOT::Experimental::RHistEngine<int> hist({/* two dimensions */});
162 /// int content = hist.GetBinContent(ROOT::Experimental::RBinIndex(3), ROOT::Experimental::RBinIndex(5));
163 /// // ... or construct the RBinIndex arguments implicitly from integers:
164 /// content = hist.GetBinContent(3, 5);
165 /// \endcode
166 ///
167 /// \note Compared to TH1 conventions, the first normal bin has index 0 and underflow and overflow bins are special
168 /// values. See also the class documentation of RBinIndex.
169 ///
170 /// Throws an exception if the number of arguments does not match the axis configuration or the bin is not found.
171 ///
172 /// \param[in] args the arguments for each axis
173 /// \return the bin content
174 /// \par See also
175 /// the \ref GetBinContent(const std::array<RBinIndex, N> &indices) const "function overload" accepting
176 /// `std::array`
177 template <typename... A>
178 const BinContentType &GetBinContent(const A &...args) const
179 {
180 std::array<RBinIndex, sizeof...(A)> indices{args...};
181 return GetBinContent(indices);
182 }
183
184 /// Add all bin contents of another histogram.
185 ///
186 /// Throws an exception if the axes configurations are not identical.
187 ///
188 /// \param[in] other another histogram
190 {
191 if (fAxes != other.fAxes) {
192 throw std::invalid_argument("axes configurations not identical in Add");
193 }
194 for (std::size_t i = 0; i < fBinContents.size(); i++) {
195 fBinContents[i] += other.fBinContents[i];
196 }
197 }
198
199 /// Clear all bin contents.
200 void Clear()
201 {
202 for (std::size_t i = 0; i < fBinContents.size(); i++) {
203 fBinContents[i] = {};
204 }
205 }
206
207 /// Clone this histogram engine.
208 ///
209 /// Copying all bin contents can be an expensive operation, depending on the number of bins.
210 ///
211 /// \return the cloned object
213 {
215 for (std::size_t i = 0; i < fBinContents.size(); i++) {
216 h.fBinContents[i] = fBinContents[i];
217 }
218 return h;
219 }
220
221 /// Whether this histogram engine type supports weighted filling.
222 static constexpr bool SupportsWeightedFilling = !std::is_integral_v<BinContentType>;
223
224 /// Fill an entry into the histogram.
225 ///
226 /// \code
227 /// ROOT::Experimental::RHistEngine<int> hist({/* two dimensions */});
228 /// auto args = std::make_tuple(8.5, 10.5);
229 /// hist.Fill(args);
230 /// \endcode
231 ///
232 /// If one of the arguments is outside the corresponding axis and flow bins are disabled, the entry will be silently
233 /// discarded.
234 ///
235 /// Throws an exception if the number of arguments does not match the axis configuration, or if an argument cannot be
236 /// converted for the axis type at run-time.
237 ///
238 /// \param[in] args the arguments for each axis
239 /// \par See also
240 /// the \ref Fill(const A &... args) "variadic function template overload" accepting arguments directly and the
241 /// \ref Fill(const std::tuple<A...> &args, RWeight weight) "overload for weighted filling"
242 template <typename... A>
243 void Fill(const std::tuple<A...> &args)
244 {
245 // We could rely on RAxes::ComputeGlobalIndex to check the number of arguments, but its exception message might
246 // be confusing for users.
247 if (sizeof...(A) != GetNDimensions()) {
248 throw std::invalid_argument("invalid number of arguments to Fill");
249 }
251 if (index.fValid) {
252 assert(index.fIndex < fBinContents.size());
253 fBinContents[index.fIndex]++;
254 }
255 }
256
257 /// Fill an entry into the histogram with a weight.
258 ///
259 /// This overload is not available for integral bin content types (see \ref SupportsWeightedFilling).
260 ///
261 /// \code
262 /// ROOT::Experimental::RHistEngine<float> hist({/* two dimensions */});
263 /// auto args = std::make_tuple(8.5, 10.5);
264 /// hist.Fill(args, ROOT::Experimental::RWeight(0.8));
265 /// \endcode
266 ///
267 /// If one of the arguments is outside the corresponding axis and flow bins are disabled, the entry will be silently
268 /// discarded.
269 ///
270 /// Throws an exception if the number of arguments does not match the axis configuration, or if an argument cannot be
271 /// converted for the axis type at run-time.
272 ///
273 /// \param[in] args the arguments for each axis
274 /// \param[in] weight the weight for this entry
275 /// \par See also
276 /// the \ref Fill(const A &... args) "variadic function template overload" accepting arguments directly and the
277 /// \ref Fill(const std::tuple<A...> &args) "overload for unweighted filling"
278 template <typename... A>
279 void Fill(const std::tuple<A...> &args, RWeight weight)
280 {
281 static_assert(SupportsWeightedFilling, "weighted filling is not supported for integral bin content types");
282
283 // We could rely on RAxes::ComputeGlobalIndex to check the number of arguments, but its exception message might
284 // be confusing for users.
285 if (sizeof...(A) != GetNDimensions()) {
286 throw std::invalid_argument("invalid number of arguments to Fill");
287 }
289 if (index.fValid) {
290 assert(index.fIndex < fBinContents.size());
291 fBinContents[index.fIndex] += weight.fValue;
292 }
293 }
294
295 /// Fill an entry into the histogram.
296 ///
297 /// \code
298 /// ROOT::Experimental::RHistEngine<int> hist({/* two dimensions */});
299 /// hist.Fill(8.5, 10.5);
300 /// \endcode
301 ///
302 /// For weighted filling, pass an RWeight as the last argument:
303 /// \code
304 /// ROOT::Experimental::RHistEngine<float> hist({/* two dimensions */});
305 /// hist.Fill(8.5, 10.5, ROOT::Experimental::RWeight(0.8));
306 /// \endcode
307 /// This is not available for integral bin content types (see \ref SupportsWeightedFilling).
308 ///
309 /// If one of the arguments is outside the corresponding axis and flow bins are disabled, the entry will be silently
310 /// discarded.
311 ///
312 /// Throws an exception if the number of arguments does not match the axis configuration, or if an argument cannot be
313 /// converted for the axis type at run-time.
314 ///
315 /// \param[in] args the arguments for each axis
316 /// \par See also
317 /// the function overloads accepting `std::tuple` \ref Fill(const std::tuple<A...> &args) "for unweighted filling"
318 /// and \ref Fill(const std::tuple<A...> &args, RWeight) "for weighted filling"
319 template <typename... A>
320 void Fill(const A &...args)
321 {
322 auto t = std::forward_as_tuple(args...);
323 if constexpr (std::is_same_v<typename Internal::LastType<A...>::type, RWeight>) {
324 static_assert(SupportsWeightedFilling, "weighted filling is not supported for integral bin content types");
325 static constexpr std::size_t N = sizeof...(A) - 1;
326 if (N != fAxes.GetNDimensions()) {
327 throw std::invalid_argument("invalid number of arguments to Fill");
328 }
329 RWeight weight = std::get<N>(t);
331 if (index.fValid) {
332 assert(index.fIndex < fBinContents.size());
333 fBinContents[index.fIndex] += weight.fValue;
334 }
335 } else {
336 Fill(t);
337 }
338 }
339
340 /// Fill an entry into the histogram using atomic instructions.
341 ///
342 /// \param[in] args the arguments for each axis
343 /// \see Fill(const std::tuple<A...> &args)
344 template <typename... A>
345 void FillAtomic(const std::tuple<A...> &args)
346 {
347 // We could rely on RAxes::ComputeGlobalIndex to check the number of arguments, but its exception message might
348 // be confusing for users.
349 if (sizeof...(A) != GetNDimensions()) {
350 throw std::invalid_argument("invalid number of arguments to Fill");
351 }
353 if (index.fValid) {
354 assert(index.fIndex < fBinContents.size());
356 }
357 }
358
359 /// Fill an entry into the histogram with a weight using atomic instructions.
360 ///
361 /// This overload is not available for integral bin content types (see \ref SupportsWeightedFilling).
362 ///
363 /// \param[in] args the arguments for each axis
364 /// \param[in] weight the weight for this entry
365 /// \see Fill(const std::tuple<A...> &args, RWeight weight)
366 template <typename... A>
367 void FillAtomic(const std::tuple<A...> &args, RWeight weight)
368 {
369 static_assert(SupportsWeightedFilling, "weighted filling is not supported for integral bin content types");
370
371 // We could rely on RAxes::ComputeGlobalIndex to check the number of arguments, but its exception message might
372 // be confusing for users.
373 if (sizeof...(A) != GetNDimensions()) {
374 throw std::invalid_argument("invalid number of arguments to Fill");
375 }
377 if (index.fValid) {
378 assert(index.fIndex < fBinContents.size());
380 }
381 }
382
383 /// Fill an entry into the histogram using atomic instructions.
384 ///
385 /// \param[in] args the arguments for each axis
386 /// \see Fill(const A &...args)
387 template <typename... A>
388 void FillAtomic(const A &...args)
389 {
390 auto t = std::forward_as_tuple(args...);
391 if constexpr (std::is_same_v<typename Internal::LastType<A...>::type, RWeight>) {
392 static_assert(SupportsWeightedFilling, "weighted filling is not supported for integral bin content types");
393 static constexpr std::size_t N = sizeof...(A) - 1;
394 if (N != fAxes.GetNDimensions()) {
395 throw std::invalid_argument("invalid number of arguments to Fill");
396 }
397 RWeight weight = std::get<N>(t);
399 if (index.fValid) {
400 assert(index.fIndex < fBinContents.size());
402 }
403 } else {
404 FillAtomic(t);
405 }
406 }
407
408 /// Scale all histogram bin contents.
409 ///
410 /// This method is not available for integral bin content types.
411 ///
412 /// \param[in] factor the scale factor
413 void Scale(double factor)
414 {
415 static_assert(!std::is_integral_v<BinContentType>, "scaling is not supported for integral bin content types");
416 for (std::size_t i = 0; i < fBinContents.size(); i++) {
417 fBinContents[i] *= factor;
418 }
419 }
420
421 /// %ROOT Streamer function to throw when trying to store an object of this class.
422 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RHistEngine"); }
423};
424
425namespace Internal {
426/// %Internal function to set the content of a single bin.
427template <typename T, std::size_t N>
428static void SetBinContent(RHistEngine<T> &hist, const std::array<RBinIndex, N> &indices, const T &value)
429{
430 RLinearizedIndex index = hist.fAxes.ComputeGlobalIndex(indices);
431 if (!index.fValid) {
432 throw std::invalid_argument("bin not found in SetBinContent");
433 }
434 assert(index.fIndex < hist.fBinContents.size());
435 hist.fBinContents[index.fIndex] = value;
436}
437} // namespace Internal
438
439} // namespace Experimental
440} // namespace ROOT
441
442#endif
#define h(i)
Definition RSha256.hxx:106
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define N
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
Bin configurations for all dimensions of a histogram.
Definition RAxes.hxx:41
std::size_t GetNDimensions() const
Definition RAxes.hxx:56
RLinearizedIndex ComputeGlobalIndexImpl(std::size_t index, const std::tuple< A... > &args) const
Definition RAxes.hxx:86
RLinearizedIndex ComputeGlobalIndex(const std::tuple< A... > &args) const
Compute the global index for all axes.
Definition RAxes.hxx:140
std::size_t ComputeTotalNBins() const
Compute the total number of bins for all axes.
Definition RAxes.hxx:67
const std::vector< RAxisVariant > & Get() const
Definition RAxes.hxx:57
A bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:22
A histogram data structure to bin data along multiple dimensions.
void Fill(const A &...args)
Fill an entry into the histogram.
const std::vector< RAxisVariant > & GetAxes() const
RHistEngine< BinContentType > Clone() const
Clone this histogram engine.
RHistEngine< BinContentType > & operator=(const RHistEngine< BinContentType > &)=delete
The copy assignment operator is deleted.
void Scale(double factor)
Scale all histogram bin contents.
std::size_t GetTotalNBins() const
void Fill(const std::tuple< A... > &args)
Fill an entry into the histogram.
void FillAtomic(const std::tuple< A... > &args)
Fill an entry into the histogram using atomic instructions.
RHistEngine< BinContentType > & operator=(RHistEngine< BinContentType > &&)=default
Efficiently move a histogram engine.
const BinContentType & GetBinContent(const std::array< RBinIndex, N > &indices) const
Get the content of a single bin.
const BinContentType & GetBinContent(const A &...args) const
Get the content of a single bin.
RHistEngine(const RHistEngine< BinContentType > &)=delete
The copy constructor is deleted.
void Add(const RHistEngine< BinContentType > &other)
Add all bin contents of another histogram.
std::size_t GetNDimensions() const
void FillAtomic(const std::tuple< A... > &args, RWeight weight)
Fill an entry into the histogram with a weight using atomic instructions.
static constexpr bool SupportsWeightedFilling
Whether this histogram engine type supports weighted filling.
void Clear()
Clear all bin contents.
RHistEngine(std::vector< RAxisVariant > axes)
Construct a histogram engine.
void FillAtomic(const A &...args)
Fill an entry into the histogram using atomic instructions.
Internal::RAxes fAxes
The axis configuration for this histogram. Relevant methods are forwarded from the public interface.
RHistEngine(std::size_t nNormalBins, std::pair< double, double > interval)
Construct a one-dimensional histogram engine with a regular axis.
void Fill(const std::tuple< A... > &args, RWeight weight)
Fill an entry into the histogram with a weight.
RHistEngine(RHistEngine< BinContentType > &&)=default
Efficiently move construct a histogram engine.
void Streamer(TBuffer &)
ROOT Streamer function to throw when trying to store an object of this class.
std::vector< BinContentType > fBinContents
The bin contents for this histogram.
A regular axis with equidistant bins in the interval .
Buffer base class used for serializing objects.
Definition TBuffer.h:43
std::enable_if_t< std::is_arithmetic_v< T > > AtomicInc(T *ptr)
static void SetBinContent(RHistEngine< T > &hist, const std::array< RBinIndex, N > &indices, const T &value)
Internal function to set the content of a single bin.
std::enable_if_t< std::is_integral_v< T > > AtomicAdd(T *ptr, T val)
A linearized index that can be invalid.
A weight for filling histograms.
Definition RWeight.hxx:17