BornAgain  1.19.79
Open-source research software to simulate and fit neutron and x-ray reflectometry and grazing-incidence small-angle scattering
CustomBinAxis Class Reference

Description

Axis with fixed bin size in sin(angle) space used for numerical comparison with IsGisaxs. The main feature of the axis is that it produces zero bin sizes.

Definition at line 24 of file CustomBinAxis.h.

Inheritance diagram for CustomBinAxis:
[legend]
Collaboration diagram for CustomBinAxis:
[legend]

Public Member Functions

 CustomBinAxis (const std::string &name, size_t nbins, double start, double end)
 CustomBinAxis constructor. More...
 
 ~CustomBinAxis () override=default
 
std::string axisName () const
 Returns the label of the axis. More...
 
Bin1D bin (size_t index) const override
 retrieve a 1d bin for the given index More...
 
std::vector< double > binBoundaries () const override
 
double binCenter (size_t index) const override
 
std::vector< double > binCenters () const override
 
std::pair< double, double > bounds () const
 Returns lower and upper bound in a pair. first is lower, second is upper. More...
 
double center () const
 Returns midpoint of axis. More...
 
void clip (double lower, double upper) override
 Clips this axis to the given values. More...
 
void clip (std::pair< double, double > bounds)
 Convenience overload to clip this axis to the given values. bounds.first is lower, bounds.second is upper value. More...
 
CustomBinAxisclone () const override
 
virtual bool contains (double value) const
 Returns true if axis contains given point. More...
 
size_t findClosestIndex (double value) const override
 find bin index which is best match for given value More...
 
double max () const override
 Returns value of last point of axis. More...
 
double min () const override
 Returns value of first point of axis. More...
 
bool operator!= (const IAxis &right) const
 
bool operator== (const IAxis &right) const
 test for equality More...
 
double operator[] (size_t index) const override
 indexed accessor retrieves a sample More...
 
void setAxisName (std::string name)
 Sets the axis label. More...
 
size_t size () const override
 Returns the number of bins. More...
 
double span () const
 Returns distance from first to last point. More...
 

Protected Member Functions

bool equals (const IAxis &other) const override
 
void print (std::ostream &ostr) const override
 
void setBinBoundaries (const std::vector< double > &bin_boundaries)
 

Protected Attributes

std::vector< double > m_bin_centers
 
double m_end
 
size_t m_nbins
 
double m_start
 

Private Attributes

std::vector< double > m_bin_boundaries
 vector containing the bin limits More...
 
std::string m_name
 axis name More...
 

Constructor & Destructor Documentation

◆ CustomBinAxis()

CustomBinAxis::CustomBinAxis ( const std::string &  name,
size_t  nbins,
double  start,
double  end 
)

CustomBinAxis constructor.

Parameters
nameAxis name
nbinsnumber of bins
startcenter of first bin (IsGisaxs convention)
endcenter of last bin (IsGisaxs convention)

Definition at line 22 of file CustomBinAxis.cpp.

23  : VariableBinAxis(name, nbins)
24  , m_start(start)
25  , m_end(end)
26 {
27  if (m_start >= m_end)
28  throw std::runtime_error("CustomBinAxis::CustomBinAxis() -> Error."
29  " start >= end is not allowed.");
30 
31  double start_sin = std::sin(start);
32  double end_sin = std::sin(end);
33  double step = (end_sin - start_sin) / (m_nbins - 1); // m_nbins-1 is intentionally
34 
35  m_bin_centers.resize(m_nbins, 0.0);
36  for (size_t i = 0; i < m_bin_centers.size(); ++i)
37  m_bin_centers[i] = std::asin(start_sin + i * step);
38 
39  std::vector<double> bin_boundaries;
40  bin_boundaries.resize(m_nbins + 1, 0.0);
41  for (size_t i = 0; i < bin_boundaries.size(); ++i)
42  bin_boundaries[i] = std::asin(start_sin - step / 2. + step * i);
43  setBinBoundaries(bin_boundaries);
44 }
std::vector< double > m_bin_centers
Definition: CustomBinAxis.h:48
void setBinBoundaries(const std::vector< double > &bin_boundaries)
VariableBinAxis(const std::string &name, const std::vector< double > &bin_boundaries)
VariableBinAxis constructor.

