forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EstimateMuonAsymmetryFromCountsTest.h
338 lines (285 loc) · 12.2 KB
/
EstimateMuonAsymmetryFromCountsTest.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
// 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 "MantidAPI/ITableWorkspace.h"
#include "MantidHistogramData/LinearGenerator.h"
#include "MantidKernel/PhysicalConstants.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidMuon/EstimateMuonAsymmetryFromCounts.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid::API;
using Mantid::MantidVec;
using Mantid::Algorithms::EstimateMuonAsymmetryFromCounts;
const std::string outputName = "EstimateMuonAsymmetryFromCounts_Output";
namespace {
struct yData {
double operator()(const double x, size_t) {
// Create a fake muon dataset
double a = 0.1; // Amplitude of the oscillations
double w = 25.; // Frequency of the oscillations
double tau = Mantid::PhysicalConstants::MuonLifetime * 1e6; // Muon life time in microseconds
double phi = 0.05;
double e = exp(-x / tau);
return (20. * (1.0 + a * cos(w * x + phi)) * e);
}
};
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, 1.0, (1.0 / static_cast<double>(maxt)), true, eData());
// Add number of good frames
ws->mutableRun().addProperty("goodfrm", 10);
// AnalysisDataService::Instance().addOrReplace("ws",ws);
return ws;
}
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");
return table;
}
IAlgorithm_sptr setUpAlg(ITableWorkspace_sptr &table) {
IAlgorithm_sptr asymmAlg = AlgorithmManager::Instance().create("EstimateMuonAsymmetryFromCounts");
asymmAlg->initialize();
asymmAlg->setChild(true);
asymmAlg->setProperty("NormalizationTable", table);
asymmAlg->setProperty("WorkspaceName", "ws");
asymmAlg->setProperty("StartX", 0.1);
asymmAlg->setProperty("EndX", 0.9);
return asymmAlg;
}
} // namespace
class EstimateMuonAsymmetryFromCountsTest : 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 EstimateMuonAsymmetryFromCountsTest *createSuite() { return new EstimateMuonAsymmetryFromCountsTest(); }
static void destroySuite(EstimateMuonAsymmetryFromCountsTest *suite) { delete suite; }
EstimateMuonAsymmetryFromCountsTest() { FrameworkManager::Instance(); }
void testInit() {
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
TS_ASSERT(alg->isInitialized())
}
void test_Execute() {
auto ws = createWorkspace(1, 50);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setPropertyValue("OutputWorkspace", outputName);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
MatrixWorkspace_sptr outWS = alg->getProperty("OutputWorkspace");
}
void test_EmptySpectrumList() {
auto ws = createWorkspace(2, 50);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setPropertyValue("OutputWorkspace", outputName);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
MatrixWorkspace_sptr outWS = alg->getProperty("OutputWorkspace");
double Delta = 0.0001;
for (int j = 0; j < 2; j++) {
// Test some X values
TS_ASSERT_DELTA(outWS->x(j)[10], 0.2000, Delta);
TS_ASSERT_DELTA(outWS->x(j)[19], 0.3800, Delta);
TS_ASSERT_DELTA(outWS->x(j)[49], 0.9800, Delta);
// Test some Y values
TS_ASSERT_DELTA(outWS->y(j)[10], 0.0176, Delta);
TS_ASSERT_DELTA(outWS->y(j)[19], -0.1128, Delta);
TS_ASSERT_DELTA(outWS->y(j)[49], 0.0672, Delta);
// Test some E values
TS_ASSERT_DELTA(outWS->e(j)[10], 0.0002, Delta);
TS_ASSERT_DELTA(outWS->e(j)[19], 0.0003, Delta);
TS_ASSERT_DELTA(outWS->e(j)[49], 0.0004, Delta);
}
}
void test_SpectrumList() {
std::vector<MatrixWorkspace_sptr> workspaces;
workspaces.emplace_back(createWorkspace(2, 50));
// First, run the algorithm without specifying any spectrum
auto table = genTable();
IAlgorithm_sptr alg1 = setUpAlg(table);
alg1->setProperty("InputWorkspace", workspaces[0]);
alg1->setPropertyValue("OutputWorkspace", outputName);
TS_ASSERT_THROWS_NOTHING(alg1->execute());
TS_ASSERT(alg1->isExecuted());
workspaces.emplace_back(alg1->getProperty("OutputWorkspace"));
// Then run the algorithm on the second spectrum only
IAlgorithm_sptr alg2 = setUpAlg(table);
alg2->setProperty("InputWorkspace", workspaces[0]);
alg2->setPropertyValue("OutputWorkspace", outputName);
alg2->setPropertyValue("Spectra", "1");
TS_ASSERT_THROWS_NOTHING(alg2->execute());
TS_ASSERT(alg2->isExecuted());
workspaces.emplace_back(alg2->getProperty("OutputWorkspace"));
for (int j = 0; j < 3; j++) {
if (j != 0) { // check we have 2 spectra
TS_ASSERT_EQUALS(workspaces[j]->getNumberHistograms(), workspaces[0]->getNumberHistograms());
}
if (j != 2) { // check results match
TS_ASSERT_EQUALS(workspaces[j]->x(j).rawData(), workspaces[2]->x(j).rawData());
TS_ASSERT_EQUALS(workspaces[j]->y(j).rawData(), workspaces[2]->y(j).rawData());
TS_ASSERT_EQUALS(workspaces[j]->e(j).rawData(), workspaces[2]->e(j).rawData());
}
}
}
void test_yUnitLabel() {
auto ws = createWorkspace(1, 50);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setProperty("OutputWorkspace", outputName);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted())
MatrixWorkspace_sptr result = alg->getProperty("OutputWorkspace");
TS_ASSERT(result);
TS_ASSERT_EQUALS(result->YUnitLabel(), "Asymmetry");
}
void test_NoRange() {
auto ws = createWorkspace(1, 50);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setProperty("StartX", 0.1);
alg->setProperty("EndX", 0.1);
alg->setProperty("OutputWorkspace", outputName);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
}
void test_BackwardsRange() {
auto ws = createWorkspace(1, 50);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setProperty("StartX", 0.9);
alg->setProperty("EndX", 0.1);
alg->setProperty("OutputWorkspace", outputName);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
}
void test_NumberOfDataPoints() {
double dx = (1.0 / 300.0);
auto fineWS = WorkspaceCreationHelper::create2DWorkspaceFromFunction(yData(), 1, 0.0, 1.0, dx, true, eData());
fineWS->mutableRun().addProperty("goodfrm", 10);
auto coarseWS =
WorkspaceCreationHelper::create2DWorkspaceFromFunction(yData(), 1, dx, 1.0 + dx, 3.0 * dx, true, eData());
coarseWS->mutableRun().addProperty("goodfrm", 10);
auto table = genTable();
IAlgorithm_sptr fineAlg = setUpAlg(table);
fineAlg->setProperty("WorkspaceName", "fine");
fineAlg->setProperty("InputWorkspace", fineWS);
fineAlg->setPropertyValue("OutputWorkspace", "fineOutWS");
TS_ASSERT_THROWS_NOTHING(fineAlg->execute());
TS_ASSERT(fineAlg->isExecuted());
MatrixWorkspace_sptr fineOutWS = fineAlg->getProperty("OutputWorkspace");
IAlgorithm_sptr coarseAlg = setUpAlg(table);
coarseAlg->setProperty("InputWorkspace", coarseWS);
coarseAlg->setProperty("WorkspaceName", "coarse");
coarseAlg->setPropertyValue("OutputWorkspace", "coarseOutWS");
TS_ASSERT_THROWS_NOTHING(coarseAlg->execute());
TS_ASSERT(coarseAlg->isExecuted());
MatrixWorkspace_sptr coarseOutWS = coarseAlg->getProperty("OutputWorkspace");
// check names in table
TS_ASSERT_EQUALS(table->String(0, 1), "fine");
TS_ASSERT_EQUALS(table->String(1, 1), "coarse");
double Delta = 0.05; // only expect numbers to be similar
for (int j = 0; j < 28; j++) {
// Test some X values
TS_ASSERT_DELTA(fineOutWS->x(0)[1 + j * 3], coarseOutWS->x(0)[j], Delta);
// Test some Y values
TS_ASSERT_DELTA(fineOutWS->y(0)[1 + j * 3], coarseOutWS->y(0)[j], Delta);
// Test some E values
TS_ASSERT_DELTA(fineOutWS->e(0)[1 + j * 3], coarseOutWS->e(0)[j], Delta);
}
}
void test_UserDefinedNorm() {
auto ws = createWorkspace(1, 50);
double userNorm = 10.2;
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setPropertyValue("OutputWorkspace", outputName);
alg->setProperty("NormalizationIn", userNorm);
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
MatrixWorkspace_sptr outWS = alg->getProperty("OutputWorkspace");
double normFromAlg = table->Double(0, 0);
double Delta = 0.0001;
TS_ASSERT_DELTA(normFromAlg, userNorm, Delta);
// Test some X values
TS_ASSERT_DELTA(outWS->x(0)[10], 0.2000, Delta);
TS_ASSERT_DELTA(outWS->x(0)[19], 0.3800, Delta);
TS_ASSERT_DELTA(outWS->x(0)[49], 0.9800, Delta);
// Test some Y values
TS_ASSERT_DELTA(outWS->y(0)[10], -0.7965, Delta);
TS_ASSERT_DELTA(outWS->y(0)[19], -0.8226, Delta);
TS_ASSERT_DELTA(outWS->y(0)[49], -0.7866, Delta);
}
void test_unNorm() {
auto ws = createWorkspace(1, 50);
auto table = genTable();
IAlgorithm_sptr alg = setUpAlg(table);
alg->setProperty("InputWorkspace", ws);
alg->setPropertyValue("OutputWorkspace", outputName);
alg->setProperty("OutputUnNormData", true);
alg->setProperty("OutputUnNormWorkspace", "out");
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
MatrixWorkspace_sptr outWS = alg->getProperty("OutputUnNormWorkspace");
double Delta = 0.0001;
// Test some X values
TS_ASSERT_DELTA(outWS->x(0)[10], 0.2000, Delta);
TS_ASSERT_DELTA(outWS->x(0)[19], 0.3800, Delta);
TS_ASSERT_DELTA(outWS->x(0)[49], 0.9800, Delta);
// Test some Y values
TS_ASSERT_DELTA(outWS->y(0)[10], 2.0757, Delta);
TS_ASSERT_DELTA(outWS->y(0)[19], 1.8098, Delta);
TS_ASSERT_DELTA(outWS->y(0)[49], 2.1769, Delta);
}
};
// turn clang off, otherwise this does not compile
// clang-format off
class EstimateMuonAsymmetryFromCountsTestPerformance : public CxxTest::TestSuite {
// clang-format on
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static EstimateMuonAsymmetryFromCountsTestPerformance *createSuite() {
return new EstimateMuonAsymmetryFromCountsTestPerformance();
}
// clang-format off
static void destroySuite(EstimateMuonAsymmetryFromCountsTestPerformance *suite) {
// clang-format on
AnalysisDataService::Instance().clear();
delete suite;
}
EstimateMuonAsymmetryFromCountsTestPerformance() { FrameworkManager::Instance(); }
void setUp() override { input = createWorkspace(1000, 100); }
void testExec2D() {
EstimateMuonAsymmetryFromCounts alg;
alg.initialize();
alg.setProperty("InputWorkspace", input);
alg.setPropertyValue("OutputWorkspace", "output");
alg.setProperty("StartX", 0.1);
alg.setProperty("EndX", 0.9);
Mantid::API::ITableWorkspace_sptr table = Mantid::API::WorkspaceFactory::Instance().createTable();
table->addColumn("double", "norm");
table->addColumn("str", "name");
table->addColumn("str", "method");
alg.setProperty("NormalizationTable", table);
alg.execute();
}
private:
MatrixWorkspace_sptr input;
};