BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
HistoUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file Device/Histo/HistoUtils.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 "Base/Math/Numeric.h"
17 #include "Device/Data/DataUtils.h"
21 #include <cmath>
22 #include <iostream>
23 #include <tspectrum.h> // third-party code, extracted from CERN ROOT (class TSpectrum2)
24 
25 std::vector<std::pair<double, double>> HistoUtils::FindPeaks(const Histogram2D& hist, double sigma,
26  const std::string& option,
27  double threshold)
28 {
29  std::unique_ptr<OutputData<double>> data(hist.createOutputData());
30  std::vector<std::vector<double>> arr = ArrayUtils::createVector2D(*data);
31  tspectrum::Spectrum2D spec;
32  auto peaks = spec.find_peaks(arr, sigma, option, threshold);
33 
34  // coordinates of peaks in histogram axes units
35  std::vector<std::pair<double, double>> result;
36 
37  for (const auto& p : peaks) {
38  double row_value = p.first;
39  double col_value = p.second;
40 
41  size_t xaxis_index = static_cast<size_t>(col_value);
42  size_t yaxis_index = hist.yAxis().size() - 1 - static_cast<size_t>(row_value);
43 
44  Bin1D xbin = hist.xAxis().bin(xaxis_index);
45  Bin1D ybin = hist.yAxis().bin(yaxis_index);
46 
47  double dx = col_value - static_cast<size_t>(col_value);
48  double dy = -1.0 * (row_value - static_cast<size_t>(row_value));
49 
50  double x = xbin.center() + xbin.binSize() * dx;
51  double y = ybin.center() + ybin.binSize() * dy;
52 
53  result.push_back(std::make_pair(x, y));
54  }
55  return result;
56 }
57 
58 //! Returns sum of relative differences between each pair of elements:
59 //! (a, b) -> 2*abs(a - b)/(|a| + |b|) ( and zero if a=b=0 within epsilon )
61 {
62  if (dat.size() != ref.size())
63  throw std::runtime_error("Error in HistoUtils::RelativeDifference: "
64  "different number of elements");
65  if (dat.empty())
66  return 0.0;
67  double sum_of_diff = 0.0;
68  for (size_t i = 0; i < dat.size(); ++i)
69  sum_of_diff += Numeric::GetRelativeDifference(dat[i], ref[i]);
70  return sum_of_diff / dat.size();
71 }
72 
74 {
76  *std::unique_ptr<OutputData<double>>(dat.getData().meanValues()),
77  *std::unique_ptr<OutputData<double>>(ref.getData().meanValues()));
78 }
79 
80 bool HistoUtils::agreesWithReference(const SimulationResult& dat, const std::string& refFileName,
81  double tol)
82 {
83  std::unique_ptr<OutputData<double>> refDat{IntensityDataIOFactory::readOutputData(refFileName)};
84  if (!refDat) {
85  std::cerr << "Could not read reference data from file " << refFileName << std::endl;
86  return false;
87  }
88  std::unique_ptr<OutputData<double>> datDat(dat.data());
89  return DataUtils::checkRelativeDifference(*datDat, *refDat, tol);
90 }
Defines namespace DataUtils.
PyObvject forward declaration.
Defines class Histogram2D.
Defines class IntensityDataIOFactory.
Defines constants and "almost equal" in namespace Numeric.
Defines class SimulationResult.
Two dimensional histogram.
Definition: Histogram2D.h:24
virtual Bin1D bin(size_t index) const =0
retrieve a 1d bin for the given index
virtual size_t size() const =0
retrieve the number of bins
Base class for 1D and 2D histograms holding values of double type.
Definition: IHistogram.h:27
const IAxis & yAxis() const
returns y-axis for 2D histograms
Definition: IHistogram.cpp:50
const IAxis & xAxis() const
returns x-axis
Definition: IHistogram.cpp:44
const OutputData< CumulativeValue > & getData() const
Definition: IHistogram.cpp:126
OutputData< double > * createOutputData(DataType dataType=DataType::INTEGRAL) const
creates new OutputData with histogram's shape and values corresponding to DataType
Definition: IHistogram.cpp:343
static OutputData< double > * readOutputData(const std::string &file_name)
Reads file and returns newly created OutputData object.
OutputData< double > * meanValues() const
Definition: OutputData.h:285
Wrapper around OutputData<double> that also provides unit conversions.
bool empty() const
size_t size() const
std::unique_ptr< OutputData< double > > data(Axes::Units units=Axes::Units::DEFAULT) const
decltype(auto) createVector2D(const T &data)
Creates 2D vector from OutputData.
bool checkRelativeDifference(const OutputData< double > &dat, const OutputData< double > &ref, const double threshold)
Returns true is relative difference is below threshold; prints informative output.
Definition: DataUtils.cpp:51
double relativeDataDifference(const OutputData< double > &dat, const OutputData< double > &ref)
Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
Definition: DataUtils.cpp:35
std::vector< std::pair< double, double > > FindPeaks(const Histogram2D &hist, double sigma=2, const std::string &option={}, double threshold=0.05)
Returns vector of peak center coordinates, for peaks in given histogram.
Definition: HistoUtils.cpp:25
double getRelativeDifference(const IHistogram &dat, const IHistogram &ref)
Definition: HistoUtils.cpp:73
bool agreesWithReference(const SimulationResult &dat, const std::string &refFileName, double tol)
Returns true if SimulatioResult agrees with data from reference file.
Definition: HistoUtils.cpp:80
double RelativeDifference(const SimulationResult &dat, const SimulationResult &ref)
Returns sum of relative differences between each pair of elements: (a, b) -> 2*abs(a - b)/(a + b) ( a...
Definition: HistoUtils.cpp:60
double GetRelativeDifference(double a, double b)
Returns the safe relative difference, which is 2(|a-b|)/(|a|+|b|) except in special cases.
Definition: Numeric.cpp:29
Definition: Bin.h:20
double center() const
Definition: Bin.h:25
double binSize() const
Definition: Bin.h:26