BornAgain  1.19.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 reflection and scattering
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 
17 #include "Base/Py/PyUtils.h"
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 = xAxis();
45  if (!axis.contains(x))
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::binCenters() const
53 {
54  return xAxis().binCenters();
55 }
56 
57 std::vector<double> Histogram1D::binValues() const
58 {
59  return IHistogram::getDataVector(IHistogram::DataType::INTEGRAL);
60 }
61 
62 std::vector<double> Histogram1D::binErrors() 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(xAxis().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->xAxis().contains(x)) {
95  *it_result = *it_origin;
96  ++it_result;
97  }
98  ++it_origin;
99  }
100  return result;
101 }
Defines class Histogram1D.
_object PyObject
Definition: PyObject.h:25
Defines PyUtils namespace.
Defines class VariableBinAxis.
Axis with fixed bin size.
Definition: FixedBinAxis.h:23
One dimensional histogram.
Definition: Histogram1D.h:23
std::vector< double > binValues() const
returns vector of bin content (the value accumulated by bins)
Definition: Histogram1D.cpp:57
int fill(double x, double weight=1.0)
Increment bin with abscissa x with a weight.
Definition: Histogram1D.cpp:42
std::vector< double > binCenters() const
returns vector of histogram bin centers
Definition: Histogram1D.cpp:52
PyObject * binErrorsNumpy() const
Definition: Histogram1D.cpp:79
PyObject * binValuesNumpy() const
Definition: Histogram1D.cpp:74
std::vector< double > binErrors() 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
Histogram1D(int nbinsx, double xlow, double xup)
Constructor for fix bin size histograms.
Definition: Histogram1D.cpp:20
PyObject * binCentersNumpy() const
Definition: Histogram1D.cpp:69
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:37
virtual std::vector< double > binCenters() const
Definition: IAxis.cpp:22
virtual size_t findClosestIndex(double value) const =0
find bin index which is best match for given value
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
const IAxis & xAxis() const
returns x-axis
Definition: IHistogram.cpp:44
std::vector< double > getDataVector(DataType dataType) const
returns vector of values of requested DataType
Definition: IHistogram.cpp:320
OutputData< CumulativeValue > m_data
Definition: IHistogram.h:193
void init_from_data(const OutputData< double > &source)
Definition: IHistogram.cpp:288
iterator end()
Returns read/write iterator that points to the one past last element.
Definition: OutputData.h:93
void addAxis(const IAxis &new_axis)
Definition: OutputData.h:295
iterator begin()
Returns read/write iterator that points to the first element.
Definition: OutputData.h:343
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:430
Axis with variable bin size.
PyObject * createNumpyArray(const std::vector< double > &data)
Definition: PyUtils.cpp:172