forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplyDeadTimeCorrTest.h
230 lines (191 loc) · 8.64 KB
/
ApplyDeadTimeCorrTest.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
// 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/IAlgorithm.h"
#include "MantidAPI/TableRow.h"
#include "MantidAPI/Workspace.h"
#include "MantidDataHandling/LoadMuonNexus2.h"
#include "MantidDataObjects/TableWorkspace.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidFrameworkTestHelpers/WorkspaceCreationHelper.h"
#include "MantidMuon/ApplyDeadTimeCorr.h"
#include <stdexcept>
using namespace Mantid::Algorithms;
using namespace Mantid::API;
class ApplyDeadTimeCorrTest : public CxxTest::TestSuite {
public:
void testName() {
ApplyDeadTimeCorr applyDeadTime;
TS_ASSERT_EQUALS(applyDeadTime.name(), "ApplyDeadTimeCorr")
}
void testCategory() {
ApplyDeadTimeCorr applyDeadTime;
TS_ASSERT_EQUALS(applyDeadTime.category(), "Muon;CorrectionFunctions\\EfficiencyCorrections")
}
void testInit() {
ApplyDeadTimeCorr applyDeadTime;
applyDeadTime.initialize();
TS_ASSERT(applyDeadTime.isInitialized())
}
void testExec() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
auto deadTimes = makeDeadTimeTable(32);
ApplyDeadTimeCorr applyDeadTime;
applyDeadTime.initialize();
applyDeadTime.setChild(true);
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("InputWorkspace", inputWs));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("DeadTimeTable", deadTimes));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("OutputWorkspace", "__NotUsed"));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.execute());
TS_ASSERT(applyDeadTime.isExecuted());
double numGoodFrames = 1.0;
const Run &run = inputWs->run();
TS_ASSERT(run.hasProperty("goodfrm"))
numGoodFrames = boost::lexical_cast<double>(run.getProperty("goodfrm")->value());
MatrixWorkspace_sptr outputWs = applyDeadTime.getProperty("OutputWorkspace");
TS_ASSERT(outputWs);
TS_ASSERT_EQUALS(outputWs->y(0)[0],
inputWs->y(0)[0] / (1 - inputWs->y(0)[0] * (deadValue() / ((inputWs->x(0)[1] - inputWs->x(0)[0]) *
numGoodFrames))));
TS_ASSERT_EQUALS(
outputWs->y(0)[40],
inputWs->y(0)[40] /
(1 - inputWs->y(0)[40] * (deadValue() / ((inputWs->x(0)[1] - inputWs->x(0)[0]) * numGoodFrames))));
TS_ASSERT_EQUALS(
outputWs->y(31)[20],
inputWs->y(31)[20] /
(1 - inputWs->y(31)[20] * (deadValue() / ((inputWs->x(0)[1] - inputWs->x(0)[0]) * numGoodFrames))));
TS_ASSERT_DELTA(35.9991, outputWs->y(12)[2], 0.001);
TS_ASSERT_DELTA(4901.5439, outputWs->y(20)[14], 0.001);
}
void testDifferentSize() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
// Bigger row count than file (expect to fail)
auto deadTimes = makeDeadTimeTable(64);
ApplyDeadTimeCorr applyDeadTime;
applyDeadTime.initialize();
applyDeadTime.setChild(true);
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("InputWorkspace", inputWs));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("DeadTimeTable", deadTimes));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("OutputWorkspace", "__NotUsed"));
TS_ASSERT_THROWS(applyDeadTime.execute(), const std::logic_error &);
// Check new table wasn't created
MatrixWorkspace_sptr output = applyDeadTime.getProperty("OutputWorkspace");
TS_ASSERT(!output);
}
void testSelectedSpectrum() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
std::shared_ptr<ITableWorkspace> deadTimes = std::make_shared<Mantid::DataObjects::TableWorkspace>();
deadTimes->addColumn("int", "Spectrum Number");
deadTimes->addColumn("double", "DeadTime Value");
// Spectrum: 3,6,9,12,15,18,21 .....
for (int i = 0; i < 7; ++i) {
Mantid::API::TableRow row = deadTimes->appendRow();
row << (i + 1) * 3 << deadValue();
}
//.... Index will therefore be 2,5,8,11,14,17,20
ApplyDeadTimeCorr applyDeadTime;
applyDeadTime.initialize();
applyDeadTime.setChild(true);
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("InputWorkspace", inputWs));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("DeadTimeTable", deadTimes));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("OutputWorkspace", "__NotUsed"));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.execute());
TS_ASSERT(applyDeadTime.isExecuted());
double numGoodFrames = 1.0;
const Run &run = inputWs->run();
TS_ASSERT(run.hasProperty("goodfrm"))
numGoodFrames = boost::lexical_cast<double>(run.getProperty("goodfrm")->value());
MatrixWorkspace_sptr outputWs = applyDeadTime.getProperty("OutputWorkspace");
TS_ASSERT(outputWs);
TS_ASSERT_EQUALS(outputWs->y(0)[0], inputWs->y(0)[0]);
TS_ASSERT_EQUALS(
outputWs->y(14)[40],
inputWs->y(14)[40] /
(1 - inputWs->y(14)[40] * (deadValue() / ((inputWs->x(0)[1] - inputWs->x(0)[0]) * numGoodFrames))));
TS_ASSERT_EQUALS(outputWs->y(31)[20], inputWs->y(31)[20]);
// Should be the same (no dead time associated with it)
TS_ASSERT_DELTA(36.0, outputWs->y(12)[2], 0.1);
// Should be new value (dead time applied based on spectrum number)
TS_ASSERT_DELTA(4901.5439, outputWs->y(20)[14], 0.001);
}
/// Test algorithm rejects an input workspace with uneven bin widths
void testUnevenBinWidths() {
constexpr size_t numSpectra(2);
auto workspace = WorkspaceCreationHelper::create2DWorkspace(static_cast<int>(numSpectra), 10);
// Rebin the workspace to make bin widths uneven
auto rebin = AlgorithmFactory::Instance().create("Rebin", 1);
rebin->initialize();
rebin->setChild(true);
rebin->setProperty("InputWorkspace", workspace);
rebin->setPropertyValue("OutputWorkspace", "__NotUsed");
rebin->setPropertyValue("Params", "0, 3, 6, 1, 10"); // uneven bins
rebin->execute();
MatrixWorkspace_sptr rebinned = rebin->getProperty("OutputWorkspace");
// Dead time table
auto deadTimes = makeDeadTimeTable(numSpectra);
// Test that algorithm throws when property is set
ApplyDeadTimeCorr applyDT;
applyDT.initialize();
applyDT.setChild(true);
TS_ASSERT_THROWS(applyDT.setProperty("InputWorkspace", rebinned), const std::invalid_argument &);
}
// Test that algorithm throws if input workspace does not contain number of
// good frames
void testNoGoodfrmPresent() {
MatrixWorkspace_sptr inputWs = loadDataFromFile();
auto deadTimes = makeDeadTimeTable(32);
auto &run = inputWs->mutableRun();
run.removeLogData("goodfrm");
TS_ASSERT(!run.hasProperty("goodfrm"));
ApplyDeadTimeCorr applyDeadTime;
applyDeadTime.initialize();
applyDeadTime.setChild(true);
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("InputWorkspace", inputWs));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("DeadTimeTable", deadTimes));
TS_ASSERT_THROWS_NOTHING(applyDeadTime.setProperty("OutputWorkspace", "__NotUsed"));
TS_ASSERT_THROWS(applyDeadTime.execute(), const std::invalid_argument &);
TS_ASSERT(!applyDeadTime.isExecuted());
}
private:
/**
* Generates a dead time table with the given number of spectra
* @param numSpectra :: [input] Number of rows in the table
* @returns :: Dead time table
*/
std::shared_ptr<ITableWorkspace> makeDeadTimeTable(size_t numSpectra) {
std::shared_ptr<ITableWorkspace> deadTimes = std::make_shared<Mantid::DataObjects::TableWorkspace>();
deadTimes->addColumn("int", "Spectrum Number");
deadTimes->addColumn("double", "DeadTime Value");
for (size_t i = 0; i < numSpectra; i++) {
Mantid::API::TableRow row = deadTimes->appendRow();
row << static_cast<int>(i + 1) << deadValue();
}
return deadTimes;
}
/**
* Loads data from the test data file
* @returns :: Workspace with loaded data
*/
MatrixWorkspace_sptr loadDataFromFile() {
Mantid::DataHandling::LoadMuonNexus2 loader;
loader.initialize();
loader.setChild(true);
loader.setPropertyValue("Filename", "emu00006473.nxs");
loader.setPropertyValue("OutputWorkspace", "__NotUsed");
TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT_EQUALS(loader.isExecuted(), true);
Workspace_sptr data = loader.getProperty("OutputWorkspace");
auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(data);
TS_ASSERT(data);
return matrixWS;
}
/// Test dead time value
double deadValue() const { return -0.00456; }
};