forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertFitFunctionForMuonTFAsymmetry.cpp
381 lines (347 loc) · 14.6 KB
/
ConvertFitFunctionForMuonTFAsymmetry.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// 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 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidMuon/ConvertFitFunctionForMuonTFAsymmetry.h"
#include "MantidAPI/ADSValidator.h"
#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/FunctionProperty.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MultiDomainFunction.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/CompositeValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/PhysicalConstants.h"
#include <vector>
namespace {
constexpr double MICROSECONDS_PER_SECOND{1000000.0};
constexpr double MUON_LIFETIME_MICROSECONDS{Mantid::PhysicalConstants::MuonLifetime * MICROSECONDS_PER_SECOND};
const std::string INSERT_FUNCTION{"f0.f1.f1."};
std::string trimTie(const std::string &stringTie) {
auto index = stringTie.find_first_of(".");
std::string domain = stringTie.substr(0, index);
std::string userFunc = stringTie.substr(9 + index, std::string::npos);
return domain + userFunc;
}
std::string rmInsertFunction(const std::string &originalTie) {
auto stringTie = originalTie;
// check the tie only exists in the user function (f)
auto seperator = stringTie.find_first_of("=");
// the wrapped name added 9 characters
auto LHName = stringTie.substr(0, seperator);
LHName = trimTie(LHName);
// this one includes = sign
auto RHName = stringTie.substr(seperator, std::string::npos);
RHName = trimTie(RHName);
return LHName + RHName;
}
int findName(const std::vector<std::string> &colNames, const char *name) {
for (size_t j = 0; j < colNames.size(); j++) {
if (colNames[j] == name) {
return static_cast<int>(j);
}
}
return -1;
}
} // namespace
namespace Mantid {
namespace Muon {
using namespace Kernel;
using namespace API;
using std::size_t;
// Register the class into the algorithm factory
DECLARE_ALGORITHM(ConvertFitFunctionForMuonTFAsymmetry)
/** Initialisation method. Declares properties to be used in algorithm.
*
*/
void ConvertFitFunctionForMuonTFAsymmetry::init() {
declareProperty(std::make_unique<FunctionProperty>("InputFunction"), "The fitting function to be converted.");
// table of name, norms
// if construct -> read relevant norms into sorted list
declareProperty(std::make_unique<API::WorkspaceProperty<API::ITableWorkspace>>(
"NormalizationTable", "", Direction::Input, API::PropertyMode::Optional),
"Name of the table containing the normalizations for the asymmetries.");
// list of workspaces
declareProperty(
std::make_unique<Kernel::ArrayProperty<std::string>>("WorkspaceList", std::make_shared<API::ADSValidator>()),
"An ordered list of workspaces (to get the initial values "
"for the normalizations).");
std::vector<std::string> allowedModes{"Construct", "Extract"};
auto modeVal = std::make_shared<Kernel::CompositeValidator>();
modeVal->add(std::make_shared<Kernel::StringListValidator>(allowedModes));
modeVal->add(std::make_shared<Kernel::MandatoryValidator<std::string>>());
declareProperty("Mode", "Construct", modeVal,
"Mode to run in. Construct will convert the"
"input function into one suitable for calculating the"
" TF Asymmetry. Extract will find the original user function"
" from a function that is suitable for TF Asymmetry calculations.");
declareProperty(std::make_unique<FunctionProperty>("OutputFunction", Direction::Output),
"The converted fitting function.");
declareProperty("CopyTies", true,
"Set to true to copy over ties from input function"
"(default is true).");
}
/*
* Validate the input parameters
* @returns map with keys corresponding to properties with errors and values
* containing the error messages.
*/
std::map<std::string, std::string> ConvertFitFunctionForMuonTFAsymmetry::validateInputs() {
// create the map
std::map<std::string, std::string> result;
// check norm table is correct
API::ITableWorkspace_const_sptr tabWS = getProperty("NormalizationTable");
if (tabWS) {
if (tabWS->columnCount() == 0) {
result["NormalizationTable"] = "Please provide a non-empty NormalizationTable.";
}
// NormalizationTable should have three columns: (norm, name, method)
if (tabWS->columnCount() != 3) {
result["NormalizationTable"] = "NormalizationTable must have three columns";
}
auto names = tabWS->getColumnNames();
int normCount = 0;
int wsNamesCount = 0;
for (const std::string &name : names) {
if (name == "norm") {
normCount += 1;
}
if (name == "name") {
wsNamesCount += 1;
}
}
if (normCount == 0) {
result["NormalizationTable"] = "NormalizationTable needs norm column";
}
if (wsNamesCount == 0) {
result["NormalizationTable"] = "NormalizationTable needs a name column";
}
if (normCount > 1) {
result["NormalizationTable"] = "NormalizationTable has " + std::to_string(normCount) + " norm columns";
}
if (wsNamesCount > 1) {
result["NormalizationTable"] = "NormalizationTable has " + std::to_string(wsNamesCount) + " name columns";
}
} else {
const std::vector<std::string> wsNames = getProperty("WorkspaceList");
for (std::string name : wsNames) {
API::MatrixWorkspace_const_sptr ws = API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>(name);
const Mantid::API::Run &run = ws->run();
if (!run.hasProperty("analysis_asymmetry_norm")) {
result["NormalizationTable"] = "NormalizationTable has not been "
"included and no sample logs for "
"nrmalization.";
}
}
}
// Check units, should be microseconds
return result;
}
/** Executes the algorithm
*
*/
void ConvertFitFunctionForMuonTFAsymmetry::exec() {
IFunction_sptr inputFitFunction = getProperty("InputFunction");
auto mode = getPropertyValue("Mode");
if (mode == "Construct") {
std::vector<double> norms = getNorms();
auto outputFitFunction = getTFAsymmFitFunction(inputFitFunction, norms);
setOutput(outputFitFunction);
} else {
try {
auto outputFitFunction = extractFromTFAsymmFitFunction(inputFitFunction);
setOutput(outputFitFunction);
} catch (...) {
throw std::runtime_error("The input function was not of the form N*(1+f)+A*exp(-lambda*t)");
}
}
}
void ConvertFitFunctionForMuonTFAsymmetry::setOutput(const Mantid::API::IFunction_sptr &function) {
IFunction_sptr outputFitFunction = function;
const std::vector<std::string> wsNames = getProperty("WorkspaceList");
if (wsNames.size() == 1) {
// if single domain func, strip off multi domain
auto TFFunc = std::dynamic_pointer_cast<CompositeFunction>(function);
outputFitFunction = TFFunc->getFunction(0);
}
setProperty("OutputFunction", outputFitFunction);
}
/** Extracts the user's original function f from the normalization function
* N(1+f)+expDecay
* and adds in the ties
* @param original :: [input] normalization function
* @return :: user function
*/
Mantid::API::IFunction_sptr
ConvertFitFunctionForMuonTFAsymmetry::extractFromTFAsymmFitFunction(const Mantid::API::IFunction_sptr &original) {
auto multi = std::make_shared<MultiDomainFunction>();
IFunction_sptr tmp = original;
size_t numDomains = original->getNumberDomains();
for (size_t j = 0; j < numDomains; j++) {
auto TFFunc = std::dynamic_pointer_cast<CompositeFunction>(original);
if (numDomains > 1) {
// get correct domain
tmp = TFFunc->getFunction(j);
multi->setDomainIndex(j, j);
}
IFunction_sptr userFunc = extractUserFunction(tmp);
multi->addFunction(userFunc);
}
// if multi data set we need to do the ties manually
bool copyTies = getProperty("CopyTies");
if (numDomains > 1 && copyTies) {
auto originalNames = original->getParameterNames();
for (auto name : originalNames) {
auto index = original->parameterIndex(name);
auto originalTie = original->getTie(index);
if (originalTie) {
auto stringTie = originalTie->asString();
// check the tie only exists in the user function (f)
auto start = stringTie.find_first_of(".") + 1;
auto end = stringTie.find_first_of("=");
// the wrapped name added 9 characters
auto LHName = stringTie.substr(start, 9);
// need to do in 2 steps
auto RHName = stringTie.substr(end, std::string::npos);
start = RHName.find_first_of(".") + 1;
RHName = RHName.substr(start, 9);
if (LHName == INSERT_FUNCTION && LHName == RHName) {
// get new tie
auto newTie = rmInsertFunction(stringTie);
multi->addTies(newTie);
}
}
}
}
return std::dynamic_pointer_cast<IFunction>(multi);
}
/** Extracts the user's original function f from the normalization function
* N(1+f)+expDecay
* @param original :: [input] normalization function
* @return :: user function
*/
IFunction_sptr ConvertFitFunctionForMuonTFAsymmetry::extractUserFunction(const IFunction_sptr &TFFuncIn) {
// N(1+g) + exp
auto TFFunc = std::dynamic_pointer_cast<CompositeFunction>(TFFuncIn);
if (TFFunc == nullptr) {
throw std::runtime_error("Input function is not of the correct form");
}
// getFunction(0) -> N(1+g)
TFFunc = std::dynamic_pointer_cast<CompositeFunction>(TFFunc->getFunction(0));
if (TFFunc == nullptr) {
throw std::runtime_error("Input function is not of the correct form");
}
// getFunction(1) -> 1+g
TFFunc = std::dynamic_pointer_cast<CompositeFunction>(TFFunc->getFunction(1));
if (TFFunc == nullptr) {
throw std::runtime_error("Input function is not of the correct form");
}
// getFunction(1) -> g
return TFFunc->getFunction(1);
}
/** Get the nomralisation constants from the table
* the order is the same as the workspace list
* @return :: vector of normals
*/
std::vector<double> ConvertFitFunctionForMuonTFAsymmetry::getNorms() {
API::ITableWorkspace_sptr table = getProperty("NormalizationTable");
const std::vector<std::string> wsNames = getProperty("WorkspaceList");
std::vector<double> norms(wsNames.size(), 0);
if (table) {
auto colNames = table->getColumnNames();
auto wsNamesIndex = findName(colNames, "name");
auto normIndex = findName(colNames, "norm");
for (size_t row = 0; row < table->rowCount(); row++) {
for (size_t wsPosition = 0; wsPosition < wsNames.size(); wsPosition++) {
std::string wsName = wsNames[wsPosition];
std::replace(wsName.begin(), wsName.end(), ' ', ';');
if (table->String(row, wsNamesIndex) == wsName) {
norms[wsPosition] = table->Double(row, normIndex);
}
}
}
} else {
for (size_t wsPosition = 0; wsPosition < wsNames.size(); wsPosition++) {
std::string wsName = wsNames[wsPosition];
API::MatrixWorkspace_const_sptr ws =
API::AnalysisDataService::Instance().retrieveWS<API::MatrixWorkspace>(wsName);
const Mantid::API::Run &run = ws->run();
norms[wsPosition] = std::stod(run.getProperty("analysis_asymmetry_norm")->value());
}
}
return norms;
}
/** Gets the fitting function for TFAsymmetry fit
* @param original :: The user function f
* @param norms :: vector of normalization constants
* @returns :: The normalization function N(1+f) +ExpDecay
*/
Mantid::API::IFunction_sptr
ConvertFitFunctionForMuonTFAsymmetry::getTFAsymmFitFunction(const Mantid::API::IFunction_sptr &original,
const std::vector<double> &norms) {
auto multi = std::make_shared<MultiDomainFunction>();
auto tmp = std::dynamic_pointer_cast<MultiDomainFunction>(original);
size_t numDomains = original->getNumberDomains();
for (size_t j = 0; j < numDomains; j++) {
IFunction_sptr userFunc;
auto constant = FunctionFactory::Instance().createInitialized("name = FlatBackground, A0 = 1.0, ties=(A0=1)");
if (numDomains == 1) {
userFunc = original;
} else {
userFunc = tmp->getFunction(j);
multi->setDomainIndex(j, j);
}
auto inBrace = std::make_shared<CompositeFunction>();
inBrace->addFunction(constant);
inBrace->addFunction(userFunc);
auto norm = FunctionFactory::Instance().createInitialized("name = FlatBackground, A0 "
"=" +
std::to_string(norms[j]));
auto product =
std::dynamic_pointer_cast<CompositeFunction>(FunctionFactory::Instance().createFunction("ProductFunction"));
product->addFunction(norm);
product->addFunction(inBrace);
auto composite =
std::dynamic_pointer_cast<CompositeFunction>(FunctionFactory::Instance().createFunction("CompositeFunction"));
constant = FunctionFactory::Instance().createInitialized(
"name = ExpDecayMuon, A = 0.0, Lambda = -" + std::to_string(MUON_LIFETIME_MICROSECONDS) +
",ties = (A = 0.0, Lambda = -" + std::to_string(MUON_LIFETIME_MICROSECONDS) + ")");
composite->addFunction(product);
composite->addFunction(constant);
multi->addFunction(composite);
}
// if multi data set we need to do the ties manually
bool copyTies = getProperty("CopyTies");
if (numDomains > 1 && copyTies) {
auto originalNames = original->getParameterNames();
for (auto name : originalNames) {
auto index = original->parameterIndex(name);
auto originalTie = original->getTie(index);
if (originalTie) {
auto stringTie = originalTie->asString();
// change name to reflect new postion
auto insertPosition = stringTie.find_first_of(".");
stringTie.insert(insertPosition + 1, INSERT_FUNCTION);
// need to change the other side of =
insertPosition = stringTie.find_first_of("=");
insertPosition = stringTie.find_first_of(".", insertPosition);
stringTie.insert(insertPosition + 1, INSERT_FUNCTION);
multi->addTies(stringTie);
}
}
}
return std::dynamic_pointer_cast<IFunction>(multi);
}
} // namespace Muon
} // namespace Mantid