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