BornAgain  1.18.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SpectrumUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************** //
2 //
3 // BornAgain: simulate and fit scattering at grazing incidence
4 //
5 //! @file Device/Instrument/SpectrumUtils.cpp
6 //! @brief PyObvject forward declaration.
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 
16 #include <cmath>
17 #include <tspectrum.h>
18 
19 std::vector<std::pair<double, double>> SpectrumUtils::FindPeaks(const Histogram2D& hist,
20  double sigma,
21  const std::string& option,
22  double threshold)
23 {
24  std::unique_ptr<OutputData<double>> data(hist.createOutputData());
25  std::vector<std::vector<double>> arr = ArrayUtils::createVector2D(*data);
26  tspectrum::Spectrum2D spec;
27  auto peaks = spec.find_peaks(arr, sigma, option, threshold);
28 
29  // coordinates of peaks in histogram axes units
30  std::vector<std::pair<double, double>> result;
31 
32  for (const auto& p : peaks) {
33  double row_value = p.first;
34  double col_value = p.second;
35 
36  size_t xaxis_index = static_cast<size_t>(col_value);
37  size_t yaxis_index = hist.getYaxis().size() - 1 - static_cast<size_t>(row_value);
38 
39  Bin1D xbin = hist.getXaxis().getBin(xaxis_index);
40  Bin1D ybin = hist.getYaxis().getBin(yaxis_index);
41 
42  double dx = col_value - static_cast<size_t>(col_value);
43  double dy = -1.0 * (row_value - static_cast<size_t>(row_value));
44 
45  double x = xbin.getMidPoint() + xbin.getBinSize() * dx;
46  double y = ybin.getMidPoint() + ybin.getBinSize() * dy;
47 
48  result.push_back(std::make_pair(x, y));
49  }
50  return result;
51 }
PyObvject forward declaration.
Two dimensional histogram.
Definition: Histogram2D.h:25
virtual Bin1D getBin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
retrieve the number of bins
OutputData< double > * createOutputData(DataType dataType=DataType::INTEGRAL) const
creates new OutputData with histogram's shape and values corresponding to DataType
Definition: IHistogram.cpp:334
const IAxis & getYaxis() const
returns y-axis for 2D histograms
Definition: IHistogram.cpp:50
const IAxis & getXaxis() const
returns x-axis
Definition: IHistogram.cpp:44
decltype(auto) createVector2D(const T &data)
Creates 2D vector from OutputData.
std::vector< std::pair< double, double > > FindPeaks(const Histogram2D &hist, double sigma=2, const std::string &option={}, double threshold=0.05)
Definition: Bin.h:20
double getBinSize() const
Definition: Bin.h:26
double getMidPoint() const
Definition: Bin.h:25