-
Notifications
You must be signed in to change notification settings - Fork 122
/
IqtFit.cpp
173 lines (136 loc) · 6.29 KB
/
IqtFit.cpp
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 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 +
#include "MantidAPI/AlgorithmManager.h"
#include "MantidCurveFitting/Algorithms/IqtFit.h"
#include "MantidCurveFitting/Algorithms/QENSFitSequential.h"
#include "MantidCurveFitting/Algorithms/QENSFitSimultaneous.h"
#include "MantidKernel/Logger.h"
using namespace Mantid::API;
namespace {
Mantid::Kernel::Logger g_log("IqtFit");
MatrixWorkspace_sptr cropWorkspace(const MatrixWorkspace_sptr &workspace, double startX, double endX) {
auto cropper = AlgorithmManager::Instance().create("CropWorkspace");
cropper->setLogging(false);
cropper->setAlwaysStoreInADS(false);
cropper->setProperty("InputWorkspace", workspace);
cropper->setProperty("OutputWorkspace", "__cropped");
cropper->setProperty("XMin", startX);
cropper->setProperty("XMax", endX);
cropper->execute();
return cropper->getProperty("OutputWorkspace");
}
MatrixWorkspace_sptr convertToHistogram(const MatrixWorkspace_sptr &workspace) {
auto converter = AlgorithmManager::Instance().create("ConvertToHistogram");
converter->setLogging(false);
converter->setAlwaysStoreInADS(false);
converter->setProperty("InputWorkspace", workspace);
converter->setProperty("OutputWorkspace", "__converted");
converter->execute();
return converter->getProperty("OutputWorkspace");
}
struct HistogramConverter {
explicit HistogramConverter() : m_converted() {}
MatrixWorkspace_sptr operator()(const MatrixWorkspace_sptr &workspace, double startX, double endX) const {
auto it = m_converted.find(workspace.get());
if (it != m_converted.end())
return it->second;
else {
auto histogram = convertToHistogram(cropWorkspace(workspace, startX, endX));
m_converted[workspace.get()] = histogram;
return histogram;
}
}
private:
mutable std::unordered_map<MatrixWorkspace *, MatrixWorkspace_sptr> m_converted;
};
std::string getPropertySuffix(std::size_t index) { return index == 0 ? "" : "_" + std::to_string(index); }
} // namespace
namespace Mantid {
namespace CurveFitting {
namespace Algorithms {
using namespace API;
/// Algorithms name for identification. @see Algorithm::name
template <> const std::string IqtFit<QENSFitSequential>::name() const { return "IqtFitSequential"; }
template <> const std::string IqtFit<QENSFitSimultaneous>::name() const { return "IqtFitSimultaneous"; }
template <typename Base> const std::string IqtFit<Base>::name() const { return "IqtFit"; }
/// Algorithm's version for identification. @see Algorithm::version
template <typename Base> int IqtFit<Base>::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
template <typename Base> const std::string IqtFit<Base>::category() const { return "Workflow\\MIDAS"; }
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
template <> const std::string IqtFit<QENSFitSequential>::summary() const {
return "Fits data files (\\*\\_iqt) generated by I(Q, t) sequentially.";
}
template <> const std::string IqtFit<QENSFitSimultaneous>::summary() const {
return "Fits data files (\\*\\_iqt) generated by I(Q, t) simultaneously.";
}
template <typename Base> const std::string IqtFit<Base>::summary() const {
return "Fits an \\*\\_iqt file generated by I(Q, t) sequentially.";
}
/// Algorithm's see also for related algorithms. @see Algorithm::seeAlso
template <> const std::vector<std::string> IqtFit<QENSFitSequential>::seeAlso() const { return {"QENSFitSequential"}; }
template <> const std::vector<std::string> IqtFit<QENSFitSimultaneous>::seeAlso() const {
return {"QENSFitSimultaneous"};
}
template <typename Base> const std::vector<std::string> IqtFit<Base>::seeAlso() const { return {}; }
template <> std::map<std::string, std::string> IqtFit<QENSFitSequential>::validateInputs() {
auto errors = QENSFitSequential::validateInputs();
const std::vector<double> startX = QENSFitSequential::getProperty("StartX");
for (const double &start : startX) {
if (start < 0) {
errors["StartX"] = "StartX must be greater than or equal to 0.";
}
}
return errors;
}
template <typename Base> std::map<std::string, std::string> IqtFit<Base>::validateInputs() {
return Base::validateInputs();
}
template <typename Base> bool IqtFit<Base>::isFitParameter(const std::string &name) const {
return name.rfind("A0") != std::string::npos || name.rfind("Height") != std::string::npos ||
name.rfind("Stretching") != std::string::npos || name.rfind("Lifetime") != std::string::npos;
}
template <typename Base> bool IqtFit<Base>::throwIfElasticQConversionFails() const { return true; }
template <typename Base> std::vector<API::MatrixWorkspace_sptr> IqtFit<Base>::getWorkspaces() const {
auto workspaces = Base::getWorkspaces();
HistogramConverter convert;
for (std::size_t i = 0; i < workspaces.size(); ++i)
workspaces[i] = convert(workspaces[i], getStartX(i), getEndX(i));
return workspaces;
}
template <> double IqtFit<QENSFitSimultaneous>::getStartX(std::size_t index) const {
return QENSFitSimultaneous::getProperty("StartX" + getPropertySuffix(index));
}
template <> double IqtFit<QENSFitSequential>::getStartX(std::size_t i) const {
std::vector<double> startX = QENSFitSequential::getProperty("StartX");
if (startX.size() == 1) {
return startX[0];
} else {
return startX[i];
}
}
template <> double IqtFit<QENSFitSimultaneous>::getEndX(std::size_t index) const {
return QENSFitSimultaneous::getProperty("EndX" + getPropertySuffix(index));
}
template <> double IqtFit<QENSFitSequential>::getEndX(std::size_t i) const {
std::vector<double> endX = QENSFitSequential::getProperty("EndX");
if (endX.size() == 1) {
return endX[0];
} else {
return endX[i];
}
}
// Register the algorithms into the AlgorithmFactory
template class IqtFit<QENSFitSequential>;
template class IqtFit<QENSFitSimultaneous>;
using IqtFitSequential = IqtFit<QENSFitSequential>;
using IqtFitSimultaneous = IqtFit<QENSFitSimultaneous>;
DECLARE_ALGORITHM(IqtFitSequential)
DECLARE_ALGORITHM(IqtFitSimultaneous)
} // namespace Algorithms
} // namespace CurveFitting
} // namespace Mantid