Mantid
Loading...
Searching...
No Matches
FitPeak.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2013 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
13#include "MantidAlgorithms/DllConfig.h"
16
17namespace Mantid {
18namespace HistogramData {
19class HistogramX;
20class HistogramY;
21} // namespace HistogramData
22
23namespace Algorithms {
26size_t getIndex(const HistogramData::HistogramX &vecx, double x);
27
30class MANTID_ALGORITHMS_DLL FitOneSinglePeak final : public API::Algorithm {
31public:
34
36 const std::string summary() const override { return "Fit a single peak with checking mechanism. "; }
37
39 void setWorskpace(const API::MatrixWorkspace_sptr &dataws, size_t wsindex);
40
42 void setFittingMethod(std::string minimizer, const std::string &costfunction);
43
45 void setFunctions(const API::IPeakFunction_sptr &peakfunc, const API::IBackgroundFunction_sptr &bkgdfunc);
46
48 void setFitWindow(double leftwindow, double rightwindow);
49
51 void setPeakRange(double xpeakleft, double xpeakright);
52
54 void setupGuessedFWHM(double usrwidth, int minfwhm, int maxfwhm, int stepsize, bool fitwithsteppedfwhm);
55
56 void setFitPeakCriteria(bool usepeakpostol, double peakpostol);
57
58 std::string getDebugMessage();
59
61 bool simpleFit();
62
64 void highBkgdFit();
65
67 std::map<std::string, double> getPeakError();
68
70 std::map<std::string, double> getBackgroundError();
71
73 double getFitCostFunctionValue();
74
76 API::MatrixWorkspace_sptr genFitWindowWS();
77
78private:
80 const std::string name() const override { return "FitOneSinglePeak"; }
81
83 int version() const override { return 1; }
84 const std::vector<std::string> seeAlso() const override { return {"Fit"}; }
86 void init() override;
88 void exec() override;
89
91 bool hasSetupToFitPeak(std::string &errmsg);
92
94 double estimatePeakHeight(const API::IPeakFunction_const_sptr &peakfunc, const API::MatrixWorkspace_sptr &dataws,
95 size_t wsindex, size_t ixmin, size_t ixmax);
96
99 double checkFittedPeak(const API::IPeakFunction_sptr &peakfunc, double costfuncvalue, std::string &errorreason);
100
102 double fitPeakFunction(const API::IPeakFunction_sptr &peakfunc, const API::MatrixWorkspace_sptr &dataws,
103 size_t wsindex, double startx, double endx);
104
106 double fitFunctionSD(API::IFunction_sptr fitfunc, const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
107 double xmin, double xmax);
108
110 double calChiSquareSD(const API::IFunction_sptr &fitfunc, const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
111 double xmin, double xmax);
112
114 double fitCompositeFunction(const API::IPeakFunction_sptr &peakfunc, const API::IBackgroundFunction_sptr &bkgdfunc,
115 const API::MatrixWorkspace_sptr &dataws, size_t wsindex, double startx, double endx);
116
118 double fitFunctionMD(const API::IFunction_sptr &fitfunc, const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
119 std::vector<double> vec_xmin, std::vector<double> vec_xmax);
121 void removeBackground(const API::MatrixWorkspace_sptr &purePeakWS);
122
124 void processNStoreFitResult(double rwp, bool storebkgd);
125
127 std::map<std::string, double> backup(const API::IFunction_const_sptr &func);
128
129 void pop(const std::map<std::string, double> &funcparammap, const API::IFunction_sptr &func);
130
132 std::map<std::string, double> storeFunctionError(const API::IFunction_const_sptr &func);
133
135
146
151
155 size_t m_wsIndex;
156
158 double m_minFitX;
160 double m_maxFitX;
162 size_t i_minFitX;
164 size_t i_maxFitX;
165
174
176 std::map<std::string, double> m_bestPeakFunc;
178 std::map<std::string, double> m_bestBkgdFunc;
179
181 std::map<std::string, double> m_bkupPeakFunc;
183 std::map<std::string, double> m_bkupBkgdFunc;
184
186 std::map<std::string, double> m_fitErrorPeakFunc;
188 std::map<std::string, double> m_fitErrorBkgdFunc;
189
191 std::string m_minimizer;
193 std::string m_costFunction;
194
195 std::vector<double> m_vecFWHM;
196
199
202
204 double m_bestRwp;
205
208
211
213 std::stringstream m_sstream;
214};
215
218class MANTID_ALGORITHMS_DLL FitPeak final : public API::Algorithm {
219public:
220 FitPeak();
221
223 const std::string name() const override { return "FitPeak"; }
225 const std::string summary() const override { return "Fit a single peak with checking mechanism. "; }
227 int version() const override { return (1); }
229 const std::string category() const override { return "Optimization"; }
230
231private:
232 void init() override;
233 void exec() override;
234
236 void processProperties();
237
239 void prescreenInputData();
240
242 void setupOutput(const std::map<std::string, double> &m_fitErrorPeakFunc,
243 const std::map<std::string, double> &m_fitErrorBkgdFunc);
244
247 double startx, double endx);
248
251
254 API::MatrixWorkspace_sptr dataws, size_t wsindex, double startx, double endx);
255
258
261 size_t ixmin, size_t ixmax);
262
264 double fitFunctionSD(API::IFunction_sptr fitfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, double xmin,
265 double xmax, bool calmode);
266
268 double fitFunctionMD(API::IFunction_sptr fitfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex,
269 std::vector<double> vec_xmin, std::vector<double> vec_xmax);
270
272 void processNStoreFitResult(double rwp, bool storebkgd);
273
275 void setupGuessedFWHM(std::vector<double> &vec_FWHM);
276
278 void pop(const std::map<std::string, double> &funcparammap, API::IFunction_sptr func);
279
282
284 void createFunctions();
285
287 double checkFittedPeak(API::IPeakFunction_sptr peakfunc, double costfuncvalue, std::string &errorreason);
288
290 DataObjects::TableWorkspace_sptr genOutputTableWS(const API::IPeakFunction_sptr &peakfunc,
291 std::map<std::string, double> peakerrormap,
292 const API::IBackgroundFunction_sptr &bkgdfunc,
293 std::map<std::string, double> bkgderrormap);
294
296 std::vector<std::string> addFunctionParameterNames(const std::vector<std::string> &funcnames);
297
299 std::string parseFunctionTypeFull(const std::string &fullstring, bool &defaultparorder);
300
303 size_t m_wsIndex;
304
310 double m_minFitX;
312 double m_maxFitX;
317
319 size_t i_minFitX;
321 size_t i_maxFitX;
326
329
332
337
346
351
354
356 std::vector<std::string> m_peakParameterNames;
358 std::vector<std::string> m_bkgdParameterNames;
359
361 std::string m_minimizer;
362
364 std::map<std::string, double> m_bkupBkgdFunc;
366 std::map<std::string, double> m_bkupPeakFunc;
368 std::map<std::string, double> m_bestPeakFunc;
370 std::map<std::string, double> m_bestBkgdFunc;
372 double m_bestRwp;
373
376
378 std::vector<double> m_vecybkup;
379 std::vector<double> m_vecebkup;
380
381 std::string m_costFunction;
382
384 // std::map<std::string, double> m_fitErrorPeakFunc;
385 // std::map<std::string, double> m_fitErrorBkgdFunc;
386
389};
390
391} // namespace Algorithms
392} // namespace Mantid
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
FitOneSinglePeak: a class to perform peak fitting on a single peak.
Definition: FitPeak.h:30
std::map< std::string, double > m_bestBkgdFunc
Best background parameters.
Definition: FitPeak.h:178
double m_finalGoodnessValue
Final goodness value (Rwp/Chi-square)
Definition: FitPeak.h:207
size_t m_wsIndex
Input worskpace index.
Definition: FitPeak.h:155
std::string m_minimizer
Minimzer.
Definition: FitPeak.h:191
int version() const override
Version.
Definition: FitPeak.h:83
double m_maxPeakX
peak right boundary (client-defined)
Definition: FitPeak.h:169
API::IPeakFunction_sptr m_peakFunc
Peak function.
Definition: FitPeak.h:148
bool m_peakRangeSet
Flag whether the peak range is set.
Definition: FitPeak.h:139
std::map< std::string, double > m_bkupBkgdFunc
Backed up background function parameters.
Definition: FitPeak.h:183
API::MatrixWorkspace_sptr m_dataWS
Input data workspace.
Definition: FitPeak.h:153
std::map< std::string, double > m_bkupPeakFunc
Backed up peak function parameters.
Definition: FitPeak.h:181
double m_peakPositionTolerance
Peak position tolerance.
Definition: FitPeak.h:198
std::map< std::string, double > m_fitErrorBkgdFunc
Fit error of background function.
Definition: FitPeak.h:188
const std::string summary() const override
Summary of algorithms purpose.
Definition: FitPeak.h:36
std::map< std::string, double > m_fitErrorPeakFunc
Fit error of peak function.
Definition: FitPeak.h:186
const std::string name() const override
Name.
Definition: FitPeak.h:80
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso algorithms related to this algorithm.
Definition: FitPeak.h:84
bool m_peakWindowSet
Peak widnow is set up.
Definition: FitPeak.h:143
API::IBackgroundFunction_sptr m_bkgdFunc
Background function.
Definition: FitPeak.h:150
size_t i_maxFitX
index of m_maxFitX
Definition: FitPeak.h:164
std::map< std::string, double > m_bestPeakFunc
Best peak parameters.
Definition: FitPeak.h:176
std::stringstream m_sstream
String stream.
Definition: FitPeak.h:213
double m_userPeakCentre
Peak centre provided by user.
Definition: FitPeak.h:201
bool m_usePeakPositionTolerance
Flag to apply peak position tolerance.
Definition: FitPeak.h:145
double m_minFitX
Lower boundary of fitting range.
Definition: FitPeak.h:158
bool m_peakWidthSet
Flag whether the peak width is set.
Definition: FitPeak.h:141
std::string m_costFunction
Cost function.
Definition: FitPeak.h:193
double m_maxFitX
Upper boundary of fitting range.
Definition: FitPeak.h:160
size_t i_maxPeakX
index of m_maxPeakX
Definition: FitPeak.h:173
double m_minPeakX
peak left boundary (client-defined)
Definition: FitPeak.h:167
std::vector< double > m_vecFWHM
Definition: FitPeak.h:195
bool m_fitMethodSet
Flag to show whether fitting parameters are set.
Definition: FitPeak.h:137
size_t i_minFitX
index of m_minFitX
Definition: FitPeak.h:162
size_t i_minPeakX
index of m_minPeakX
Definition: FitPeak.h:171
FitPeak : Fit a single peak.
Definition: FitPeak.h:218
double fitFunctionMD(API::IFunction_sptr fitfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, std::vector< double > vec_xmin, std::vector< double > vec_xmax)
Fit a function in multi-domain.
void setupGuessedFWHM(std::vector< double > &vec_FWHM)
Set up a vector of guessed FWHM.
size_t i_maxFitX
Vector index of m_maxFitX.
Definition: FitPeak.h:321
std::vector< double > m_vecybkup
Backups.
Definition: FitPeak.h:378
std::string m_minimizer
Minimizer.
Definition: FitPeak.h:361
bool m_fitBkgdFirst
fitting strategy
Definition: FitPeak.h:328
const std::string category() const override
Algorithm's category for identification.
Definition: FitPeak.h:229
API::IBackgroundFunction_sptr fitBackground(API::IBackgroundFunction_sptr bkgdfunc)
Fit background with multiple domain.
double m_minFitX
Minimum fit position.
Definition: FitPeak.h:310
double estimatePeakHeight(API::IPeakFunction_sptr peakfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, size_t ixmin, size_t ixmax)
Estimate the peak height from a set of data containing pure peaks.
size_t i_maxPeakX
Vector index of m_maxPeakX.
Definition: FitPeak.h:325
double m_maxFitX
Maximum fit position.
Definition: FitPeak.h:312
bool m_usePeakPositionTolerance
Use peak position tolerance as a criterial for peak fit.
Definition: FitPeak.h:348
bool m_fitWithStepPeakWidth
Flag about guessed FWHM (pixels)
Definition: FitPeak.h:345
DataObjects::TableWorkspace_sptr m_peakParameterTableWS
Definition: FitPeak.h:352
API::MatrixWorkspace_sptr m_dataWS
Input data workspace.
Definition: FitPeak.h:302
double m_maxPeakX
Maximum peak position.
Definition: FitPeak.h:316
API::MatrixWorkspace_sptr genPurePeakWS()
Backup data.
std::vector< std::string > m_bkgdParameterNames
Background.
Definition: FitPeak.h:358
double checkFittedPeak(API::IPeakFunction_sptr peakfunc, double costfuncvalue, std::string &errorreason)
Check the fitted peak value to see whether it is valud.
int m_fwhmFitStep
Step width of tried FWHM.
Definition: FitPeak.h:343
DataObjects::TableWorkspace_sptr m_bkgdParameterTableWS
Definition: FitPeak.h:353
int m_maxGuessedPeakWidth
Maximum guessed peak width (pixels)
Definition: FitPeak.h:341
API::IBackgroundFunction_sptr m_bkgdFunc
Background function.
Definition: FitPeak.h:308
double m_minPeakX
Minimum peak position.
Definition: FitPeak.h:314
double m_peakPositionTolerance
Tolerance on peak positions as criteria.
Definition: FitPeak.h:350
std::map< std::string, double > m_bestPeakFunc
Best fitted peak function.
Definition: FitPeak.h:368
int version() const override
Algorithm's version.
Definition: FitPeak.h:227
double m_finalGoodnessValue
Final.
Definition: FitPeak.h:375
const std::string name() const override
Algorithm's name.
Definition: FitPeak.h:223
API::IPeakFunction_sptr m_peakFunc
Peak function.
Definition: FitPeak.h:306
std::string m_costFunction
Definition: FitPeak.h:381
double fitFunctionSD(API::IFunction_sptr fitfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, double xmin, double xmax, bool calmode)
Fit a function.
double m_bestRwp
Best Rwp ...
Definition: FitPeak.h:372
bool m_lightWeightOutput
Fitting result.
Definition: FitPeak.h:388
void makePurePeakWS(API::MatrixWorkspace_sptr purePeakWS)
Make a pure peak WS in the fit window region.
bool m_outputRawParams
output option
Definition: FitPeak.h:331
std::map< std::string, double > m_bkupPeakFunc
Storage map for peak function.
Definition: FitPeak.h:366
double fitCompositeFunction(API::IPeakFunction_sptr peakfunc, API::IBackgroundFunction_sptr bkgdfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, double startx, double endx)
Fit peak and background composite function.
std::map< std::string, double > m_bkupBkgdFunc
Storage map for background function.
Definition: FitPeak.h:364
std::vector< double > m_vecebkup
Definition: FitPeak.h:379
double m_userGuessedFWHM
User guessed FWHM.
Definition: FitPeak.h:334
int m_minGuessedPeakWidth
Minimum guessed peak width (pixels)
Definition: FitPeak.h:339
size_t i_minFitX
Vector index of m_minFitX.
Definition: FitPeak.h:319
std::vector< std::string > m_peakParameterNames
Peak.
Definition: FitPeak.h:356
const std::string summary() const override
Summary of algorithms purpose.
Definition: FitPeak.h:225
void pop(const std::map< std::string, double > &funcparammap, API::IFunction_sptr func)
Pop.
std::map< std::string, double > m_bestBkgdFunc
Best background function.
Definition: FitPeak.h:370
double m_userPeakCentre
User guessed peak centre.
Definition: FitPeak.h:336
size_t i_minPeakX
Vector index of m_minPeakX.
Definition: FitPeak.h:323
double fitPeakFunction(API::IPeakFunction_sptr peakfunc, API::MatrixWorkspace_sptr dataws, size_t wsindex, double startx, double endx)
Fit a single peak function with pure peak workspace.
void processNStoreFitResult(double rwp, bool storebkgd)
Process and store fit result.
std::shared_ptr< IBackgroundFunction > IBackgroundFunction_sptr
std::shared_ptr< IPeakFunction > IPeakFunction_sptr
std::shared_ptr< const IPeakFunction > IPeakFunction_const_sptr
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
std::shared_ptr< const IFunction > IFunction_const_sptr
shared pointer to the function base class (const version)
Definition: IFunction.h:734
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
size_t getIndex(const HistogramData::HistogramX &vecx, double x)
Get an index of a value in a sorted vector.
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
Helper class which provides the Collimation Length for SANS instruments.