Mantid
Loading...
Searching...
No Matches
FitPowderDiffPeaks.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 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
19#include "MantidKernel/System.h"
20
21namespace Mantid {
22namespace CurveFitting {
23namespace Algorithms {
24
45class MANTID_CURVEFITTING_DLL FitPowderDiffPeaks final : public API::Algorithm {
46public:
48
50 const std::string name() const override { return "FitPowderDiffPeaks"; }
52 const std::string summary() const override { return "Fit peaks in powder diffraction pattern. "; }
53
55 int version() const override { return 1; }
56 const std::vector<std::string> seeAlso() const override { return {"LeBailFit"}; }
57
59 const std::string category() const override { return "Diffraction\\Fitting"; }
60
61private:
63 void init() override;
65 void exec() override;
66
68 void processInputProperties();
69
71 void genPeaksFromTable(const DataObjects::TableWorkspace_sptr &peakparamws);
72
74 Functions::BackToBackExponential_sptr genPeak(std::map<std::string, int> hklmap,
75 std::map<std::string, double> parammap,
76 std::map<std::string, std::string> bk2bk2braggmap, bool &good,
77 std::vector<int> &hkl, double &d_h);
78
80 bool getHKLFromMap(std::map<std::string, int> intmap, std::vector<int> &hkl);
81
83 void importInstrumentParameterFromTable(const DataObjects::TableWorkspace_sptr &parameterWS);
84
86 void parseBraggPeakTable(const DataObjects::TableWorkspace_sptr &peakws,
87 std::vector<std::map<std::string, double>> &parammaps,
88 std::vector<std::map<std::string, int>> &hklmaps);
89
91 void fitPeaksWithGoodStartingValues();
92
94 void fitPeaksRobust();
95
98 double leftdev, double rightdev, size_t m_wsIndex, double &chi2);
99
100 //---------------------------------------------------------------------------
101
103 bool fitSinglePeakRobust(const Functions::BackToBackExponential_sptr &peak,
104 const Functions::BackgroundFunction_sptr &backgroundfunction, double peakleftbound,
105 double peakrightbound, const std::map<std::string, double> &rightpeakparammap,
106 double &finalchi2);
107
109 bool fitSinglePeakSimulatedAnnealing(const Functions::BackToBackExponential_sptr &peak,
110 const std::vector<std::string> &paramtodomc);
111
114
116 bool fitSinglePeakConfident(const Functions::BackToBackExponential_sptr &peak,
117 const Functions::BackgroundFunction_sptr &backgroundfunction, double leftbound,
118 double rightbound, double &chi2, bool &annhilatedpeak);
119
122 double dampingfactor);
123
125 bool fitOverlappedPeaks(std::vector<Functions::BackToBackExponential_sptr> peaks,
126 const Functions::BackgroundFunction_sptr &backgroundfunction, double gfwhm);
127
129 bool doFitMultiplePeaks(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
130 const API::CompositeFunction_sptr &peaksfunc,
131 std::vector<Functions::BackToBackExponential_sptr> peakfuncs, std::vector<bool> &vecfitgood,
132 std::vector<double> &vecchi2s);
133
135 void estimatePeakHeightsLeBail(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
136 std::vector<Functions::BackToBackExponential_sptr> peaks);
137
139 void setOverlappedPeaksConstraints(const std::vector<Functions::BackToBackExponential_sptr> &peaks);
140
142 bool doFit1PeakSimple(const DataObjects::Workspace2D_sptr &dataws, size_t workspaceindex,
143 const Functions::BackToBackExponential_sptr &peakfunction, const std::string &minimzername,
144 size_t maxiteration, double &chi2);
145
147 // bool doFit1PeakBackgroundSimple(DataObjects::Workspace2D_sptr dataws,
148 // size_t workspaceindex,
149 // BackToBackExponential_sptr peakfunction,
150 // BackgroundFunction_sptr backgroundfunction,
151 // string minimzername, size_t maxiteration, double &chi2);
152
154 bool doFit1PeakBackground(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
156 const Functions::BackgroundFunction_sptr &backgroundfunction, double &chi2);
157
159 bool doFit1PeakSequential(const DataObjects::Workspace2D_sptr &dataws, size_t workspaceindex,
160 const Functions::BackToBackExponential_sptr &peakfunction,
161 std::vector<std::string> minimzernames, std::vector<size_t> maxiterations,
162 const std::vector<double> &dampfactors, double &chi2);
163
165 bool doFitNPeaksSimple(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
166 const API::CompositeFunction_sptr &peaksfunc,
167 const std::vector<Functions::BackToBackExponential_sptr> &peakfuncs,
168 const std::string &minimizername, size_t maxiteration, double &chi2);
169
171 void storeFunctionParameters(const API::IFunction_sptr &function, std::map<std::string, double> &parammaps);
172
174 void restoreFunctionParameters(const API::IFunction_sptr &function, std::map<std::string, double> parammap);
175
177 void calculatePeakFitBoundary(size_t ileftpeak, size_t irightpeak, double &peakleftboundary,
178 double &peakrightboundary);
179
180 //---------------------------------------------------------------------------
181
183 bool findMaxHeight(API::MatrixWorkspace_sptr dataws, size_t wsindex, double xmin, double xmax, double &center,
184 double &centerleftbound, double &centerrightbound, int &errordirection);
185
187 DataObjects::Workspace2D_sptr genPeakParameterDataWorkspace();
188
190 std::pair<DataObjects::TableWorkspace_sptr, DataObjects::TableWorkspace_sptr> genPeakParametersWorkspace();
191
193 void cropWorkspace(double tofmin, double tofmax);
194
196 std::string parseFitParameterWorkspace(const API::ITableWorkspace_sptr &paramws);
197
199 // DataObjects::Workspace2D_sptr
200 // buildPartialWorkspace(API::MatrixWorkspace_sptr sourcews, size_t m_wsIndex,
201 // double leftbound, double rightbound);
202
206 Functions::BackToBackExponential_sptr rightpeak, double fwhm, bool ismostright,
207 size_t m_wsIndex, double &chi2);
208
210 void observePeakRange(const Functions::BackToBackExponential_sptr &thispeak,
211 const Functions::BackToBackExponential_sptr &rightpeak, double refpeakshift,
212 double &peakleftbound, double &peakrightbound);
213
215 // void estimateBackground(DataObjects::Workspace2D_sptr dataws);
216
220
222 bool estimateFWHM(DataObjects::Workspace2D_sptr dataws, size_t wsindex, double tof_h, double &leftfwhm,
223 double &rightfwhm);
224
227 double leftpeakbound, double rightpeakbound);
228
230 std::pair<bool, double> doFitPeak_Old(DataObjects::Workspace2D_sptr dataws,
231 Functions::BackToBackExponential_sptr peak, double guessedfwhm, bool calchi2);
232
233 std::pair<bool, double> doFitPeak(const DataObjects::Workspace2D_sptr &dataws,
234 const Functions::BackToBackExponential_sptr &peakfunction, double guessedfwhm);
235
237 bool doFitGaussianPeak(const DataObjects::Workspace2D_sptr &dataws, size_t workspaceindex, double in_center,
238 double leftfwhm, double rightfwhm, double &center, double &sigma, double &height);
239
241 DataObjects::Workspace2D_sptr genOutputFittedPatternWorkspace(std::vector<double> pattern, int workspaceindex);
242
244 void calculate1PeakGroup(std::vector<size_t> peakindexes, Functions::BackgroundFunction_sptr background);
245
247 std::string parseFitResult(const API::IAlgorithm_sptr &fitalg, double &chi2, bool &fitsuccess);
248
250 double calculatePeakCentreTOF(int h, int k, int l);
251
253 double getParameter(const std::string &parname);
254
256 void fitPeaksGroup(std::vector<size_t> peakindexes);
257
259 DataObjects::Workspace2D_sptr buildPartialWorkspace(const API::MatrixWorkspace_sptr &sourcews, size_t workspaceindex,
260 double leftbound, double rightbound);
261
263 void plotFunction(const API::IFunction_sptr &peakfunction, const Functions::BackgroundFunction_sptr &background,
264 const API::FunctionDomain1DVector &domain);
265
266 //-----------------------------------------------------------------------------------------------
267
270
273
276
277 // Map for all peaks to fit individually
278 // Disabled std::map<std::vector<int>,
279 // CurveFitting::BackToBackExponential_sptr> m_peaksmap;
280
282 std::vector<std::pair<double, std::pair<std::vector<int>, Functions::BackToBackExponential_sptr>>> m_vecPeakFunctions;
283
285 std::vector<double> m_peakFitChi2;
286
288 std::vector<bool> m_goodFit;
289
291 std::map<std::string, double> m_instrumentParmaeters;
292
294 std::vector<double> m_peakData;
295
297 std::vector<std::string> mPeakParameterNames;
298
301
303 double m_tofMin;
304 double m_tofMax;
305
308
311
313 std::vector<int> m_minimumHKL;
314
317
318 std::vector<size_t> m_indexGoodFitPeaks;
319 std::vector<double> m_chi2GoodFitPeaks;
320
322 enum { ROBUSTFIT, TRUSTINPUTFIT } m_fitMode;
323
325 enum { HKLCALCULATION, FROMBRAGGTABLE } m_genPeakStartingValue;
326
328 std::vector<int> m_rightmostPeakHKL;
329
332
335
338
341
344};
345
348inline double linearInterpolateX(double x0, double xf, double y0, double yf, double y) {
349 double x = ((xf - x0) * y - (xf * y0 - x0 * yf)) / (yf - y0);
350 return x;
351}
352
355inline double linearInterpolateY(double x0, double xf, double y0, double yf, double x) {
356 double y = ((xf * y0 - x0 * yf) + x * (yf - y0)) / (xf - x0);
357 return y;
358}
359
362 const Functions::BackgroundFunction_sptr &background, size_t wsindexraw,
363 size_t wsindexbkgd, size_t wsindexpeak);
364
366bool observePeakParameters(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex, double &centre, double &height,
367 double &fwhm, std::string &errmsg);
368
370size_t findMaxValue(const std::vector<double> &Y);
371
373size_t findMaxValue(const API::MatrixWorkspace_sptr &dataws, size_t wsindex, double leftbound, double rightbound);
374
376std::string getFunctionInfo(const API::IFunction_sptr &function);
377
378} // namespace Algorithms
379} // namespace CurveFitting
380} // namespace Mantid
double sigma
Definition: GetAllEi.cpp:156
double height
Definition: GetAllEi.cpp:155
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
Implements FunctionDomain1D with its own storage in form of a std::vector.
FitPowderDiffPeaks : Fit peaks in powder diffraction pattern.
const std::string category() const override
Algorithm's category for identification overriding a virtual method.
double m_rightmostPeakLeftBound
Right most peak's left boundary.
double calculatePeakCentreTOF(int h, int k, int l)
Calculate a Bragg peak's centre in TOF from its Miller indices.
std::vector< int > m_rightmostPeakHKL
Right most peak HKL.
bool fitPeak(Functions::BackToBackExponential_sptr peak, Functions::BackgroundFunction_sptr background, double leftdev, double rightdev, size_t m_wsIndex, double &chi2)
Fit a single peak.
void calculate1PeakGroup(std::vector< size_t > peakindexes, Functions::BackgroundFunction_sptr background)
Calcualte the value of a single peak in a given range.
const std::vector< std::string > seeAlso() const override
Function to return all of the seeAlso (these are not validated) algorithms related to this algorithm....
double m_rightmostPeakRightBound
Right most peak's right boundary.
std::vector< bool > m_goodFit
Peak fitting status.
std::vector< double > m_peakFitChi2
Peak fitting information.
bool estimateFWHM(DataObjects::Workspace2D_sptr dataws, size_t wsindex, double tof_h, double &leftfwhm, double &rightfwhm)
Estimate FWHM for the peak observed.
bool fitSinglePeakConfidentX(Functions::BackToBackExponential_sptr peak)
Fit peak with confidence of the centre.
double m_minPeakHeight
Minimum peak height for peak to be refined.
std::pair< bool, double > doFitPeak_Old(DataObjects::Workspace2D_sptr dataws, Functions::BackToBackExponential_sptr peak, double guessedfwhm, bool calchi2)
Fit single peak without background.
bool m_fitPeakBackgroundComposite
Fit peak + background as the last step.
void fitPeaksGroup(std::vector< size_t > peakindexes)
Fit peaks in the same group (i.e., single peak or overlapped peaks)
int version() const override
Algorithm's version for identification overriding a virtual method.
DataObjects::TableWorkspace_sptr m_profileTable
Instrument profile parameter table workspace.
Geometry::UnitCell m_unitCell
Unit cell of powder crystal.
const std::string summary() const override
Summary of algorithms purpose.
std::vector< double > m_peakData
Data for each individual peaks. (HKL)^2, vector index, function values.
DataObjects::TableWorkspace_sptr m_peakParamTable
Bragg peak parameter.
void subtractBackground(DataObjects::Workspace2D_sptr dataws)
Estimate background.
bool findMaxHeight(API::MatrixWorkspace_sptr dataws, size_t wsindex, double xmin, double xmax, double &center, double &centerleftbound, double &centerrightbound, int &errordirection)
Find max height (peak center)
std::vector< std::string > mPeakParameterNames
Peak parmeter names.
bool fitSinglePeakConfidentY(DataObjects::Workspace2D_sptr dataws, Functions::BackToBackExponential_sptr peak, double dampingfactor)
Fit peak with confident parameters.
bool estimateSinglePeakRange(Functions::BackToBackExponential_sptr peak, Functions::BackgroundFunction_sptr background, Functions::BackToBackExponential_sptr rightpeak, double fwhm, bool ismostright, size_t m_wsIndex, double &chi2)
Build a partial workspace from source.
std::map< std::string, double > m_instrumentParmaeters
Map for function (instrument parameter)
std::vector< std::pair< double, std::pair< std::vector< int >, Functions::BackToBackExponential_sptr > > > m_vecPeakFunctions
Sorted vector for peaks. double = d_h, vector = (HKL), peak.
int m_numPeaksLowerToMin
Number of peaks to fit lower to minimum HKL.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
bool m_confidentInInstrumentParameters
Flag to show whether input instrument parameters is trustful.
bool m_useGivenTOFh
Flag to use given Bragg peaks' centre in TOF.
int m_wsIndex
TOF vector of data workspace to process with.
bool doFitBackground(DataObjects::Workspace2D_sptr dataws, Functions::BackgroundFunction_sptr background, double leftpeakbound, double rightpeakbound)
Fit background function by removing the peak properly.
Class to implement unit cell of crystals.
Definition: UnitCell.h:44
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
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< CompositeFunction > CompositeFunction_sptr
shared pointer to the composite function base class
double linearInterpolateX(double x0, double xf, double y0, double yf, double y)
Formular for linear iterpolation: X = [(xf-x0)*Y - (xf*y0-x0*yf)]/(yf-y0)
size_t findMaxValue(const std::vector< double > &Y)
Find maximum value.
bool observePeakParameters(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex, double &centre, double &height, double &fwhm, std::string &errmsg)
Estimate peak parameters;.
void estimateBackgroundCoarse(const DataObjects::Workspace2D_sptr &dataws, const Functions::BackgroundFunction_sptr &background, size_t wsindexraw, size_t wsindexbkgd, size_t wsindexpeak)
Estimate background for a pattern in a coarse mode.
double linearInterpolateY(double x0, double xf, double y0, double yf, double x)
Formula for linear interpolation: Y = ( (xf*y0-x0*yf) + x*(yf-y0) )/(xf-x0)
std::string getFunctionInfo(const API::IFunction_sptr &function)
Get function parameter name, value and etc information in string.
std::shared_ptr< BackToBackExponential > BackToBackExponential_sptr
std::shared_ptr< BackgroundFunction > BackgroundFunction_sptr
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
Helper class which provides the Collimation Length for SANS instruments.