Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RRegularAxis.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_RRegularAxis
6#define ROOT_RRegularAxis
7
8#include "RBinIndex.hxx"
9#include "RBinIndexRange.hxx"
10#include "RLinearizedIndex.hxx"
11
12#include <cassert>
13#include <cstddef>
14#include <stdexcept>
15#include <string>
16#include <utility>
17
18class TBuffer;
19
20namespace ROOT {
21namespace Experimental {
22
23/**
24A regular axis with equidistant bins in the interval \f$[fLow, fHigh)\f$.
25
26For example, the following creates a regular axis with 10 normal bins between 5 and 15:
27\code
28ROOT::Experimental::RRegularAxis axis(10, 5, 15);
29\endcode
30
31It is possible to disable underflow and overflow bins by passing `enableFlowBins = false`. In that case, arguments
32outside the axis will be silently discarded.
33
34\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
35Feedback is welcome!
36*/
38public:
40
41private:
42 /// The number of normal bins
43 std::size_t fNNormalBins;
44 /// The lower end of the axis interval
45 double fLow;
46 /// The upper end of the axis interval
47 double fHigh;
48 /// The cached inverse of the bin width to speed up ComputeLinearizedIndex
49 double fInvBinWidth; //!
50 /// Whether underflow and overflow bins are enabled
52
53public:
54 /// Construct a regular axis object.
55 ///
56 /// \param[in] nNormalBins the number of normal bins, must be > 0
57 /// \param[in] interval the axis interval (lower end inclusive, upper end exclusive)
58 /// \param[in] enableFlowBins whether to enable underflow and overflow bins
59 RRegularAxis(std::size_t nNormalBins, std::pair<double, double> interval, bool enableFlowBins = true)
61 {
62 if (nNormalBins == 0) {
63 throw std::invalid_argument("nNormalBins must be > 0");
64 }
65 if (fLow >= fHigh) {
66 std::string msg = "high must be > low, but " + std::to_string(fLow) + " >= " + std::to_string(fHigh);
67 throw std::invalid_argument(msg);
68 }
70 }
71
72 std::size_t GetNNormalBins() const { return fNNormalBins; }
73 std::size_t GetTotalNBins() const { return fEnableFlowBins ? fNNormalBins + 2 : fNNormalBins; }
74 double GetLow() const { return fLow; }
75 double GetHigh() const { return fHigh; }
76 bool HasFlowBins() const { return fEnableFlowBins; }
77
78 friend bool operator==(const RRegularAxis &lhs, const RRegularAxis &rhs)
79 {
80 return lhs.fNNormalBins == rhs.fNNormalBins && lhs.fLow == rhs.fLow && lhs.fHigh == rhs.fHigh &&
81 lhs.fEnableFlowBins == rhs.fEnableFlowBins;
82 }
83
84 /// Compute the linarized index for a single argument.
85 ///
86 /// The normal bins have indices \f$0\f$ to \f$fNNormalBins - 1\f$, the underflow bin has index
87 /// \f$fNNormalBins\f$, and the overflow bin has index \f$fNNormalBins + 1\f$. If the argument is outside the
88 /// interval \f$[fLow, fHigh)\f$ and the flow bins are disabled, the return value is invalid.
89 ///
90 /// \param[in] x the argument
91 /// \return the linearized index that may be invalid
93 {
94 bool underflow = x < fLow;
95 // Put NaNs into overflow bin.
96 bool overflow = !(x < fHigh);
97 if (underflow) {
99 } else if (overflow) {
100 return {fNNormalBins + 1, fEnableFlowBins};
101 }
102
103 std::size_t bin = (x - fLow) * fInvBinWidth;
104 return {bin, true};
105 }
106
107 /// Get the linearized index for an RBinIndex.
108 ///
109 /// The normal bins have indices \f$0\f$ to \f$fNNormalBins - 1\f$, the underflow bin has index
110 /// \f$fNNormalBins\f$, and the overflow bin has index \f$fNNormalBins + 1\f$.
111 ///
112 /// \param[in] index the RBinIndex
113 /// \return the linearized index that may be invalid
115 {
116 if (index.IsUnderflow()) {
118 } else if (index.IsOverflow()) {
119 return {fNNormalBins + 1, fEnableFlowBins};
120 } else if (index.IsInvalid()) {
121 return {0, false};
122 }
123 assert(index.IsNormal());
124 std::size_t bin = index.GetIndex();
125 return {bin, bin < fNNormalBins};
126 }
127
128 /// Get the range of all normal bins.
129 ///
130 /// \return the bin index range from the first to the last normal bin, inclusive
135
136 /// Get a range of normal bins.
137 ///
138 /// \param[in] begin the begin of the bin index range (inclusive), must be normal
139 /// \param[in] end the end of the bin index range (exclusive), must be normal and >= begin
140 /// \return a bin index range \f$[begin, end)\f$
142 {
143 if (!begin.IsNormal()) {
144 throw std::invalid_argument("begin must be a normal bin");
145 }
146 if (begin.GetIndex() >= fNNormalBins) {
147 throw std::invalid_argument("begin must be inside the axis");
148 }
149 if (!end.IsNormal()) {
150 throw std::invalid_argument("end must be a normal bin");
151 }
152 if (end.GetIndex() > fNNormalBins) {
153 throw std::invalid_argument("end must be inside or past the axis");
154 }
155 if (!(end >= begin)) {
156 throw std::invalid_argument("end must be >= begin");
157 }
158 return Internal::CreateBinIndexRange(begin, end, 0);
159 }
160
161 /// Get the full range of all bins.
162 ///
163 /// This includes underflow and overflow bins, if enabled.
164 ///
165 /// \return the bin index range of all bins
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 RRegularAxis"); }
174};
175
176} // namespace Experimental
177} // namespace ROOT
178
179#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 bin index with special values for underflow and overflow bins.
Definition RBinIndex.hxx:22
std::size_t GetIndex() const
Return the index for a normal bin.
Definition RBinIndex.hxx:37
bool IsNormal() const
A bin index is normal iff it is not one of the special values.
Definition RBinIndex.hxx:46
static RBinIndex Underflow()
A regular axis with equidistant bins in the interval .
std::size_t GetNNormalBins() const
RBinIndexRange GetNormalRange(RBinIndex begin, RBinIndex end) const
Get a range of normal bins.
bool fEnableFlowBins
Whether underflow and overflow bins are enabled.
friend bool operator==(const RRegularAxis &lhs, const RRegularAxis &rhs)
RLinearizedIndex ComputeLinearizedIndex(double x) const
Compute the linarized index for a single argument.
RBinIndexRange GetFullRange() const
Get the full range of all bins.
std::size_t GetTotalNBins() const
double fInvBinWidth
The cached inverse of the bin width to speed up ComputeLinearizedIndex.
double fLow
The lower end of the axis interval.
RBinIndexRange GetNormalRange() const
Get the range of all normal bins.
void Streamer(TBuffer &)
ROOT Streamer function to throw when trying to store an object of this class.
double fHigh
The upper end of the axis interval.
RLinearizedIndex GetLinearizedIndex(RBinIndex index) const
Get the linearized index for an RBinIndex.
std::size_t fNNormalBins
The number of normal bins.
RRegularAxis(std::size_t nNormalBins, std::pair< double, double > interval, bool enableFlowBins=true)
Construct a regular axis object.
Buffer base class used for serializing objects.
Definition TBuffer.h:43
Double_t x[n]
Definition legend1.C:17
RBinIndexRange CreateBinIndexRange(RBinIndex begin, RBinIndex end, std::size_t nNormalBins)
Internal function to create RBinIndexRange.
A linearized index that can be invalid.