Mantid
Loading...
Searching...
No Matches
FitPeaks.h
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 +
7#pragma once
8
16#include "MantidAlgorithms/DllConfig.h"
21
22#include <utility>
23
24namespace Mantid {
25namespace HistogramData {
26class HistogramX;
27class HistogramY;
28} // namespace HistogramData
29
30namespace Algorithms {
31
32namespace FitPeaksAlgorithm {
36};
37
39public:
40 PeakFitResult(size_t num_peaks, size_t num_params);
41 double getPeakPosition(size_t ipeak) const;
42 double getCost(size_t ipeak) const;
43 size_t getNumberParameters() const;
44 size_t getNumberPeaks() const;
45 double getParameterValue(size_t ipeak, size_t iparam) const;
46 double getParameterError(size_t ipeak, size_t iparam) const;
47 void setRecord(size_t ipeak, const double cost, const double peak_position, const FitFunction &fit_functions);
48 void setBadRecord(size_t ipeak, const double peak_position);
49 void setFunctionParameters(size_t ipeak, std::vector<double> &param_values);
50
51private:
54 // goodness of fitting
55 std::vector<double> m_costs;
56 // fitted peak positions
57 std::vector<double> m_fitted_peak_positions;
58 // fitted peak and background parameters
59 std::vector<std::vector<double>> m_function_parameters_vector;
61 std::vector<std::vector<double>> m_function_errors_vector;
62};
63} // namespace FitPeaksAlgorithm
64
65class MANTID_ALGORITHMS_DLL FitPeaks final : public API::Algorithm {
66public:
67 FitPeaks();
68
70 const std::string name() const override { return "FitPeaks"; }
71
73 const std::string summary() const override { return "Fit one or multiple peaks in all spectra of a given workspace"; }
74
76 int version() const override { return (1); }
77
79 const std::string category() const override { return "Optimization"; }
80
81 std::map<std::string, std::string> validateInputs() override;
82
83private:
85 void init() override;
87 void exec() override;
88
90 void processInputs();
92 void processInputPeakCenters();
94 void processInputPeakTolerance();
96 void processInputFunctions();
98 void processInputFitRanges();
99
101 void generateFittedParametersValueWorkspaces();
103 void generateOutputPeakPositionWS();
105 void generateCalculatedPeaksWS();
106
109 void convertParametersNameToIndex();
110
112 std::vector<double> getExpectedPeakPositions(size_t wi);
113 std::pair<double, double> getPeakFitWindow(size_t wi, size_t ipeak);
114
116 std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fitPeaks();
117
119 void fitSpectrumPeaks(size_t wi, const std::vector<double> &expected_peak_centers,
120 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result,
121 std::vector<std::vector<double>> &lastGoodPeakParameters);
122
124 bool fitBackground(const size_t &ws_index, const std::pair<double, double> &fit_window,
125 const double &expected_peak_pos, const API::IBackgroundFunction_sptr &bkgd_func);
126
127 // Peak fitting suite
128 double fitIndividualPeak(size_t wi, const API::IAlgorithm_sptr &fitter, const double expected_peak_center,
129 const std::pair<double, double> &fitwindow, const bool estimate_peak_width,
130 const API::IPeakFunction_sptr &peakfunction, const API::IBackgroundFunction_sptr &bkgdfunc);
131
133 double fitFunctionSD(const API::IAlgorithm_sptr &fit, const API::IPeakFunction_sptr &peak_function,
134 const API::IBackgroundFunction_sptr &bkgd_function, const API::MatrixWorkspace_sptr &dataws,
135 size_t wsindex, const std::pair<double, double> &peak_range, const double &expected_peak_center,
136 bool estimate_peak_width, bool estimate_background);
137
138 double fitFunctionMD(API::IFunction_sptr fit_function, const API::MatrixWorkspace_sptr &dataws, const size_t wsindex,
139 const std::pair<double, double> &vec_xmin, const std::pair<double, double> &vec_xmax);
140
142 double fitFunctionHighBackground(const API::IAlgorithm_sptr &fit, const std::pair<double, double> &fit_window,
143 const size_t &ws_index, const double &expected_peak_center, bool observe_peak_shape,
144 const API::IPeakFunction_sptr &peakfunction,
145 const API::IBackgroundFunction_sptr &bkgdfunc);
146
147 void setupParameterTableWorkspace(const API::ITableWorkspace_sptr &table_ws,
148 const std::vector<std::string> &param_names, bool with_chi2);
149
151 void getRangeData(size_t iws, const std::pair<double, double> &fit_window, std::vector<double> &vec_x,
152 std::vector<double> &vec_y, std::vector<double> &vec_e);
153
155 void reduceByBackground(const API::IBackgroundFunction_sptr &bkgd_func, const std::vector<double> &vec_x,
156 std::vector<double> &vec_y);
157
158 API::MatrixWorkspace_sptr createMatrixWorkspace(const std::vector<double> &vec_x, const std::vector<double> &vec_y,
159 const std::vector<double> &vec_e);
160
161 bool decideToEstimatePeakParams(const bool firstPeakInSpectrum, const API::IPeakFunction_sptr &peak_function);
162
164 bool processSinglePeakFitResult(size_t wsindex, size_t peakindex, const double cost,
165 const std::vector<double> &expected_peak_positions,
166 const FitPeaksAlgorithm::FitFunction &fitfunction,
167 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result);
168
170 void calculateFittedPeaks(std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fit_results);
171
173 std::string getPeakHeightParameterName(const API::IPeakFunction_const_sptr &peak_function);
174
176 void processOutputs(std::vector<std::shared_ptr<FitPeaksAlgorithm::PeakFitResult>> fit_result_vec);
177
179 void writeFitResult(size_t wi, const std::vector<double> &expected_positions,
180 const std::shared_ptr<FitPeaksAlgorithm::PeakFitResult> &fit_result);
181
184 bool isObservablePeakProfile(const std::string &peakprofile);
185
186 //------- Workspaces-------------------------------------
193 API::MatrixWorkspace_sptr m_outputPeakPositionWorkspace; // output workspace for peak positions
206
207 //-------- Functions ------------------------------------------------------
214
216 std::string m_minimizer;
218 std::string m_costFunction;
223
224 //-------- Input param init values --------------------------------
226 std::vector<size_t> m_initParamIndexes;
227
229 std::vector<double> m_peakCenters;
233
236
237 //--------- Fitting range -----------------------------------------
244 std::vector<double> m_peakPosTolerances;
245
250
252 std::vector<std::vector<double>> m_peakWindowVector;
258
260 std::vector<std::string> m_peakParamNames;
263 std::vector<double> m_initParamValues;
268
269 // Criteria for fitting peaks
273
276 double m_bkgdSimga; // TODO - add to properties
277
278 //----- Result criterias ---------------
281};
282
283} // namespace Algorithms
284} // namespace Mantid
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
size_t m_function_parameters_number
number of function parameters
Definition: FitPeaks.h:53
std::vector< std::vector< double > > m_function_parameters_vector
Definition: FitPeaks.h:59
double getParameterValue(size_t ipeak, size_t iparam) const
get the fitted value of a particular parameter
Definition: FitPeaks.cpp:132
void setFunctionParameters(size_t ipeak, std::vector< double > &param_values)
std::vector< std::vector< double > > m_function_errors_vector
fitted peak and background parameters' fitting error
Definition: FitPeaks.h:61
void setRecord(size_t ipeak, const double cost, const double peak_position, const FitFunction &fit_functions)
set the peak fitting record/parameter for one peak
Definition: FitPeaks.cpp:144
double getParameterError(size_t ipeak, size_t iparam) const
get the fitting error of a particular parameter
Definition: FitPeaks.cpp:121
void setBadRecord(size_t ipeak, const double peak_position)
The peak postition should be negative and indicates what went wrong.
Definition: FitPeaks.cpp:175
Algorithms::PeakParameterHelper::EstimatePeakWidth m_peakWidthEstimateApproach
Flag for observing peak width: there are 3 states (1) no estimation (2) from 'observation' (3) calcul...
Definition: FitPeaks.h:248
API::MatrixWorkspace_const_sptr m_peakCenterWorkspace
Definition: FitPeaks.h:230
std::vector< double > m_peakPosTolerances
Definition: FitPeaks.h:244
API::IPeakFunction_sptr m_peakFunction
Peak profile name.
Definition: FitPeaks.h:209
API::MatrixWorkspace_sptr m_outputPeakPositionWorkspace
output workspace for peak positions
Definition: FitPeaks.h:193
API::MatrixWorkspace_sptr m_fittedPeakWS
matrix workspace contained calcalated peaks+background from fitted result it has same number of spect...
Definition: FitPeaks.h:205
API::ITableWorkspace_const_sptr m_profileStartingValueTable
table workspace for profile parameters' starting value
Definition: FitPeaks.h:265
std::string m_minimizer
Minimzer.
Definition: FitPeaks.h:216
API::IBackgroundFunction_sptr m_linearBackgroundFunction
Linear background function for high background fitting.
Definition: FitPeaks.h:213
bool m_peakPosTolCase234
peak positon tolerance case b, c and d
Definition: FitPeaks.h:280
double m_peakWidthPercentage
flag to estimate peak width from
Definition: FitPeaks.h:235
API::IBackgroundFunction_sptr m_bkgdFunction
Background function.
Definition: FitPeaks.h:211
int version() const override
Algorithm's version.
Definition: FitPeaks.h:76
size_t m_startWorkspaceIndex
start index
Definition: FitPeaks.h:239
bool m_calculateWindowInstrument
flag to calcualte peak fit window from instrument resolution
Definition: FitPeaks.h:257
const std::string summary() const override
Summary of algorithms purpose.
Definition: FitPeaks.h:73
const std::string category() const override
Algorithm's category for identification.
Definition: FitPeaks.h:79
double m_minPeakHeight
minimum peak height without background and it also serves as the criteria for observed peak parameter
Definition: FitPeaks.h:272
std::string m_costFunction
Cost function.
Definition: FitPeaks.h:218
size_t m_stopWorkspaceIndex
stop index (workspace index of the last spectrum included)
Definition: FitPeaks.h:241
std::vector< std::string > m_peakParamNames
input peak parameters' names
Definition: FitPeaks.h:260
const std::string name() const override
Algorithm's name.
Definition: FitPeaks.h:70
API::ITableWorkspace_sptr m_fittedParamTable
output analysis workspaces table workspace for fitted parameters
Definition: FitPeaks.h:196
DataObjects::EventWorkspace_const_sptr m_inputEventWS
event workspace for input
Definition: FitPeaks.h:191
bool m_highBackground
flag for high background
Definition: FitPeaks.h:275
std::vector< double > m_initParamValues
input peak parameters' starting values corresponding to above peak parameter names
Definition: FitPeaks.h:263
std::vector< double > m_peakCenters
Designed peak positions and tolerance.
Definition: FitPeaks.h:229
std::vector< std::vector< double > > m_peakWindowVector
peak windows
Definition: FitPeaks.h:252
API::MatrixWorkspace_sptr m_inputMatrixWS
mandatory input and output workspaces
Definition: FitPeaks.h:188
std::vector< size_t > m_initParamIndexes
input starting parameters' indexes in peak function
Definition: FitPeaks.h:226
bool m_fitPeaksFromRight
Fit from right or left.
Definition: FitPeaks.h:220
bool m_rawPeaksTable
flag to show that the pamarameters in table are raw parameters or effective parameters
Definition: FitPeaks.h:201
int m_fitIterations
Fit iterations.
Definition: FitPeaks.h:222
API::MatrixWorkspace_const_sptr m_peakWindowWorkspace
Definition: FitPeaks.h:253
bool m_uniformProfileStartingValue
flag for profile startng value being uniform or not
Definition: FitPeaks.h:267
API::ITableWorkspace_sptr m_fitErrorTable
table workspace for fitted parameters' fitting error. This is optional
Definition: FitPeaks.h:198
bool m_partialSpectra
flag whether the peak center workspace has only a subset of spectra to fit
Definition: FitPeaks.h:243
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< IBackgroundFunction > IBackgroundFunction_sptr
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const IPeakFunction > IPeakFunction_const_sptr
std::shared_ptr< const ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const EventWorkspace > EventWorkspace_const_sptr
shared pointer to a const Workspace2D
Helper class which provides the Collimation Length for SANS instruments.
API::IBackgroundFunction_sptr bkgdfunction
Definition: FitPeaks.h:35