Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RVariableBinAxis.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_RVariableBinAxis
6#define ROOT_RVariableBinAxis
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#include <vector>
18
19class TBuffer;
20
21namespace ROOT {
22namespace Experimental {
23
24/**
25An axis with variable bins defined by their edges.
26
27For example, the following creates an axis with 3 log-spaced bins:
28\code
29std::vector<double> binEdges = {1, 10, 100, 1000};
30ROOT::Experimental::RVariableBinAxis axis(binEdges);
31\endcode
32
33It is possible to disable underflow and overflow bins by passing `enableFlowBins = false`. In that case, arguments
34outside the axis will be silently discarded.
35
36\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
37Feedback is welcome!
38*/
40public:
42
43private:
44 /// The (ordered) edges of the normal bins
45 std::vector<double> fBinEdges;
46 /// Whether underflow and overflow bins are enabled
48
49public:
50 /// Construct an axis object with variable bins.
51 ///
52 /// \param[in] binEdges the (ordered) edges of the normal bins, must define at least one bin (i.e. size >= 2)
53 /// \param[in] enableFlowBins whether to enable underflow and overflow bins
54 explicit RVariableBinAxis(std::vector<double> binEdges, bool enableFlowBins = true)
56 {
57 if (fBinEdges.size() < 2) {
58 throw std::invalid_argument("must have >= 2 bin edges");
59 }
60 for (std::size_t i = 1; i < fBinEdges.size(); i++) {
61 if (fBinEdges[i - 1] >= fBinEdges[i]) {
62 std::string msg = "binEdges must be sorted, but for bin " + std::to_string(i - 1) + ": ";
63 msg += std::to_string(fBinEdges[i - 1]) + " >= " + std::to_string(fBinEdges[i]);
64 throw std::invalid_argument(msg);
65 }
66 }
67 }
68
69 std::size_t GetNNormalBins() const { return fBinEdges.size() - 1; }
70 std::size_t GetTotalNBins() const { return fEnableFlowBins ? fBinEdges.size() + 1 : fBinEdges.size() - 1; }
71 const std::vector<double> &GetBinEdges() const { return fBinEdges; }
72 bool HasFlowBins() const { return fEnableFlowBins; }
73
75 {
76 return lhs.fBinEdges == rhs.fBinEdges && lhs.fEnableFlowBins == rhs.fEnableFlowBins;
77 }
78
79 /// Compute the linarized index for a single argument.
80 ///
81 /// The normal bins have indices \f$0\f$ to \f$fBinEdges.size() - 2\f$, the underflow bin has index
82 /// \f$fBinEdges.size() - 1\f$, and the overflow bin has index \f$fBinEdges.size()\f$. If the argument is outside all
83 /// bin edges and the flow bins are disabled, the return value is invalid.
84 ///
85 /// \param[in] x the argument
86 /// \return the linearized index that may be invalid
88 {
89 bool underflow = x < fBinEdges.front();
90 // Put NaNs into overflow bin.
91 bool overflow = !(x < fBinEdges.back());
92 if (underflow) {
93 return {fBinEdges.size() - 1, fEnableFlowBins};
94 } else if (overflow) {
95 return {fBinEdges.size(), fEnableFlowBins};
96 }
97
98 // TODO (for later): The following can be optimized with binary search...
99 for (std::size_t bin = 0; bin < fBinEdges.size() - 2; bin++) {
100 if (x < fBinEdges[bin + 1]) {
101 return {bin, true};
102 }
103 }
104 std::size_t bin = fBinEdges.size() - 2;
105 return {bin, true};
106 }
107
108 /// Get the linearized index for an RBinIndex.
109 ///
110 /// The normal bins have indices \f$0\f$ to \f$fBinEdges.size() - 2\f$, the underflow bin has index
111 /// \f$fBinEdges.size() - 1\f$, and the overflow bin has index \f$fBinEdges.size()\f$.
112 ///
113 /// \param[in] index the RBinIndex
114 /// \return the linearized index that may be invalid
116 {
117 if (index.IsUnderflow()) {
118 return {fBinEdges.size() - 1, fEnableFlowBins};
119 } else if (index.IsOverflow()) {
120 return {fBinEdges.size(), fEnableFlowBins};
121 } else if (index.IsInvalid()) {
122 return {0, false};
123 }
124 assert(index.IsNormal());
125 std::size_t bin = index.GetIndex();
126 return {bin, bin < fBinEdges.size() - 1};
127 }
128
129 /// Get the range of all normal bins.
130 ///
131 /// \return the bin index range from the first to the last normal bin, inclusive
133 {
135 }
136
137 /// Get a range of normal bins.
138 ///
139 /// \param[in] begin the begin of the bin index range (inclusive), must be normal
140 /// \param[in] end the end of the bin index range (exclusive), must be normal and >= begin
141 /// \return a bin index range \f$[begin, end)\f$
143 {
144 if (!begin.IsNormal()) {
145 throw std::invalid_argument("begin must be a normal bin");
146 }
147 if (begin.GetIndex() >= fBinEdges.size() - 1) {
148 throw std::invalid_argument("begin must be inside the axis");
149 }
150 if (!end.IsNormal()) {
151 throw std::invalid_argument("end must be a normal bin");
152 }
153 if (end.GetIndex() > fBinEdges.size() - 1) {
154 throw std::invalid_argument("end must be inside or past the axis");
155 }
156 if (!(end >= begin)) {
157 throw std::invalid_argument("end must be >= begin");
158 }
159 return Internal::CreateBinIndexRange(begin, end, 0);
160 }
161
162 /// Get the full range of all bins.
163 ///
164 /// This includes underflow and overflow bins, if enabled.
165 ///
166 /// \return the bin index range of all bins
172
173 /// %ROOT Streamer function to throw when trying to store an object of this class.
174 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RVariableBinAxis"); }
175};
176
177} // namespace Experimental
178} // namespace ROOT
179
180#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()
An axis with variable bins defined by their edges.
RLinearizedIndex GetLinearizedIndex(RBinIndex index) const
Get the linearized index for an RBinIndex.
RBinIndexRange GetNormalRange(RBinIndex begin, RBinIndex end) const
Get a range of normal bins.
RLinearizedIndex ComputeLinearizedIndex(double x) const
Compute the linarized index for a single argument.
RBinIndexRange GetFullRange() const
Get the full range of all bins.
bool fEnableFlowBins
Whether underflow and overflow bins are enabled.
void Streamer(TBuffer &)
ROOT Streamer function to throw when trying to store an object of this class.
std::vector< double > fBinEdges
The (ordered) edges of the normal bins.
RVariableBinAxis(std::vector< double > binEdges, bool enableFlowBins=true)
Construct an axis object with variable bins.
friend bool operator==(const RVariableBinAxis &lhs, const RVariableBinAxis &rhs)
const std::vector< double > & GetBinEdges() const
RBinIndexRange GetNormalRange() const
Get the range of all 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.
A linearized index that can be invalid.