BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
FixedBinAxis Class Reference

Axis with fixed bin size. More...

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

Public Member Functions

 FixedBinAxis (const std::string &name, size_t nbins, double start, double end)
 FixedBinAxis constructor. More...
 
virtual ~FixedBinAxis ()
 
Bin1D bin (size_t index) const
 retrieve a 1d bin for the given index More...
 
std::vector< double > binBoundaries () const
 
double binCenter (size_t index) const
 
std::vector< double > binCenters () const
 
double center () const
 Returns midpoint of axis. More...
 
FixedBinAxisclone () const
 clone function More...
 
virtual bool contains (double value) const
 Returns true if axis contains given point. More...
 
FixedBinAxiscreateClippedAxis (double left, double right) const
 Creates a new clipped axis. More...
 
size_t findClosestIndex (double value) const
 find bin index which is best match for given value More...
 
std::string getName () const
 retrieve the label of the axis More...
 
double lowerBound () const
 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
 indexed accessor retrieves a sample More...
 
void setName (std::string name)
 Sets the axis label. More...
 
size_t size () const
 retrieve the number of bins More...
 
double span () const
 Returns distance from first to last point. More...
 
double upperBound () const
 Returns value of last point of axis. More...
 

Protected Member Functions

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

Private Attributes

double m_end
 
std::string m_name
 axis name More...
 
size_t m_nbins
 
double m_start
 

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 20 of file FixedBinAxis.cpp.

21  : IAxis(name), m_nbins(nbins), m_start(start), m_end(end)
22 {
23 }
double m_start
Definition: FixedBinAxis.h:60
double m_end
Definition: FixedBinAxis.h:61
size_t m_nbins
Definition: FixedBinAxis.h:59
IAxis(const std::string &name)
constructors
Definition: IAxis.h:28
QString const & name(EShape k)
Definition: particles.cpp:21

Referenced by clone(), and createClippedAxis().

◆ ~FixedBinAxis()

virtual FixedBinAxis::~FixedBinAxis ( )
inlinevirtual

Definition at line 31 of file FixedBinAxis.h.

31 {}

Member Function Documentation

◆ bin()

Bin1D FixedBinAxis::bin ( size_t  index) const
virtual

retrieve a 1d bin for the given index

Implements IAxis.

Definition at line 40 of file FixedBinAxis.cpp.

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

References m_end, m_nbins, and m_start.

Referenced by binBoundaries(), binCenters(), and createClippedAxis().

◆ binBoundaries()

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

Reimplemented from IAxis.

Definition at line 72 of file FixedBinAxis.cpp.

73 {
74  std::vector<double> result;
75  result.resize(size() + 1, 0.0);
76  for (size_t i = 0; i < size(); ++i) {
77  result[i] = bin(i).m_lower;
78  }
79  result[size()] = bin(size() - 1).m_upper;
80  return result;
81 }
Bin1D bin(size_t index) const
retrieve a 1d bin for the given index
size_t size() const
retrieve the number of bins
Definition: FixedBinAxis.h:35
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 bin(), Bin1D::m_lower, Bin1D::m_upper, and size().

Here is the call graph for this function:

◆ binCenter()

double FixedBinAxis::binCenter ( size_t  index) const
inlinevirtual

Implements IAxis.

Definition at line 44 of file FixedBinAxis.h.

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

◆ binCenters()

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

Reimplemented from IAxis.

Definition at line 62 of file FixedBinAxis.cpp.

63 {
64  std::vector<double> result;
65  result.resize(size(), 0.0);
66  for (size_t i = 0; i < size(); ++i) {
67  result[i] = bin(i).center();
68  }
69  return result;
70 }
double center() const
Definition: Bin.h:25

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

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

Here is the call graph for this function:

◆ center()

double IAxis::center ( ) const
inherited

Returns midpoint of axis.

Definition at line 47 of file IAxis.cpp.

48 {
49  return (upperBound() + lowerBound()) / 2;
50 }
virtual double upperBound() const =0
Returns value of last point of axis.
virtual double lowerBound() const =0
Returns value of first point of axis.

References IAxis::lowerBound(), and IAxis::upperBound().

Here is the call graph for this function:

◆ clone()

FixedBinAxis * FixedBinAxis::clone ( ) const
virtual

clone function

Implements IAxis.

Definition at line 25 of file FixedBinAxis.cpp.

26 {
28  return result;
29 }
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
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:

◆ contains()

bool IAxis::contains ( double  value) const
virtualinherited

Returns true if axis contains given point.

Definition at line 37 of file IAxis.cpp.

38 {
39  return value >= lowerBound() && value < upperBound();
40 }

References IAxis::lowerBound(), and IAxis::upperBound().

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

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 83 of file FixedBinAxis.cpp.

