Newer
Older
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
#ifndef MANTID_MDEVENTS_CONVERTTODIFFRACTIONMDWORKSPACETEST2_H_
#define MANTID_MDEVENTS_CONVERTTODIFFRACTIONMDWORKSPACETEST2_H_
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/IAlgorithm.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidMDAlgorithms/ConvertToDiffractionMDWorkspace3.h"
#include "MantidTestHelpers/ComponentCreationHelper.h"
#include "MantidTestHelpers/MDEventsTestHelper.h"
#include "MantidTestHelpers/WorkspaceCreationHelper.h"
#include <limits>
#include <cxxtest/TestSuite.h>
using namespace Mantid;
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::DataObjects;
using namespace Mantid::MDAlgorithms;
class ConvertToDiffractionMDWorkspace3Test : public CxxTest::TestSuite {
public:
void test_Init() {
ConvertToDiffractionMDWorkspace3 alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
}
/** Test various combinations of OutputDimensions parameter */
void test_OutputDimensions_Parameter() {
EventWorkspace_sptr in_ws = Mantid::DataObjects::MDEventsTestHelper::
createDiffractionEventWorkspace(10);
AnalysisDataService::Instance().addOrReplace("testInEW", in_ws);
IAlgorithm_sptr alg;
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 6, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "OutputDimensions", "Q (lab frame)");
TS_ASSERT(alg->isExecuted());
MDEventWorkspace3::sptr ws;
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"testOutMD"));
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(ws->getDimension(0)->getName(), "Q_lab_x");
TS_ASSERT_EQUALS(ws->getSpecialCoordinateSystem(), Mantid::Kernel::QLab);
// TODO: Now you can add differenc dimension types to each other, but this
// should be fixed
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 8, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "Append", "1", "OutputDimensions",
"HKL");
TS_ASSERT(alg->isExecuted());
// If Append is False, then it does work. The workspace gets replaced
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 8, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "Append", "0", "OutputDimensions",
"HKL");
TS_ASSERT(alg->isExecuted());
// Let's remove the old workspace and try again - it will work.
AnalysisDataService::Instance().remove("testOutMD");
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 8, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "Append", "1", "OutputDimensions",
"HKL");
TS_ASSERT(alg->isExecuted());
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"testOutMD"));
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(ws->getDimension(0)->getName(), "[H,0,0]");
TS_ASSERT_EQUALS(ws->getSpecialCoordinateSystem(), Mantid::Kernel::HKL);
AnalysisDataService::Instance().remove("testOutMD");
alg = FrameworkManager::Instance().exec(
"ConvertToDiffractionMDWorkspace", 6, "InputWorkspace", "testInEW",
"OutputWorkspace", "testOutMD", "OutputDimensions", "Q (sample frame)");
TS_ASSERT(alg->isExecuted());
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"testOutMD"));
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(ws->getDimension(0)->getName(), "Q_sample_x");
TS_ASSERT_EQUALS(ws->getSpecialCoordinateSystem(), Mantid::Kernel::QSample);
}
void do_test_MINITOPAZ(EventType type, size_t numTimesToAdd = 1,
bool OneEventPerBin = false,
bool MakeWorkspace2D = false,
size_t nEventsRetrieved = 400) {
int numEventsPer = 100;
EventWorkspace_sptr in_ws = Mantid::DataObjects::MDEventsTestHelper::
createDiffractionEventWorkspace(numEventsPer);
if (type == WEIGHTED)
in_ws *= 2.0;
if (type == WEIGHTED_NOTIME) {
for (size_t i = 0; i < in_ws->getNumberHistograms(); i++) {
EventList &el = in_ws->getSpectrum(i);
el.compressEvents(0.0, &el);
}
}
// Rebin the workspace to have a manageable number bins
AnalysisDataService::Instance().addOrReplace("inputWS", in_ws);
FrameworkManager::Instance().exec("Rebin", 8, "InputWorkspace", "inputWS",
"OutputWorkspace", "inputWS", "Params",
"0, 500, 16e3", "PreserveEvents",
MakeWorkspace2D ? "0" : "1");
ConvertToDiffractionMDWorkspace3 alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setPropertyValue("InputWorkspace", "inputWS");
alg.setProperty("OneEventPerBin", OneEventPerBin);
alg.setPropertyValue("Extents", "-50, 50");
alg.setPropertyValue("OutputWorkspace", "test_md3");
TS_ASSERT_THROWS_NOTHING(alg.execute();)
TS_ASSERT(alg.isExecuted())
MDEventWorkspace3::sptr ws;
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"test_md3"));
TS_ASSERT(ws);
if (!ws)
return;
size_t npoints = ws->getNPoints();
// # of points != # of bins exactly because some are off the extents
TS_ASSERT(nEventsRetrieved <= npoints);
TS_ASSERT_EQUALS(ws->getNumExperimentInfo(), 1);
TSM_ASSERT("ExperimentInfo object is valid", ws->getExperimentInfo(0));
// Add to an existing MDEW
for (size_t i = 1; i < numTimesToAdd; i++) {
std::cout << "Iteration " << i << '\n';
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setPropertyValue("InputWorkspace", "inputWS");
alg.setProperty("Append", true);
alg.setPropertyValue("OutputWorkspace", "test_md3");
TS_ASSERT_THROWS_NOTHING(alg.execute();)
TS_ASSERT(alg.isExecuted())
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"test_md3"));
TS_ASSERT(ws);
if (!ws)
return;
TS_ASSERT_EQUALS(
npoints * (i + 1),
ws->getNPoints()); // There are now twice as many points as before
TS_ASSERT_EQUALS(ws->getNumExperimentInfo(), (i + 1));
TSM_ASSERT("ExperimentInfo object is valid",
ws->getExperimentInfo(static_cast<uint16_t>(i)));
}
AnalysisDataService::Instance().remove("test_md3");
}
void test_MINITOPAZ() { do_test_MINITOPAZ(TOF); }
void test_MINITOPAZ_Weighted() { do_test_MINITOPAZ(WEIGHTED); }
void test_MINITOPAZ_addToExistingWorkspace() { do_test_MINITOPAZ(TOF, 2); }
void test_MINITOPAZ_OneEventPerBin_fromEventWorkspace() {
do_test_MINITOPAZ(TOF, 1, true, false);
}
void test_MINITOPAZ_OneEventPerBin_fromWorkspace2D() {
do_test_MINITOPAZ(TOF, 1, true, true);
}
void test_MINITOPAZ_fromWorkspace2D() {
// this is questionable change, indicating that ConvertToMD and
// CovertToDiffractionWorkspace treat 0 differently
do_test_MINITOPAZ(TOF, 1, false, true, 399);
}
void test_MINITOPAZ_autoExtents() {
int numEventsPer = 100;
EventWorkspace_sptr in_ws = Mantid::DataObjects::MDEventsTestHelper::
createDiffractionEventWorkspace(numEventsPer);
// Rebin the workspace to have a manageable number bins
AnalysisDataService::Instance().addOrReplace("inputWS", in_ws);
FrameworkManager::Instance().exec("Rebin", 8, "InputWorkspace", "inputWS",
"OutputWorkspace", "inputWS", "Params",
"0, 500, 16e3", "PreserveEvents", "0");
ConvertToDiffractionMDWorkspace3 alg;
TS_ASSERT_THROWS_NOTHING(alg.initialize())
TS_ASSERT(alg.isInitialized())
alg.setPropertyValue("InputWorkspace", "inputWS");
alg.setPropertyValue("OutputWorkspace", "test_md3");
TS_ASSERT_THROWS_NOTHING(alg.execute();)
TS_ASSERT(alg.isExecuted())
MDEventWorkspace3::sptr ws;
TS_ASSERT_THROWS_NOTHING(
ws = AnalysisDataService::Instance().retrieveWS<MDEventWorkspace3>(
"test_md3"));
TS_ASSERT(ws);
if (!ws)
return;
auto dim = ws->getDimension(0);
TS_ASSERT_DELTA(dim->getMinimum(), -50, 1e-3);
TS_ASSERT_DELTA(dim->getMaximum(), -0.9411, 1e-3);
dim = ws->getDimension(1);
TS_ASSERT_DELTA(dim->getMinimum(), -0.4669, 1e-3);
TS_ASSERT_DELTA(dim->getMaximum(), 0.474, 1e-3);
dim = ws->getDimension(2);
TS_ASSERT_DELTA(dim->getMinimum(), 0, 1e-3);
TS_ASSERT_DELTA(dim->getMaximum(), 0.705, 1e-3);
}
};
#endif /* MANTID_MDEVENTS_MAKEDIFFRACTIONMDEVENTWORKSPACETEST_H_ */