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*/
40 /// The (ordered) edges of the normal bins
41 std::vector<double> fBinEdges;
42 /// Whether underflow and overflow bins are enabled
44
45public:
46 /// Construct an axis object with variable bins.
47 ///
48 /// \param[in] binEdges the (ordered) edges of the normal bins, must define at least one bin (i.e. size >= 2)
49 /// \param[in] enableFlowBins whether to enable underflow and overflow bins
50 explicit RVariableBinAxis(std::vector<double> binEdges, bool enableFlowBins = true)
52 {
53 if (fBinEdges.size() < 2) {
54 throw std::invalid_argument("must have >= 2 bin edges");
55 }
56 for (std::size_t i = 1; i < fBinEdges.size(); i++) {
57 if (fBinEdges[i - 1] >= fBinEdges[i]) {
58 std::string msg = "binEdges must be sorted, but for bin " + std::to_string(i - 1) + ": ";
59 msg += std::to_string(fBinEdges[i - 1]) + " >= " + std::to_string(fBinEdges[i]);
60 throw std::invalid_argument(msg);
61 }
62 }
63 }
64
65 std::size_t GetNNormalBins() const { return fBinEdges.size() - 1; }
66 std::size_t GetTotalNBins() const { return fEnableFlowBins ? fBinEdges.size() + 1 : fBinEdges.size() - 1; }
67 const std::vector<double> &GetBinEdges() const { return fBinEdges; }
68 bool HasFlowBins() const { return fEnableFlowBins; }
69
71 {
72 return lhs.fBinEdges == rhs.fBinEdges && lhs.fEnableFlowBins == rhs.fEnableFlowBins;
73 }
74
75 /// Compute the linarized index for a single argument.
76 ///
77 /// The normal bins have indices \f$0\f$ to \f$fBinEdges.size() - 2\f$, the underflow bin has index
78 /// \f$fBinEdges.size() - 1\f$, and the overflow bin has index \f$fBinEdges.size()\f$. If the argument is outside all
79 /// bin edges and the flow bins are disabled, the return value is invalid.
80 ///
81 /// \param[in] x the argument
82 /// \return the linearized index that may be invalid
84 {
85 bool underflow = x < fBinEdges.front();
86 // Put NaNs into overflow bin.
87 bool overflow = !(x < fBinEdges.back());
88 if (underflow) {
89 return {fBinEdges.size() - 1, fEnableFlowBins};
90 } else if (overflow) {
91 return {fBinEdges.size(), fEnableFlowBins};
92 }
93
94 // TODO (for later): The following can be optimized with binary search...
95 for (std::size_t bin = 0; bin < fBinEdges.size() - 2; bin++) {
96 if (x < fBinEdges[bin + 1]) {
97 return {bin, true};
98 }
99 }
100 std::size_t bin = fBinEdges.size() - 2;
101 return {bin, true};
102 }
103
104 /// Get the linearized index for an RBinIndex.
105 ///
106 /// The normal bins have indices \f$0\f$ to \f$fBinEdges.size() - 2\f$, the underflow bin has index
107 /// \f$fBinEdges.size() - 1\f$, and the overflow bin has index \f$fBinEdges.size()\f$.
108 ///
109 /// \param[in] index the RBinIndex
110 /// \return the linearized index that may be invalid
112 {
113 if (index.IsUnderflow()) {
114 return {fBinEdges.size() - 1, fEnableFlowBins};
115 } else if (index.IsOverflow()) {
116 return {fBinEdges.size(), fEnableFlowBins};
117 } else if (index.IsInvalid()) {
118 return {0, false};
119 }
120 assert(index.IsNormal());
121 std::size_t bin = index.GetIndex();
122 return {bin, bin < fBinEdges.size() - 1};
123 }
124
125 /// Get the range of all normal bins.
126 ///
127 /// \return the bin index range from the first to the last normal bin, inclusive
129 {
131 }
132
133 /// Get a range of normal bins.
134 ///
135 /// \param[in] begin the begin of the bin index range (inclusive), must be normal
136 /// \param[in] end the end of the bin index range (exclusive), must be normal and >= begin
137 /// \return a bin index range \f$[begin, end)\f$
139 {
140 if (!begin.IsNormal()) {
141 throw std::invalid_argument("begin must be a normal bin");
142 }
143 if (begin.GetIndex() >= fBinEdges.size() - 1) {
144 throw std::invalid_argument("begin must be inside the axis");
145 }
146 if (!end.IsNormal()) {
147 throw std::invalid_argument("end must be a normal bin");
148 }
149 if (end.GetIndex() > fBinEdges.size() - 1) {
150 throw std::invalid_argument("end must be inside or past the axis");
151 }
152 if (!(end >= begin)) {
153 throw std::invalid_argument("end must be >= begin");
154 }
155 return Internal::CreateBinIndexRange(begin, end, 0);
156 }
157
158 /// Get the full range of all bins.
159 ///
160 /// This includes underflow and overflow bins, if enabled.
161 ///
162 /// \return the bin index range of all bins
168
169 /// %ROOT Streamer function to throw when trying to store an object of this class.
170 void Streamer(TBuffer &) { throw std::runtime_error("unable to store RVariableBinAxis"); }
171};
172
173} // namespace Experimental
174} // namespace ROOT
175
176#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.
Namespace for new ROOT classes and functions.
A linearized index that can be invalid.