References m_bin_centers, m_end, VariableBinAxis::m_nbins, m_start, and VariableBinAxis::setBinBoundaries().

Referenced by clone().

Here is the call graph for this function:

◆ ~CustomBinAxis()

CustomBinAxis::~CustomBinAxis ( )
overridedefault

Member Function Documentation

◆ axisName()

std::string IAxis::axisName ( ) const
inlineinherited

◆ bin()

Bin1D CustomBinAxis::bin ( size_t  index) const
overridevirtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 51 of file CustomBinAxis.cpp.

52 {
53  if (index >= m_nbins)
54  throw std::runtime_error("CustomBinAxis::bin() -> Error. Wrong index.");
55 
56  Bin1D result(m_bin_centers[index], m_bin_centers[index]);
57  return result;
58 }
Definition: Bin.h:20

References m_bin_centers, and VariableBinAxis::m_nbins.

◆ binBoundaries()

std::vector<double> VariableBinAxis::binBoundaries ( ) const
inlineoverridevirtualinherited

Reimplemented from IAxis.

Definition at line 48 of file VariableBinAxis.h.

48 { return m_bin_boundaries; }
std::vector< double > m_bin_boundaries
vector containing the bin limits

References VariableBinAxis::m_bin_boundaries.

Referenced by ConstKBinAxis::clip().

◆ binCenter()

double VariableBinAxis::binCenter ( size_t  index) const
overridevirtualinherited

Implements IAxis.

Definition at line 64 of file VariableBinAxis.cpp.

65 {
66  return bin(index).center();
67 }
double center() const
Definition: Bin.h:30
Bin1D bin(size_t index) const override
retrieve a 1d bin for the given index

References VariableBinAxis::bin(), and Bin1D::center().

Here is the call graph for this function:

◆ binCenters()

std::vector< double > CustomBinAxis::binCenters ( ) const
overridevirtual

Reimplemented from IAxis.

Definition at line 60 of file CustomBinAxis.cpp.

61 {
62  return m_bin_centers;
63 }

References m_bin_centers.

◆ bounds()

std::pair< double, double > IAxis::bounds ( ) const
inherited

Returns lower and upper bound in a pair. first is lower, second is upper.

Definition at line 50 of file IAxis.cpp.

51 {
52  return {min(), max()};
53 }
virtual double max() const =0
Returns value of last point of axis.
virtual double min() const =0
Returns value of first point of axis.

References IAxis::max(), and IAxis::min().

Referenced by IAxis::clip().

Here is the call graph for this function:

◆ center()

double IAxis::center ( ) const
inherited

Returns midpoint of axis.

Definition at line 60 of file IAxis.cpp.

61 {
62  return (max() + min()) / 2;
63 }

References IAxis::max(), and IAxis::min().

Here is the call graph for this function:

◆ clip() [1/2]

void CustomBinAxis::clip ( double  lower,
double  upper 
)
overridevirtual

Clips this axis to the given values.

Reimplemented from IAxis.

Definition at line 65 of file CustomBinAxis.cpp.

66 {
67  throw std::runtime_error("CustomBinAxis::clip() -> Error."
68  " Not implemented.");
69 }

◆ clip() [2/2]

void IAxis::clip ( std::pair< double, double >  bounds)
inherited

Convenience overload to clip this axis to the given values. bounds.first is lower, bounds.second is upper value.

Definition at line 40 of file IAxis.cpp.

41 {
42  return clip(bounds.first, bounds.second);
43 }
std::pair< double, double > bounds() const
Returns lower and upper bound in a pair. first is lower, second is upper.
Definition: IAxis.cpp:50
virtual void clip(double lower, double upper)
Clips this axis to the given values.
Definition: IAxis.cpp:35

References IAxis::bounds(), and IAxis::clip().

