-
Notifications
You must be signed in to change notification settings - Fork 122
/
FitPeak.h
423 lines (333 loc) · 13.2 KB
/
FitPeak.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2013 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IBackgroundFunction.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/MatrixWorkspace_fwd.h"
#include "MantidAlgorithms/DllConfig.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidKernel/cow_ptr.h"
namespace Mantid {
namespace HistogramData {
class HistogramX;
class HistogramY;
} // namespace HistogramData
namespace Algorithms {
/// Get an index of a value in a sorted vector. The index should be the item
/// with value nearest to X
size_t getIndex(const HistogramData::HistogramX &vecx, double x);
/** FitOneSinglePeak: a class to perform peak fitting on a single peak
*/
class MANTID_ALGORITHMS_DLL FitOneSinglePeak : public API::Algorithm {
public:
/// Constructor
FitOneSinglePeak();
/// Summary of algorithms purpose
const std::string summary() const override {
return "Fit a single peak with checking mechanism. ";
}
/// Set workspaces
void setWorskpace(const API::MatrixWorkspace_sptr &dataws, size_t wsindex);
/// Set fitting method
void setFittingMethod(std::string minimizer, const std::string &costfunction);
/// Set functions
void setFunctions(const API::IPeakFunction_sptr &peakfunc,
const API::IBackgroundFunction_sptr &bkgdfunc);
/// Set fit range
void setFitWindow(double leftwindow, double rightwindow);
/// Set peak range
void setPeakRange(double xpeakleft, double xpeakright);
/// Set peak width to guess
void setupGuessedFWHM(double usrwidth, int minfwhm, int maxfwhm, int stepsize,
bool fitwithsteppedfwhm);
void setFitPeakCriteria(bool usepeakpostol, double peakpostol);
std::string getDebugMessage();
/// Fit peak and background together
bool simpleFit();
/// Fit peak first considering high background
void highBkgdFit();
/// Get fitting error for peak function
std::map<std::string, double> getPeakError();
/// Get fitting error for background function
std::map<std::string, double> getBackgroundError();
/// Get cost function value from fitting
double getFitCostFunctionValue();
/// Generate a partial workspace at fit window
API::MatrixWorkspace_sptr genFitWindowWS();
private:
/// Name
const std::string name() const override { return "FitOneSinglePeak"; }
/// Version
int version() const override { return 1; }
const std::vector<std::string> seeAlso() const override { return {"Fit"}; }
/// Init
void init() override;
/// Exec
void exec() override;
/// Check whether it is ready to fit
bool hasSetupToFitPeak(std::string &errmsg);
/// Estimate the peak height from a set of data containing pure peaks
double estimatePeakHeight(const API::IPeakFunction_const_sptr &peakfunc,
const API::MatrixWorkspace_sptr &dataws,
size_t wsindex, size_t ixmin, size_t ixmax);
/// Check a peak function whether it is valid comparing to user specified
/// criteria
double checkFittedPeak(const API::IPeakFunction_sptr &peakfunc,
double costfuncvalue, std::string &errorreason);
/// Fit peak function (flexible)
double fitPeakFunction(const API::IPeakFunction_sptr &peakfunc,
const API::MatrixWorkspace_sptr &dataws,
size_t wsindex, double startx, double endx);
/// Fit function in single domain
double fitFunctionSD(API::IFunction_sptr fitfunc,
const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
double xmin, double xmax);
/// Calculate chi-square of a single domain function
double calChiSquareSD(const API::IFunction_sptr &fitfunc,
const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
double xmin, double xmax);
/// Fit peak and background composite function
double fitCompositeFunction(const API::IPeakFunction_sptr &peakfunc,
const API::IBackgroundFunction_sptr &bkgdfunc,
const API::MatrixWorkspace_sptr &dataws,
size_t wsindex, double startx, double endx);
/// Fit function in multiple-domain
double fitFunctionMD(const API::IFunction_sptr &fitfunc,
const API::MatrixWorkspace_sptr &dataws, size_t wsindex,
std::vector<double> vec_xmin,
std::vector<double> vec_xmax);
/// remove background
void removeBackground(const API::MatrixWorkspace_sptr &purePeakWS);
/// Process and store fit result
void processNStoreFitResult(double rwp, bool storebkgd);
/// Back up fit result
std::map<std::string, double> backup(const API::IFunction_const_sptr &func);
void pop(const std::map<std::string, double> &funcparammap,
const API::IFunction_sptr &func);
/// Store function fitting error
std::map<std::string, double>
storeFunctionError(const API::IFunction_const_sptr &func);
API::IBackgroundFunction_sptr
fitBackground(API::IBackgroundFunction_sptr bkgdfunc);
/// Flag to show whether fitting parameters are set
bool m_fitMethodSet;
/// Flag whether the peak range is set
bool m_peakRangeSet;
/// Flag whether the peak width is set
bool m_peakWidthSet;
/// Peak widnow is set up
bool m_peakWindowSet;
/// Flag to apply peak position tolerance
bool m_usePeakPositionTolerance;
/// Peak function
API::IPeakFunction_sptr m_peakFunc;
/// Background function
API::IBackgroundFunction_sptr m_bkgdFunc;
/// Input data workspace
API::MatrixWorkspace_sptr m_dataWS;
/// Input worskpace index
size_t m_wsIndex;
/// Lower boundary of fitting range
double m_minFitX;
/// Upper boundary of fitting range
double m_maxFitX;
/// index of m_minFitX
size_t i_minFitX;
/// index of m_maxFitX
size_t i_maxFitX;
/// peak left boundary (client-defined)
double m_minPeakX;
/// peak right boundary (client-defined)
double m_maxPeakX;
/// index of m_minPeakX
size_t i_minPeakX;
/// index of m_maxPeakX
size_t i_maxPeakX;
/// Best peak parameters
std::map<std::string, double> m_bestPeakFunc;
/// Best background parameters
std::map<std::string, double> m_bestBkgdFunc;
/// Backed up peak function parameters
std::map<std::string, double> m_bkupPeakFunc;
/// Backed up background function parameters
std::map<std::string, double> m_bkupBkgdFunc;
/// Fit error of peak function
std::map<std::string, double> m_fitErrorPeakFunc;
/// Fit error of background function
std::map<std::string, double> m_fitErrorBkgdFunc;
/// Minimzer
std::string m_minimizer;
/// Cost function
std::string m_costFunction;
std::vector<double> m_vecFWHM;
/// Peak position tolerance
double m_peakPositionTolerance;
/// Peak centre provided by user
double m_userPeakCentre;
///
double m_bestRwp;
/// Final goodness value (Rwp/Chi-square)
double m_finalGoodnessValue;
///
size_t m_numFitCalls;
/// String stream
std::stringstream m_sstream;
};
/** FitPeak : Fit a single peak
*/
class MANTID_ALGORITHMS_DLL FitPeak : public API::Algorithm {
public:
FitPeak();
/// Algorithm's name
const std::string name() const override { return "FitPeak"; }
/// Summary of algorithms purpose
const std::string summary() const override {
return "Fit a single peak with checking mechanism. ";
}
/// Algorithm's version
int version() const override { return (1); }
/// Algorithm's category for identification
const std::string category() const override { return "Optimization"; }
private:
void init() override;
void exec() override;
/// Process input propeties
void processProperties();
/// Check the input properties and functions
void prescreenInputData();
/// Set up the output workspaces
void setupOutput(const std::map<std::string, double> &m_fitErrorPeakFunc,
const std::map<std::string, double> &m_fitErrorBkgdFunc);
/// Fit a single peak function with pure peak workspace
double fitPeakFunction(API::IPeakFunction_sptr peakfunc,
API::MatrixWorkspace_sptr dataws, size_t wsindex,
double startx, double endx);
/// Fit background with multiple domain
API::IBackgroundFunction_sptr
fitBackground(API::IBackgroundFunction_sptr bkgdfunc);
/// Fit peak and background composite function
double fitCompositeFunction(API::IPeakFunction_sptr peakfunc,
API::IBackgroundFunction_sptr bkgdfunc,
API::MatrixWorkspace_sptr dataws, size_t wsindex,
double startx, double endx);
/// Make a pure peak WS in the fit window region
void makePurePeakWS(API::MatrixWorkspace_sptr purePeakWS);
/// Estimate the peak height from a set of data containing pure peaks
double estimatePeakHeight(API::IPeakFunction_sptr peakfunc,
API::MatrixWorkspace_sptr dataws, size_t wsindex,
size_t ixmin, size_t ixmax);
/// Fit a function.
double fitFunctionSD(API::IFunction_sptr fitfunc,
API::MatrixWorkspace_sptr dataws, size_t wsindex,
double xmin, double xmax, bool calmode);
/// Fit a function in multi-domain
double fitFunctionMD(API::IFunction_sptr fitfunc,
API::MatrixWorkspace_sptr dataws, size_t wsindex,
std::vector<double> vec_xmin,
std::vector<double> vec_xmax);
/// Process and store fit result
void processNStoreFitResult(double rwp, bool storebkgd);
/// Set up a vector of guessed FWHM
void setupGuessedFWHM(std::vector<double> &vec_FWHM);
/// Pop
void pop(const std::map<std::string, double> &funcparammap,
API::IFunction_sptr func);
/// Backup data
API::MatrixWorkspace_sptr genPurePeakWS();
/// Create functions
void createFunctions();
/// Check the fitted peak value to see whether it is valud
double checkFittedPeak(API::IPeakFunction_sptr peakfunc, double costfuncvalue,
std::string &errorreason);
/// Generate table workspace
DataObjects::TableWorkspace_sptr
genOutputTableWS(const API::IPeakFunction_sptr &peakfunc,
std::map<std::string, double> peakerrormap,
const API::IBackgroundFunction_sptr &bkgdfunc,
std::map<std::string, double> bkgderrormap);
/// Add function's parameter names after peak function name
std::vector<std::string>
addFunctionParameterNames(const std::vector<std::string> &funcnames);
/// Parse peak type from full peak type/parameter names string
std::string parseFunctionTypeFull(const std::string &fullstring,
bool &defaultparorder);
/// Input data workspace
API::MatrixWorkspace_sptr m_dataWS;
size_t m_wsIndex;
/// Peak function
API::IPeakFunction_sptr m_peakFunc;
/// Background function
API::IBackgroundFunction_sptr m_bkgdFunc;
/// Minimum fit position
double m_minFitX;
/// Maximum fit position
double m_maxFitX;
/// Minimum peak position
double m_minPeakX;
/// Maximum peak position
double m_maxPeakX;
/// Vector index of m_minFitX
size_t i_minFitX;
/// Vector index of m_maxFitX
size_t i_maxFitX;
/// Vector index of m_minPeakX
size_t i_minPeakX;
/// Vector index of m_maxPeakX
size_t i_maxPeakX;
/// fitting strategy
bool m_fitBkgdFirst;
/// output option
bool m_outputRawParams;
/// User guessed FWHM
double m_userGuessedFWHM;
/// User guessed peak centre
double m_userPeakCentre;
/// Minimum guessed peak width (pixels)
int m_minGuessedPeakWidth;
/// Maximum guessed peak width (pixels)
int m_maxGuessedPeakWidth;
/// Step width of tried FWHM
int m_fwhmFitStep;
/// Flag about guessed FWHM (pixels)
bool m_fitWithStepPeakWidth;
/// Use peak position tolerance as a criterial for peak fit
bool m_usePeakPositionTolerance;
/// Tolerance on peak positions as criteria
double m_peakPositionTolerance;
DataObjects::TableWorkspace_sptr m_peakParameterTableWS;
DataObjects::TableWorkspace_sptr m_bkgdParameterTableWS;
/// Peak
std::vector<std::string> m_peakParameterNames;
/// Background
std::vector<std::string> m_bkgdParameterNames;
/// Minimizer
std::string m_minimizer;
/// Storage map for background function
std::map<std::string, double> m_bkupBkgdFunc;
/// Storage map for peak function
std::map<std::string, double> m_bkupPeakFunc;
/// Best fitted peak function
std::map<std::string, double> m_bestPeakFunc;
/// Best background function
std::map<std::string, double> m_bestBkgdFunc;
/// Best Rwp ...
double m_bestRwp;
/// Final
double m_finalGoodnessValue;
/// Backups
std::vector<double> m_vecybkup;
std::vector<double> m_vecebkup;
std::string m_costFunction;
/// Fitting result
// std::map<std::string, double> m_fitErrorPeakFunc;
// std::map<std::string, double> m_fitErrorBkgdFunc;
/// Option on output
bool m_lightWeightOutput;
};
} // namespace Algorithms
} // namespace Mantid