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
13#include "MantidCurveFitting/DllConfig.h"
20
21#include <span>
22
23namespace Mantid {
24namespace CurveFitting {
25namespace Algorithms {
26
47class MANTID_CURVEFITTING_DLL FitPowderDiffPeaks final : public API::Algorithm {
48public:
50
52 const std::string name() const override { return "FitPowderDiffPeaks"; }
54 const std::string summary() const override { return "Fit peaks in powder diffraction pattern. "; }
55
57 int version() const override { return 1; }
58 const std::vector<std::string> seeAlso() const override { return {"LeBailFit"}; }
59
61 const std::string category() const override { return "Diffraction\\Fitting"; }
62
63private:
65 void init() override;
67 void exec() override;
68
70 void processInputProperties();
71
73 void genPeaksFromTable(const DataObjects::TableWorkspace_sptr &peakparamws);
74
76 Functions::BackToBackExponential_sptr genPeak(std::map<std::string, int> hklmap,
77 std::map<std::string, double> parammap,
78 std::map<std::string, std::string> bk2bk2braggmap, bool &good,
79 std::vector<int> &hkl, double &d_h);
80
82 bool getHKLFromMap(std::map<std::string, int> intmap, std::vector<int> &hkl);
83
85 void importInstrumentParameterFromTable(const DataObjects::TableWorkspace_sptr &parameterWS);
86
88 void parseBraggPeakTable(const DataObjects::TableWorkspace_sptr &peakws,
89 std::vector<std::map<std::string, double>> &parammaps,
90 std::vector<std::map<std::string, int>> &hklmaps);
91
93 void fitPeaksWithGoodStartingValues();
94
96 void fitPeaksRobust();
97
100 double leftdev, double rightdev, size_t m_wsIndex, double &chi2);
101
102 //---------------------------------------------------------------------------
103
105 bool fitSinglePeakRobust(const Functions::BackToBackExponential_sptr &peak,
106 const Functions::BackgroundFunction_sptr &backgroundfunction, double peakleftbound,
107 double peakrightbound, const std::map<std::string, double> &rightpeakparammap,
108 double &finalchi2);
109
111 bool fitSinglePeakSimulatedAnnealing(const Functions::BackToBackExponential_sptr &peak,
112 const std::vector<std::string> &paramtodomc);
113
116
118 bool fitSinglePeakConfident(const Functions::BackToBackExponential_sptr &peak,
119 const Functions::BackgroundFunction_sptr &backgroundfunction, double leftbound,
120 double rightbound, double &chi2, bool &annhilatedpeak);
121
124 double dampingfactor);
125
127 bool fitOverlappedPeaks(std::vector<Functions::BackToBackExponential_sptr> peaks,
128 const Functions::BackgroundFunction_sptr &backgroundfunction, double gfwhm);
129
131 bool doFitMultiplePeaks(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
132 const API::CompositeFunction_sptr &peaksfunc,
133 std::vector<Functions::BackToBackExponential_sptr> peakfuncs, std::vector<bool> &vecfitgood,
134 std::vector<double> &vecchi2s);
135
137 void estimatePeakHeightsLeBail(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
138 std::vector<Functions::BackToBackExponential_sptr> peaks);
139
141 void setOverlappedPeaksConstraints(const std::vector<Functions::BackToBackExponential_sptr> &peaks);
142
144 bool doFit1PeakSimple(const DataObjects::Workspace2D_sptr &dataws, size_t workspaceindex,
145 const Functions::BackToBackExponential_sptr &peakfunction, const std::string &minimzername,
146 size_t maxiteration, double &chi2);
147
149 // bool doFit1PeakBackgroundSimple(DataObjects::Workspace2D_sptr dataws,
150 // size_t workspaceindex,
151 // BackToBackExponential_sptr peakfunction,
152 // BackgroundFunction_sptr backgroundfunction,
153 // string minimzername, size_t maxiteration, double &chi2);
154
156 bool doFit1PeakBackground(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
158 const Functions::BackgroundFunction_sptr &backgroundfunction, double &chi2);
159
161 bool doFit1PeakSequential(const DataObjects::Workspace2D_sptr &dataws, size_t workspaceindex,
162 const Functions::BackToBackExponential_sptr &peakfunction,
163 std::vector<std::string> minimzernames, std::vector<size_t> maxiterations,
164 const std::vector<double> &dampfactors, double &chi2);
165
167 bool doFitNPeaksSimple(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex,
168 const API::CompositeFunction_sptr &peaksfunc,
169 const std::vector<Functions::BackToBackExponential_sptr> &peakfuncs,
170 const std::string &minimizername, size_t maxiteration, double &chi2);
171
173 void storeFunctionParameters(const API::IFunction_sptr &function, std::map<std::string, double> &parammaps);
174
176 void restoreFunctionParameters(const API::IFunction_sptr &function, std::map<std::string, double> parammap);
177
179 void calculatePeakFitBoundary(size_t ileftpeak, size_t irightpeak, double &peakleftboundary,
180 double &peakrightboundary);
181
182 //---------------------------------------------------------------------------
183
185 bool findMaxHeight(API::MatrixWorkspace_sptr dataws, size_t wsindex, double xmin, double xmax, double &center,
186 double &centerleftbound, double &centerrightbound, int &errordirection);
187
189 DataObjects::Workspace2D_sptr genPeakParameterDataWorkspace();
190
192 std::pair<DataObjects::TableWorkspace_sptr, DataObjects::TableWorkspace_sptr> genPeakParametersWorkspace();
193
195 void cropWorkspace(double tofmin, double tofmax);
196
198 std::string parseFitParameterWorkspace(const API::ITableWorkspace_sptr &paramws);
199
201 // DataObjects::Workspace2D_sptr
202 // buildPartialWorkspace(API::MatrixWorkspace_sptr sourcews, size_t m_wsIndex,
203 // double leftbound, double rightbound);
204
208 Functions::BackToBackExponential_sptr rightpeak, double fwhm, bool ismostright,
209 size_t m_wsIndex, double &chi2);
210
212 void observePeakRange(const Functions::BackToBackExponential_sptr &thispeak,
213 const Functions::BackToBackExponential_sptr &rightpeak, double refpeakshift,
214 double &peakleftbound, double &peakrightbound);
215
217 // void estimateBackground(DataObjects::Workspace2D_sptr dataws);
218
222
224 bool estimateFWHM(DataObjects::Workspace2D_sptr dataws, size_t wsindex, double tof_h, double &leftfwhm,
225 double &rightfwhm);
226
229 double leftpeakbound, double rightpeakbound);
230
232 std::pair<bool, double> doFitPeak_Old(DataObjects::Workspace2D_sptr dataws,
233 Functions::BackToBackExponential_sptr peak, double guessedfwhm, bool calchi2);
234
235 std::pair<bool, double> doFitPeak(const DataObjects::Workspace2D_sptr &dataws,
236 const Functions::BackToBackExponential_sptr &peakfunction, double guessedfwhm);
237
239 bool doFitGaussianPeak(const DataObjects::Workspace2D_sptr &dataws, size_t workspaceindex, double in_center,
240 double leftfwhm, double rightfwhm, double &center, double &sigma, double &height);
241
243 DataObjects::Workspace2D_sptr genOutputFittedPatternWorkspace(const std::vector<double> &pattern, int workspaceindex);
244
247
249 std::string parseFitResult(const API::IAlgorithm_sptr &fitalg, double &chi2, bool &fitsuccess);
250
252 double calculatePeakCentreTOF(int h, int k, int l);
253
255 double getParameter(const std::string &parname);
256
258 void fitPeaksGroup(std::vector<size_t> peakindexes);
259
261 DataObjects::Workspace2D_sptr buildPartialWorkspace(const API::MatrixWorkspace_sptr &sourcews, size_t workspaceindex,
262 double leftbound, double rightbound);
263
265 void plotFunction(const API::IFunction_sptr &peakfunction, const Functions::BackgroundFunction_sptr &background,
266 const API::FunctionDomain1DVector &domain);
267
268 //-----------------------------------------------------------------------------------------------
269
272
275
278
279 // Map for all peaks to fit individually
280 // Disabled std::map<std::vector<int>,
281 // CurveFitting::BackToBackExponential_sptr> m_peaksmap;
282
284 std::vector<std::pair<double, std::pair<std::vector<int>, Functions::BackToBackExponential_sptr>>> m_vecPeakFunctions;
285
287 std::vector<double> m_peakFitChi2;
288
290 std::vector<bool> m_goodFit;
291
293 std::map<std::string, double> m_instrumentParmaeters;
294
296 std::vector<double> m_peakData;
297
299 std::vector<std::string> mPeakParameterNames;
300
303
305 double m_tofMin;
306 double m_tofMax;
307
310
313
315 std::vector<int> m_minimumHKL;
316
319
320 std::vector<size_t> m_indexGoodFitPeaks;
321 std::vector<double> m_chi2GoodFitPeaks;
322
324 enum { ROBUSTFIT, TRUSTINPUTFIT } m_fitMode;
325
327 enum { HKLCALCULATION, FROMBRAGGTABLE } m_genPeakStartingValue;
328
330 std::vector<int> m_rightmostPeakHKL;
331
334
337
340
343
346};
347
350inline double linearInterpolateX(double x0, double xf, double y0, double yf, double y) {
351 double x = ((xf - x0) * y - (xf * y0 - x0 * yf)) / (yf - y0);
352 return x;
353}
354
357inline double linearInterpolateY(double x0, double xf, double y0, double yf, double x) {
358 double y = ((xf * y0 - x0 * yf) + x * (yf - y0)) / (xf - x0);
359 return y;
360}
361
364 const Functions::BackgroundFunction_sptr &background, size_t wsindexraw,
365 size_t wsindexbkgd, size_t wsindexpeak);
366
368bool observePeakParameters(const DataObjects::Workspace2D_sptr &dataws, size_t wsindex, double &centre, double &height,
369 double &fwhm, std::string &errmsg);
370
372size_t findMaxValue(std::span<double const> Y);
373
375size_t findMaxValue(const API::MatrixWorkspace_sptr &dataws, size_t wsindex, double leftbound, double rightbound);
376
378std::string getFunctionInfo(const API::IFunction_sptr &function);
379
380} // namespace Algorithms
381} // namespace CurveFitting
382} // namespace Mantid
double centre
double background
double height
Definition GetAllEi.cpp:155
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
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:748
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)
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)
size_t findMaxValue(std::span< double const > Y)
Find maximum value.
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.