Here is the call graph for this function:

◆ clone()

CustomBinAxis * CustomBinAxis::clone ( ) const
overridevirtual

Implements IAxis.

Definition at line 46 of file CustomBinAxis.cpp.

47 {
48  return new CustomBinAxis(axisName(), m_nbins, m_start, m_end);
49 }
CustomBinAxis(const std::string &name, size_t nbins, double start, double end)
CustomBinAxis constructor.
std::string axisName() const
Returns the label of the axis.
Definition: IAxis.h:61

References CustomBinAxis(), IAxis::axisName(), m_end, VariableBinAxis::m_nbins, and m_start.

Here is the call graph for this function:

◆ contains()

bool IAxis::contains ( double  value) const
virtualinherited

Returns true if axis contains given point.

Definition at line 45 of file IAxis.cpp.

46 {
47  return value >= min() && value < max();
48 }

References IAxis::max(), and IAxis::min().

Referenced by RectangularDetector::indexOfSpecular(), and SphericalDetector::indexOfSpecular().

Here is the call graph for this function:

◆ equals()

bool CustomBinAxis::equals ( const IAxis other) const
overrideprotectedvirtual

Reimplemented from IAxis.

Definition at line 78 of file CustomBinAxis.cpp.

79 {
80  if (!IAxis::equals(other))
81  return false;
82  if (const auto* otherAxis = dynamic_cast<const CustomBinAxis*>(&other)) {
83  if (size() != otherAxis->size())
84  return false;
85  if (!BaseUtils::algo::almostEqual(m_start, otherAxis->m_start))
86  return false;
87  if (!BaseUtils::algo::almostEqual(m_end, otherAxis->m_end))
88  return false;
89  return true;
90  }
91  return false;
92 }
Axis with fixed bin size in sin(angle) space used for numerical comparison with IsGisaxs....
Definition: CustomBinAxis.h:24
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:20
size_t size() const override
Returns the number of bins.
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:33

References BaseUtils::algo::almostEqual(), IAxis::equals(), m_end, m_start, and VariableBinAxis::size().

Here is the call graph for this function:

◆ findClosestIndex()

size_t VariableBinAxis::findClosestIndex ( double  value) const
overridevirtualinherited

find bin index which is best match for given value

Implements IAxis.

Definition at line 69 of file VariableBinAxis.cpp.

70 {
71  if (m_bin_boundaries.size() < 2)
72  throw std::runtime_error("VariableBinAxis::findClosestIndex() -> Error! "
73  "VariableBinAxis not correctly initialized");
74  if (value < min())
75  return 0;
76  if (value >= max())
77  return m_nbins - 1;
78 
79  auto top_limit = std::lower_bound(m_bin_boundaries.begin(), m_bin_boundaries.end(), value);
80  if (*top_limit != value)
81  --top_limit;
82  size_t nbin = top_limit - m_bin_boundaries.begin();
83  return nbin;
84 }
double max() const override
Returns value of last point of axis.
double min() const override
Returns value of first point of axis.

References VariableBinAxis::m_bin_boundaries, VariableBinAxis::m_nbins, VariableBinAxis::max(), and VariableBinAxis::min().

Referenced by ConstKBinAxis::clip(), and VariableBinAxis::clip().

Here is the call graph for this function:

◆ max()

double VariableBinAxis::max ( ) const
overridevirtualinherited

Returns value of last point of axis.

Implements IAxis.

Definition at line 59 of file VariableBinAxis.cpp.

60 {
61  return m_bin_boundaries.back();
62 }

References VariableBinAxis::m_bin_boundaries.

Referenced by ConstKBinAxis::clip(), VariableBinAxis::clip(), and VariableBinAxis::findClosestIndex().

◆ min()

double VariableBinAxis::min ( ) const
overridevirtualinherited

Returns value of first point of axis.

Implements IAxis.

Definition at line 54 of file VariableBinAxis.cpp.

55 {
56  return m_bin_boundaries.front();
57 }

References VariableBinAxis::m_bin_boundaries.

