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