Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RAxisVariant.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_RAxisVariant
6#define ROOT_RAxisVariant
7
8#include "RBinIndex.hxx"
9#include "RBinIndexRange.hxx"
10#include "RCategoricalAxis.hxx"
11#include "RRegularAxis.hxx"
12#include "RSliceSpec.hxx"
13#include "RVariableBinAxis.hxx"
14
15#include <cstdint>
16#include <stdexcept>
17#include <utility>
18#include <variant>
19
20class TBuffer;
21
22namespace ROOT {
23namespace Experimental {
24
25/**
26A variant of all supported axis types.
27
28This class provides easy access to the contained axis object and dispatching methods for common accessors.
29
30\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
31Feedback is welcome!
32*/
34public:
35 using VariantType = std::variant<RRegularAxis, RVariableBinAxis, RCategoricalAxis>;
36
37private:
39
40public:
41 RAxisVariant(VariantType axis) : fVariant(std::move(axis)) {}
42 RAxisVariant(RRegularAxis axis) : fVariant(std::move(axis)) {}
45
46 const VariantType &GetVariant() const { return fVariant; }
47
48 /// \return the RRegularAxis or nullptr, if this variant stores a different axis type
49 const RRegularAxis *GetRegularAxis() const { return std::get_if<RRegularAxis>(&fVariant); }
50 /// \return the RVariableBinAxis or nullptr, if this variant stores a different axis type
51 const RVariableBinAxis *GetVariableBinAxis() const { return std::get_if<RVariableBinAxis>(&fVariant); }
52 /// \return the RCategoricalAxis or nullptr, if this variant stores a different axis type
53 const RCategoricalAxis *GetCategoricalAxis() const { return std::get_if<RCategoricalAxis>(&fVariant); }
54
55 std::uint64_t GetNNormalBins() const
56 {
57 if (auto *regular = GetRegularAxis()) {
58 return regular->GetNNormalBins();
59 } else if (auto *variable = GetVariableBinAxis()) {
60 return variable->GetNNormalBins();
61 } else if (auto *categorical = GetCategoricalAxis()) {
62 return categorical->GetNNormalBins();
63 } else {
64 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
65 }
66 }
67
68 std::uint64_t GetTotalNBins() const
69 {
70 if (auto *regular = GetRegularAxis()) {
71 return regular->GetTotalNBins();
72 } else if (auto *variable = GetVariableBinAxis()) {
73 return variable->GetTotalNBins();
74 } else if (auto *categorical = GetCategoricalAxis()) {
75 return categorical->GetTotalNBins();
76 } else {
77 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
78 }
79 }
80
81 /// Get the range of all normal bins.
82 ///
83 /// \return the bin index range from the first to the last normal bin, inclusive
85 {
86 if (auto *regular = GetRegularAxis()) {
87 return regular->GetNormalRange();
88 } else if (auto *variable = GetVariableBinAxis()) {
89 return variable->GetNormalRange();
90 } else if (auto *categorical = GetCategoricalAxis()) {
91 return categorical->GetNormalRange();
92 } else {
93 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
94 }
95 }
96
97 /// Get a range of normal bins.
98 ///
99 /// \param[in] begin the begin of the bin index range (inclusive), must be normal
100 /// \param[in] end the end of the bin index range (exclusive), must be normal and >= begin
101 /// \return a bin index range \f$[begin, end)\f$
103 {
104 if (auto *regular = GetRegularAxis()) {
105 return regular->GetNormalRange(begin, end);
106 } else if (auto *variable = GetVariableBinAxis()) {
107 return variable->GetNormalRange(begin, end);
108 } else if (auto *categorical = GetCategoricalAxis()) {
109 return categorical->GetNormalRange(begin, end);
110 } else {
111 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
112 }
113 }
114
115 /// Get the full range of all bins.
116 ///
117 /// This includes underflow and overflow bins, if enabled.
118 ///
119 /// \return the bin index range of all bins
121 {
122 if (auto *regular = GetRegularAxis()) {
123 return regular->GetFullRange();
124 } else if (auto *variable = GetVariableBinAxis()) {
125 return variable->GetFullRange();
126 } else if (auto *categorical = GetCategoricalAxis()) {
127 return categorical->GetFullRange();
128 } else {
129 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
130 }
131 }
132
133 /// Slice this axis according to the specification.
134 ///
135 /// Axes throw exceptions if the slicing cannot be performed. For example, the rebin operation must divide the
136 /// number of normal bins for RRegularAxis and RVariableBinAxis, while RCategoricalAxis cannot be sliced at all.
137 ///
138 /// \param[in] sliceSpec the slice specification
139 /// \return the sliced axis
141 {
142 if (auto *regular = GetRegularAxis()) {
143 return regular->Slice(sliceSpec);
144 } else if (auto *variable = GetVariableBinAxis()) {
145 return variable->Slice(sliceSpec);
146 } else if (auto *categorical = GetCategoricalAxis()) {
147 return categorical->Slice(sliceSpec);
148 } else {
149 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
150 }
151 }
152
153 friend bool operator==(const RAxisVariant &lhs, const RAxisVariant &rhs) { return lhs.fVariant == rhs.fVariant; }
154
155 /// %ROOT Streamer function to throw when trying to store an object of this class.
156 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RAxisVariant"); }
157};
158
159} // namespace Experimental
160} // namespace ROOT
161
162#endif
A variant of all supported axis types.
RAxisVariant(RCategoricalAxis axis)
friend bool operator==(const RAxisVariant &lhs, const RAxisVariant &rhs)
RBinIndexRange GetNormalRange(RBinIndex begin, RBinIndex end) const
Get a range of normal bins.
RBinIndexRange GetFullRange() const
Get the full range of all bins.
std::uint64_t GetNNormalBins() const
RAxisVariant(RVariableBinAxis axis)
void Streamer(TBuffer &)
ROOT Streamer function to throw when trying to store an object of this class.
const VariantType & GetVariant() const
const RRegularAxis * GetRegularAxis() const
const RVariableBinAxis * GetVariableBinAxis() const
std::uint64_t GetTotalNBins() const
std::variant< RRegularAxis, RVariableBinAxis, RCategoricalAxis > VariantType
RBinIndexRange GetNormalRange() const
Get the range of all normal bins.
RAxisVariant Slice(const RSliceSpec &sliceSpec) const
Slice this axis according to the specification.
const RCategoricalAxis * GetCategoricalAxis() const
A bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:23
An axis with categorical bins.
A regular axis with equidistant bins in the interval .
Specification of a slice operation along one dimension.
An axis with variable bins defined by their edges.
Buffer base class used for serializing objects.
Definition TBuffer.h:43