Mantid
Loading...
Searching...
No Matches
EqualBinsChecker.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include <cmath>
10#include <ostream>
11#include <sstream>
12#include <stdexcept>
13#include <vector>
14
15namespace {
16// Initialize the logger
17Mantid::Kernel::Logger g_log("EqualBinsChecker");
18} // namespace
19
20namespace Mantid::Kernel {
21
32EqualBinsChecker::EqualBinsChecker(const MantidVec &xData, const double errorLevel, const double warningLevel)
33 : m_xData(xData), m_errorLevel(errorLevel), m_warn(warningLevel > 0), m_warningLevel(warningLevel),
34 m_refBinType(ReferenceBin::Average), m_errorType(ErrorType::Cumulative) {}
35
42void EqualBinsChecker::setReferenceBin(const ReferenceBin &refBinType) { m_refBinType = refBinType; }
43
50void EqualBinsChecker::setErrorType(const ErrorType &errorType) { m_errorType = errorType; }
51
56std::string EqualBinsChecker::validate() const {
57 const auto &xData = m_xData;
58 const auto xSize = xData.size();
59 // First check for empty input
60 if (xSize == 0) {
61 return "Input workspace must not be empty";
62 }
63
64 // reference bin width to compare to
65 const double dx = getReferenceDx();
66
67 // Check each width against dx
68 bool printWarning = false;
69 for (size_t bin = 0; bin < xSize - 2; bin++) {
70 const double diff = getDifference(bin, dx);
71 if (diff > m_errorLevel) {
72 std::stringstream errorStr;
73 errorStr << "X axis must be linear (all bins must have the same width) ";
74 errorStr << "dx=" << xData[bin + 1] - xData[bin] << " reference dx=" << dx << " bin number=" << bin;
75 return errorStr.str();
76 } else if (m_warn && diff > m_warningLevel) {
77 // just warn the user
78 printWarning = true;
79 }
80 }
81
82 if (printWarning) {
83 g_log.warning() << "Bin widths differ by more than " << m_warningLevel * 100 << "% of average." << std::endl;
84 }
85
86 return std::string();
87}
88
96 if (m_xData.size() < 2) {
97 throw std::runtime_error("No bins in input X data");
98 }
99
101 // average bin width
102 const auto xSize = m_xData.size();
103 return (m_xData[xSize - 1] - m_xData[0]) / static_cast<double>(xSize - 1);
104 } else {
105 // first bin width
106 return m_xData[1] - m_xData[0];
107 }
108}
109
117double EqualBinsChecker::getDifference(const size_t bin, const double dx) const {
118 if (bin > m_xData.size() - 2) {
119 throw std::invalid_argument("Not enough bins in input X data");
120 }
121
123 return std::fabs(dx - m_xData[bin + 1] + m_xData[bin]) / dx;
124 } else {
125 // cumulative errors
126 return std::fabs((m_xData[bin + 1] - m_xData[0] - static_cast<double>(bin + 1) * dx) / dx);
127 }
128}
129
130} // namespace Mantid::Kernel
virtual double getDifference(const size_t bin, const double dx) const
Returns the error (simple or cumulative) at the given point.
virtual double getReferenceDx() const
Returns the bin width to compare against: either the average or the first depending on options.
EqualBinsChecker(const MantidVec &xData, const double errorLevel, const double warningLevel=-1)
Constructor, setting data and thresholds for errors and warnings.
virtual std::string validate() const
Perform validation of the given X array.
virtual void setErrorType(const ErrorType &errorType)
Set whether to use cumulative errors or compare each in turn.
ErrorType
Type of errors to check.
ReferenceBin
Type of bin to compare others to.
virtual void setReferenceBin(const ReferenceBin &refBinType)
Set whether to compare each bin to the first bin width or the average.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172