int PoldiFitPeaks1D2::getBestChebyshevPolynomialDegree( const Workspace2D_sptr &dataWorkspace, const RefinedRange_sptr &range) { double chiSquareMin = 1e10; int nMin = -1; try { int n = 0; while ((n < 3)) { IAlgorithm_sptr fit = getFitAlgorithm(dataWorkspace, range, n); bool fitSuccess = fit->execute(); if (fitSuccess) { ITableWorkspace_sptr fitCharacteristics = fit->getProperty("OutputParameters"); TableRow row = fitCharacteristics->getRow(fitCharacteristics->rowCount() - 1); double chiSquare = row.Double(1); if (fabs(chiSquare - 1) < fabs(chiSquareMin - 1)) { chiSquareMin = chiSquare; nMin = n; } } ++n; } } catch (std::runtime_error) { nMin = -1; } if (nMin == -1) { g_log.information() << "Range [" << range->getXStart() << " - " << range->getXEnd() << "] is excluded."; } else { g_log.information() << "Chi^2 for range [" << range->getXStart() << " - " << range->getXEnd() << "] is minimal at n = " << nMin << " with Chi^2 = " << chiSquareMin << std::endl; } return nMin; }
PoldiPeakCollection_sptr PoldiFitPeaks1D2::fitPeaks(const PoldiPeakCollection_sptr &peaks) { g_log.information() << "Peaks to fit: " << peaks->peakCount() << std::endl; std::vector<RefinedRange_sptr> rawRanges = getRefinedRanges(peaks); std::vector<RefinedRange_sptr> reducedRanges = getReducedRanges(rawRanges); g_log.information() << "Ranges used for fitting: " << reducedRanges.size() << std::endl; Workspace2D_sptr dataWorkspace = getProperty("InputWorkspace"); m_fitplots->removeAll(); for (size_t i = 0; i < reducedRanges.size(); ++i) { RefinedRange_sptr currentRange = reducedRanges[i]; int nMin = getBestChebyshevPolynomialDegree(dataWorkspace, currentRange); if (nMin > -1) { IAlgorithm_sptr fit = getFitAlgorithm(dataWorkspace, currentRange, nMin); fit->execute(); IFunction_sptr fitFunction = fit->getProperty("Function"); CompositeFunction_sptr composite = boost::dynamic_pointer_cast<CompositeFunction>(fitFunction); if (!composite) { throw std::runtime_error("Not a composite function!"); } std::vector<PoldiPeak_sptr> peaks = currentRange->getPeaks(); for (size_t i = 0; i < peaks.size(); ++i) { setValuesFromProfileFunction(peaks[i], composite->getFunction(i)); MatrixWorkspace_sptr fpg = fit->getProperty("OutputWorkspace"); m_fitplots->addWorkspace(fpg); } } } return getReducedPeakCollection(peaks); }
void PoldiFitPeaks1D::exec() { setPeakFunction(getProperty("PeakFunction")); // Number of points around the peak center to use for the fit m_fwhmMultiples = getProperty("FwhmMultiples"); // try to construct PoldiPeakCollection from provided TableWorkspace TableWorkspace_sptr poldiPeakTable = getProperty("PoldiPeakTable"); m_peaks = getInitializedPeakCollection(poldiPeakTable); g_log.information() << "Peaks to fit: " << m_peaks->peakCount() << '\n'; Workspace2D_sptr dataWorkspace = getProperty("InputWorkspace"); auto fitPlotGroup = boost::make_shared<WorkspaceGroup>(); for (size_t i = 0; i < m_peaks->peakCount(); ++i) { PoldiPeak_sptr currentPeak = m_peaks->peak(i); IFunction_sptr currentProfile = getPeakProfile(currentPeak); IAlgorithm_sptr fit = getFitAlgorithm(dataWorkspace, currentPeak, currentProfile); bool fitSuccess = fit->execute(); if (fitSuccess) { setValuesFromProfileFunction(currentPeak, fit->getProperty("Function")); MatrixWorkspace_sptr fpg = fit->getProperty("OutputWorkspace"); fitPlotGroup->addWorkspace(fpg); } } setProperty("OutputWorkspace", m_peaks->asTableWorkspace()); setProperty("FitPlotsWorkspace", fitPlotGroup); }