BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
Histogram1D.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Histo/Histogram1D.cpp
6 //! @brief Implements class Histogram1D.
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************** //
14 
18 #include <memory>
19 
20 Histogram1D::Histogram1D(int nbinsx, double xlow, double xup)
21 {
22  m_data.addAxis(FixedBinAxis("x-axis", nbinsx, xlow, xup));
23 }
24 
25 Histogram1D::Histogram1D(int nbinsx, const std::vector<double>& xbins)
26 {
27  m_data.addAxis(VariableBinAxis("x-axis", nbinsx, xbins));
28 }
29 
30 Histogram1D::Histogram1D(const IAxis& axis) : IHistogram(axis) {}
31 
33 {
34  init_from_data(data);
35 }
36 
38 {
39  return new Histogram1D(*this);
40 }
41 
42 int Histogram1D::fill(double x, double weight)
43 {
44  const IAxis& axis = getXaxis();
45  if (x < axis.getMin() || x >= axis.getMax())
46  return -1;
47  size_t index = axis.findClosestIndex(x);
48  m_data[index].add(weight);
49  return (int)index;
50 }
51 
52 std::vector<double> Histogram1D::getBinCenters() const
53 {
54  return getXaxis().getBinCenters();
55 }
56 
57 std::vector<double> Histogram1D::getBinValues() const
58 {
59  return IHistogram::getDataVector(IHistogram::DataType::INTEGRAL);
60 }
61 
62 std::vector<double> Histogram1D::getBinErrors() const
63 {
64  return IHistogram::getDataVector(IHistogram::DataType::STANDARD_ERROR);
65 }
66 
67 #ifdef BORNAGAIN_PYTHON
68 
70 {
72 }
73 
75 {
77 }
78 
80 {
82 }
83 
84 #endif // BORNAGAIN_PYTHON
85 
86 Histogram1D* Histogram1D::crop(double xmin, double xmax)
87 {
88  const std::unique_ptr<IAxis> xaxis(getXaxis().createClippedAxis(xmin, xmax));
89  Histogram1D* result = new Histogram1D(*xaxis);
92  while (it_origin != m_data.end()) {
93  double x = m_data.getAxisValue(it_origin.getIndex(), 0);
94  if (result->getXaxis().contains(x)) {
95  *it_result = *it_origin;
96  ++it_result;
97  }
98  ++it_origin;
99  }
100  return result;
101 }
Defines various functions to interact from numpy on Python side.
Defines class Histogram1D.
_object PyObject
Definition: PyObject.h:20
Defines class VariableBinAxis.
Axis with fixed bin size.
Definition: FixedBinAxis.h:24
One dimensional histogram.
Definition: Histogram1D.h:24
std::vector< double > getBinCenters() const
returns vector of histogram bin centers
Definition: Histogram1D.cpp:52
int fill(double x, double weight=1.0)
Increment bin with abscissa x with a weight.
Definition: Histogram1D.cpp:42
std::vector< double > getBinErrors() const
returns vector of bin errors
Definition: Histogram1D.cpp:62
Histogram1D * crop(double xmin, double xmax)
Creates new histogram by applying crop on axis.
Definition: Histogram1D.cpp:86
PyObject * getBinCentersNumpy() const
Definition: Histogram1D.cpp:69
std::vector< double > getBinValues() const
returns vector of bin content (the value accumulated by bins)
Definition: Histogram1D.cpp:57
Histogram1D(int nbinsx, double xlow, double xup)
Constructor for fix bin size histograms.
Definition: Histogram1D.cpp:20
PyObject * getBinErrorsNumpy() const
Definition: Histogram1D.cpp:79
PyObject * getBinValuesNumpy() const
Definition: Histogram1D.cpp:74
Histogram1D * clone() const
Returns clone of other histogram.
Definition: Histogram1D.cpp:37
Interface for one-dimensional axes.
Definition: IAxis.h:25
virtual bool contains(double value) const
Returns true if axis contains given point.
Definition: IAxis.cpp:40
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
virtual double getMin() const =0
Returns value of first point of axis.
virtual std::vector< double > getBinCenters() const
Definition: IAxis.cpp:23
virtual double getMax() const =0
Returns value of last point of axis.
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
std::vector< double > getDataVector(DataType dataType) const
returns vector of values of requested DataType
Definition: IHistogram.cpp:311
OutputData< CumulativeValue > m_data
Definition: IHistogram.h:193
void init_from_data(const OutputData< double > &source)
Definition: IHistogram.cpp:278
const IAxis & getXaxis() const
returns x-axis
Definition: IHistogram.cpp:44
iterator end()
Returns read/write iterator that points to the one past last element.
Definition: OutputData.h:96
void addAxis(const IAxis &new_axis)
Definition: OutputData.h:289
iterator begin()
Returns read/write iterator that points to the first element.
Definition: OutputData.h:344
double getAxisValue(size_t global_index, size_t i_selected_axis) const
Returns the value of selected axis for given global_index.
Definition: OutputData.h:433
Axis with variable bin size.
PyObject * createNumpyArray(const std::vector< double > &data)
Definition: ArrayUtils.cpp:20