-
Notifications
You must be signed in to change notification settings - Fork 122
/
FitResolutionConvolvedModel.cpp
189 lines (159 loc) · 7.05 KB
/
FitResolutionConvolvedModel.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
#include "MantidMDAlgorithms/Quantification/FitResolutionConvolvedModel.h"
#include "MantidAPI/IMDEventWorkspace.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidMDAlgorithms/Quantification/ResolutionConvolvedCrossSection.h"
#include "MantidMDAlgorithms/Quantification/ForegroundModelFactory.h"
#include "MantidMDAlgorithms/Quantification/MDResolutionConvolutionFactory.h"
namespace Mantid {
namespace MDAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(FitResolutionConvolvedModel)
using Kernel::Direction;
using Kernel::ListValidator;
using Kernel::MandatoryValidator;
using API::ITableWorkspace;
using API::ITableWorkspace_sptr;
using API::IMDEventWorkspace;
using API::IMDEventWorkspace_sptr;
using API::MatrixWorkspace;
using API::MatrixWorkspace_sptr;
using API::WorkspaceProperty;
namespace {
// Property names
const char *INPUT_WS_NAME = "InputWorkspace";
const char *SIMULATED_NAME = "OutputWorkspace";
const char *OUTPUT_PARS = "OutputParameters";
const char *OUTPUTCOV_MATRIX = "CovarianceMatrix";
const char *RESOLUTION_NAME = "ResolutionFunction";
const char *FOREGROUND_NAME = "ForegroundModel";
const char *PARS_NAME = "Parameters";
const char *MAX_ITER_NAME = "MaxIterations";
}
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string FitResolutionConvolvedModel::name() const {
return "FitResolutionConvolvedModel";
}
/// Algorithm's version for identification. @see Algorithm::version
int FitResolutionConvolvedModel::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string FitResolutionConvolvedModel::category() const {
return "Inelastic\\Quantification";
}
//----------------------------------------------------------------------------------------------
/// Returns the number of iterations that should be performed
int FitResolutionConvolvedModel::niterations() const {
int maxIter = getProperty(MAX_ITER_NAME);
return maxIter;
}
/// Returns the name of the max iterations property
std::string FitResolutionConvolvedModel::maxIterationsPropertyName() const {
return MAX_ITER_NAME;
}
/// Returns the name of the output parameters property
std::string FitResolutionConvolvedModel::outputParsPropertyName() const {
return OUTPUT_PARS;
}
/// Returns the name of the covariance matrix property
std::string FitResolutionConvolvedModel::covMatrixPropertyName() const {
return OUTPUTCOV_MATRIX;
}
/**
* Create the function string required by fit
* @return Creates a string that can be passed to fit containing the necessary
* setup for the function
*/
std::string FitResolutionConvolvedModel::createFunctionString() const {
std::ostringstream stringBuilder;
const char seperator(',');
stringBuilder << "name=" << ResolutionConvolvedCrossSection().name()
<< seperator << "ResolutionFunction="
<< this->getPropertyValue(RESOLUTION_NAME) << seperator
<< "ForegroundModel=" << this->getPropertyValue(FOREGROUND_NAME)
<< seperator << this->getPropertyValue("Parameters");
return stringBuilder.str();
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void FitResolutionConvolvedModel::init() {
declareProperty(Kernel::make_unique<WorkspaceProperty<IMDEventWorkspace>>(
INPUT_WS_NAME, "", Direction::Input),
"The input MDEvent workspace");
declareProperty(Kernel::make_unique<WorkspaceProperty<IMDEventWorkspace>>(
SIMULATED_NAME, "", Direction::Output),
"The simulated output workspace");
declareProperty(Kernel::make_unique<WorkspaceProperty<ITableWorkspace>>(
OUTPUT_PARS, "", Direction::Output),
"The name of the TableWorkspace in which to store the final "
"fit parameters");
declareProperty(Kernel::make_unique<WorkspaceProperty<API::ITableWorkspace>>(
OUTPUTCOV_MATRIX, "", Direction::Output),
"The name of the TableWorkspace in which to store the final "
"covariance matrix");
std::vector<std::string> models =
MDResolutionConvolutionFactory::Instance().getKeys();
declareProperty(RESOLUTION_NAME, "",
boost::make_shared<ListValidator<std::string>>(models),
"The name of a resolution model", Direction::Input);
models = ForegroundModelFactory::Instance().getKeys();
declareProperty(FOREGROUND_NAME, "",
boost::make_shared<ListValidator<std::string>>(models),
"The name of a foreground function", Direction::Input);
declareProperty(MAX_ITER_NAME, 20,
"The maximum number of iterations to perform for the fitting",
Direction::Input);
declareProperty(PARS_NAME, "",
boost::make_shared<MandatoryValidator<std::string>>(),
"The parameters/attributes for the function & model. See Fit "
"documentation for format",
Direction::Input);
}
//----------------------------------------------------------------------------------------------
/**
* Execute the algorithm.
*/
void FitResolutionConvolvedModel::exec() {
API::IAlgorithm_sptr fit = createFittingAlgorithm();
fit->setPropertyValue("Function", createFunctionString());
fit->setProperty("InputWorkspace", getPropertyValue(INPUT_WS_NAME));
fit->setProperty("DomainType",
"Simple"); // Parallel not quite giving correct answers
fit->setProperty("Minimizer", "Levenberg-MarquardtMD");
const int maxIter = niterations();
fit->setProperty("MaxIterations", maxIter);
fit->setProperty("CreateOutput", true);
fit->setPropertyValue("Output", getPropertyValue(SIMULATED_NAME));
try {
fit->execute();
} catch (std::exception &exc) {
throw std::runtime_error(
std::string("FitResolutionConvolvedModel - Error running Fit: ") +
exc.what());
}
// Pass on the relevant properties
IMDEventWorkspace_sptr simulatedData = fit->getProperty("OutputWorkspace");
this->setProperty(SIMULATED_NAME, simulatedData);
if (this->existsProperty(OUTPUT_PARS)) {
ITableWorkspace_sptr outputPars = fit->getProperty("OutputParameters");
setProperty(OUTPUT_PARS, outputPars);
}
if (this->existsProperty(OUTPUTCOV_MATRIX)) {
ITableWorkspace_sptr covarianceMatrix =
fit->getProperty("OutputNormalisedCovarianceMatrix");
setProperty(OUTPUTCOV_MATRIX, covarianceMatrix);
}
}
/**
* Create the fitting Child Algorithm
* @return A shared pointer to the new algorithm
*/
API::IAlgorithm_sptr FitResolutionConvolvedModel::createFittingAlgorithm() {
const double startProgress(0.0), endProgress(1.0);
const bool enableLogging(true);
return createChildAlgorithm("Fit", startProgress, endProgress, enableLogging);
}
} // namespace MDAlgorithms
} // namespace Mantid