forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSIBackgroundSubtractionTest.h
175 lines (139 loc) · 6.02 KB
/
PSIBackgroundSubtractionTest.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2020 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/FrameworkManager.h"
#include "MantidMuon/PSIBackgroundSubtraction.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include <cxxtest/TestSuite.h>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Muon;
namespace {
constexpr char *WORKSPACE_NAME = "DummyWS";
MatrixWorkspace_sptr createCountsTestWorkspace(const size_t numberOfHistograms, const size_t numberOfBins,
bool addLogs = true) {
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::create2DWorkspace(numberOfHistograms, numberOfBins);
ws->setYUnit("Counts");
if (addLogs) {
for (size_t index = 0; index < numberOfHistograms; index++) {
ws->mutableRun().addProperty("First good spectra " + std::to_string(index), int(double(numberOfBins) / 2.));
ws->mutableRun().addProperty("Last good spectra " + std::to_string(index), numberOfBins);
}
}
AnalysisDataService::Instance().addOrReplace(WORKSPACE_NAME, ws);
return ws;
}
MatrixWorkspace_sptr createInvalidTestWorkspace(const size_t numberOfHistograms, const size_t numberOfBins) {
MatrixWorkspace_sptr ws = WorkspaceCreationHelper::create2DWorkspace(numberOfHistograms, numberOfBins);
ws->setYUnit("Asymmetry");
AnalysisDataService::Instance().addOrReplace(WORKSPACE_NAME, ws);
return ws;
}
void clearADS() { AnalysisDataService::Instance().clear(); }
} // namespace
class MockPSIBackgroundSubtraction : public PSIBackgroundSubtraction {
public:
MockPSIBackgroundSubtraction() {}
// set return fit and backgrounds
void setReturnFitQuality(const double fitQuality) { m_fitQuality = fitQuality; }
void setReturnBackground(const double background) { m_background = background; }
private:
std::tuple<double, double> calculateBackgroundFromFit(IAlgorithm_sptr &, const std::pair<double, double> &range,
const int &index) override {
(void)range;
(void)index;
return std::make_tuple(m_background, m_fitQuality);
}
double m_background{0.00};
double m_fitQuality{0.00};
};
class PSIBackgroundSubtractionTest : public CxxTest::TestSuite {
public:
PSIBackgroundSubtractionTest() { Mantid::API::FrameworkManager::Instance(); }
static PSIBackgroundSubtractionTest *createSuite() { return new PSIBackgroundSubtractionTest(); }
static void destroySuite(PSIBackgroundSubtractionTest *suite) { delete suite; }
void test_algorithm_initializes() {
PSIBackgroundSubtraction alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize());
TS_ASSERT(alg.isInitialized());
}
void test_that_algorithm_does_not_execute_if_invalid_y_label() {
PSIBackgroundSubtraction alg;
auto ws = createInvalidTestWorkspace(2, 100);
alg.initialize();
alg.setProperty("InputWorkspace", ws);
TS_ASSERT_THROWS(alg.execute(), const std::runtime_error &);
clearADS();
}
void test_that_algorithm_does_not_execute_if_no_good_data() {
PSIBackgroundSubtraction alg;
auto ws = createCountsTestWorkspace(2, 100, false);
alg.initialize();
alg.setProperty("InputWorkspace", ws);
TS_ASSERT_THROWS(alg.execute(), const std::runtime_error &);
clearADS();
}
void test_that_algorithm_does_not_execute_if_bad_first_good_data() {
PSIBackgroundSubtraction alg;
int numberOfHistograms = 2;
int numberOfBins = 100;
auto ws = createCountsTestWorkspace(numberOfHistograms, numberOfBins, false);
for (int index = 0; index < numberOfHistograms; index++) {
ws->mutableRun().addProperty("First good spectra " + std::to_string(index), -1);
ws->mutableRun().addProperty("Last good spectra " + std::to_string(index), numberOfBins - 10);
}
alg.initialize();
alg.setProperty("InputWorkspace", ws);
TS_ASSERT_THROWS(alg.execute(), const std::runtime_error &);
clearADS();
}
void test_that_algorithm_does_not_execute_if_bad_last_good_data() {
PSIBackgroundSubtraction alg;
int numberOfHistograms = 2;
int numberOfBins = 100;
auto ws = createCountsTestWorkspace(numberOfHistograms, numberOfBins, false);
for (int index = 0; index < numberOfHistograms; index++) {
ws->mutableRun().addProperty("First good spectra " + std::to_string(index), 1);
ws->mutableRun().addProperty("Last good spectra " + std::to_string(index), numberOfBins * 2);
}
alg.initialize();
alg.setProperty("InputWorkspace", ws);
TS_ASSERT_THROWS(alg.execute(), const std::runtime_error &);
clearADS();
}
void test_that_algorithm_does_not_execute_if_last_before_first_good_data() {
PSIBackgroundSubtraction alg;
int numberOfHistograms = 2;
int numberOfBins = 100;
auto ws = createCountsTestWorkspace(numberOfHistograms, numberOfBins, false);
for (int index = 0; index < numberOfHistograms; index++) {
ws->mutableRun().addProperty("First good spectra " + std::to_string(index), 50);
ws->mutableRun().addProperty("Last good spectra " + std::to_string(index), 40);
}
alg.initialize();
alg.setProperty("InputWorkspace", ws);
TS_ASSERT_THROWS(alg.execute(), const std::runtime_error &);
clearADS();
}
void test_background_correctly_removed_from_input_workspace() {
MockPSIBackgroundSubtraction alg;
double background = 20;
double fitQuality = 1.00;
auto ws = createCountsTestWorkspace(4, 100);
auto wsClone = ws->clone();
alg.setReturnBackground(background);
alg.setReturnFitQuality(fitQuality);
alg.initialize();
alg.setProperty("InputWorkspace", ws);
alg.execute();
for (size_t index = 0; index < ws->getNumberHistograms(); ++index) {
for (size_t i = 0; i < ws->y(index).size(); ++i)
TS_ASSERT_EQUALS(ws->y(index)[i], wsClone->y(index)[i] - background)
}
clearADS();
}
};