BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
IHistogram.h
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Histo/IHistogram.h
6 //! @brief Defines class IHistogram.
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 
15 #ifndef BORNAGAIN_CORE_INTENSITY_IHISTOGRAM_H
16 #define BORNAGAIN_CORE_INTENSITY_IHISTOGRAM_H
17 
19 #include "Device/Data/OutputData.h"
20 
21 class Histogram1D;
22 
23 //! Base class for 1D and 2D histograms holding values of double type.
24 //! @ingroup tools
25 
27 {
28 public:
30 
32  IHistogram(const IHistogram& other);
33  virtual ~IHistogram() {}
34 
35  IHistogram(const IAxis& axis_x);
36  IHistogram(const IAxis& axis_x, const IAxis& axis_y);
37 
38  virtual IHistogram* clone() const = 0;
39 
40  //! Returns number of histogram dimensions.
41  virtual size_t getRank() const = 0;
42 
43  //! Returns total number of histogram bins. For 2D histograms the result will be the product
44  //! of bin numbers along X and Y axes.
45  size_t getTotalNumberOfBins() const;
46 
47  //! returns x-axis
48  const IAxis& getXaxis() const;
49 
50  //! returns y-axis for 2D histograms
51  const IAxis& getYaxis() const;
52 
53  //! Returns x-axis min (lower edge of first bin).
54  double getXmin() const;
55 
56  //! Returns x-axis max (upper edge of last bin).
57  double getXmax() const;
58 
59  //! Returns number of bins on x-axis
60  size_t getNbinsX() const;
61 
62  //! Returns y-axis min (lower edge of first bin) for 2D histograms.
63  double getYmin() const;
64 
65  //! Returns y-axis max (upper edge of last bin) for 2D histograms.
66  double getYmax() const;
67 
68  //! Returns number of bins on y-axis
69  size_t getNbinsY() const;
70 
71  //! Returns global bin index for given axes indices. For 1D, just returns binx.
72  size_t getGlobalBin(size_t binx, size_t biny = 0) const;
73 
74  //! Returns closest global bin index for given axes coordinates.
75  size_t findGlobalBin(double x, double y) const;
76 
77  //! Returns x-axis index for global bin index i.
78  size_t getXaxisIndex(size_t i) const;
79 
80  //! Returns y-axis index for global bin index i.
81  size_t getYaxisIndex(size_t i) const;
82 
83  //! Returns the center of bin i of the x axis.
84  double getXaxisValue(size_t i);
85 
86  //! Returns the center of bin i of the y axis.
87  double getYaxisValue(size_t i);
88 
89  //! Returns content (accumulated value) of bin i.
90  double getBinContent(size_t i) const;
91 
92  const OutputData<CumulativeValue>& getData() const { return m_data; }
94 
95  //! Returns content (accumulated value) of the 2D histogram bin.
96  double getBinContent(size_t binx, size_t biny) const;
97 
98  //! Sets content of the bin corresponding to the globalbin number
99  void setBinContent(size_t i, double value);
100 
101  //! Add the value to the bin
102  void addBinContent(size_t i, double value);
103 
104  //! Returns error of the bin with given index.
105  double getBinError(size_t i) const;
106 
107  //! Returns error of the bin with given indices (for 2D histograms).
108  double getBinError(size_t binx, size_t biny) const;
109 
110  //! Returns average value in the bin with given index.
111  double getBinAverage(size_t i) const;
112 
113  //! Returns average value of the bin with given indices (for 2D histograms).
114  double getBinAverage(size_t binx, size_t biny) const;
115 
116  //! Returns number of entries in the bin with given index.
117  int getBinNumberOfEntries(size_t i) const;
118 
119  //! Returns number of entries in the bin with given indices (for 2D histograms).
120  int getBinNumberOfEntries(size_t binx, size_t biny) const;
121 
122  //! Returns histogram maximum value (maximum of getBinContent() over all bins)
123  double getMaximum() const;
124 
125  //! Returns globalbin index with maximum content
126  size_t getMaximumBinIndex() const;
127 
128  //! Returns histogram minimum value (minimum of getBinContent() over all bins)
129  double getMinimum() const;
130 
131  //! Returns globalbin index with minimum content
132  size_t getMinimumBinIndex() const;
133 
134  //! Multiply this histogram (every bin content value) by a constant
135  void scale(double value);
136 
137  //! Returns integral of bins content (computed as a sum of all bin content).
138  double integral() const;
139 
140  // double& operator[](size_t index);
141  // const double& operator[](size_t index) const;
142 
143 #ifdef BORNAGAIN_PYTHON
144  //! Returns numpy array with bin content (accumulated values).
145  PyObject* array(DataType dataType = DataType::INTEGRAL) const;
146 
147  //! \deprecated Use array() instead.
148  PyObject* getArray(DataType dataType = DataType::INTEGRAL) const;
149 #endif
150 
151  //! Reset histogram content (axes remains)
152  void reset();
153 
154  static IHistogram* createHistogram(const OutputData<double>& source);
155 
156  //! create new histogram from file content
157  static IHistogram* createFrom(const std::string& filename);
158 
159  //! create new histogram from numpy array
160  static IHistogram* createFrom(const std::vector<std::vector<double>>& data);
161 
162  //! creates new OutputData with histogram's shape and values corresponding to DataType
163  OutputData<double>* createOutputData(DataType dataType = DataType::INTEGRAL) const;
164 
165  //! Returns true if objects a) have same dimensions b) bin boundaries of axes coincide
166  bool hasSameShape(const IHistogram& other) const;
167 
168  //! Returns true if object have same rank and number of axes bins
169  bool hasSameDimensions(const IHistogram& other) const;
170 
171  //! addition-assignment operator for two histograms
172  const IHistogram& operator+=(const IHistogram& right);
173 
174  //! returns histogram representing relative difference of two histograms.
176 
177  //! Saves histogram in file
178  //! Following formats are available: *.txt, *.tif, *.int (*.txt.gz, *.tif.gz, *.int.gz)
179  void save(const std::string& filename);
180 
181  //! Loads histogram from file, the shape of array in file should match
182  //! Following formats are available: *.txt, *.tif, *.int (*.txt.gz, *.tif.gz, *.int.gz)
183  //! Only bin content will be loaded, histogram axes remain the same.
184  void load(const std::string& filename);
185 
186 protected:
187  void check_x_axis() const;
188  void check_y_axis() const;
189  void init_from_data(const OutputData<double>& source);
190  double getBinData(size_t i, DataType dataType) const;
191  std::vector<double> getDataVector(DataType dataType) const;
192  void copyContentFrom(const IHistogram& other);
194 };
195 
196 #endif // BORNAGAIN_CORE_INTENSITY_IHISTOGRAM_H
Defines class CumulativeValue.
Defines and implements template class OutputData.
_object PyObject
Definition: PyObject.h:20
One dimensional histogram.
Definition: Histogram1D.h:24
Interface for one-dimensional axes.
Definition: IAxis.h:25
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
@ STANDARD_ERROR
Definition: IHistogram.h:29
double getYmin() const
Returns y-axis min (lower edge of first bin) for 2D histograms.
Definition: IHistogram.cpp:71
void addBinContent(size_t i, double value)
Add the value to the bin.
Definition: IHistogram.cpp:141
double getYmax() const
Returns y-axis max (upper edge of last bin) for 2D histograms.
Definition: IHistogram.cpp:76
void scale(double value)
Multiply this histogram (every bin content value) by a constant.
Definition: IHistogram.cpp:199
size_t findGlobalBin(double x, double y) const
Returns closest global bin index for given axes coordinates.
Definition: IHistogram.cpp:95
size_t getNbinsY() const
Returns number of bins on y-axis.
Definition: IHistogram.cpp:81
size_t getNbinsX() const
Returns number of bins on x-axis.
Definition: IHistogram.cpp:66
PyObject * getArray(DataType dataType=DataType::INTEGRAL) const
Definition: IHistogram.cpp:222
PyObject * array(DataType dataType=DataType::INTEGRAL) const
Returns numpy array with bin content (accumulated values).
Definition: IHistogram.cpp:216
size_t getGlobalBin(size_t binx, size_t biny=0) const
Returns global bin index for given axes indices. For 1D, just returns binx.
Definition: IHistogram.cpp:86
virtual size_t getRank() const =0
Returns number of histogram dimensions.
double getMinimum() const
Returns histogram minimum value (minimum of getBinContent() over all bins)
Definition: IHistogram.cpp:188
size_t getMinimumBinIndex() const
Returns globalbin index with minimum content.
Definition: IHistogram.cpp:194
void load(const std::string &filename)
Loads histogram from file, the shape of array in file should match Following formats are available: *...
Definition: IHistogram.cpp:385
double getBinData(size_t i, DataType dataType) const
returns data of requested type for globalbin number
Definition: IHistogram.cpp:295
void check_x_axis() const
Definition: IHistogram.cpp:258
double getXmin() const
Returns x-axis min (lower edge of first bin).
Definition: IHistogram.cpp:56
double getYaxisValue(size_t i)
Returns the center of bin i of the y axis.
Definition: IHistogram.cpp:120
OutputData< double > * createOutputData(DataType dataType=DataType::INTEGRAL) const
creates new OutputData with histogram's shape and values corresponding to DataType
Definition: IHistogram.cpp:334
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
static IHistogram * createFrom(const std::string &filename)
create new histogram from file content
Definition: IHistogram.cpp:248
void setBinContent(size_t i, double value)
Sets content of the bin corresponding to the globalbin number.
Definition: IHistogram.cpp:136
double integral() const
Returns integral of bins content (computed as a sum of all bin content).
Definition: IHistogram.cpp:206
double getBinContent(size_t i) const
Returns content (accumulated value) of bin i.
Definition: IHistogram.cpp:126
IHistogram * relativeDifferenceHistogram(const IHistogram &rhs)
returns histogram representing relative difference of two histograms.
Definition: IHistogram.cpp:364
size_t getYaxisIndex(size_t i) const
Returns y-axis index for global bin index i.
Definition: IHistogram.cpp:109
void check_y_axis() const
Definition: IHistogram.cpp:268
size_t getTotalNumberOfBins() const
Returns total number of histogram bins.
Definition: IHistogram.cpp:39
size_t getMaximumBinIndex() const
Returns globalbin index with maximum content.
Definition: IHistogram.cpp:182
size_t getXaxisIndex(size_t i) const
Returns x-axis index for global bin index i.
Definition: IHistogram.cpp:104
void copyContentFrom(const IHistogram &other)
Copy content (but not the axes) from other histogram. Dimensions should be the same.
Definition: IHistogram.cpp:322
double getMaximum() const
Returns histogram maximum value (maximum of getBinContent() over all bins)
Definition: IHistogram.cpp:176
void save(const std::string &filename)
Saves histogram in file Following formats are available: *.txt, *.tif, *.int (*.txt....
Definition: IHistogram.cpp:380
const OutputData< CumulativeValue > & getData() const
Definition: IHistogram.h:92
double getBinError(size_t i) const
Returns error of the bin with given index.
Definition: IHistogram.cpp:146
virtual IHistogram * clone() const =0
virtual ~IHistogram()
Definition: IHistogram.h:33
const IAxis & getYaxis() const
returns y-axis for 2D histograms
Definition: IHistogram.cpp:50
int getBinNumberOfEntries(size_t i) const
Returns number of entries in the bin with given index.
Definition: IHistogram.cpp:166
void reset()
Reset histogram content (axes remains)
Definition: IHistogram.cpp:228
double getBinAverage(size_t i) const
Returns average value in the bin with given index.
Definition: IHistogram.cpp:156
static IHistogram * createHistogram(const OutputData< double > &source)
Definition: IHistogram.cpp:233
double getXmax() const
Returns x-axis max (upper edge of last bin).
Definition: IHistogram.cpp:61
bool hasSameDimensions(const IHistogram &other) const
Returns true if object have same rank and number of axes bins.
Definition: IHistogram.cpp:349
bool hasSameShape(const IHistogram &other) const
Returns true if objects a) have same dimensions b) bin boundaries of axes coincide.
Definition: IHistogram.cpp:344
double getXaxisValue(size_t i)
Returns the center of bin i of the x axis.
Definition: IHistogram.cpp:114
const IHistogram & operator+=(const IHistogram &right)
addition-assignment operator for two histograms
Definition: IHistogram.cpp:354
void init_from_data(const OutputData< double > &source)
Definition: IHistogram.cpp:278
OutputData< CumulativeValue > & getData()
Definition: IHistogram.h:93
const IAxis & getXaxis() const
returns x-axis
Definition: IHistogram.cpp:44
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")