84 {
85  if (left >= right)
86  throw std::runtime_error("FixedBinAxis::createClippedAxis() -> Error. "
87  "'left' should be smaller than 'right'");
88 
89  if (left < lowerBound())
90  left = bin(0).center();
91  if (right >= upperBound())
92  right = bin(size() - 1).center();
93 
94  size_t nbin1 = findClosestIndex(left);
95  size_t nbin2 = findClosestIndex(right);
96 
97  return new FixedBinAxis(getName(), nbin2 - nbin1 + 1, bin(nbin1).m_lower, bin(nbin2).m_upper);
98 }
size_t findClosestIndex(double value) const
find bin index which is best match for given value
double lowerBound() const
Returns value of first point of axis.
Definition: FixedBinAxis.h:41
double upperBound() const
Returns value of last point of axis.
Definition: FixedBinAxis.h:42

References FixedBinAxis(), bin(), Bin1D::center(), findClosestIndex(), IAxis::getName(), lowerBound(), size(), and upperBound().

Here is the call graph for this function:

◆ equals()

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

Reimplemented from IAxis.

Definition at line 107 of file FixedBinAxis.cpp.

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

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

Here is the call graph for this function:

◆ findClosestIndex()

size_t FixedBinAxis::findClosestIndex ( double  value) const
virtual

find bin index which is best match for given value

Implements IAxis.

Definition at line 50 of file FixedBinAxis.cpp.

51 {
52  if (value < lowerBound()) {
53  return 0;
54  } else if (value >= upperBound()) {
55  return m_nbins - 1;
56  }
57 
58  double step = (m_end - m_start) / m_nbins;
59  return int((value - m_start) / step);
60 }

References lowerBound(), m_end, m_nbins, m_start, and upperBound().

Referenced by createClippedAxis().

Here is the call graph for this function:

◆ getName()

◆ lowerBound()

double FixedBinAxis::lowerBound ( ) const
inlinevirtual

Returns value of first point of axis.

Implements IAxis.

Definition at line 41 of file FixedBinAxis.h.

41 { return m_start; }

References m_start.

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

◆ operator!=()

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

Definition at line 70 of file IAxis.h.

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

◆ operator==()

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

test for equality

Definition at line 69 of file IAxis.h.

69 { return equals(right); }

References IAxis::equals().

Here is the call graph for this function:

◆ operator[]()

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

indexed accessor retrieves a sample

Implements IAxis.

Definition at line 31 of file FixedBinAxis.cpp.

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

References m_end, m_nbins, and m_start.

◆ print()

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

Implements IAxis.

Definition at line 100 of file FixedBinAxis.cpp.

101 {
102  ostr << "FixedBinAxis(\"" << getName() << "\", " << size() << ", "
103  << std::setprecision(std::numeric_limits<double>::digits10 + 2) << lowerBound() << ", "
104  << upperBound() << ")";
105 }

References IAxis::getName(), lowerBound(), size(), and upperBound().

Here is the call graph for this function:

◆ 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, and RealSpace::Particles::name().

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 35 of file FixedBinAxis.h.

35 { return m_nbins; }

References m_nbins.

Referenced by StandardSimulations::BasicSpecularQ(), binBoundaries(), binCenters(), createClippedAxis(), equals(), print(), and StandardSimulations::TOFRWithPointwiseResolution().

◆ span()

double IAxis::span ( ) const
inherited

Returns distance from first to last point.

Definition at line 42 of file IAxis.cpp.

43 {
44  return upperBound() - lowerBound();
45 }

References IAxis::lowerBound(), and IAxis::upperBound().

Referenced by RectangularDetector::getHeight(), RectangularDetector::getWidth(), and DetectorUtils::isQuadratic().

Here is the call graph for this function:

◆ upperBound()

double FixedBinAxis::upperBound ( ) const
inlinevirtual

Returns value of last point of axis.

Implements IAxis.

Definition at line 42 of file FixedBinAxis.h.

42 { return m_end; }

References m_end.

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

Member Data Documentation

◆ m_end

double FixedBinAxis::m_end
private

Definition at line 61 of file FixedBinAxis.h.

Referenced by bin(), clone(), equals(), findClosestIndex(), operator[](), and upperBound().

◆ m_name

std::string IAxis::m_name
privateinherited

axis name

Definition at line 96 of file IAxis.h.

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

◆ m_nbins

size_t FixedBinAxis::m_nbins
private

Definition at line 59 of file FixedBinAxis.h.

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

◆ m_start

double FixedBinAxis::m_start
private

Definition at line 60 of file FixedBinAxis.h.

Referenced by bin(), clone(), equals(), findClosestIndex(), lowerBound(), and operator[]().


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