Referenced by ConstKBinAxis::clip(), VariableBinAxis::clip(), and VariableBinAxis::findClosestIndex().

◆ operator!=()

bool IAxis::operator!= ( const IAxis right) const
inlineinherited

Definition at line 90 of file IAxis.h.

90 { return !(*this == right); }

◆ operator==()

bool IAxis::operator== ( const IAxis right) const
inlineinherited

test for equality

Definition at line 89 of file IAxis.h.

89 { return equals(right); }

References IAxis::equals().

Here is the call graph for this function:

◆ operator[]()

double VariableBinAxis::operator[] ( size_t  index) const
overridevirtualinherited

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 40 of file VariableBinAxis.cpp.

41 {
42  return bin(index).center();
43 }

References VariableBinAxis::bin(), and Bin1D::center().

Here is the call graph for this function:

◆ print()

void CustomBinAxis::print ( std::ostream &  ostr) const
overrideprotectedvirtual

Implements IAxis.

Definition at line 71 of file CustomBinAxis.cpp.

72 {
73  ostr << "CustomBinAxis(\"" << axisName() << "\", " << size() << ", "
74  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << m_start << ", " << m_end
75  << ")";
76 }

References IAxis::axisName(), m_end, m_start, and VariableBinAxis::size().

Here is the call graph for this function:

◆ setAxisName()

void IAxis::setAxisName ( std::string  name)
inlineinherited

Sets the axis label.

Definition at line 39 of file IAxis.h.

39 { m_name = name; }

References IAxis::m_name.

◆ setBinBoundaries()

void VariableBinAxis::setBinBoundaries ( const std::vector< double > &  bin_boundaries)
protectedinherited

Definition at line 146 of file VariableBinAxis.cpp.

147 {
148  // checking that values are sorted
149  std::vector<double> vec_sorted = bin_boundaries;
150  std::sort(vec_sorted.begin(), vec_sorted.end());
151  for (size_t i = 0; i < bin_boundaries.size(); ++i) {
152  if (vec_sorted[i] != bin_boundaries[i])
153  throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
154  "Array with bin edges is not sorted.");
155  }
156 
157  std::vector<double> vec = bin_boundaries;
158  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
159 
160  if (vec.size() != bin_boundaries.size())
161  throw std::runtime_error("VariableBinAxis::setBinBoundaries() -> Error. "
162  "Array with bin edges contains repeating values.");
163 
164  m_bin_boundaries = bin_boundaries;
165 }

References VariableBinAxis::m_bin_boundaries.

Referenced by ConstKBinAxis::ConstKBinAxis(), CustomBinAxis(), VariableBinAxis::VariableBinAxis(), ConstKBinAxis::clip(), and VariableBinAxis::clip().

◆ size()

size_t VariableBinAxis::size ( ) const
inlineoverridevirtualinherited

◆ span()

double IAxis::span ( ) const
inherited

Returns distance from first to last point.

Definition at line 55 of file IAxis.cpp.

56 {
57  return max() - min();
58 }

References IAxis::max(), and IAxis::min().

Referenced by RectangularDetector::height(), and RectangularDetector::width().

Here is the call graph for this function:

Member Data Documentation

◆ m_bin_boundaries

◆ m_bin_centers

std::vector<double> CustomBinAxis::m_bin_centers
protected

Definition at line 48 of file CustomBinAxis.h.

Referenced by CustomBinAxis(), bin(), and binCenters().

◆ m_end

double CustomBinAxis::m_end
protected

Definition at line 47 of file CustomBinAxis.h.

Referenced by CustomBinAxis(), clone(), equals(), and print().

◆ m_name

std::string IAxis::m_name
privateinherited

axis name

Definition at line 103 of file IAxis.h.

Referenced by IAxis::axisName(), and IAxis::setAxisName().

◆ m_nbins

◆ m_start

double CustomBinAxis::m_start
protected

Definition at line 46 of file CustomBinAxis.h.

Referenced by CustomBinAxis(), clone(), equals(), and print().


The documentation for this class was generated from the following files: