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
17class TBuffer;
18
19namespace ROOT {
20namespace Experimental {
21
22/**
23A regular axis with equidistant bins in the interval \f$[fLow, fHigh)\f$.
24
25For example, the following creates a regular axis with 10 normal bins between 5 and 15:
26\code
27ROOT::Experimental::RRegularAxis axis(10, 5, 15);
28\endcode
29
30It is possible to disable underflow and overflow bins by passing `enableFlowBins = false`. In that case, arguments
31outside the axis will be silently discarded.
32
33\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
34Feedback is welcome!
35*/
37 /// The number of normal bins
38 std::size_t fNNormalBins;
39 /// The lower end of the axis interval
40 double fLow;
41 /// The upper end of the axis interval
42 double fHigh;
43 /// The cached inverse of the bin width to speed up ComputeLinearizedIndex
44 double fInvBinWidth; //!
45 /// Whether underflow and overflow bins are enabled
47
48public:
49 /// Construct a regular axis object.
50 ///
51 /// \param[in] nNormalBins the number of normal bins, must be > 0
52 /// \param[in] low the lower end of the axis interval (inclusive)
53 /// \param[in] high the upper end of the axis interval (exclusive), must be > low
54 /// \param[in] enableFlowBins whether to enable underflow and overflow bins
55 RRegularAxis(std::size_t nNormalBins, double low, double high, bool enableFlowBins = true)
57 {
58 if (nNormalBins == 0) {
59 throw std::invalid_argument("nNormalBins must be > 0");
60 }
61 if (low >= high) {
62 std::string msg = "high must be > low, but " + std::to_string(low) + " >= " + std::to_string(high);
63 throw std::invalid_argument(msg);
64 }
65 fInvBinWidth = nNormalBins / (high - low);
66 }
67
68 std::size_t GetNNormalBins() const { return fNNormalBins; }
69 std::size_t GetTotalNBins() const { return fEnableFlowBins ? fNNormalBins + 2 : fNNormalBins; }
70 double GetLow() const { return fLow; }
71 double GetHigh() const { return fHigh; }
72 bool HasFlowBins() const { return fEnableFlowBins; }
73
74 friend bool operator==(const RRegularAxis &lhs, const RRegularAxis &rhs)
75 {
76 return lhs.fNNormalBins == rhs.fNNormalBins && lhs.fLow == rhs.fLow && lhs.fHigh == rhs.fHigh &&
77 lhs.fEnableFlowBins == rhs.fEnableFlowBins;
78 }
79
80 /// Compute the linarized index for a single argument.
81 ///
82 /// The normal bins have indices \f$0\f$ to \f$fNNormalBins - 1\f$, the underflow bin has index
83 /// \f$fNNormalBins\f$, and the overflow bin has index \f$fNNormalBins + 1\f$. If the argument is outside the
84 /// interval \f$[fLow, fHigh)\f$ and the flow bins are disabled, the return value is invalid.
85 ///
86 /// \param[in] x the argument
87 /// \return the linearized index that may be invalid
89 {
90 bool underflow = x < fLow;
91 // Put NaNs into overflow bin.
92 bool overflow = !(x < fHigh);
93 if (underflow) {
95 } else if (overflow) {
96 return {fNNormalBins + 1, fEnableFlowBins};
97 }
98
99 std::size_t bin = (x - fLow) * fInvBinWidth;
100 return {bin, true};
101 }
102
103 /// Get the linearized index for an RBinIndex.
104 ///
105 /// The normal bins have indices \f$0\f$ to \f$fNNormalBins - 1\f$, the underflow bin has index
106 /// \f$fNNormalBins\f$, and the overflow bin has index \f$fNNormalBins + 1\f$.
107 ///
108 /// \param[in] index the RBinIndex
109 /// \return the linearized index that may be invalid
111 {
112 if (index.IsUnderflow()) {
114 } else if (index.IsOverflow()) {
115 return {fNNormalBins + 1, fEnableFlowBins};
116 } else if (index.IsInvalid()) {
117 return {0, false};
118 }
119 assert(index.IsNormal());
120 std::size_t bin = index.GetIndex();
121 return {bin, bin < fNNormalBins};
122 }
123
124 /// Get the range of all normal bins.
125 ///
126 /// \return the bin index range from the first to the last normal bin, inclusive
131
132 /// Get a range of normal bins.
133 ///
134 /// \param[in] begin the begin of the bin index range (inclusive), must be normal
135 /// \param[in] end the end of the bin index range (exclusive), must be normal and >= begin
136 /// \return a bin index range \f$[begin, end)\f$
138 {
139 if (!begin.IsNormal()) {
140 throw std::invalid_argument("begin must be a normal bin");
141 }
142 if (begin.GetIndex() >= fNNormalBins) {
143 throw std::invalid_argument("begin must be inside the axis");
144 }
145 if (!end.IsNormal()) {
146 throw std::invalid_argument("end must be a normal bin");
147 }
148 if (end.GetIndex() > fNNormalBins) {
149 throw std::invalid_argument("end must be inside or past the axis");
150 }
151 if (!(end >= begin)) {
152 throw std::invalid_argument("end must be >= begin");
153 }
154 return Internal::CreateBinIndexRange(begin, end, 0);
155 }
156
157 /// Get the full range of all bins.
158 ///
159 /// This includes underflow and overflow bins, if enabled.
160 ///
161 /// \return the bin index range of all bins
167
168 /// %ROOT Streamer function to throw when trying to store an object of this class.
169 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RRegularAxis"); }
170};
171
172} // namespace Experimental
173} // namespace ROOT
174
175#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
RRegularAxis(std::size_t nNormalBins, double low, double high, bool enableFlowBins=true)
Construct a regular axis object.
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.
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.
Namespace for new ROOT classes and functions.
A linearized index that can be invalid.