BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FixedBinAxis Class Reference
Inheritance diagram for FixedBinAxis:
Collaboration diagram for FixedBinAxis:

Public Member Functions

 FixedBinAxis (const std::string &name, size_t nbins, double start, double end)
 
virtual ~FixedBinAxis ()
 
FixedBinAxisclone () const
 
size_t size () const
 
double operator[] (size_t index) const
 
Bin1D getBin (size_t index) const
 
double getMin () const
 
double getMax () const
 
double getBinCenter (size_t index) const
 
size_t findClosestIndex (double value) const
 
std::vector< double > getBinCenters () const
 
std::vector< double > getBinBoundaries () const
 
FixedBinAxiscreateClippedAxis (double left, double right) const
 
std::string pyString (const std::string &units, size_t) const final
 
std::string getName () const
 
void setName (std::string name)
 
bool operator== (const IAxis &right) const
 
bool operator!= (const IAxis &right) const
 
virtual bool contains (double value) const
 

Protected Member Functions

void print (std::ostream &ostr) const
 
virtual bool equals (const IAxis &other) const
 

Private Attributes

size_t m_nbins
 
double m_start
 
double m_end
 
std::string m_name
 

Detailed Description

Axis with fixed bin size.

Definition at line 23 of file FixedBinAxis.h.

Constructor & Destructor Documentation

◆ FixedBinAxis()

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

FixedBinAxis constructor.

Parameters
nameAxis name
nbinsnumber of bins
startlow edge of first bin
endupper edge of last bin

Definition at line 22 of file FixedBinAxis.cpp.

23  : IAxis(name), m_nbins(nbins), m_start(start), m_end(end)
24 {
25 }
double m_start
Definition: FixedBinAxis.h:63
double m_end
Definition: FixedBinAxis.h:64
size_t m_nbins
Definition: FixedBinAxis.h:62
IAxis(const std::string &name)
constructors
Definition: IAxis.h:28

Referenced by clone(), and createClippedAxis().

◆ ~FixedBinAxis()

virtual FixedBinAxis::~FixedBinAxis ( )
inlinevirtual

Definition at line 32 of file FixedBinAxis.h.

32 {}

Member Function Documentation

◆ clone()

FixedBinAxis * FixedBinAxis::clone ( ) const
virtual

clone function

Implements IAxis.

Definition at line 27 of file FixedBinAxis.cpp.

28 {
30  return result;
31 }
Axis with fixed bin size.
Definition: FixedBinAxis.h:24
FixedBinAxis(const std::string &name, size_t nbins, double start, double end)
FixedBinAxis constructor.
std::string getName() const
retrieve the label of the axis
Definition: IAxis.h:40

References FixedBinAxis(), IAxis::getName(), m_end, m_nbins, and m_start.

Here is the call graph for this function:

◆ size()

size_t FixedBinAxis::size ( ) const
inlinevirtual

retrieve the number of bins

Implements IAxis.

Definition at line 36 of file FixedBinAxis.h.

36 { return m_nbins; }

References m_nbins.

Referenced by StandardSimulations::BasicSpecularQ(), createClippedAxis(), equals(), getBinBoundaries(), getBinCenters(), print(), pyString(), and StandardSimulations::TOFRWithPointwiseResolution().

◆ operator[]()

double FixedBinAxis::operator[] ( size_t  index) const
virtual

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 33 of file FixedBinAxis.cpp.

34 {
35  if (index >= m_nbins)
36  throw Exceptions::OutOfBoundsException("FixedBinAxis::operator[] -> Error. Wrong index.");
37 
38  double step = (m_end - m_start) / m_nbins;
39  return m_start + (index + 0.5) * step;
40 }

References m_end, m_nbins, and m_start.

◆ getBin()

Bin1D FixedBinAxis::getBin ( size_t  index) const
virtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 42 of file FixedBinAxis.cpp.

43 {
44  if (index >= m_nbins)
45  throw Exceptions::OutOfBoundsException("FixedBinAxis::getBin() -> Error. Wrong index.");
46 
47  double step = (m_end - m_start) / m_nbins;
48  Bin1D result(m_start + step * index, m_start + step * (index + 1));
49  return result;
50 }
Definition: Bin.h:20

References m_end, m_nbins, and m_start.

