forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MuonGroupingCountsTest.h
298 lines (238 loc) · 11.5 KB
/
MuonGroupingCountsTest.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
// 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 "MantidMuon/MuonGroupingCounts.h"
#include "MantidTestHelpers/MuonWorkspaceCreationHelper.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Muon;
using namespace MuonWorkspaceCreationHelper;
namespace {
// Simple class to set up the ADS with the configuration required by the
// algorithm (a MatrixWorkspace).
class setUpADSWithWorkspace {
public:
setUpADSWithWorkspace(const Workspace_sptr &ws) { AnalysisDataService::Instance().addOrReplace(inputWSName, ws); };
~setUpADSWithWorkspace() { AnalysisDataService::Instance().clear(); };
std::string const inputWSName = "inputData";
};
// Set only mandatory fields; input and output workspace
IAlgorithm_sptr algorithmWithoutOptionalPropertiesSet(const std::string &inputWSName) {
auto alg = std::make_shared<MuonGroupingCounts>();
alg->initialize();
alg->setProperty("InputWorkspace", inputWSName);
alg->setProperty("OutputWorkspace", "__notUsed");
alg->setAlwaysStoreInADS(false);
alg->setLogging(false);
return alg;
}
// Set up algorithm without any optional properties
// i.e. just the input workspace and group name.
IAlgorithm_sptr setUpAlgorithmWithoutOptionalProperties(const WorkspaceGroup_sptr &ws, const std::string &name) {
setUpADSWithWorkspace setup(ws);
IAlgorithm_sptr alg = algorithmWithoutOptionalPropertiesSet(setup.inputWSName);
alg->setProperty("GroupName", name);
return alg;
}
// Set up algorithm with GroupName applied
IAlgorithm_sptr setUpAlgorithmWithGroupName(const WorkspaceGroup_sptr &ws, const std::string &name) {
setUpADSWithWorkspace setup(ws);
IAlgorithm_sptr alg = algorithmWithoutOptionalPropertiesSet(setup.inputWSName);
alg->setProperty("GroupName", name);
return alg;
}
// Set up algorithm with TimeOffset applied
IAlgorithm_sptr setUpAlgorithmWithGroupNameAndDetectors(const WorkspaceGroup_sptr &ws, const std::string &name,
const std::vector<int> &detectors) {
setUpADSWithWorkspace setup(ws);
IAlgorithm_sptr alg = algorithmWithoutOptionalPropertiesSet(setup.inputWSName);
alg->setProperty("GroupName", name);
alg->setProperty("Grouping", detectors);
return alg;
}
// Retrieve the output workspace from an executed algorithm
MatrixWorkspace_sptr getOutputWorkspace(const IAlgorithm_sptr &alg) {
Workspace_sptr outputWS = alg->getProperty("OutputWorkspace");
auto wsOut = std::dynamic_pointer_cast<MatrixWorkspace>(outputWS);
return wsOut;
}
} // namespace
class MuonGroupingCountsTest : public CxxTest::TestSuite {
public:
static MuonGroupingCountsTest *createSuite() { return new MuonGroupingCountsTest(); }
static void destroySuite(MuonGroupingCountsTest *suite) { delete suite; }
// --------------------------------------------------------------------------
// Initialization / Execution
// --------------------------------------------------------------------------
void test_that_algorithm_initializes() {
MuonGroupingCounts alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
}
void test_that_algorithm_executes_with_no_optional_properties_set() {
auto ws = createMultiPeriodWorkspaceGroup(2, 1, 10, "group1");
auto alg = setUpAlgorithmWithoutOptionalProperties(ws, "group1");
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted())
}
// --------------------------------------------------------------------------
// Validation : Group Names and Detector Grouping
// --------------------------------------------------------------------------
void test_that_input_workspace_cannot_be_a_Workspace2D() {
auto ws = createCountsWorkspace(5, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = std::make_shared<MuonGroupingCounts>();
alg->initialize();
TS_ASSERT_THROWS_ANYTHING(alg->setProperty("InputWorkspace", setup.inputWSName));
}
void test_that_input_workspace_can_be_a_WorkspaceGroup() {
auto ws = createMultiPeriodWorkspaceGroup(2, 1, 10, "group1");
setUpADSWithWorkspace setup(ws);
auto alg = std::make_shared<MuonGroupingCounts>();
alg->initialize();
TSM_ASSERT_THROWS_NOTHING("", alg->setProperty("InputWorkspace", setup.inputWSName))
}
void test_that_group_name_must_be_supplied() {
auto ws = createMultiPeriodWorkspaceGroup(2, 1, 10, "group1");
setUpADSWithWorkspace setup(ws);
IAlgorithm_sptr alg = algorithmWithoutOptionalPropertiesSet(setup.inputWSName);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
}
void test_that_group_names_with_alphanumeric_characters_or_underscores_are_allowed() {
auto ws = createMultiPeriodWorkspaceGroup(2, 1, 10, "group1");
std::vector<std::string> validNames = {"fwd", "fwd2", "bwd_2"};
for (auto &&validName : validNames) {
auto alg = setUpAlgorithmWithGroupName(ws, validName);
TSM_ASSERT_THROWS_NOTHING("", alg->execute());
}
}
void test_that_exec_throws_if_group_name_is_not_alphanumeric_or_underscored() {
auto ws = createMultiPeriodWorkspaceGroup(2, 1, 10, "group1");
std::vector<std::string> invalidNames = {"@", "fwd!", "#1", "fwd @", " "};
for (auto &&invalidName : invalidNames) {
auto alg = setUpAlgorithmWithGroupName(ws, invalidName);
TS_ASSERT_THROWS_ANYTHING(alg->execute());
}
}
void test_that_cannot_add_spectra_to_group_which_exceed_those_in_the_workspace() {
auto ws = createMultiPeriodWorkspaceGroup(1, 5, 10, "group1");
std::vector<int> detectors = {6, 7, 8, 9, 10};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group1", detectors);
alg->setRethrows(true);
TS_ASSERT_THROWS_ANYTHING(alg->execute());
TS_ASSERT(!alg->isExecuted());
}
// --------------------------------------------------------------------------
// Validation : multi period data
// --------------------------------------------------------------------------
void test_that_at_least_one_period_must_be_specified() {
auto ws = createMultiPeriodWorkspaceGroup(2, 3, 10, "group");
std::vector<int> detectors = {1, 2};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group", detectors);
std::vector<int> summedPeriods = {};
std::vector<int> subtractedPeriods = {};
alg->setProperty("SummedPeriods", summedPeriods);
alg->setProperty("SubtractedPeriods", subtractedPeriods);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
}
void test_that_supplying_too_many_periods_to_SummedPeriods_throws_on_execute() {
auto ws = createMultiPeriodWorkspaceGroup(2, 3, 10, "group");
std::vector<int> detectors = {1, 2, 3};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group", detectors);
std::vector<int> summedPeriods = {3};
alg->setProperty("SummedPeriods", summedPeriods);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
}
void test_that_supplying_too_many_periods_to_SubtractedPeriods_throws_on_execute() {
auto ws = createMultiPeriodWorkspaceGroup(2, 3, 10, "group");
std::vector<int> detectors = {1, 2, 3};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group", detectors);
std::vector<int> subtractedPeriods = {3};
alg->setProperty("SubtractedPeriods", subtractedPeriods);
TS_ASSERT_THROWS(alg->execute(), const std::runtime_error &);
}
// --------------------------------------------------------------------------
// Correct Output
// --------------------------------------------------------------------------
void test_that_single_period_data_combines_detectors_correctly() {
// Spec 1 y-vals : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
// Spec 2 y-vals : 11, 12, 13, 14, 15, 16 ,17, 18, 19, 20
// Spec 3 y-vals : 21, 22, 23, 24, 25, 26, 27, 28, 29, 30
auto ws = createMultiPeriodWorkspaceGroup(1, 3, 10, "group");
std::vector<int> detectors = {1, 2, 3};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group", detectors);
alg->execute();
auto wsOut = getOutputWorkspace(alg);
TS_ASSERT_DELTA(wsOut->readX(0)[0], 0.000, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[4], 0.400, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[9], 0.900, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[0], 33.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 45.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 60.000, 0.001);
// Quadrature errors : Sqrt(3 * 0.005^2 )
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.00866, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.00866, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.00866, 0.0001);
}
void test_that_summing_periods_combines_detectors_correctly() {
// Period 1 :
// Spec 1 y-vals : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
// Spec 2 y-vals : 11, 12, 13, 14, 15, 16 ,17, 18, 19, 20
// Period 2 :
// Spec 1 y-vals : 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
// Spec 2 y-vals : 12, 13, 14, 15, 16, 17 ,18, 19, 20, 21
auto ws = createMultiPeriodWorkspaceGroup(2, 2, 10, "group");
std::vector<int> detectors = {1, 2};
std::vector<int> summedPeriods = {1, 2};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group", detectors);
alg->setProperty("SummedPeriods", summedPeriods);
alg->execute();
auto wsOut = getOutputWorkspace(alg);
TS_ASSERT_DELTA(wsOut->readX(0)[0], 0.000, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[4], 0.400, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[9], 0.900, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[0], 26.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 42.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 62.000, 0.001);
// Quadrature errors : Sqrt(4 * 0.005^2 )
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.0100, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.0100, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.0100, 0.0001);
}
void test_that_subtracting_periods_combines_detectors_correctly() {
// Period 1 :
// Spec 1 y-vals : 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
// Spec 2 y-vals : 11, 12, 13, 14, 15, 16 ,17, 18, 19, 20
// Period 2 :
// Spec 1 y-vals : 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
// Spec 2 y-vals : 12, 13, 14, 15, 16, 17 ,18, 19, 20, 21
auto ws = createMultiPeriodWorkspaceGroup(2, 2, 10, "group");
std::vector<int> detectors = {1, 2};
std::vector<int> summedPeriods = {2};
std::vector<int> subtractedPeriods = {1};
auto alg = setUpAlgorithmWithGroupNameAndDetectors(ws, "group", detectors);
alg->setProperty("SummedPeriods", summedPeriods);
alg->setProperty("SubtractedPeriods", subtractedPeriods);
alg->execute();
auto wsOut = getOutputWorkspace(alg);
TS_ASSERT_DELTA(wsOut->readX(0)[0], 0.000, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[4], 0.400, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[9], 0.900, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[0], 2.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 2.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 2.000, 0.001);
// Quadrature errors : Sqrt(4 * 0.005^2 )
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.0100, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.0100, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.0100, 0.0001);
}
};