Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
EqualBinsChecker.cpp 4.36 KiB
#include "MantidKernel/EqualBinsChecker.h"
#include "MantidKernel/Logger.h"
#include <ostream>
#include <cmath>
#include <stdexcept>
#include <vector>

namespace {
// Initialize the logger
Mantid::Kernel::Logger g_log("EqualBinsChecker");
}

namespace Mantid {
namespace Kernel {

/**
 * Constructor, setting data and thresholds for errors and warnings.
 * By default, checks against average bin width and uses cumulative errors.
 * @param xData :: [input] Reference to bins to check
 * @param errorLevel :: [input] Threshold for error. If bin differences are
 * larger than this, check fails.
 * @param warningLevel :: [input] Threshold for warning. If bin difference are
 * larger than this, the user is warned but the check doesn't necessarily fail.
 * If set negative, warnings are off and only error level is used (default).
 */
EqualBinsChecker::EqualBinsChecker(const MantidVec &xData,
                                   const double errorLevel,
                                   const double warningLevel)
    : m_xData(xData), m_errorLevel(errorLevel), m_warn(warningLevel > 0),
      m_warningLevel(warningLevel), m_refBinType(ReferenceBin::Average),
      m_errorType(ErrorType::Cumulative) {}

/**
 * Set whether to compare each bin to the first bin width or the average.
 * Used for FFT without "AcceptXRoundingErrors" - compatibility with previous
 * behaviour.
 * @param refBinType :: [input] Either average bin or first bin
 */
void EqualBinsChecker::setReferenceBin(const ReferenceBin &refBinType) {
  m_refBinType = refBinType;
}

/**
 * Set whether to use cumulative errors or compare each in turn.
 * Used for FFT without "AcceptXRoundingErrors" - compatibility with previous
 * behaviour.
 * @param errorType :: [input] Either cumulative or individual errors
 */
void EqualBinsChecker::setErrorType(const ErrorType &errorType) {
  m_errorType = errorType;
}

/**
 * Perform validation of the given X array
 * @returns :: Error string (empty if no error)
 */
std::string EqualBinsChecker::validate() const {
  const auto &xData = m_xData;
  const auto xSize = xData.size();
  // First check for empty input
  if (xSize == 0) {
    return "Input workspace must not be empty";
  }

  // reference bin width to compare to
  const double dx = getReferenceDx();

  // Check each width against dx
  bool printWarning = false;
  for (size_t bin = 0; bin < xSize - 2; bin++) {