Referenced by createClippedAxis(), getBinBoundaries(), and getBinCenters().

◆ getMin()

double FixedBinAxis::getMin ( ) const
inlinevirtual

Returns value of first point of axis.

Implements IAxis.

Definition at line 42 of file FixedBinAxis.h.

42 { return m_start; }

References m_start.

Referenced by createClippedAxis(), findClosestIndex(), print(), and pyString().

◆ getMax()

double FixedBinAxis::getMax ( ) const
inlinevirtual

Returns value of last point of axis.

Implements IAxis.

Definition at line 43 of file FixedBinAxis.h.

43 { return m_end; }

References m_end.

Referenced by createClippedAxis(), findClosestIndex(), print(), and pyString().

◆ getBinCenter()

double FixedBinAxis::getBinCenter ( size_t  index) const
inlinevirtual

Implements IAxis.

Definition at line 45 of file FixedBinAxis.h.

45 { return (*this)[index]; }

◆ findClosestIndex()

size_t FixedBinAxis::findClosestIndex ( double  value) const
virtual

find bin index which is best match for given value

Implements IAxis.

Definition at line 52 of file FixedBinAxis.cpp.

53 {
54  if (value < getMin()) {
55  return 0;
56  } else if (value >= getMax()) {
57  return m_nbins - 1;
58  }
59 
60  double step = (m_end - m_start) / m_nbins;
61  return int((value - m_start) / step);
62 }
double getMax() const
Returns value of last point of axis.
Definition: FixedBinAxis.h:43
double getMin() const
Returns value of first point of axis.
Definition: FixedBinAxis.h:42

References getMax(), getMin(), m_end, m_nbins, and m_start.

Referenced by createClippedAxis().

Here is the call graph for this function:

◆ getBinCenters()

std::vector< double > FixedBinAxis::getBinCenters ( ) const
virtual

Reimplemented from IAxis.

Definition at line 64 of file FixedBinAxis.cpp.

65 {
66  std::vector<double> result;
67  result.resize(size(), 0.0);
68  for (size_t i = 0; i < size(); ++i) {
69  result[i] = getBin(i).getMidPoint();
70  }
71  return result;
72 }
size_t size() const
retrieve the number of bins
Definition: FixedBinAxis.h:36
Bin1D getBin(size_t index) const
retrieve a 1d bin for the given index
double getMidPoint() const
Definition: Bin.h:25

References getBin(), Bin1D::getMidPoint(), and size().

Referenced by StandardSimulations::BasicSpecularQ(), and StandardSimulations::TOFRWithPointwiseResolution().

Here is the call graph for this function:

◆ getBinBoundaries()

std::vector< double > FixedBinAxis::getBinBoundaries ( ) const
virtual

Reimplemented from IAxis.

Definition at line 74 of file FixedBinAxis.cpp.

75 {
76  std::vector<double> result;
77  result.resize(size() + 1, 0.0);
78  for (size_t i = 0; i < size(); ++i) {
79  result[i] = getBin(i).m_lower;
80  }
81  result[size()] = getBin(size() - 1).m_upper;
82  return result;
83 }
double m_upper
upper bound of the bin
Definition: Bin.h:24
double m_lower
lower bound of the bin
Definition: Bin.h:23

References getBin(), Bin1D::m_lower, Bin1D::m_upper, and size().

Here is the call graph for this function:

◆ createClippedAxis()

FixedBinAxis * FixedBinAxis::createClippedAxis ( double  left,
double  right 
) const
virtual

Creates a new clipped axis.

Reimplemented from IAxis.

Definition at line 85 of file FixedBinAxis.cpp.

86 {
87  if (left >= right)
88  throw Exceptions::LogicErrorException("FixedBinAxis::createClippedAxis() -> Error. "
89  "'left' should be smaller than 'right'");
90 
91  if (left < getMin())
92  left = getBin(0).getMidPoint();
93  if (right >= getMax())
94  right = getBin(size() - 1).getMidPoint();
95 
96  size_t nbin1 = findClosestIndex(left);
97  size_t nbin2 = findClosestIndex(right);
98 
99  return new FixedBinAxis(getName(), nbin2 - nbin1 + 1, getBin(nbin1).m_lower,
100  getBin(nbin2).m_upper);
101 }
size_t findClosestIndex(double value) const
find bin index which is best match for given value

