forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculateMuonAsymmetryTest.h
387 lines (305 loc) · 12.6 KB
/
CalculateMuonAsymmetryTest.h
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
382
383
384
385
386
387
// 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 +
#pragma once
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
#include "MantidHistogramData/LinearGenerator.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidMuon/CalculateMuonAsymmetry.h"
#include <cxxtest/TestSuite.h>
#include <utility>
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/IFunction.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/TableRow.h"
using namespace Mantid::API;
using Mantid::MantidVec;
using Mantid::Algorithms::CalculateMuonAsymmetry;
const std::string outputName = "CalculateMuonAsymmetry_Output";
namespace {
struct yData {
double operator()(const double x, size_t) {
// Create a fake muon dataset
double a = 0.20; // Amplitude of the oscillations
double w = 5.0; // Frequency of the oscillations
double phi = 0.1;
return (3.4 * (1.0 + a * sin(w * x + phi)));
}
};
struct eData {
double operator()(const double, size_t) { return 0.005; }
};
MatrixWorkspace_sptr createWorkspace(size_t nspec, size_t maxt) {
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::create2DWorkspaceFromFunction(
yData(), static_cast<int>(nspec), 0.0, 10.0, 10.0 * (1.0 / static_cast<double>(maxt)), true, eData());
ws->setYUnit("Asymmetry");
return ws;
}
void genData() {
auto ws1 = createWorkspace(1, 200);
AnalysisDataService::Instance().addOrReplace("ws1", ws1);
auto ws2 = createWorkspace(1, 200);
AnalysisDataService::Instance().addOrReplace("ws2", ws2);
auto ws3 = createWorkspace(1, 200);
AnalysisDataService::Instance().addOrReplace("ws3", ws3);
auto ws4 = createWorkspace(1, 200);
AnalysisDataService::Instance().addOrReplace("ws4", ws4);
}
ITableWorkspace_sptr genTable() {
Mantid::API::ITableWorkspace_sptr table = Mantid::API::WorkspaceFactory::Instance().createTable();
table->addColumn("double", "norm");
table->addColumn("str", "name");
table->addColumn("str", "method");
// populate table
TableRow row = table->appendRow();
row << 2.2 << "ws1"
<< "Estimate";
row = table->appendRow();
row << 2.2 << "ws2"
<< "Estimate";
row = table->appendRow();
row << 2.2 << "ws3"
<< "Estimate";
row = table->appendRow();
row << 2.2 << "ws4"
<< "Estimate";
return table;
}
IAlgorithm_sptr setUpFuncAlg(const std::vector<std::string> &wsNames, const IFunction_sptr &func) {
auto asymmAlg = AlgorithmManager::Instance().create("ConvertFitFunctionForMuonTFAsymmetry");
asymmAlg->initialize();
asymmAlg->setChild(true);
asymmAlg->setProperty("WorkspaceList", wsNames);
ITableWorkspace_sptr table = genTable();
asymmAlg->setProperty("NormalizationTable", table);
asymmAlg->setProperty("InputFunction", func);
return asymmAlg;
}
IFunction_sptr genSingleFunc(const std::vector<std::string> &wsNames) {
IFunction_sptr func = FunctionFactory::Instance().createInitialized("name=GausOsc,Frequency=3.0");
auto alg = setUpFuncAlg(std::move(wsNames), func);
alg->execute();
IFunction_sptr funcOut = alg->getProperty("OutputFunction");
return funcOut;
}
IFunction_sptr genDoubleFunc(const std::vector<std::string> &wsNames) {
std::string multiFuncString = "composite=MultiDomainFunction,NumDeriv=1;";
multiFuncString += "name=GausOsc,$domains=i,Frequency=3.0;";
multiFuncString += "name=GausOsc,$domains=i,Frequency=3.0;";
IFunction_sptr func = FunctionFactory::Instance().createInitialized(multiFuncString);
auto alg = setUpFuncAlg(std::move(wsNames), func);
alg->execute();
IFunction_sptr funcOut = alg->getProperty("OutputFunction");
std::cout << funcOut << std::endl;
return funcOut;
}
IAlgorithm_sptr setUpAlg(ITableWorkspace_sptr &table, const IFunction_sptr &func,
const std::vector<std::string> &wsNamesNorm, const std::vector<std::string> &wsOut) {
auto asymmAlg = AlgorithmManager::Instance().create("CalculateMuonAsymmetry");
asymmAlg->initialize();
asymmAlg->setChild(true);
asymmAlg->setProperty("NormalizationTable", table);
asymmAlg->setProperty("StartX", 0.1);
asymmAlg->setProperty("EndX", 0.9);
asymmAlg->setProperty("InputFunction", func);
asymmAlg->setProperty("UnNormalizedWorkspaceList", wsNamesNorm);
asymmAlg->setProperty("ReNormalizedWorkspaceList", wsOut);
return asymmAlg;
}
void clearADS() { AnalysisDataService::Instance().clear(); }
class CalculateMuonAsymmetryTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static CalculateMuonAsymmetryTest *createSuite() { return new CalculateMuonAsymmetryTest(); }
static void destroySuite(CalculateMuonAsymmetryTest *suite) { delete suite; }
CalculateMuonAsymmetryTest() { FrameworkManager::Instance(); }
void test_Execute() {
genData();
std::vector<std::string> wsNames = {"ws1"};
std::vector<std::string> wsOut = {"ws2"};
auto func = genSingleFunc(wsNames);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table, func, wsNames, wsOut);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
clearADS();
}
void test_singleFit() {
genData();
std::vector<std::string> wsNames = {"ws1"};
std::vector<std::string> wsOut = {"ws2"};
auto func = genSingleFunc(wsNames);
auto table = genTable();
auto alg = setUpAlg(table, func, wsNames, wsOut);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
std::vector<std::string> output = alg->getProperty("ReNormalizedWorkspaceList");
MatrixWorkspace_sptr outWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(output[0]);
double delta = 0.0001;
TS_ASSERT_DELTA(table->Double(1, 0), 3.4, delta);
TS_ASSERT_EQUALS(table->String(1, 1), "ws2");
TS_ASSERT_EQUALS(table->String(1, 2), "Calculated");
TS_ASSERT_DELTA(outWS->x(0)[10], 0.5, delta);
TS_ASSERT_DELTA(outWS->x(0)[40], 2.0, delta);
TS_ASSERT_DELTA(outWS->x(0)[100], 5.0, delta);
TS_ASSERT_DELTA(outWS->y(0)[10], 0.1031, delta);
TS_ASSERT_DELTA(outWS->y(0)[40], -0.1250, delta);
TS_ASSERT_DELTA(outWS->y(0)[100], -0.0065, delta);
TS_ASSERT_DELTA(outWS->e(0)[10], 0.0015, delta);
TS_ASSERT_DELTA(outWS->e(0)[40], 0.0015, delta);
TS_ASSERT_DELTA(outWS->e(0)[100], 0.0015, delta);
clearADS();
}
void test_badFittingRange() {
genData();
std::vector<std::string> wsNames = {"ws1"};
std::vector<std::string> wsOut = {"ws2"};
auto func = genSingleFunc(wsNames);
auto table = genTable();
auto alg = setUpAlg(table, func, wsNames, wsOut);
alg->setProperty("StartX", 10.);
alg->setProperty("EndX", 1.);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
clearADS();
}
void test_mismatchWSLists() {
genData();
std::vector<std::string> wsNames = {"ws1"};
std::vector<std::string> wsOut = {"ws2", "ws3"};
auto func = genSingleFunc(wsNames);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table, func, wsNames, wsOut);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
clearADS();
}
void test_multiFuncSingleWS() {
genData();
// need the 2 here to get multi func
std::vector<std::string> wsNames = {"ws1", "ws3"};
std::vector<std::string> wsOut = {"ws2"};
auto func = genDoubleFunc(wsNames);
auto table = genTable();
wsNames = {"ws1"};
IAlgorithm_sptr alg = setUpAlg(table, func, wsNames, wsOut);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
clearADS();
}
void test_yUnitLabel() {
genData();
std::vector<std::string> wsNames = {"ws1"};
std::vector<std::string> wsOut = {"ws2"};
auto func = genSingleFunc(wsNames);
auto table = genTable();
auto alg = setUpAlg(table, func, wsNames, wsOut);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
std::vector<std::string> output = alg->getProperty("ReNormalizedWorkspaceList");
MatrixWorkspace_sptr outWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(output[0]);
TS_ASSERT_EQUALS(outWS->YUnitLabel(), "Asymmetry");
}
void test_multiFuncWS() {
genData();
// need the 2 here to get multi func
std::vector<std::string> wsNames = {"ws1", "ws2"};
std::vector<std::string> wsOut = {"ws3", "ws4"};
auto func = genDoubleFunc(wsNames);
auto table = genTable();
auto alg = setUpAlg(table, func, wsNames, wsOut);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
std::vector<std::string> output = alg->getProperty("ReNormalizedWorkspaceList");
for (int j = 0; j < 2; j++) {
MatrixWorkspace_sptr outWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(output[j]);
double delta = 0.0001;
TS_ASSERT_DELTA(table->Double(j + 2, 0), 3.4, delta);
TS_ASSERT_EQUALS(table->String(j + 2, 1), output[j]);
TS_ASSERT_EQUALS(table->String(j + 2, 2), "Calculated");
TS_ASSERT_DELTA(outWS->x(0)[10], 0.5, delta);
TS_ASSERT_DELTA(outWS->x(0)[40], 2.0, delta);
TS_ASSERT_DELTA(outWS->x(0)[100], 5.0, delta);
TS_ASSERT_DELTA(outWS->y(0)[10], 0.1031, delta);
TS_ASSERT_DELTA(outWS->y(0)[40], -0.1250, delta);
TS_ASSERT_DELTA(outWS->y(0)[100], -0.0065, delta);
TS_ASSERT_DELTA(outWS->e(0)[10], 0.0015, delta);
TS_ASSERT_DELTA(outWS->e(0)[40], 0.0015, delta);
TS_ASSERT_DELTA(outWS->e(0)[100], 0.0015, delta);
}
clearADS();
}
void test_simultaneous_fit_with_double_pulse_mode_enabled() {
genData();
// need the 2 here to get multi func
std::vector<std::string> wsNames = {"ws1", "ws2"};
std::vector<std::string> wsOut = {"ws3", "ws4"};
auto func = genDoubleFunc(wsNames);
auto table = genTable();
auto alg = setUpAlg(table, func, wsNames, wsOut);
alg->setProperty("EnableDoublePulse", true);
alg->setProperty("PulseOffset", 0.33);
alg->setProperty("FirstPulseWeight", 0.5);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
std::vector<std::string> output = alg->getProperty("ReNormalizedWorkspaceList");
for (int j = 0; j < 2; j++) {
MatrixWorkspace_sptr outWS = AnalysisDataService::Instance().retrieveWS<MatrixWorkspace>(output[j]);
double delta = 0.0001;
TS_ASSERT_DELTA(table->Double(j + 2, 0), 3.4, delta);
TS_ASSERT_EQUALS(table->String(j + 2, 1), output[j]);
TS_ASSERT_EQUALS(table->String(j + 2, 2), "Calculated");
TS_ASSERT_DELTA(outWS->x(0)[10], 0.5, delta);
TS_ASSERT_DELTA(outWS->x(0)[40], 2.0, delta);
TS_ASSERT_DELTA(outWS->x(0)[100], 5.0, delta);
TS_ASSERT_DELTA(outWS->y(0)[10], 0.1031, delta);
TS_ASSERT_DELTA(outWS->y(0)[40], -0.1250, delta);
TS_ASSERT_DELTA(outWS->y(0)[100], -0.0065, delta);
TS_ASSERT_DELTA(outWS->e(0)[10], 0.0015, delta);
TS_ASSERT_DELTA(outWS->e(0)[40], 0.0015, delta);
TS_ASSERT_DELTA(outWS->e(0)[100], 0.0015, delta);
}
clearADS();
}
};
class CalculateMuonAsymmetryTestPerformance : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created
// statically This means the constructor isn't called when running other
// tests
static CalculateMuonAsymmetryTestPerformance *createSuite() { return new CalculateMuonAsymmetryTestPerformance(); }
static void destroySuite(CalculateMuonAsymmetryTestPerformance *suite) {
AnalysisDataService::Instance().clear();
delete suite;
}
CalculateMuonAsymmetryTestPerformance() { FrameworkManager::Instance(); }
void setUp() override { input = createWorkspace(1000, 100); }
void testExec1D() {
genData();
std::vector<std::string> wsNames = {"ws1"};
std::vector<std::string> wsOut = {"ws2"};
auto func = genSingleFunc(wsNames);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table, func, wsNames, wsOut);
alg->execute();
clearADS();
}
void testExec2D() {
genData();
std::vector<std::string> wsNames = {"ws1", "ws2"};
std::vector<std::string> wsOut = {"ws3", "ws4"};
auto func = genDoubleFunc(wsNames);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table, func, wsNames, wsOut);
alg->execute();
clearADS();
}
private:
MatrixWorkspace_sptr input;
};
} // namespace