diff --git a/Framework/Algorithms/inc/MantidAlgorithms/FindPeakBackground.h b/Framework/Algorithms/inc/MantidAlgorithms/FindPeakBackground.h index 62ce8bda1e0f32d9677e690ff1b173b7a76bcb88..26f32d2c04d3bcd3cba0e3406d249e833cf16e95 100644 --- a/Framework/Algorithms/inc/MantidAlgorithms/FindPeakBackground.h +++ b/Framework/Algorithms/inc/MantidAlgorithms/FindPeakBackground.h @@ -74,7 +74,9 @@ private: /// create output workspace void createOutputWorkspaces(); - void findStartStopIndex(size_t &istart, size_t &istop); + /// main method to find peak background + int findBackground(std::vector<size_t> &peak_min_max_indexes, + std::vector<double> &bkgd3); /// set histogram data to find background void setHistogram(const HistogramData::Histogram &histogram); @@ -88,12 +90,19 @@ private: /// set fit window void setFitWindow(const std::vector<double> &window); + /// add result to output workpsace + void addResultToOutput(const int &goodfit, + const std::vector<size_t> &peakindexes, + const std::vector<double> &bkgdparams); + /// histogram data to find peak background boost::shared_ptr<const HistogramData::Histogram> m_histogram; // HistogramData::Histogram& m_histogram; /// fit window std::vector<double> m_vecFitWindows; + size_t m_iStartX; /// l0 in original code + size_t m_iStopX; /// n in original code /// background order: 0 for flat, 1 for linear, 2 for quadratic size_t m_backgroundOrder; /// constant sigma diff --git a/Framework/Algorithms/src/FindPeakBackground.cpp b/Framework/Algorithms/src/FindPeakBackground.cpp index ec60398fc8b0cfe996924c09fe254520b4c830bf..350ecb4e91e7e58aa4f29cb21484c47995282abd 100644 --- a/Framework/Algorithms/src/FindPeakBackground.cpp +++ b/Framework/Algorithms/src/FindPeakBackground.cpp @@ -71,30 +71,85 @@ void FindPeakBackground::exec() { // Get input and validate processInputProperties(); - auto &inpX = m_histogram->x(); - auto &inpY = m_histogram->y(); - size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size(); - size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size(); - - // determine the fit window with their index in X (or Y) - size_t n = sizey; - size_t l0 = 0; - if (m_vecFitWindows.size() > 1) { - Mantid::Algorithms::FindPeaks fp; - l0 = fp.getIndex(inpX, m_vecFitWindows[0]); - n = fp.getIndex(inpX, m_vecFitWindows[1]); - if (n < sizey) - n++; - } - // m_vecFitWindows won't be used again form this point till end. + // auto &inpX = m_histogram->x(); + // auto &inpY = m_histogram->y(); + // size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size(); + // size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size(); + + // // determine the fit window with their index in X (or Y) + // size_t n = sizey; + // size_t l0 = 0; + // if (m_vecFitWindows.size() > 1) { + // Mantid::Algorithms::FindPeaks fp; + // l0 = fp.getIndex(inpX, m_vecFitWindows[0]); + // n = fp.getIndex(inpX, m_vecFitWindows[1]); + // if (n < sizey) + // n++; + // } + // // m_vecFitWindows won't be used again form this point till end. // Set up output table workspace createOutputWorkspaces(); - // 3. Get Y values + // set progress 10% Progress prog(this, 0.0, 1.0, 1); // Find background + std::vector<size_t> peakindexes; + std::vector<double> bkgdparams; + int goodfit = findBackground(peakindexes, bkgdparams); + + // add to output workspace + addResultToOutput(goodfit, peakindexes, bkgdparams); + + prog.report(); + + // 4. Set the output + setProperty("OutputWorkspace", m_outPeakTableWS); +} + +//---------------------------------------------------------------------------------------------- +/** + * @brief FindPeakBackground::addResultToOutput + * @param goodfit + * @param peakindexes + * @param bkgdparams + */ +void FindPeakBackground::addResultToOutput( + const int &goodfit, const std::vector<size_t> &peakindexes, + const std::vector<double> &bkgdparams) { + if (goodfit > 0) { + int min_peak = static_cast<int>(peakindexes[0]); + int max_peak = static_cast<int>(peakindexes[1]); + double a0 = bkgdparams[0]; + double a1 = bkgdparams[1]; + double a2 = bkgdparams[2]; + + API::TableRow t = m_outPeakTableWS->getRow(0); + t << static_cast<int>(m_inputWSIndex) << min_peak << max_peak << a0 << a1 + << a2 << goodfit; + } + + return; +} + +//---------------------------------------------------------------------------------------------- +/** Main method to process find peak background + * Note: all inputs shall be set up by this method + * @brief FindPeakBackground::findBackground + * @param peak_min_max_indexes + * @param bkgd3 + * @return integer as good fit or not! + */ +int FindPeakBackground::findBackground( + std::vector<size_t> &peak_min_max_indexes, std::vector<double> &bkgd3) { + // get something from previously set up class variable + auto &inpX = m_histogram->x(); + auto &inpY = m_histogram->y(); + size_t l0 = m_iStartX; + size_t n = m_iStopX; + + int goodfit(0); double Ymean, Yvariance, Ysigma; MantidVec maskedY; auto in = std::min_element(inpY.cbegin(), inpY.cend()); @@ -162,10 +217,10 @@ void FindPeakBackground::exec() { if (inpY[l + l0] > peaks[ipeak].maxY) peaks[ipeak].maxY = inpY[l + l0]; } - } + } // END-FOR (l) + size_t min_peak, max_peak; double a0 = 0., a1 = 0., a2 = 0.; - int goodfit; if (!peaks.empty()) { g_log.debug() << "Peaks' size = " << peaks.size() << " -> esitmate background. \n"; @@ -186,20 +241,28 @@ void FindPeakBackground::exec() { goodfit = 2; } - estimateBackground(inpX, inpY, l0, n, min_peak, max_peak, (!peaks.empty()), a0, a1, a2); // Add a new row - API::TableRow t = m_outPeakTableWS->getRow(0); - t << static_cast<int>(m_inputWSIndex) << static_cast<int>(min_peak) - << static_cast<int>(max_peak) << a0 << a1 << a2 << goodfit; - } - prog.report(); + // API::TableRow t = m_outPeakTableWS->getRow(0); + // t << static_cast<int>(m_inputWSIndex) << static_cast<int>(min_peak) + // << static_cast<int>(max_peak) << a0 << a1 << a2 << goodfit; - // 4. Set the output - setProperty("OutputWorkspace", m_outPeakTableWS); + // set up good fit result + peak_min_max_indexes.resize(2); + peak_min_max_indexes[0] = min_peak; + peak_min_max_indexes[1] = max_peak; + + bkgd3.resize(3); + bkgd3[0] = a0; + bkgd3[1] = a1; + bkgd3[2] = a2; + + } // END-IF: if (n - l0 > 5) + + return goodfit; } //---------------------------------------------------------------------------------------------- @@ -444,6 +507,10 @@ void FindPeakBackground::setBackgroundOrder(size_t order) { * @param fitwindow */ void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) { + + size_t l0 = 0; + size_t n = m_histogram->y().size(); + // validate input if (m_vecFitWindows.size() == 0) { m_vecFitWindows.resize(2); @@ -453,11 +520,37 @@ void FindPeakBackground::setFitWindow(const std::vector<double> &fitwindow) { m_vecFitWindows[0] >= m_vecFitWindows[1]) { throw std::invalid_argument("Fit window has either wrong item number or " "window value is not in ascending order."); + } else { + Mantid::Algorithms::FindPeaks fp; + l0 = fp.getIndex(m_histogram->x(), m_vecFitWindows[0]); + n = fp.getIndex(m_histogram->x(), m_vecFitWindows[1]); + if (n < m_histogram->y().size()) + n++; } - // m_vecFitWindows.resize(2); // copy the input to class variable m_vecFitWindows = fitwindow; + m_iStartX = l0; + m_iStopX = n; + + // ---- from here .. all from original code + + // auto &inpX = m_histogram->x(); + // auto &inpY = m_histogram->y(); + // size_t sizex = inpX.size(); // inpWS->x(inpwsindex).size(); + // size_t sizey = inpY.size(); // inpWS->y(inpwsindex).size(); + + // determine the fit window with their index in X (or Y) + // size_t n = sizey; + // size_t l0 = 0; + // if (m_vecFitWindows.size() > 1) { + // Mantid::Algorithms::FindPeaks fp; + // l0 = fp.getIndex(inpX, m_vecFitWindows[0]); + // n = fp.getIndex(inpX, m_vecFitWindows[1]); + // if (n < sizey) + // n++; + // } + // m_vecFitWindows won't be used again form this point till end. return; }