References findClosestIndex(), FixedBinAxis(), getBin(), getMax(), Bin1D::getMidPoint(), getMin(), IAxis::getName(), and size().

Here is the call graph for this function:

◆ pyString()

std::string FixedBinAxis::pyString ( const std::string &  units,
size_t   
) const
finalvirtual

Implements IAxis.

Definition at line 126 of file FixedBinAxis.cpp.

127 {
128  std::ostringstream result;
129  result << "ba.FixedBinAxis(" << pyfmt::printString(getName()) << ", " << size() << ", "
130  << pyfmt::printValue(getMin(), units) << ", " << pyfmt::printValue(getMax(), units)
131  << ")";
132  return result.str();
133 }
std::string printString(const std::string &value)
Definition: PyFmt.cpp:116
std::string printValue(double value, const std::string &units)
Definition: PyFmt.cpp:104

References getMax(), getMin(), IAxis::getName(), pyfmt::printString(), pyfmt::printValue(), and size().

Here is the call graph for this function:

◆ print()

void FixedBinAxis::print ( std::ostream &  ostr) const
protectedvirtual

Implements IAxis.

Definition at line 103 of file FixedBinAxis.cpp.

104 {
105  ostr << "FixedBinAxis(\"" << getName() << "\", " << size() << ", "
106  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << getMin() << ", "
107  << getMax() << ")";
108 }

References getMax(), getMin(), IAxis::getName(), and size().

Here is the call graph for this function:

◆ equals()

bool FixedBinAxis::equals ( const IAxis other) const
protectedvirtual

Reimplemented from IAxis.

Definition at line 110 of file FixedBinAxis.cpp.

111 {
112  if (!IAxis::equals(other))
113  return false;
114  if (const FixedBinAxis* otherAxis = dynamic_cast<const FixedBinAxis*>(&other)) {
115  if (size() != otherAxis->size())
116  return false;
117  if (!algo::almostEqual(m_start, otherAxis->m_start))
118  return false;
119  if (!algo::almostEqual(m_end, otherAxis->m_end))
120  return false;
121  return true;
122  }
123  return false;
124 }
virtual bool equals(const IAxis &other) const
Definition: IAxis.cpp:18
bool almostEqual(double a, double b)
Returns true if two doubles agree within machine epsilon.
Definition: Algorithms.h:30

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

Here is the call graph for this function:

◆ getName()

◆ setName()

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

Sets the axis label.

Definition at line 43 of file IAxis.h.

43 { m_name = name; }

References IAxis::m_name.

◆ operator==()

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

test for equality

Definition at line 63 of file IAxis.h.

63 { return equals(right); }

References IAxis::equals().

Here is the call graph for this function:

◆ operator!=()

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

Definition at line 64 of file IAxis.h.

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

◆ contains()

bool IAxis::contains ( double  value) const
virtualinherited

Returns true if axis contains given point.

Definition at line 40 of file IAxis.cpp.

41 {
42  return value >= getMin() && value < getMax();
43 }
virtual double getMin() const =0
Returns value of first point of axis.
virtual double getMax() const =0
Returns value of last point of axis.

References IAxis::getMax(), and IAxis::getMin().

Referenced by Histogram1D::crop(), Histogram2D::crop(), RectangularDetector::indexOfSpecular(), and SphericalDetector::indexOfSpecular().

Here is the call graph for this function:

Member Data Documentation

◆ m_nbins

size_t FixedBinAxis::m_nbins
private

Definition at line 62 of file FixedBinAxis.h.

Referenced by clone(), findClosestIndex(), getBin(), operator[](), and size().

◆ m_start

double FixedBinAxis::m_start
private

Definition at line 63 of file FixedBinAxis.h.

Referenced by clone(), equals(), findClosestIndex(), getBin(), getMin(), and operator[]().

◆ m_end

double FixedBinAxis::m_end
private

Definition at line 64 of file FixedBinAxis.h.

Referenced by clone(), equals(), findClosestIndex(), getBin(), getMax(), and operator[]().

◆ m_name

std::string IAxis::m_name
privateinherited

axis name

Definition at line 92 of file IAxis.h.

Referenced by IAxis::getName(), and IAxis::setName().


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