Skip to content
Snippets Groups Projects
Commit b12a6be8 authored by Matthias Puchner's avatar Matthias Puchner
Browse files

add convenience functions to axis classes; rectified error texts

necessary for later refactoring
parent 86ba6af2
No related branches found
No related tags found
1 merge request!81Refactorings no. 2 for the upcoming removing of "region of interest" (ROI)
...@@ -45,19 +45,24 @@ ConstKBinAxis* ConstKBinAxis::clone() const ...@@ -45,19 +45,24 @@ ConstKBinAxis* ConstKBinAxis::clone() const
return new ConstKBinAxis(getName(), m_nbins, m_start, m_end); return new ConstKBinAxis(getName(), m_nbins, m_start, m_end);
} }
ConstKBinAxis* ConstKBinAxis::createClippedAxis(double left, double right) const ConstKBinAxis* ConstKBinAxis::createClippedAxis(double lower, double upper) const
{ {
if (left >= right) return static_cast<ConstKBinAxis*>(IAxis::createClippedAxis(lower, upper));
}
void ConstKBinAxis::clip(double lower, double upper)
{
if (lower >= upper)
throw std::runtime_error( throw std::runtime_error(
"ConstKBinAxis::createClippedAxis() -> Error. 'left'' should be smaller than 'right'"); "ConstKBinAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
if (left < lowerBound()) if (lower < lowerBound())
left = bin(0).center(); lower = bin(0).center();
if (right >= upperBound()) if (upper >= upperBound())
right = bin(size() - 1).center(); upper = bin(size() - 1).center();
size_t nbin1 = findClosestIndex(left); size_t nbin1 = findClosestIndex(lower);
size_t nbin2 = findClosestIndex(right); size_t nbin2 = findClosestIndex(upper);
size_t new_nbins = nbin2 - nbin1 + 1; size_t new_nbins = nbin2 - nbin1 + 1;
std::vector<double> new_boundaries; std::vector<double> new_boundaries;
...@@ -66,11 +71,10 @@ ConstKBinAxis* ConstKBinAxis::createClippedAxis(double left, double right) const ...@@ -66,11 +71,10 @@ ConstKBinAxis* ConstKBinAxis::createClippedAxis(double left, double right) const
new_boundaries.push_back(old_boundaries[nbin1 + i]); new_boundaries.push_back(old_boundaries[nbin1 + i]);
} }
ConstKBinAxis* result = new ConstKBinAxis(getName(), new_nbins); m_nbins = new_nbins;
result->m_start = new_boundaries.front(); m_start = new_boundaries.front();
result->m_end = new_boundaries.back(); m_end = new_boundaries.back();
result->setBinBoundaries(new_boundaries); setBinBoundaries(new_boundaries);
return result;
} }
bool ConstKBinAxis::equals(const IAxis& other) const bool ConstKBinAxis::equals(const IAxis& other) const
......
...@@ -32,7 +32,8 @@ public: ...@@ -32,7 +32,8 @@ public:
ConstKBinAxis* clone() const override; ConstKBinAxis* clone() const override;
ConstKBinAxis* createClippedAxis(double left, double right) const override; ConstKBinAxis* createClippedAxis(double lower, double upper) const override;
virtual void clip(double lower, double upper) override;
protected: protected:
ConstKBinAxis(const std::string& name, size_t nbins); ConstKBinAxis(const std::string& name, size_t nbins);
......
...@@ -60,9 +60,15 @@ std::vector<double> CustomBinAxis::binCenters() const ...@@ -60,9 +60,15 @@ std::vector<double> CustomBinAxis::binCenters() const
return m_bin_centers; return m_bin_centers;
} }
CustomBinAxis* CustomBinAxis::createClippedAxis(double /* left */, double /* right */) const CustomBinAxis* CustomBinAxis::createClippedAxis(double /* lower */, double /* upper */) const
{ {
throw std::runtime_error("VariableBinAxis::CustomBinAxis() -> Error." throw std::runtime_error("CustomBinAxis::createClippedAxis() -> Error."
" Not implemented.");
}
void CustomBinAxis::clip(double /* lower */, double /* upper */)
{
throw std::runtime_error("CustomBinAxis::clip() -> Error."
" Not implemented."); " Not implemented.");
} }
......
...@@ -37,7 +37,8 @@ public: ...@@ -37,7 +37,8 @@ public:
std::vector<double> binCenters() const; std::vector<double> binCenters() const;
CustomBinAxis* createClippedAxis(double left, double right) const; CustomBinAxis* createClippedAxis(double lower, double upper) const override;
virtual void clip(double lower, double upper) override;
protected: protected:
void print(std::ostream& ostr) const; void print(std::ostream& ostr) const;
......
...@@ -80,21 +80,33 @@ std::vector<double> FixedBinAxis::binBoundaries() const ...@@ -80,21 +80,33 @@ std::vector<double> FixedBinAxis::binBoundaries() const
return result; return result;
} }
FixedBinAxis* FixedBinAxis::createClippedAxis(double left, double right) const FixedBinAxis* FixedBinAxis::createClippedAxis(double lower, double upper) const
{ {
if (left >= right) return static_cast<FixedBinAxis*>(IAxis::createClippedAxis(lower, upper));
throw std::runtime_error("FixedBinAxis::createClippedAxis() -> Error. " }
"'left' should be smaller than 'right'");
if (left < lowerBound())
left = bin(0).center();
if (right >= upperBound())
right = bin(size() - 1).center();
size_t nbin1 = findClosestIndex(left);
size_t nbin2 = findClosestIndex(right);
return new FixedBinAxis(getName(), nbin2 - nbin1 + 1, bin(nbin1).m_lower, bin(nbin2).m_upper); void FixedBinAxis::clip(double lower, double upper)
{
if (lower >= upper)
throw std::runtime_error(
"FixedBinAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
if (lower < lowerBound())
lower = bin(0).center();
if (upper >= upperBound())
upper = bin(size() - 1).center();
const size_t nbin1 = findClosestIndex(lower);
const size_t nbin2 = findClosestIndex(upper);
// create tmp vars until everything is calculated, otherwise the calculation will be corrupted
// by partially changed values
const auto newStart = bin(nbin1).m_lower;
const auto newEnd = bin(nbin2).m_upper;
m_nbins = nbin2 - nbin1 + 1;
m_start = newStart;
m_end = newEnd;
} }
void FixedBinAxis::print(std::ostream& ostr) const void FixedBinAxis::print(std::ostream& ostr) const
......
...@@ -49,7 +49,8 @@ public: ...@@ -49,7 +49,8 @@ public:
std::vector<double> binBoundaries() const; std::vector<double> binBoundaries() const;
FixedBinAxis* createClippedAxis(double left, double right) const; FixedBinAxis* createClippedAxis(double lower, double upper) const override;
void clip(double lower, double upper) override;
protected: protected:
void print(std::ostream& ostr) const; void print(std::ostream& ostr) const;
......
...@@ -29,9 +29,21 @@ std::vector<double> IAxis::binBoundaries() const ...@@ -29,9 +29,21 @@ std::vector<double> IAxis::binBoundaries() const
throw std::runtime_error("IAxis::binBoundaries() -> Error. Not implemented."); throw std::runtime_error("IAxis::binBoundaries() -> Error. Not implemented.");
} }
IAxis* IAxis::createClippedAxis(double /* left */, double /* right */) const IAxis* IAxis::createClippedAxis(double lower, double upper) const
{ {
throw std::runtime_error("IAxis::createClippedAxis() -> Error. Not implemented."); auto newAxis = clone();
newAxis->clip(lower, upper);
return newAxis;
}
void IAxis::clip(double lower, double upper)
{
throw std::runtime_error("IAxis::clip() -> Error. Not implemented.");
}
void IAxis::clip(const std::pair<double, double> bounds)
{
return clip(bounds.first, bounds.second);
} }
bool IAxis::contains(double value) const bool IAxis::contains(double value) const
...@@ -39,6 +51,11 @@ bool IAxis::contains(double value) const ...@@ -39,6 +51,11 @@ bool IAxis::contains(double value) const
return value >= lowerBound() && value < upperBound(); return value >= lowerBound() && value < upperBound();
} }
std::pair<double, double> IAxis::bounds() const
{
return {lowerBound(), upperBound()};
}
double IAxis::span() const double IAxis::span() const
{ {
return upperBound() - lowerBound(); return upperBound() - lowerBound();
......
...@@ -44,6 +44,10 @@ public: ...@@ -44,6 +44,10 @@ public:
//! Returns value of last point of axis //! Returns value of last point of axis
virtual double upperBound() const = 0; virtual double upperBound() const = 0;
//! Returns lower and upper bound in a pair.
//! first is lower, second is upper.
std::pair<double, double> bounds() const;
//! Returns distance from first to last point //! Returns distance from first to last point
double span() const; double span() const;
...@@ -72,10 +76,16 @@ public: ...@@ -72,10 +76,16 @@ public:
virtual bool contains(double value) const; virtual bool contains(double value) const;
//! Creates a new clipped axis //! Creates a new clipped axis
virtual IAxis* createClippedAxis(double left, double right) const; virtual IAxis* createClippedAxis(double lower, double upper) const;
//! Clips this axis to the given values
virtual void clip(double lower, double upper);
//! Convenience overload to clip this axis to the given values.
//! bounds.first is lower, bounds.second is upper value.
void clip(const std::pair<double, double> bounds);
//! test for equality //! test for equality
//!
bool operator==(const IAxis& right) const { return equals(right); } bool operator==(const IAxis& right) const { return equals(right); }
bool operator!=(const IAxis& right) const { return !(*this == right); } bool operator!=(const IAxis& right) const { return !(*this == right); }
......
...@@ -70,17 +70,22 @@ std::vector<double> PointwiseAxis::binBoundaries() const ...@@ -70,17 +70,22 @@ std::vector<double> PointwiseAxis::binBoundaries() const
return result; return result;
} }
PointwiseAxis* PointwiseAxis::createClippedAxis(double left, double right) const PointwiseAxis* PointwiseAxis::createClippedAxis(double lower, double upper) const
{ {
if (left >= right) return static_cast<PointwiseAxis*>(IAxis::createClippedAxis(lower, upper));
throw std::runtime_error("Error in PointwiseAxis::createClippedAxis: " }
"'left' should be smaller than 'right'");
void PointwiseAxis::clip(double lower, double upper)
{
if (lower >= upper)
throw std::runtime_error(
"PointwiseAxis::clip() -> Error. 'lower' should be smaller than 'upper'");
using diff_t = std::vector<double>::iterator::difference_type; using diff_t = std::vector<double>::iterator::difference_type;
auto begin = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(left)); const auto begin = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(lower));
auto end = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(right)) + 1; const auto end = m_coordinates.begin() + static_cast<diff_t>(findClosestIndex(upper)) + 1;
return new PointwiseAxis(getName(), std::vector<double>(begin, end)); m_coordinates = std::vector<double>(begin, end);
} }
void PointwiseAxis::print(std::ostream& ostr) const void PointwiseAxis::print(std::ostream& ostr) const
......
...@@ -75,8 +75,8 @@ public: ...@@ -75,8 +75,8 @@ public:
std::vector<double> binBoundaries() const override; std::vector<double> binBoundaries() const override;
//! Creates a new clipped axis PointwiseAxis* createClippedAxis(double lower, double upper) const override;
PointwiseAxis* createClippedAxis(double left, double right) const override; virtual void clip(double lower, double upper) override;
private: private:
void print(std::ostream& ostr) const override; void print(std::ostream& ostr) const override;
......
...@@ -97,28 +97,32 @@ std::vector<double> VariableBinAxis::binCenters() const ...@@ -97,28 +97,32 @@ std::vector<double> VariableBinAxis::binCenters() const
return result; return result;
} }
VariableBinAxis* VariableBinAxis::createClippedAxis(double left, double right) const VariableBinAxis* VariableBinAxis::createClippedAxis(double lower, double upper) const
{ {
return static_cast<VariableBinAxis*>(IAxis::createClippedAxis(lower, upper));
}
if (left >= right) void VariableBinAxis::clip(double lower, double upper)
throw std::runtime_error("VariableBinAxis::createClippedAxis() -> Error. " {
"'left'' should be smaller than 'right'"); if (lower >= upper)
throw std::runtime_error("VariableBinAxis::clip() -> Error. "
"'lower' should be smaller than 'upper'");
if (left < lowerBound()) if (lower < lowerBound())
left = bin(0).center(); lower = bin(0).center();
if (right >= upperBound()) if (upper >= upperBound())
right = bin(size() - 1).center(); upper = bin(size() - 1).center();
size_t nbin1 = findClosestIndex(left); const size_t nbin1 = findClosestIndex(lower);
size_t nbin2 = findClosestIndex(right); const size_t nbin2 = findClosestIndex(upper);
const size_t new_nbins = nbin2 - nbin1 + 1;
size_t new_nbins = nbin2 - nbin1 + 1;
std::vector<double> new_boundaries; std::vector<double> new_boundaries;
for (size_t i = 0; i < new_nbins + 1; ++i) { for (size_t i = 0; i < new_nbins + 1; ++i)
new_boundaries.push_back(m_bin_boundaries[nbin1 + i]); new_boundaries.push_back(m_bin_boundaries[nbin1 + i]);
}
return new VariableBinAxis(getName(), new_nbins, new_boundaries); m_nbins = new_nbins;
setBinBoundaries(new_boundaries);
} }
void VariableBinAxis::print(std::ostream& ostr) const void VariableBinAxis::print(std::ostream& ostr) const
...@@ -156,7 +160,7 @@ void VariableBinAxis::setBinBoundaries(const std::vector<double>& bin_boundaries ...@@ -156,7 +160,7 @@ void VariableBinAxis::setBinBoundaries(const std::vector<double>& bin_boundaries
std::sort(vec_sorted.begin(), vec_sorted.end()); std::sort(vec_sorted.begin(), vec_sorted.end());
for (size_t i = 0; i < bin_boundaries.size(); ++i) { for (size_t i = 0; i < bin_boundaries.size(); ++i) {
if (vec_sorted[i] != bin_boundaries[i]) if (vec_sorted[i] != bin_boundaries[i])
throw std::runtime_error("VariableBinAxis::VariableBinAxis() -> Error. " throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
"Array with bin edges is not sorted."); "Array with bin edges is not sorted.");
} }
...@@ -164,7 +168,7 @@ void VariableBinAxis::setBinBoundaries(const std::vector<double>& bin_boundaries ...@@ -164,7 +168,7 @@ void VariableBinAxis::setBinBoundaries(const std::vector<double>& bin_boundaries
vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
if (vec.size() != bin_boundaries.size()) if (vec.size() != bin_boundaries.size())
throw std::runtime_error("VariableBinAxis::VariableBinAxis() -> Error. " throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
"Array with bin edges contains repeating values."); "Array with bin edges contains repeating values.");
m_bin_boundaries = bin_boundaries; m_bin_boundaries = bin_boundaries;
......
...@@ -49,7 +49,8 @@ public: ...@@ -49,7 +49,8 @@ public:
std::vector<double> binCenters() const; std::vector<double> binCenters() const;
std::vector<double> binBoundaries() const { return m_bin_boundaries; } std::vector<double> binBoundaries() const { return m_bin_boundaries; }
virtual VariableBinAxis* createClippedAxis(double left, double right) const; virtual VariableBinAxis* createClippedAxis(double lower, double upper) const override;
virtual void clip(double lower, double upper) override;
protected: protected:
VariableBinAxis(const std::string& name, size_t nbins = 0); VariableBinAxis(const std::string& name, size_t nbins = 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment