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