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 "RLinearizedIndex.hxx"
12#include "RRegularAxis.hxx"
13#include "RSliceSpec.hxx"
14#include "RVariableBinAxis.hxx"
15
16#include <cstdint>
17#include <stdexcept>
18#include <utility>
19#include <variant>
20
21class TBuffer;
22
23namespace ROOT {
24namespace Experimental {
25
26/**
27A variant of all supported axis types.
28
29This class provides easy access to the contained axis object and dispatching methods for common accessors.
30
31\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
32Feedback is welcome!
33*/
34class RAxisVariant final {
35public:
36 using VariantType = std::variant<RRegularAxis, RVariableBinAxis, RCategoricalAxis>;
37
38private:
40
41public:
42 RAxisVariant(VariantType axis) : fVariant(std::move(axis)) {}
43 RAxisVariant(RRegularAxis axis) : fVariant(std::move(axis)) {}
44 RAxisVariant(RVariableBinAxis axis) : fVariant(std::move(axis)) {}
45 RAxisVariant(RCategoricalAxis axis) : fVariant(std::move(axis)) {}
46
47 const VariantType &GetVariant() const { return fVariant; }
48
49 /// \return the RRegularAxis or nullptr, if this variant stores a different axis type
50 const RRegularAxis *GetRegularAxis() const { return std::get_if<RRegularAxis>(&fVariant); }
51 /// \return the RVariableBinAxis or nullptr, if this variant stores a different axis type
52 const RVariableBinAxis *GetVariableBinAxis() const { return std::get_if<RVariableBinAxis>(&fVariant); }
53 /// \return the RCategoricalAxis or nullptr, if this variant stores a different axis type
54 const RCategoricalAxis *GetCategoricalAxis() const { return std::get_if<RCategoricalAxis>(&fVariant); }
55
56 std::uint64_t GetNNormalBins() const
57 {
58 if (auto *regular = GetRegularAxis()) {
59 return regular->GetNNormalBins();
60 } else if (auto *variable = GetVariableBinAxis()) {
61 return variable->GetNNormalBins();
62 } else if (auto *categorical = GetCategoricalAxis()) {
63 return categorical->GetNNormalBins();
64 } else {
65 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
66 }
67 }
68
69 std::uint64_t GetTotalNBins() const
70 {
71 if (auto *regular = GetRegularAxis()) {
72 return regular->GetTotalNBins();
73 } else if (auto *variable = GetVariableBinAxis()) {
74 return variable->GetTotalNBins();
75 } else if (auto *categorical = GetCategoricalAxis()) {
76 return categorical->GetTotalNBins();
77 } else {
78 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
79 }
80 }
81
82 /// \return true if the bin index is valid
84 {
85 RLinearizedIndex linIndex;
86 if (auto *regular = GetRegularAxis()) {
87 linIndex = regular->GetLinearizedIndex(index);
88 } else if (auto *variable = GetVariableBinAxis()) {
89 linIndex = variable->GetLinearizedIndex(index);
90 } else if (auto *categorical = GetCategoricalAxis()) {
91 linIndex = categorical->GetLinearizedIndex(index);
92 } else {
93 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
94 }
95 return linIndex.fValid;
96 }
97
98 /// Get the range of all normal bins.
99 ///
100 /// \return the bin index range from the first to the last normal bin, inclusive
102 {
103 if (auto *regular = GetRegularAxis()) {
104 return regular->GetNormalRange();
105 } else if (auto *variable = GetVariableBinAxis()) {
106 return variable->GetNormalRange();
107 } else if (auto *categorical = GetCategoricalAxis()) {
108 return categorical->GetNormalRange();
109 } else {
110 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
111 }
112 }
113
114 /// Get a range of normal bins.
115 ///
116 /// \param[in] begin the begin of the bin index range (inclusive), must be normal
117 /// \param[in] end the end of the bin index range (exclusive), must be normal and >= begin
118 /// \return a bin index range \f$[begin, end)\f$
120 {
121 if (auto *regular = GetRegularAxis()) {
122 return regular->GetNormalRange(begin, end);
123 } else if (auto *variable = GetVariableBinAxis()) {
124 return variable->GetNormalRange(begin, end);
125 } else if (auto *categorical = GetCategoricalAxis()) {
126 return categorical->GetNormalRange(begin, end);
127 } else {
128 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
129 }
130 }
131
132 /// Get the full range of all bins.
133 ///
134 /// This includes underflow and overflow bins, if enabled.
135 ///
136 /// \return the bin index range of all bins
138 {
139 if (auto *regular = GetRegularAxis()) {
140 return regular->GetFullRange();
141 } else if (auto *variable = GetVariableBinAxis()) {
142 return variable->GetFullRange();
143 } else if (auto *categorical = GetCategoricalAxis()) {
144 return categorical->GetFullRange();
145 } else {
146 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
147 }
148 }
149
150 /// Slice this axis according to the specification.
151 ///
152 /// Axes throw exceptions if the slicing cannot be performed. For example, the rebin operation must divide the
153 /// number of normal bins for RRegularAxis and RVariableBinAxis, while RCategoricalAxis cannot be sliced at all.
154 ///
155 /// \param[in] sliceSpec the slice specification
156 /// \return the sliced axis
157 RAxisVariant Slice(const RSliceSpec &sliceSpec) const
158 {
159 if (auto *regular = GetRegularAxis()) {
160 return regular->Slice(sliceSpec);
161 } else if (auto *variable = GetVariableBinAxis()) {
162 return variable->Slice(sliceSpec);
163 } else if (auto *categorical = GetCategoricalAxis()) {
164 return categorical->Slice(sliceSpec);
165 } else {
166 throw std::logic_error("unimplemented axis type"); // GCOVR_EXCL_LINE
167 }
168 }
169
170 friend bool operator==(const RAxisVariant &lhs, const RAxisVariant &rhs) { return lhs.fVariant == rhs.fVariant; }
171
172 /// %ROOT Streamer function to throw when trying to store an object of this class.
173 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RAxisVariant"); }
174};
175
176} // namespace Experimental
177} // namespace ROOT
178
179#endif
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
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
bool IsValidIndex(RBinIndex index) 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
A linearized index that can be invalid.