12using Mantid::HistogramData::Histogram;
19template <
typename vector_like>
size_t findXIndex(
const vector_like &vecx,
const double x,
const size_t startindex) {
21 if (
x <= vecx.front()) {
23 }
else if (
x >= vecx.back()) {
24 index = vecx.size() - 1;
26 const auto fiter = std::lower_bound(vecx.cbegin() + startindex, vecx.cend(),
x);
27 if (fiter == vecx.cend())
28 throw std::runtime_error(
"It seems impossible to have this value. ");
30 index =
static_cast<size_t>(fiter - vecx.cbegin());
52 size_t stop_index,
double &peak_center,
size_t &peak_center_index,
double &peak_height) {
53 const auto &vector_x = histogram.points();
56 auto peak_center_iter = std::lower_bound(vector_x.begin() + start_index, vector_x.begin() + stop_index, peak_center);
57 if (peak_center_iter == vector_x.begin() + stop_index)
59 peak_center_index =
static_cast<size_t>(peak_center_iter - vector_x.begin());
62 const auto &vector_y = histogram.y();
63 peak_center = *peak_center_iter;
64 peak_height = vector_y[peak_center_index] - bkgd_values.
getCalculated(peak_center_index - start_index);
69 const size_t windowSize = stop_index - start_index;
70 const size_t searchBox = std::max(
static_cast<size_t>(.3 *
static_cast<double>(windowSize)),
static_cast<size_t>(5));
71 size_t left = std::max(peak_center_index - searchBox, start_index);
72 if (searchBox > peak_center_index) {
76 const size_t rght = std::min(peak_center_index + searchBox, stop_index);
78 for (
size_t i =
left; i < rght; ++i) {
79 const double y = vector_y[i] - bkgd_values.
getCalculated(i - start_index);
80 if (
y > peak_height) {
82 peak_center = vector_x[i];
83 peak_center_index = i;
103 const double peakWidthPercentage) {
104 double peak_fwhm(-0.);
108 const double peak_center = histogram.points()[ipeak];
109 peak_fwhm = peak_center * peakWidthPercentage;
114 const auto &x_vec = histogram.points();
115 const auto &y_vec = histogram.y();
116 const size_t numPoints = std::min(istart - istop, bkgd_values.
size()) - 1;
120 for (
size_t i = 0; i < numPoints; ++i) {
122 if (y_vec[istart + i] < 0.0) {
125 const auto yavg = 0.5 * (y_vec[istart + i] - bkgd_values.
getCalculated(i) + y_vec[istart + i + 1] -
127 const auto dx = x_vec[istart + i + 1] - x_vec[istart + i];
130 peak_fwhm = 2 * std::sqrt(M_LN2 / M_PI) * area / y_vec[ipeak];
133 throw std::runtime_error(
"This case for observing peak width is not supported.");
159 const EstimatePeakWidth peakWidthEstimateApproach,
const double peakWidthPercentage,
160 const double minPeakHeight) {
163 const auto &vector_x = histogram.points();
164 FunctionDomain1DVector domain(vector_x.cbegin() + peak_window.first, vector_x.cbegin() + peak_window.second);
166 bkgdfunction->function(domain, bkgd_values);
170 double peak_center = peakfunction->centre();
171 size_t peak_center_index;
173 int result =
observePeakCenter(histogram, bkgd_values, peak_window.first, peak_window.second, peak_center,
174 peak_center_index, peak_height);
180 if (peak_height < minPeakHeight || std::isnan(peak_height))
184 peakfunction->setHeight(peak_height);
185 peakfunction->setCentre(peak_center);
189 const double peak_fwhm =
observePeakFwhm(histogram, bkgd_values, peak_center_index, peak_window.first,
190 peak_window.second, peakWidthEstimateApproach, peakWidthPercentage);
191 if (peak_fwhm > 0.0) {
192 peakfunction->setFwhm(peak_fwhm);
199template MANTID_ALGORITHMS_DLL
size_t findXIndex(
const HistogramData::Points &,
const double,
const size_t);
200template MANTID_ALGORITHMS_DLL
size_t findXIndex(
const HistogramData::HistogramX &,
const double,
const size_t);
201template MANTID_ALGORITHMS_DLL
size_t findXIndex(
const std::vector<double> &,
const double,
const size_t);
std::map< DeltaEMode::Type, std::string > index
Implements FunctionDomain1D with its own storage in form of a std::vector.
A class to store values calculated by a function.
size_t size() const
Return the number of values.
double getCalculated(size_t i) const
Get i-th calculated value.
std::shared_ptr< IBackgroundFunction > IBackgroundFunction_sptr
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
MANTID_ALGORITHMS_DLL double observePeakFwhm(const HistogramData::Histogram &histogram, const API::FunctionValues &bkgd_values, size_t ipeak, size_t istart, size_t istop, const EstimatePeakWidth peakWidthEstimateApproach, const double peakWidthPercentage)
Observe peak width.
MANTID_ALGORITHMS_DLL int observePeakCenter(const HistogramData::Histogram &histogram, const API::FunctionValues &bkgd_values, size_t start_index, size_t stop_index, double &peak_center, size_t &peak_center_index, double &peak_height)
observe peak center
MANTID_ALGORITHMS_DLL size_t findXIndex(const vector_like &vecx, const double x, const size_t startindex=0)
Get an index of a value in a sorted vector.
MANTID_ALGORITHMS_DLL int estimatePeakParameters(const HistogramData::Histogram &histogram, const std::pair< size_t, size_t > &peak_window, const API::IPeakFunction_sptr &peakfunction, const API::IBackgroundFunction_sptr &bkgdfunction, bool observe_peak_width, const EstimatePeakWidth peakWidthEstimateApproach, const double peakWidthPercentage, const double minPeakHeight)
Estimate peak parameters by 'observation'.
Helper class which provides the Collimation Length for SANS instruments.