Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RAxes.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_RAxes
6#define ROOT_RAxes
7
8#include "RBinIndex.hxx"
10#include "RRegularAxis.hxx"
11#include "RVariableBinAxis.hxx"
12
13#include <array>
14#include <cassert>
15#include <stdexcept>
16#include <tuple>
17#include <utility>
18#include <variant>
19#include <vector>
20
21class TBuffer;
22
23namespace ROOT {
24namespace Experimental {
25
26/// Variant of all supported axis types.
27using RAxisVariant = std::variant<RRegularAxis, RVariableBinAxis>;
28
29namespace Internal {
30
31/**
32Bin configurations for all dimensions of a histogram.
33*/
34class RAxes final {
35 std::vector<RAxisVariant> fAxes;
36
37public:
38 /// \param[in] axes the axis objects, must have size > 0
39 explicit RAxes(std::vector<RAxisVariant> axes) : fAxes(std::move(axes))
40 {
41 if (fAxes.empty()) {
42 throw std::invalid_argument("must have at least 1 axis object");
43 }
44 }
45
46 std::size_t GetNDimensions() const { return fAxes.size(); }
47 const std::vector<RAxisVariant> &Get() const { return fAxes; }
48
49 friend bool operator==(const RAxes &lhs, const RAxes &rhs) { return lhs.fAxes == rhs.fAxes; }
50
51 /// Compute the total number of bins for all axes.
52 ///
53 /// It is the product of each dimension's total number of bins.
54 ///
55 /// \return the total number of bins
56 std::size_t ComputeTotalNBins() const
57 {
58 std::size_t totalNBins = 1;
59 for (auto &&axis : fAxes) {
60 if (auto *regular = std::get_if<RRegularAxis>(&axis)) {
61 totalNBins *= regular->GetTotalNBins();
62 } else if (auto *variable = std::get_if<RVariableBinAxis>(&axis)) {
63 totalNBins *= variable->GetTotalNBins();
64 } else {
65 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
66 }
67 }
68 return totalNBins;
69 }
70
71private:
72 template <std::size_t I, typename... A>
73 RLinearizedIndex ComputeGlobalIndex(std::size_t index, const std::tuple<A...> &args) const
74 {
75 const auto &axis = fAxes[I];
77 if (auto *regular = std::get_if<RRegularAxis>(&axis)) {
78 index *= regular->GetTotalNBins();
79 linIndex = regular->ComputeLinearizedIndex(std::get<I>(args));
80 } else if (auto *variable = std::get_if<RVariableBinAxis>(&axis)) {
81 index *= variable->GetTotalNBins();
82 linIndex = variable->ComputeLinearizedIndex(std::get<I>(args));
83 } else {
84 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
85 }
86 if (!linIndex.fValid) {
87 return {0, false};
88 }
89 index += linIndex.fIndex;
90 if constexpr (I + 1 < sizeof...(A)) {
91 return ComputeGlobalIndex<I + 1>(index, args);
92 }
93 return {index, true};
94 }
95
96public:
97 /// Compute the global index for all axes.
98 ///
99 /// \param[in] args the arguments
100 /// \return the global index that may be invalid
101 template <typename... A>
102 RLinearizedIndex ComputeGlobalIndex(const std::tuple<A...> &args) const
103 {
104 if (sizeof...(A) != fAxes.size()) {
105 throw std::invalid_argument("invalid number of arguments to ComputeGlobalIndex");
106 }
107 return ComputeGlobalIndex<0, A...>(0, args);
108 }
109
110 /// Compute the global index for all axes.
111 ///
112 /// \param[in] indices the array of RBinIndex
113 /// \return the global index that may be invalid
114 template <std::size_t N>
115 RLinearizedIndex ComputeGlobalIndex(const std::array<RBinIndex, N> &indices) const
116 {
117 if (N != fAxes.size()) {
118 throw std::invalid_argument("invalid number of indices passed to ComputeGlobalIndex");
119 }
120 std::size_t globalIndex = 0;
121 for (std::size_t i = 0; i < N; i++) {
122 const auto &index = indices[i];
123 const auto &axis = fAxes[i];
125 if (auto *regular = std::get_if<RRegularAxis>(&axis)) {
126 globalIndex *= regular->GetTotalNBins();
127 linIndex = regular->GetLinearizedIndex(index);
128 } else if (auto *variable = std::get_if<RVariableBinAxis>(&axis)) {
129 globalIndex *= variable->GetTotalNBins();
130 linIndex = variable->GetLinearizedIndex(index);
131 } else {
132 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
133 }
134 if (!linIndex.fValid) {
135 return {0, false};
136 }
137 globalIndex += linIndex.fIndex;
138 }
139 return {globalIndex, true};
140 }
141
142 /// %ROOT Streamer function to throw when trying to store an object of this class.
143 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RAxes"); }
144};
145
146} // namespace Internal
147} // namespace Experimental
148} // namespace ROOT
149
150#endif
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
Bin configurations for all dimensions of a histogram.
Definition RAxes.hxx:34
friend bool operator==(const RAxes &lhs, const RAxes &rhs)
Definition RAxes.hxx:49
std::size_t GetNDimensions() const
Definition RAxes.hxx:46
RLinearizedIndex ComputeGlobalIndex(const std::tuple< A... > &args) const
Compute the global index for all axes.
Definition RAxes.hxx:102
std::size_t ComputeTotalNBins() const
Compute the total number of bins for all axes.
Definition RAxes.hxx:56
RAxes(std::vector< RAxisVariant > axes)
Definition RAxes.hxx:39
void Streamer(TBuffer &)
ROOT Streamer function to throw when trying to store an object of this class.
Definition RAxes.hxx:143
RLinearizedIndex ComputeGlobalIndex(const std::array< RBinIndex, N > &indices) const
Compute the global index for all axes.
Definition RAxes.hxx:115
RLinearizedIndex ComputeGlobalIndex(std::size_t index, const std::tuple< A... > &args) const
Definition RAxes.hxx:73
std::vector< RAxisVariant > fAxes
Definition RAxes.hxx:35
const std::vector< RAxisVariant > & Get() const
Definition RAxes.hxx:47
Buffer base class used for serializing objects.
Definition TBuffer.h:43
#define I(x, y, z)
std::variant< RRegularAxis, RVariableBinAxis > RAxisVariant
Variant of all supported axis types.
Definition RAxes.hxx:27
Namespace for new ROOT classes and functions.
A linearized index that can be invalid.