forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanningWorkspaceBuilderTest.h
559 lines (447 loc) · 24.4 KB
/
ScanningWorkspaceBuilderTest.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// 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 "MantidDataObjects/ScanningWorkspaceBuilder.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/DetectorInfo.h"
#include "MantidHistogramData/BinEdges.h"
#include "MantidHistogramData/Histogram.h"
#include "MantidHistogramData/LinearGenerator.h"
#include "MantidTypes/SpectrumDefinition.h"
#include <cmath>
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::HistogramData;
using namespace Mantid::Kernel;
using Mantid::DataObjects::ScanningWorkspaceBuilder;
using Mantid::Types::Core::DateAndTime;
namespace {
Instrument_const_sptr createSimpleInstrument(size_t nDetectors, size_t nBins) {
const auto &wsWithInstrument =
WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(int(nDetectors), int(nBins));
return wsWithInstrument->getInstrument();
}
} // namespace
class ScanningWorkspaceBuilderTest : 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 ScanningWorkspaceBuilderTest *createSuite() { return new ScanningWorkspaceBuilderTest(); }
static void destroySuite(ScanningWorkspaceBuilderTest *suite) { delete suite; }
void tearDown() override {
positions.clear();
rotations.clear();
relativeRotations.clear();
}
void test_create_scanning_workspace_with_instrument_and_time_ranges() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges));
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace());
const auto &detectorInfo = ws->detectorInfo();
// Now check every detector has every time range set correctly
checkTimeRanges(detectorInfo);
// Quick check to see if the instrument is set as expected
TS_ASSERT_EQUALS(instrument->getNumberDetectors(), ws->getInstrument()->getNumberDetectors())
}
void test_create_scanning_workspace_with_histogram() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
BinEdges x(nBins + 1, LinearGenerator(0.0, 1.0));
Counts y(std::vector<double>(nBins, 5.0));
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges));
TS_ASSERT_THROWS_NOTHING(builder.setHistogram(Histogram(x, y)));
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace());
for (size_t i = 0; i < ws->getNumberHistograms(); ++i) {
const auto &hist = ws->histogram(i);
const auto &xValues = hist.x();
for (size_t i = 0; i < xValues.size(); ++i)
TS_ASSERT_EQUALS(xValues[i], double(i))
const auto &yValues = hist.y();
for (size_t i = 0; i < yValues.size(); ++i)
TS_ASSERT_EQUALS(yValues[i], 5.0)
}
}
void test_create_scanning_workspace_with_incorrectly_sized_histogram() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto wrongNBins = nBins - 2;
BinEdges x(wrongNBins + 1, LinearGenerator(0.0, 1.0));
Counts y(std::vector<double>(wrongNBins, 5.0));
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_EQUALS(builder.setHistogram(Histogram(x, y)), const std::logic_error &e, std::string(e.what()),
"Histogram supplied does not have the correct size.")
}
void test_create_scanning_workspace_with_time_durations() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(0, timeDurations))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace())
const auto &detectorInfo = ws->detectorInfo();
// Now check every detector has every time range set correctly
checkTimeRanges(detectorInfo);
}
void test_create_scanning_workspace_fails_if_no_time_ranges_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_EQUALS(builder.buildWorkspace(), const std::logic_error &e, std::string(e.what()),
"Can not build workspace - time ranges have not "
"been set. Please call setTimeRanges() before "
"building.")
}
void test_create_scanning_workspace_fails_if_time_ranges_have_the_wrong_dimensions() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
std::vector<std::pair<DateAndTime, DateAndTime>> timeRangesWrongSize = {{0, 1}, {1, 2}};
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_EQUALS(builder.setTimeRanges(std::move(timeRangesWrongSize)), const std::logic_error &e,
std::string(e.what()),
"Number of start time, end time pairs supplied "
"does not match the number of time indexes.")
}
void test_create_scanning_workspace_fails_if_time_durations_have_the_wrong_dimensions() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
std::vector<double> timeDurationsWrongSize = {0, 1e-9};
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_EQUALS(builder.setTimeRanges(0, timeDurationsWrongSize), const std::logic_error &e,
std::string(e.what()),
"Number of time durations supplied does not match "
"the number of time indexes.")
}
void test_creating_workspace_with_positions() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges))
initalisePositions(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setPositions(std::move(positions)))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace())
const auto &detectorInfo = ws->detectorInfo();
for (size_t i = 0; i < nDetectors; ++i) {
for (size_t j = 0; j < nTimeIndexes; ++j) {
TS_ASSERT_EQUALS(V3D(double(i), double(j), 1.0), detectorInfo.position({i, j}))
}
}
}
void test_creating_workspace_with_positions_with_too_many_detectors() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initalisePositions(nDetectors + 1, nTimeIndexes);
TS_ASSERT_THROWS_EQUALS(builder.setPositions(std::move(positions)), const std::logic_error &e,
std::string(e.what()),
"Number of positions supplied does not match the number of detectors.")
}
void test_creating_workspace_with_positions_with_too_many_time_indexes() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initalisePositions(nDetectors, nTimeIndexes + 1);
TS_ASSERT_THROWS_EQUALS(builder.setPositions(std::move(positions)), const std::logic_error &e,
std::string(e.what()),
"Number of positions supplied does not match the "
"number of time indexes.")
}
void test_creating_workspace_with_rotations() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges))
initaliseRotations(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRotations(std::move(rotations)))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace())
const auto &detectorInfo = ws->detectorInfo();
for (size_t i = 0; i < nDetectors; ++i) {
for (size_t j = 0; j < nTimeIndexes; ++j) {
auto quat = Quat(double(i), double(j), 1.0, 2.0);
quat.normalize();
TS_ASSERT_EQUALS(quat, detectorInfo.rotation({i, j}))
}
}
}
void test_creating_workspace_with_rotations_with_too_many_detectors() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initaliseRotations(nDetectors + 1, nTimeIndexes);
TS_ASSERT_THROWS_EQUALS(builder.setRotations(std::move(rotations)), const std::logic_error &e,
std::string(e.what()),
"Number of rotations supplied does not match the number of detectors.")
}
void test_creating_workspace_with_rotations_with_too_many_time_indexes() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initaliseRotations(nDetectors, nTimeIndexes + 1);
TS_ASSERT_THROWS_EQUALS(builder.setRotations(std::move(rotations)), const std::logic_error &e,
std::string(e.what()),
"Number of rotations supplied does not match the "
"number of time indexes.")
}
void test_creating_workspace_with_relative_rotations() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges))
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRelativeRotationsForScans(relativeRotations, V3D(0, 0, 0), V3D(0, 1, 0)))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace())
const auto &detInfo = ws->detectorInfo();
for (size_t i = 0; i < nDetectors; ++i) {
TS_ASSERT_DELTA(0.0, detInfo.position({i, 0}).X(), 1e-12)
TS_ASSERT_DELTA(5.0, detInfo.position({i, 0}).Z(), 1e-12)
TS_ASSERT_DELTA(2.5, detInfo.position({i, 1}).X(), 1e-12)
TS_ASSERT_DELTA(5.0 * sqrt(3) / 2, detInfo.position({i, 1}).Z(), 1e-12)
TS_ASSERT_DELTA(5.0 * sqrt(3) / 2, detInfo.position({i, 2}).X(), 1e-12)
TS_ASSERT_DELTA(2.5, detInfo.position({i, 2}).Z(), 1e-12)
TS_ASSERT_DELTA(5.0, detInfo.position({i, 3}).X(), 1e-12)
TS_ASSERT_DELTA(0.0, detInfo.position({i, 3}).Z(), 1e-12)
for (size_t j = 0; j < nTimeIndexes; ++j) {
TS_ASSERT_DELTA(double(i) * 0.1, detInfo.position({i, j}).Y(), 1e-12)
}
}
for (size_t i = 0; i < nDetectors; ++i) {
for (size_t j = 0; j < nTimeIndexes; ++j) {
// Rounding to nearest int required to avoid problem of Euler angles
// returning -180/0/180
TS_ASSERT_DELTA(0.0, std::lround(detInfo.rotation({i, j}).getEulerAngles("XYZ")[0]) % 180, 1e-12)
TS_ASSERT_DELTA(0.0, std::lround(detInfo.rotation({i, j}).getEulerAngles("XYZ")[2]) % 180, 1e-12)
}
TS_ASSERT_DELTA(0.0, std::lround(detInfo.rotation({i, 0}).getEulerAngles("XYZ")[1]) % 180, 1e-12)
TS_ASSERT_DELTA(30.0, detInfo.rotation({i, 1}).getEulerAngles("XYZ")[1], 1e-12)
TS_ASSERT_DELTA(60.0, detInfo.rotation({i, 2}).getEulerAngles("XYZ")[1], 1e-12)
TS_ASSERT_DELTA(90.0, detInfo.rotation({i, 3}).getEulerAngles("XYZ")[1], 1e-12)
}
}
void test_creating_workspace_with_relative_rotations_and_offset() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges))
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRelativeRotationsForScans(relativeRotations, V3D(0, 0, 1), V3D(0, 1, 0)))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace())
const auto &detInfo = ws->detectorInfo();
for (size_t i = 0; i < nDetectors; ++i) {
TS_ASSERT_DELTA(0.0, detInfo.position({i, 0}).X(), 1e-12)
TS_ASSERT_DELTA(5.0, detInfo.position({i, 0}).Z(), 1e-12)
TS_ASSERT_DELTA(4.0, detInfo.position({i, 3}).X(), 1e-12)
TS_ASSERT_DELTA(1.0, detInfo.position({i, 3}).Z(), 1e-12)
for (size_t j = 0; j < nTimeIndexes; ++j) {
TS_ASSERT_DELTA(double(i) * 0.1, detInfo.position({i, j}).Y(), 1e-12)
}
}
}
void test_creating_workspace_with_relative_rotations_on_previously_rotated_detectors() {
const auto &instWS = WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(int(nDetectors), int(nBins));
auto &instDetInfo = instWS->mutableDetectorInfo();
Quat rotation = Quat(90.0, V3D(0, 0, 1));
for (size_t i = 0; i < instDetInfo.size(); ++i) {
instDetInfo.setRotation(i, rotation);
}
const auto &instrument = instWS->getInstrument();
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges))
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRelativeRotationsForScans(relativeRotations, V3D(0, 0, 1), V3D(0, 1, 0)))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace())
const auto &detInfo = ws->detectorInfo();
for (size_t i = 0; i < nDetectors; ++i) {
for (size_t j = 0; j < nTimeIndexes; ++j) {
TS_ASSERT_DELTA(0.0, detInfo.rotation({i, j}).getEulerAngles("YXZ")[1], 1e-12)
TS_ASSERT_DELTA(90.0, detInfo.rotation({i, j}).getEulerAngles("YXZ")[2], 1e-12)
}
TS_ASSERT_DELTA(0.0, std::lround(detInfo.rotation({i, 0}).getEulerAngles("XYZ")[1]) % 180, 1e-12)
TS_ASSERT_DELTA(30.0, detInfo.rotation({i, 1}).getEulerAngles("XYZ")[1], 1e-12)
TS_ASSERT_DELTA(60.0, detInfo.rotation({i, 2}).getEulerAngles("XYZ")[1], 1e-12)
TS_ASSERT_DELTA(90.0, detInfo.rotation({i, 3}).getEulerAngles("XYZ")[1], 1e-12)
}
}
void test_creating_workspace_with_relative_rotations_fails_with_wrong_time_index_size() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initialiseRelativeRotations(nTimeIndexes + 1);
TS_ASSERT_THROWS_EQUALS(builder.setRelativeRotationsForScans(relativeRotations, V3D(0, 0, 0), V3D(0, 1, 0)),
const std::logic_error &e, std::string(e.what()),
"Number of instrument angles supplied does not "
"match the number of time indexes.")
}
void test_creating_workspace_with_positions_fails_with_positions_already_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initalisePositions(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setPositions(positions))
TS_ASSERT_THROWS_EQUALS(builder.setPositions(positions), const std::logic_error &e, std::string(e.what()),
"Can not set positions, as positions "
"or instrument angles have already been set.")
}
void test_creating_workspace_with_rotations_fails_with_positions_already_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initaliseRotations(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRotations(rotations))
TS_ASSERT_THROWS_EQUALS(builder.setRotations(rotations), const std::logic_error &e, std::string(e.what()),
"Can not set rotations, as rotations "
"or instrument angles have already been set.")
}
void test_creating_workspace_with_positions_fails_with_relative_rotations_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRelativeRotationsForScans(relativeRotations, V3D(0, 0, 0), V3D(0, 1, 0)))
initalisePositions(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_EQUALS(builder.setPositions(std::move(positions)), const std::logic_error &e,
std::string(e.what()),
"Can not set positions, as positions "
"or instrument angles have already been set.")
}
void test_creating_workspace_with_rotations_fails_with_relative_rotations_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRelativeRotationsForScans(relativeRotations, V3D(0, 0, 0), V3D(0, 1, 0)))
initaliseRotations(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_EQUALS(builder.setRotations(std::move(rotations)), const std::logic_error &e,
std::string(e.what()),
"Can not set rotations, as rotations "
"or instrument angles have already been set.")
}
void test_creating_workspace_with_relative_rotations_fails_with_positions_already_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initalisePositions(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setPositions(std::move(positions)))
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_EQUALS(
builder.setRelativeRotationsForScans(std::move(relativeRotations), V3D(0, 0, 0), V3D(0, 1, 0)),
const std::logic_error &e, std::string(e.what()),
"Can not set instrument angles, as positions "
"and/or rotations have already been set.")
}
void test_creating_workspace_with_relative_rotations_fails_with_rotations_already_set() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
initaliseRotations(nDetectors, nTimeIndexes);
TS_ASSERT_THROWS_NOTHING(builder.setRotations(std::move(rotations)))
initialiseRelativeRotations(nTimeIndexes);
TS_ASSERT_THROWS_EQUALS(
builder.setRelativeRotationsForScans(std::move(relativeRotations), V3D(0, 0, 0), V3D(0, 1, 0)),
const std::logic_error &e, std::string(e.what()),
"Can not set instrument angles, as positions "
"and/or rotations have already been set.")
}
void test_creating_workspace_with_time_oriented_index_info() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges));
TS_ASSERT_THROWS_NOTHING(builder.setIndexingType(ScanningWorkspaceBuilder::IndexingType::TimeOriented))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace());
const auto &indexInfo = ws->indexInfo();
const auto &detectorIDs = ws->detectorInfo().detectorIDs();
const auto &spectrumDefinitions = *(indexInfo.spectrumDefinitions());
for (size_t i = 0; i < nDetectors; ++i) {
for (size_t j = 0; j < nTimeIndexes; ++j) {
const auto index = i * nTimeIndexes + j;
TS_ASSERT_EQUALS(spectrumDefinitions[index].size(), 1)
TS_ASSERT_EQUALS(spectrumDefinitions[index][0].first, i)
TS_ASSERT_EQUALS(spectrumDefinitions[index][0].second, j)
TS_ASSERT_EQUALS(detectorIDs[spectrumDefinitions[index][0].first], i + 1)
}
}
}
void test_creating_workspace_with_detector_oriented_index_info() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges));
TS_ASSERT_THROWS_NOTHING(builder.setIndexingType(ScanningWorkspaceBuilder::IndexingType::DetectorOriented))
MatrixWorkspace_const_sptr ws;
TS_ASSERT_THROWS_NOTHING(ws = builder.buildWorkspace());
const auto &indexInfo = ws->indexInfo();
const auto &detectorIDs = ws->detectorInfo().detectorIDs();
const auto &spectrumDefinitions = *(indexInfo.spectrumDefinitions());
for (size_t i = 0; i < nTimeIndexes; ++i) {
for (size_t j = 0; j < nDetectors; ++j) {
const auto index = i * nDetectors + j;
TS_ASSERT_EQUALS(spectrumDefinitions[index].size(), 1)
TS_ASSERT_EQUALS(spectrumDefinitions[index][0].first, j)
TS_ASSERT_EQUALS(spectrumDefinitions[index][0].second, i)
TS_ASSERT_EQUALS(detectorIDs[spectrumDefinitions[index][0].first], j + 1)
}
}
}
void test_setting_indexing_type_twice_throws_and_error() {
const auto &instrument = createSimpleInstrument(nDetectors, nBins);
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
TS_ASSERT_THROWS_NOTHING(builder.setTimeRanges(timeRanges));
TS_ASSERT_THROWS_NOTHING(builder.setIndexingType(ScanningWorkspaceBuilder::IndexingType::DetectorOriented))
TS_ASSERT_THROWS_EQUALS(builder.setIndexingType(ScanningWorkspaceBuilder::IndexingType::TimeOriented),
const std::logic_error &e, std::string(e.what()), "Indexing type has been set already.")
}
private:
size_t nDetectors = 5;
size_t nTimeIndexes = 4;
size_t nBins = 10;
const std::vector<std::pair<DateAndTime, DateAndTime>> timeRanges = {{0, 2}, {2, 3}, {3, 6}, {6, 10}};
std::vector<double> timeDurations = {2e-9, 1e-9, 3e-9, 4e-9};
std::vector<std::vector<V3D>> positions;
std::vector<std::vector<Quat>> rotations;
std::vector<double> relativeRotations;
void initalisePositions(size_t nDetectors, size_t nTimeIndexes) {
for (size_t i = 0; i < nDetectors; ++i) {
std::vector<V3D> timePositions;
for (size_t j = 0; j < nTimeIndexes; ++j) {
timePositions.emplace_back(V3D(double(i), double(j), 1.0));
}
positions.emplace_back(timePositions);
}
}
void initaliseRotations(size_t nDetectors, size_t nTimeIndexes) {
for (size_t i = 0; i < nDetectors; ++i) {
std::vector<Quat> timeRotations;
for (size_t j = 0; j < nTimeIndexes; ++j) {
timeRotations.emplace_back(Quat(double(i), double(j), 1.0, 2.0));
}
rotations.emplace_back(timeRotations);
}
}
void initialiseRelativeRotations(size_t nTimeIndexes) {
for (size_t i = 0; i < nTimeIndexes; ++i) {
relativeRotations.emplace_back(double(i) * 30.0);
}
}
Instrument_const_sptr createSimpleInstrument(size_t nDetectors, size_t nBins) {
const auto &wsWithInstrument =
WorkspaceCreationHelper::create2DWorkspaceWithFullInstrument(int(nDetectors), int(nBins));
return wsWithInstrument->getInstrument();
}
void checkTimeRanges(const DetectorInfo &detectorInfo) {
for (size_t i = 0; i < nTimeIndexes; ++i) {
TS_ASSERT_EQUALS(detectorInfo.scanIntervals()[i], timeRanges[i]);
}
}
};
class ScanningWorkspaceBuilderTestPerformance : public CxxTest::TestSuite {
public:
void test_large_scanning_workspace() { make_scanning_workspace(1000, 500, 1000); }
void test_lots_of_small_scanning_workspaces() {
for (size_t i = 0; i < 200; ++i)
make_scanning_workspace(100, 50, 100);
}
void make_scanning_workspace(size_t nDetectors, size_t nTimeIndexes, size_t nBins) {
const auto &instrument = createSimpleInstrument(nDetectors, nTimeIndexes);
std::vector<std::pair<DateAndTime, DateAndTime>> timeRanges;
for (size_t i = 0; i < nTimeIndexes; ++i) {
timeRanges.emplace_back(std::pair<DateAndTime, DateAndTime>(DateAndTime(i * 2), DateAndTime(i * 2 + 1)));
}
auto builder = ScanningWorkspaceBuilder(instrument, nTimeIndexes, nBins);
builder.setTimeRanges(timeRanges);
MatrixWorkspace_const_sptr ws;
ws = builder.buildWorkspace();
}
};