-
Notifications
You must be signed in to change notification settings - Fork 122
/
ALCBaselineModellingModel.cpp
197 lines (157 loc) · 6.09 KB
/
ALCBaselineModellingModel.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "MantidQtCustomInterfaces/Muon/ALCBaselineModellingModel.h"
#include "MantidQtCustomInterfaces/Muon/ALCHelper.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/TextAxis.h"
#include "MantidAPI/TableRow.h"
using namespace Mantid::API;
namespace MantidQt
{
namespace CustomInterfaces
{
void ALCBaselineModellingModel::fit(IFunction_const_sptr function, const std::vector<Section>& sections)
{
// Create a copy of the data
IAlgorithm_sptr clone = AlgorithmManager::Instance().create("CloneWorkspace");
clone->setChild(true);
clone->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data));
clone->setProperty("OutputWorkspace", "__NotUsed__");
clone->execute();
Workspace_sptr cloned = clone->getProperty("OutputWorkspace");
MatrixWorkspace_sptr dataToFit = boost::dynamic_pointer_cast<MatrixWorkspace>(cloned);
assert(dataToFit); // CloneWorkspace should take care of that
disableUnwantedPoints(dataToFit, sections);
IFunction_sptr funcToFit =
FunctionFactory::Instance().createInitialized(function->asString());
IAlgorithm_sptr fit = AlgorithmManager::Instance().create("Fit");
fit->setChild(true);
fit->setProperty("Function", funcToFit);
fit->setProperty("InputWorkspace", dataToFit);
fit->setProperty("CreateOutput", true);
fit->execute();
MatrixWorkspace_sptr fitOutput = fit->getProperty("OutputWorkspace");
m_parameterTable = fit->getProperty("OutputParameters");
enableDisabledPoints(fitOutput,m_data);
setCorrectedData(fitOutput);
setFittedFunction(funcToFit);
m_sections = sections;
}
void ALCBaselineModellingModel::setData(MatrixWorkspace_const_sptr data)
{
m_data = data;
emit dataChanged();
}
/**
* Disable points in the workpsace in the way that points which are not included in any of specified
* sections are not used when fitting given workspace
* @param ws :: Workspace to disable points in
* @param sections :: Section we want to use for fitting
*/
void ALCBaselineModellingModel::disableUnwantedPoints(MatrixWorkspace_sptr ws,
const std::vector<IALCBaselineModellingModel::Section>& sections)
{
// Whether point with particular index should be disabled
std::vector<bool> toDisable(ws->blocksize(), true);
// Find points which are in at least one section, and exclude them from disable list
for (size_t i = 0; i < ws->blocksize(); ++i)
{
for (auto it = sections.begin(); it != sections.end(); ++it)
{
if ( ws->dataX(0)[i] >= it->first && ws->dataX(0)[i] <= it->second )
{
toDisable[i] = false;
break; // No need to check other sections
}
}
}
// XXX: Points are disabled by settings their errors to very high value. This makes those
// points to have very low weights during the fitting, effectively disabling them.
const double DISABLED_ERR = std::numeric_limits<double>::max();
// Disable chosen points
for (size_t i = 0; i < ws->blocksize(); ++i)
{
if (toDisable[i])
{
ws->dataE(0)[i] = DISABLED_ERR;
}
}
}
/**
* Enable points that were disabled for fit
* @param destWs :: Workspace to enable points in
* @param sourceWs :: Workspace with original errors
*/
void ALCBaselineModellingModel::enableDisabledPoints (MatrixWorkspace_sptr destWs, MatrixWorkspace_const_sptr sourceWs)
{
// Unwanted points were disabled by setting their errors to very high values.
// We recover here the original errors stored in sourceWs
destWs->dataE(0) = sourceWs->readE(0);
}
MatrixWorkspace_sptr ALCBaselineModellingModel::exportWorkspace()
{
if ( m_data && m_data->getNumberHistograms() == 3 ) {
// Export results only if data have been fit, that is,
// if m_data has three histograms
return boost::const_pointer_cast<MatrixWorkspace>(m_data);
} else {
return MatrixWorkspace_sptr();
}
}
ITableWorkspace_sptr ALCBaselineModellingModel::exportSections()
{
if ( !m_sections.empty() ) {
ITableWorkspace_sptr table = WorkspaceFactory::Instance().createTable("TableWorkspace");
table->addColumn("double", "Start X");
table->addColumn("double", "End X");
for(auto it = m_sections.begin(); it != m_sections.end(); ++it)
{
TableRow newRow = table->appendRow();
newRow << it->first << it->second;
}
return table;
} else {
return ITableWorkspace_sptr();
}
}
ITableWorkspace_sptr ALCBaselineModellingModel::exportModel()
{
if ( m_parameterTable ) {
return m_parameterTable;
} else {
return ITableWorkspace_sptr();
}
}
void ALCBaselineModellingModel::setCorrectedData(MatrixWorkspace_const_sptr data)
{
m_data = data;
emit correctedDataChanged();
}
void ALCBaselineModellingModel::setFittedFunction(IFunction_const_sptr function)
{
m_fittedFunction = function;
emit fittedFunctionChanged();
}
MatrixWorkspace_const_sptr ALCBaselineModellingModel::data() const
{
IAlgorithm_sptr extract = AlgorithmManager::Instance().create("ExtractSingleSpectrum");
extract->setChild(true);
extract->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data));
extract->setProperty("WorkspaceIndex", 0);
extract->setProperty("OutputWorkspace", "__NotUsed__");
extract->execute();
MatrixWorkspace_const_sptr result = extract->getProperty("OutputWorkspace");
return result;
}
MatrixWorkspace_const_sptr ALCBaselineModellingModel::correctedData() const
{
IAlgorithm_sptr extract = AlgorithmManager::Instance().create("ExtractSingleSpectrum");
extract->setChild(true);
extract->setProperty("InputWorkspace", boost::const_pointer_cast<MatrixWorkspace>(m_data));
extract->setProperty("WorkspaceIndex", 2);
extract->setProperty("OutputWorkspace", "__NotUsed__");
extract->execute();
MatrixWorkspace_const_sptr result = extract->getProperty("OutputWorkspace");
return result;
}
} // namespace CustomInterfaces
} // namespace Mantid