20 #include <initializer_list> 22 #include <unordered_map> 25 #include "RStringView.h" 29 namespace Experimental {
74 if (rawbin < 0)
return 0;
91 public std::iterator<std::random_access_iterator_tag,
93 const int * , const int & > {
157 const int *
operator*() const noexcept {
return &fCursor; }
183 TAxisBase(std::string_view title,
int nbinsNoOver,
bool canGrow) noexcept:
268 return lhs.fCursor < rhs.fCursor;
274 return lhs.fCursor > rhs.fCursor;
280 return lhs.fCursor <= rhs.fCursor;
286 return lhs.fCursor >= rhs.fCursor;
292 return lhs.fCursor == rhs.fCursor;
298 return lhs.fCursor != rhs.fCursor;
330 TAxisBase(title, nbins, kind == kGrow), fKind(kind), fBinBorders(2)
335 fBinBorders[0] = from;
351 TAxisConfig(title, nbins, from, to, kEquidistant) {}
367 TAxisConfig(std::string_view title,
const std::vector<double> &binborders):
368 TAxisBase(title, binborders.size() - 1, false ), fKind(kIrregular),
369 fBinBorders(binborders) { }
376 TAxisConfig(std::string_view title, std::vector<double> &&binborders) noexcept:
377 TAxisBase(title, binborders.size() - 1,
false ), fKind(kIrregular),
378 fBinBorders(std::move(binborders)) { }
385 TAxisConfig(std::string_view title,
const std::vector<std::string_view> &labels):
388 fLabels(labels.
begin(), labels.
end()) { }
395 TAxisConfig(std::string_view title, std::vector<std::string> &&labels):
398 fLabels(
std::move(labels)) { }
408 const std::vector<double> &
GetBinBorders() const noexcept {
return fBinBorders; }
411 const std::vector <std::string> &
GetBinLabels() const noexcept {
return fLabels; }
424 double fInvBinWidth = 0.;
430 static double GetInvBinWidth(
int nbinsNoOver,
double lowOrHigh,
double highOrLow) {
431 return nbinsNoOver / std::abs(highOrLow - lowOrHigh);
443 double high,
bool canGrow) noexcept:
444 TAxisBase(title, nbinsNoOver, canGrow), fLow(low),
445 fInvBinWidth(GetInvBinWidth(nbinsNoOver, low, high)) { }
454 explicit TAxisEquidistant(
int nbinsNoOver,
double low,
double high,
bool canGrow) noexcept:
480 double high) noexcept:
490 int rawbin = (
x - fLow) * fInvBinWidth;
495 static bool CanGrow() noexcept {
return false; }
515 return fLow + (bin - 0.5) / fInvBinWidth;
522 return fLow + (bin - 1) / fInvBinWidth;
529 return GetBinFrom(bin + 1);
532 int GetBinIndexForLowEdge(
double x)
const noexcept;
539 return lhs.GetNBins() == rhs.GetNBins()
540 && lhs.GetMinimum() == rhs.GetMinimum()
541 && lhs.GetInverseBinWidth() == rhs.GetInverseBinWidth();
561 double high) noexcept:
636 TAxisBase(binborders.size() - 1, CanGrow()), fBinBorders(binborders) {
637 #ifdef R__DO_RANGE_CHECKS 638 if (!std::is_sorted(fBinBorders.begin(), fBinBorders.end()))
640 #endif // R__DO_RANGE_CHECKS 648 TAxisBase(binborders.size() - 1, CanGrow()), fBinBorders(std::move(binborders)) {
649 #ifdef R__DO_RANGE_CHECKS 650 if (!std::is_sorted(fBinBorders.begin(), fBinBorders.end()))
652 #endif // R__DO_RANGE_CHECKS 657 explicit TAxisIrregular(std::string_view title,
const std::vector<double> &binborders):
658 TAxisBase(title, binborders.size() - 1, CanGrow()), fBinBorders(binborders) {
659 #ifdef R__DO_RANGE_CHECKS 660 if (!std::is_sorted(fBinBorders.begin(), fBinBorders.end()))
662 #endif // R__DO_RANGE_CHECKS 669 explicit TAxisIrregular(std::string_view title, std::vector<double> &&binborders) noexcept:
670 TAxisBase(title, binborders.size() - 1, CanGrow()), fBinBorders(std::move(binborders)) {
671 #ifdef R__DO_RANGE_CHECKS 672 if (!std::is_sorted(fBinBorders.begin(), fBinBorders.end()))
674 #endif // R__DO_RANGE_CHECKS 684 const auto bBegin = fBinBorders.begin();
685 const auto bEnd = fBinBorders.end();
686 auto iNotLess = std::lower_bound(bBegin, bEnd,
x);
687 int rawbin = iNotLess - bBegin;
706 return std::numeric_limits<double>::min();
708 return std::numeric_limits<double>::max();
709 return 0.5 * (fBinBorders[bin - 1] + fBinBorders[bin]);
719 return std::numeric_limits<double>::min();
722 return fBinBorders[bin - 1];
732 return std::numeric_limits<double>::max();
733 return GetBinFrom(bin + 1);
737 static bool CanGrow() noexcept {
return false; }
740 const std::vector<double> &
GetBinBorders() const noexcept {
return fBinBorders; }
768 explicit TAxisLabels(std::string_view title,
const std::vector<std::string_view> &labels):
769 TAxisGrow(title, labels.size(), 0., static_cast<double>(labels.size())) {
770 for (
size_t i = 0,
n = labels.size(); i <
n; ++i)
771 fLabelsIndex[std::string(labels[i])] = i;
775 explicit TAxisLabels(std::string_view title,
const std::vector<std::string> &labels):
776 TAxisGrow(title, labels.size(), 0., static_cast<double>(labels.size())) {
777 for (
size_t i = 0,
n = labels.size(); i <
n; ++i)
778 fLabelsIndex[labels[i]] = i;
782 explicit TAxisLabels(
const std::vector<std::string_view> &labels):
791 auto insertResult = fLabelsIndex.insert({label, -1});
792 if (insertResult.second) {
794 int idx = fLabelsIndex.size() - 1;
795 insertResult.first->second = idx;
798 return insertResult.first->second;
803 return GetBinIndex(label) - 0.5;
808 std::vector <std::string_view> vec(fLabelsIndex.size());
809 for (
const auto &kv: fLabelsIndex)
810 vec.at(kv.second) = kv.first;
820 template<TAxisConfig::EKind>
829 cfg.GetBinBorders()[0], cfg.GetBinBorders()[1]);
838 return TAxisGrow(cfg.GetTitle(), cfg.GetNBinsNoOver(),
839 cfg.GetBinBorders()[0], cfg.GetBinBorders()[1]);
898 double GetFrom()
const {
return GetBinFrom(1); }
double GetFrom() const
Get the lower axis limit.
TAxisView(const TAxisEquidistant &equi)
Construct a view on a TAxisEquidistant, TAxisGrow or TAxisLabel.
TAxisBase(std::string_view title, int nbinsNoOver, bool canGrow) noexcept
Construct a TAxisBase.
TAxisConfig(std::vector< double > &&binborders) noexcept
Represents a TAxisIrregular with binborders.
const_iterator operator+(int d) noexcept
Namespace for new ROOT classes and functions.
TAxisLabels(std::string_view title, const std::vector< std::string > &labels)
Construct a TAxisLables from a vector of strings, with title.
TAxisBase & operator=(const TAxisBase &)=default
const_iterator begin() const noexcept
Get a const_iterator pointing to the first non-underflow bin.
double GetMinimum() const noexcept
Get the low end of the axis range.
double GetBinCenter(int i) const noexcept
Get the bin center of bin index i. Forwards to the underlying axis.
TAxisConfig(const std::vector< double > &binborders)
Represents a TAxisIrregular with binborders.
const_iterator operator++(int) noexcept
i++
void swap(TDirectoryEntry &e1, TDirectoryEntry &e2) noexcept
double GetBinTo(int bin) const noexcept
Get the higher bin border for a given bin index.
TAxisLabels(const std::vector< std::string > &labels)
Construct a TAxisLables from a vector of strings.
const std::vector< double > & GetBinBorders() const noexcept
Get the bin borders; non-empty if the GetKind() == kIrregular.
Axis_t operator()(const TAxisConfig &cfg)
const_iterator & operator--() noexcept
–i
double GetInverseBinWidth() const noexcept
Get the inverse of the width of the bins.
TAxisEquidistant(std::string_view title, int nbinsNoOver, double low, double high, bool canGrow) noexcept
Initialize a TAxisEquidistant.
double GetBinTo(int bin) const noexcept
Get the high bin border for the given bin index.
int FindBin(double x) const noexcept
Find the bin containing coordinate x. Forwards to the underlying axis.
Common view on a TAxis, no matter what its kind.
int GetNBinsNoOver() const noexcept
Get the number of bins, excluding under- and overflow.
Axis with equidistant bin borders.
double GetBinFrom(int bin) const noexcept
Get the lower bin border for a given bin index.
double GetTo() const
Get the upper axis limit.
TAxisConfig(Grow_t, int nbins, double from, double to)
Represents a TAxisGrow with nbins from from to to.
EFindStatus
Status of FindBin(x)
std::vector< double > fBinBorders
Bin borders of the TAxisIrregular.
const int * operator*() const noexcept
int AdjustOverflowBinNumber(int rawbin) const
Given rawbin (<0 for underflow, >= GetNBinsNoOver() for overflow), determine the actual bin number ta...
int FindBin(double x) const noexcept
Find the bin index corresponding to coordinate x.
const_iterator end_with_overflow() const noexcept
Get a const_iterator pointing right beyond the overflow bin.
The bins of the source axis have finer granularity, but the bin borders are compatible.
std::vector< double > fBinBorders
Bin borders, one more than the number of non-overflow bins.
static constexpr const int kNOverflowBins[4]
Extra bins for each EAxisOverflow value.
double GetBinFrom(int i) const noexcept
Get the minimal coordinate of bin index i. Forwards to the underlying axis.
represents a TAxisEquidistant
const_iterator begin_with_underflow() const noexcept
Get a const_iterator pointing the underflow bin.
int GetUnderflowBin() const noexcept
Get the bin index for the underflow bin.
const std::vector< double > & GetBinBorders() const noexcept
Access to the bin borders used by this axis.
const TAxisIrregular * GetAsIrregular() const
Get the axis as a TAxisIrregular; returns nullptr if it's a TAxisEquidistant.
TAxisEquidistant(int nbinsNoOver, double low, double high) noexcept
Initialize a TAxisEquidistant.
An axis that can extend its range, keeping the number of its bins unchanged.
EAxisCompatibility CanMap(TAxisEquidistant &target, TAxisEquidistant &source) noexcept
Whether (and how) the source axis can be merged into the target axis.
double GetBinFrom(int bin) const noexcept
Get the low bin border for the given bin index.
TAxisConfig(const std::vector< std::string_view > &labels)
Represents a TAxisLabels with labels.
bool operator>=(TAxisBase::const_iterator lhs, TAxisBase::const_iterator rhs) noexcept
i >= j
TAxisIrregular(std::string_view title, std::vector< double > &&binborders) noexcept
Construct a TAxisIrregular from a vector of bin borders.
double GetBinCenter(const std::string &label)
Get the center of the bin with label.
TAxisBase(int nbinsNoOver, bool canGrow) noexcept
Construct a TAxisBase.
bool operator<(TAxisBase::const_iterator lhs, TAxisBase::const_iterator rhs) noexcept
i < j
const std::vector< std::string > & GetBinLabels() const noexcept
Get the bin labels; non-empty if the GetKind() == kLabels.
EKind fKind
The kind of axis represented by this configuration.
TAxisConfig(std::string_view title, int nbins, double from, double to)
Represents a TAxisEquidistant with nbins from from to to, and axis title.
Objects used to configure the different axis types.
unsigned int fNBins
Number of bins including under- and overflow.
TAxisConfig(std::string_view title, const std::vector< double > &binborders)
Represents a TAxisIrregular with binborders and title.
TAxisGrow(std::string_view title, int nbins, double low, double high) noexcept
Initialize a TAxisGrow.
Random const_iterator through bins.
TAxisIrregular(std::string_view title, const std::vector< double > &binborders)
Construct a TAxisIrregular from a vector of bin borders.
double GetBinCenter(int bin) const noexcept
Get the bin center for the given bin index.
TAxisConfig(int nbins, double from, double to)
Represents a TAxisEquidistant with nbins from from to to.
int GetNOverflowBins() const noexcept
Get the number of over- and underflow bins: 0 for growable axes, 2 otherwise.
const_iterator & operator++() noexcept
++i
const_iterator operator--(int) noexcept
represents a TAxisIrregular
Axis_t operator()(const TAxisConfig &cfg)
TAxisConfig(std::string_view title, int nbins, double from, double to, EKind kind)
Represents a TAxisEquidistant with nbins from from to to, and axis title.
int GetBinIndex(const std::string &label)
Get the bin index with label.
Tag type signalling that an axis should be able to grow; used for calling the appropriate constructor...
static constexpr const int kIgnoreBin
FindBin() returns this bin to signal that the bin number is invalid.
TAxisConfig(std::vector< std::string > &&labels)
Represents a TAxisLabels with labels.
bool operator==(TAxisBase::const_iterator lhs, TAxisBase::const_iterator rhs) noexcept
i == j
int GetNBins() const noexcept
Get the number of bins, including under- and overflow.
int GetOverflowBin() const noexcept
Get the bin index for the underflow bin (or the next bin outside range if CanGrow()).
std::vector< std::string_view > GetBinLabels() const
Build a vector of labels. The position in the vector defines the label's bin.
bool operator>(TAxisBase::const_iterator lhs, TAxisBase::const_iterator rhs) noexcept
i > j
The returned bin index is valid.
TAxisEquidistant(int nbinsNoOver, double low, double high, bool canGrow) noexcept
Initialize a TAxisEquidistant.
TAxisConfig(std::string_view title, std::vector< double > &&binborders) noexcept
Represents a TAxisIrregular with binborders and title.
std::string fTitle
Title of this axis, used for graphics / text.
TAxisGrow(int nbins, double low, double high) noexcept
Initialize a TAxisGrow.
bool CanGrow() const
This axis kind can increase its range.
const_iterator operator-(int d) noexcept
An axis with non-equidistant bins (also known as "variable binning").
TAxisView(const TAxisIrregular &irr)
Construct a view on a TAxisIrregular.
int operator->() const noexcept
bool IsUnderflowBin(int bin) const noexcept
Whether the bin index is referencing a bin lower than the axis range.
bool operator!=(TAxisBase::const_iterator lhs, TAxisBase::const_iterator rhs) noexcept
i != j
TAxisEquidistant(std::string_view title, int nbinsNoOver, double low, double high) noexcept
Initialize a TAxisEquidistant.
const TAxisEquidistant * GetAsEquidistant() const
Get the axis as a TAxisEquidistant; returns nullptr if it's a TAxisIrregular.
TAxisBase()
Default construct a TAxisBase (for use by derived classes for I/O)
double GetBinTo(int i) const noexcept
Get the maximal coordinate of bin index i. Forwards to the underlying axis.
static bool CanGrow() noexcept
This axis cannot be extended.
EKind GetKind() const noexcept
Get the axis kind represented by this TAxisConfig.
static double GetInvBinWidth(int nbinsNoOver, double lowOrHigh, double highOrLow)
Determine the inverse bin width.
The source is a subset of bins of the target axis.
TAxisLabels(const std::vector< std::string_view > &labels)
Construct a TAxisLables from a vector of string_views.
const_iterator(int cursor) noexcept
Initialize a const_iterator with its position.
const std::string & GetTitle() const
const_iterator end() const noexcept
Get a const_iterator pointing right beyond the last non-overflow bin (i.e.
Converts a TAxisConfig of whatever kind to the corresponding TAxisBase-derived object.
double GetBinWidth() const noexcept
Get the width of the bins.
std::unordered_map< std::string, int > fLabelsIndex
Map of label (view on fLabels's elements) to bin index.
double GetBinCenter(int bin) const noexcept
Get the bin center of the bin with the given index.
TAxisIrregular(std::vector< double > &&binborders) noexcept
Construct a TAxisIrregular from a vector of bin borders.
Histogram axis base class.
TAxisConfig(std::string_view title, std::vector< std::string > &&labels)
Represents a TAxisLabels with labels and title.
const_iterator & operator-=(int d) noexcept
TAxisConfig(std::string_view title, Grow_t, int nbins, double from, double to)
Represents a TAxisGrow with nbins from from to to, and axis title.
A TAxisGrow that has a label assigned to each bin and a bin width of 1.
Source and target axes are identical.
int GetNBins() const noexcept
Get the number of bins. Forwards to the underlying axis.
static constexpr const Grow_t Grow
Tag signalling that an axis should be able to grow; used for calling the appropriate constructor like...
Axis_t operator()(const TAxisConfig &cfg) noexcept
The source axis and target axis have different binning.
Coordinate could fit after growing the axis.
bool IsOverflowBin(int bin) const noexcept
Whether the bin index is referencing a bin higher than the axis range.
const bool fCanGrow
Whether this axis can grow (and thus has no overflow bins).
static bool CanGrow() noexcept
This axis cannot grow.
double GetMaximum() const noexcept
Get the high end of the axis range.
int FindBin(double x) const noexcept
Find the bin index for the given coordinate.
TAxisIrregular(const std::vector< double > &binborders)
Construct a TAxisIrregular from a vector of bin borders.
TAxisLabels(std::string_view title, const std::vector< std::string_view > &labels)
Construct a TAxisLables from a vector of string_views, with title.
const_iterator & operator+=(int d) noexcept
#define R__ERROR_HERE(GROUP)
std::vector< std::string > fLabels
Bin labels for a TAxisLabels.
const std::string & GetTitle() const
bool operator<=(TAxisBase::const_iterator lhs, TAxisBase::const_iterator rhs) noexcept
i <= j
Axis_t operator()(const TAxisConfig &cfg) noexcept
TAxisConfig(std::string_view title, const std::vector< std::string_view > &labels)
Represents a TAxisLabels with labels and title.