forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplyMuonDetectorGroupingTest.h
383 lines (306 loc) · 15.4 KB
/
ApplyMuonDetectorGroupingTest.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
// 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 <cxxtest/TestSuite.h>
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Workspace.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAlgorithms/CompareWorkspaces.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidFrameworkTestHelpers/MuonWorkspaceCreationHelper.h"
#include "MantidMuon/ApplyMuonDetectorGrouping.h"
using namespace Mantid;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Algorithms;
using namespace Mantid::Muon;
using namespace MuonWorkspaceCreationHelper;
namespace {
// Set sensible default algorithm properties
IAlgorithm_sptr algorithmWithPropertiesSet(const std::string &inputWSName, const std::string &inputGroupName) {
auto alg = std::make_shared<ApplyMuonDetectorGrouping>();
alg->initialize();
alg->setProperty("InputWorkspace", inputWSName);
alg->setProperty("InputWorkspaceGroup", inputGroupName);
alg->setProperty("groupName", "test");
alg->setProperty("Grouping", "1,2,3");
alg->setProperty("AnalysisType", "Counts");
alg->setProperty("TimeMin", 0.0);
alg->setProperty("TimeMax", 30.0);
alg->setProperty("RebinArgs", "");
alg->setProperty("TimeOffset", 0.0);
alg->setProperty("SummedPeriods", std::to_string(1));
alg->setProperty("SubtractedPeriods", "");
alg->setLogging(false);
return alg;
}
// Simple class to set up the ADS with the configuration required by the
// algorithm (a MatrixWorkspace and an empty GroupWorkspace).
class setUpADSWithWorkspace {
public:
setUpADSWithWorkspace(const Workspace_sptr &ws) {
AnalysisDataService::Instance().addOrReplace(inputWSName, ws);
wsGroup = std::make_shared<WorkspaceGroup>();
AnalysisDataService::Instance().addOrReplace(groupWSName, wsGroup);
};
~setUpADSWithWorkspace() { AnalysisDataService::Instance().clear(); };
WorkspaceGroup_sptr wsGroup;
static constexpr const char *inputWSName = "inputData";
static constexpr const char *groupWSName = "inputGroup";
};
} // namespace
class ApplyMuonDetectorGroupingTest : public CxxTest::TestSuite {
public:
// WorkflowAlgorithms do not appear in the FrameworkManager without this line
ApplyMuonDetectorGroupingTest() { Mantid::API::FrameworkManager::Instance(); }
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static ApplyMuonDetectorGroupingTest *createSuite() { return new ApplyMuonDetectorGroupingTest(); }
static void destroySuite(ApplyMuonDetectorGroupingTest *suite) { delete suite; }
void test_algorithm_initializes() {
ApplyMuonDetectorGrouping alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
}
void test_algorithm_executes_with_default_arguments() {
auto ws = createCountsWorkspace(5, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
TS_ASSERT_THROWS_NOTHING(alg->execute());
TS_ASSERT(alg->isExecuted());
}
void test_output_produced_in_ADS_for_counts_analysis() {
auto ws = createCountsWorkspace(5, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->execute();
TS_ASSERT(AnalysisDataService::Instance().doesExist("inputGroup"))
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
// Raw + Rebinned
TS_ASSERT_EQUALS(wsGroup->getNumberOfEntries(), 2);
}
void test_output_produced_in_ADS_for_asymmetry_analysis() {
auto ws = createAsymmetryWorkspace(3, 10);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("AnalysisType", "Asymmetry");
alg->execute();
TS_ASSERT(AnalysisDataService::Instance().doesExist("inputGroup"))
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
// Raw + Rebinned + unNorm + unNorm_Raw
TS_ASSERT_EQUALS(wsGroup->getNumberOfEntries(), 4);
}
void test_workspaces_named_correctly() {
auto ws = createCountsWorkspace(3, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
TS_ASSERT(wsGroup->getItem("inputGroup; Group; test; Counts; #1"));
TS_ASSERT(wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw"));
}
void test_grouping_a_single_detector_does_not_change_the_data() {
auto ws = createCountsWorkspace(1, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("Grouping", "1");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut = wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw");
CompareWorkspaces algCompare;
algCompare.initialize();
algCompare.setProperty("Workspace1", ws->getName());
algCompare.setProperty("Workspace2", wsOut->getName());
algCompare.setProperty("Tolerance", 0.001);
algCompare.setProperty("CheckAllData", true);
algCompare.execute();
TS_ASSERT(algCompare.getProperty("Result"));
}
void test_grouping_with_counts_analysis_gives_correct_values() {
auto ws = createCountsWorkspace(5, 10, 0.0);
setUpADSWithWorkspace setup(ws);
Mantid::API::IAlgorithm_sptr alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut =
std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw"));
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], 30.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 42.000, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 57.000, 0.001);
// Quadrature errors : Sqrt(0.005)
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.00866, 0.00001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.00866, 0.00001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.00866, 0.00001);
}
void test_grouping_with_single_detector_and_asymmetry_analysis_gives_correct_values() {
MatrixWorkspace_sptr ws = createAsymmetryWorkspace(1, 10);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("AnalysisType", "Asymmetry");
alg->setProperty("Grouping", "1");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut = std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Asym; #1_Raw"));
// Check values against calculation by hand.
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], 1.404, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], -0.766, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], -0.1978, 0.001);
// Errors are simply normalized by a constant.
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.00096, 0.00001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.00116, 0.00001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.00145, 0.00001);
}
void test_grouping_with_multiple_detectors_and_asymmetry_analysis_gives_correct_values() {
MatrixWorkspace_sptr ws = createAsymmetryWorkspace(3, 10);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("AnalysisType", "Asymmetry");
alg->setProperty("Grouping", "1,2,3");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut = std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Asym; #1_Raw"));
// Check values against calculation by hand.
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], 1.465, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], -0.873, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], -0.0307, 0.001);
// Errors : quadrature addition + normalized by a constant.
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.000288, 0.000001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.000346, 0.000001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.000434, 0.000001);
}
void test_grouping_with_summed_multiple_periods_and_counts_analysis_gives_correct_values() {
// Period 1 yvalues : 1,2,3,4,5,6,7,8,9,10
// Period 2 yvalues : 2,3,4,5,6,7,8,9,10,11
WorkspaceGroup_sptr ws = createMultiPeriodWorkspaceGroup(3, 1, 10, "MuonAnalysis");
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("Grouping", "1");
alg->setProperty("SummedPeriods", "1,2");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut =
std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw"));
// Check values against calculation by hand.
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], 3, 0.0001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 11, 0.0001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 21, 0.0001);
// Errors : quadrature addition from periods (1 + 2).
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.00707, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.00707, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.00707, 0.0001);
}
void test_grouping_with_subtracted_multiple_periods_and_counts_analysis_gives_correct_values() {
// Period 1 y-values : 1,2,3,4,5,6,7,8,9,10
// Period 2 y-values : 2,3,4,5,6,7,8,9,10,11
// Period 3 y-values : 3,4,5,6,7,8,9,10,11,12
WorkspaceGroup_sptr ws = createMultiPeriodWorkspaceGroup(3, 1, 10, "MuonAnalysis");
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("SummedPeriods", "2,3");
alg->setProperty("SubtractedPeriods", "1");
alg->setProperty("Grouping", "1");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut =
std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw"));
// Check values against calculation by hand.
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], 4, 0.0001);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 8, 0.0001);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 13, 0.0001);
// Errors : quadrature addition from periods (1 + 2 - 3)
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.00866, 0.00001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.00866, 0.00001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.00866, 0.00001);
}
void test_dead_time_correction_is_applied_correctly() {
std::vector<double> deadTimes = {0.0025};
ITableWorkspace_sptr deadTimeTable = createDeadTimeTable(1, deadTimes);
auto ws = createAsymmetryWorkspace(1, 10);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("Grouping", "1");
alg->setProperty("ApplyDeadTimeCorrection", true);
alg->setProperty("DeadTimeTable", deadTimeTable);
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOut =
std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw"));
// Check values against calculation by hand.
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], 12.86, 0.1);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 1.01, 0.1);
TS_ASSERT_DELTA(wsOut->readY(0)[9], 2.78, 0.1);
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.0050, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.0050, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[9], 0.0050, 0.0001);
}
void test_rebinning_is_applied_correctly() {
auto ws = createCountsWorkspace(3, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("RebinArgs", "0.2");
alg->execute();
WorkspaceGroup_sptr wsGroup =
std::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve("inputGroup"));
auto wsOutNoRebin =
std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Counts; #1_Raw"));
// Check values against calculation by hand.
TS_ASSERT_DELTA(wsOutNoRebin->readX(0)[0], 0.000, 0.001);
TS_ASSERT_DELTA(wsOutNoRebin->readX(0)[4], 0.400, 0.001);
TS_ASSERT_DELTA(wsOutNoRebin->readX(0)[9], 0.900, 0.001);
auto wsOut = std::dynamic_pointer_cast<MatrixWorkspace>(wsGroup->getItem("inputGroup; Group; test; Counts; #1"));
// Check values against calculation by hand.
TS_ASSERT_DELTA(wsOut->readX(0)[0], 0.000, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[1], 0.200, 0.001);
TS_ASSERT_DELTA(wsOut->readX(0)[4], 0.800, 0.001);
TS_ASSERT_DELTA(wsOut->readY(0)[0], 63, 0.1);
TS_ASSERT_DELTA(wsOut->readY(0)[1], 75, 0.1);
TS_ASSERT_DELTA(wsOut->readY(0)[4], 111, 0.1);
TS_ASSERT_DELTA(wsOut->readE(0)[0], 0.0122, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[1], 0.0122, 0.0001);
TS_ASSERT_DELTA(wsOut->readE(0)[4], 0.0122, 0.0001);
}
/// 26/06/18 : unNorm workspaces required in ADS
void test_unNorm_workspaces_named_correctly() {
auto ws = createCountsWorkspace(3, 10, 0.0);
setUpADSWithWorkspace setup(ws);
auto alg = algorithmWithPropertiesSet("inputData", "inputGroup");
alg->setProperty("AnalysisType", "Asymmetry");
alg->execute();
auto name = AnalysisDataService::Instance().getObjectNames();
TS_ASSERT(AnalysisDataService::Instance().doesExist("inputGroup; Group; test; Asym; #1_unNorm"));
TS_ASSERT(AnalysisDataService::Instance().doesExist("inputGroup; Group; test; Asym; #1_unNorm_Raw"));
}
};