Newer
Older
#ifndef MANTID_MDALGORITHMS_CONVERTCWPDMDTOSPECTRATEST_H_
#define MANTID_MDALGORITHMS_CONVERTCWPDMDTOSPECTRATEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidMDAlgorithms/ConvertCWPDMDToSpectra.h"
#include "MantidDataHandling/LoadSpiceAscii.h"
#include "MantidMDAlgorithms/ConvertSpiceDataToRealSpace.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidAPI/IMDEventWorkspace.h"
using Mantid::MDAlgorithms::ConvertCWPDMDToSpectra;
using Mantid::DataHandling::LoadSpiceAscii;
using Mantid::MDAlgorithms::ConvertSpiceDataToRealSpace;
using namespace Mantid::Kernel;
class ConvertCWPDMDToSpectraTest : 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 ConvertCWPDMDToSpectraTest *createSuite() {
return new ConvertCWPDMDToSpectraTest();
}
static void destroySuite(ConvertCWPDMDToSpectraTest *suite) { delete suite; }
//----------------------------------------------------------------------------------------------
void test_Init() {
ConvertCWPDMDToSpectra tetalg;
tetalg.initialize();
TS_ASSERT(tetalg.isInitialized());
// Create test workspaces
createTestWorkspaces();
//----------------------------------------------------------------------------------------------
/** Unit test to reduce/bin the HB2A data
* @brief test_ReduceHB2AData
*/
// Init
ConvertCWPDMDToSpectra alg;
alg.initialize();
// Set properties
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputWorkspace", m_dataMD->name()));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputMonitorWorkspace", m_monitorMD->name()));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("BinningParams", "0, 0.1, 120."));
TS_ASSERT_THROWS_NOTHING(
alg.setProperty("LinearInterpolateZeroCounts", false));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("ScaleFactor", 65000.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "ReducedData"));
// Execute
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
// Get ouput
MatrixWorkspace_sptr outws = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve("ReducedData"));
TS_ASSERT(outws);
// Check output
TS_ASSERT_EQUALS(outws->getNumberHistograms(), 1);
const Mantid::MantidVec &vecX = outws->readX(0);
const Mantid::MantidVec &vecY = outws->readY(0);
const Mantid::MantidVec &vecE = outws->readE(0);
TS_ASSERT_DELTA(vecX.front(), 0.0, 0.0001);
TS_ASSERT_DELTA(vecX.back(), 120.0, 0.0001);
double y1101 = vecY[1101];
double e1101 = vecE[1101];
TS_ASSERT_DELTA(y1101, 186.0716, 0.0001);
TS_ASSERT(e1101 > sqrt(y1101));
TS_ASSERT(e1101 < sqrt(y1101 * 1.05));
// Sample logs: temperature
TimeSeriesProperty<double> *tempbseries =
dynamic_cast<TimeSeriesProperty<double> *>(
outws->run().getProperty("temp_b"));
TS_ASSERT(tempbseries);
TS_ASSERT_EQUALS(tempbseries->size(), 61);
DateAndTime t0 = tempbseries->nthTime(0);
DateAndTime t3 = tempbseries->nthTime(3);
TS_ASSERT_EQUALS(
(t3.totalNanoseconds() - t0.totalNanoseconds()) / 1000000000, 90);
// Clean
AnalysisDataService::Instance().remove("ReducedData");
//----------------------------------------------------------------------------------------------
/** Unit test to reduce/bin the HB2A data with more options
* @brief test_ReduceHB2AData
*/
void test_ReduceHB2ADataMoreOptions() {
// Init
ConvertCWPDMDToSpectra alg;
alg.initialize();
// Set properties
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputWorkspace", m_dataMD->name()));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputMonitorWorkspace", m_monitorMD->name()));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("UnitOutput", "dSpacing"));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("BinningParams", "0.5, 0.01, 5.0"));
TS_ASSERT_THROWS_NOTHING(
alg.setProperty("LinearInterpolateZeroCounts", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("ScaleFactor", 10.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("NeutronWaveLength", 2.41));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "ReducedData"));
// Execute
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
// Get ouput
MatrixWorkspace_sptr outws = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve("ReducedData"));
TS_ASSERT(outws);
// Check unit and range of X
std::string unit = outws->getAxis(0)->unit()->unitID();
TS_ASSERT_EQUALS(unit, "dSpacing");
const Mantid::MantidVec &vecX = outws->readX(0);
TS_ASSERT_DELTA(vecX.front(), 0.5, 0.0001);
TS_ASSERT_DELTA(vecX.back(), 5.00, 0.0001);
// Check statistics
// Clean
AnalysisDataService::Instance().remove("ReducedData");
}
//----------------------------------------------------------------------------------------------
/** Unit test to reduce/bin the HB2A data with more options
* @brief test_ReduceHB2AData
*/
void test_ReduceHB2ADataAutoBinBoundary() {
// Init
ConvertCWPDMDToSpectra alg;
alg.initialize();
// Set properties
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputWorkspace", m_dataMD->name()));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputMonitorWorkspace", m_monitorMD->name()));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("UnitOutput", "dSpacing"));
TS_ASSERT_THROWS_NOTHING(alg.setPropertyValue("BinningParams", "0.01"));
TS_ASSERT_THROWS_NOTHING(
alg.setProperty("LinearInterpolateZeroCounts", true));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("ScaleFactor", 10.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("NeutronWaveLength", 2.41));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "ReducedData"));
// Execute
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
// Get ouput
MatrixWorkspace_sptr outws = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve("ReducedData"));
TS_ASSERT(outws);
// Check unit and range of X
std::string unit = outws->getAxis(0)->unit()->unitID();
TS_ASSERT_EQUALS(unit, "dSpacing");
const Mantid::MantidVec &vecX = outws->readX(0);
TS_ASSERT_DELTA(vecX.front(), 1.3416, 0.0001);
TS_ASSERT_DELTA(vecX.back(), 23.0216, 0.001);
// Check statistics
// Clean
AnalysisDataService::Instance().remove("ReducedData");
}
//----------------------------------------------------------------------------------------------
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
/** Create workspaces for testing
* @brief createTestWorkspaces
*/
void createTestWorkspaces() {
LoadSpiceAscii spcloader;
spcloader.initialize();
// Load HB2A spice file
TS_ASSERT_THROWS_NOTHING(
spcloader.setProperty("Filename", "HB2A_exp0231_scan0001.dat"));
TS_ASSERT_THROWS_NOTHING(
spcloader.setProperty("OutputWorkspace", "DataTable"));
TS_ASSERT_THROWS_NOTHING(
spcloader.setProperty("RunInfoWorkspace", "LogParentWS"));
TS_ASSERT_THROWS_NOTHING(spcloader.setPropertyValue(
"DateAndTimeLog", "date,MM/DD/YYYY,time,HH:MM:SS AM"));
TS_ASSERT_THROWS_NOTHING(
spcloader.setProperty("IgnoreUnlistedLogs", false));
spcloader.execute();
// Retrieve the workspaces as the inputs of ConvertSpiceDataToRealSpace
ITableWorkspace_sptr datatablews =
boost::dynamic_pointer_cast<ITableWorkspace>(
AnalysisDataService::Instance().retrieve("DataTable"));
TS_ASSERT(datatablews);
MatrixWorkspace_sptr parentlogws =
boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve("LogParentWS"));
TS_ASSERT(parentlogws);
// Set up ConvertSpiceDataToRealSpace
ConvertSpiceDataToRealSpace loader;
loader.initialize();
loader.setProperty("InputWorkspace", datatablews);
loader.setProperty("RunInfoWorkspace", parentlogws);
loader.setProperty("Instrument", "HB2A");
loader.setPropertyValue("OutputWorkspace", "HB2A_MD");
loader.setPropertyValue("OutputMonitorWorkspace", "MonitorMDW");
loader.execute();
TS_ASSERT(loader.isExecuted());
// Get on hold of MDWorkspaces for test
m_dataMD = boost::dynamic_pointer_cast<IMDEventWorkspace>(
AnalysisDataService::Instance().retrieve("HB2A_MD"));
m_monitorMD = boost::dynamic_pointer_cast<IMDEventWorkspace>(
AnalysisDataService::Instance().retrieve("MonitorMDW"));
TS_ASSERT(m_dataMD);
TS_ASSERT(m_monitorMD);
// Clean
AnalysisDataService::Instance().remove(datatablews->name());
AnalysisDataService::Instance().remove(parentlogws->name());
}
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
//----------------------------------------------------------------------------------------------
/** Unit test to reduce/bin the HB2A data with excluded detectors
* @brief test_ReduceHB2AData
*/
void test_ExcludeDetectors() {
// Set up
std::vector<int> vecExcludedDetID;
vecExcludedDetID.push_back(10);
for (int i = 20; i < 30; ++i)
vecExcludedDetID.push_back(i);
vecExcludedDetID.push_back(49);
// Init
ConvertCWPDMDToSpectra alg;
alg.initialize();
// Set properties
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputWorkspace", m_dataMD->name()));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("InputMonitorWorkspace", m_monitorMD->name()));
TS_ASSERT_THROWS_NOTHING(
alg.setPropertyValue("BinningParams", "0, 0.1, 120."));
TS_ASSERT_THROWS_NOTHING(
alg.setProperty("LinearInterpolateZeroCounts", false));
TS_ASSERT_THROWS_NOTHING(
alg.setProperty("ExcludedDetectorIDs", vecExcludedDetID));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("ScaleFactor", 65000.0));
TS_ASSERT_THROWS_NOTHING(alg.setProperty("OutputWorkspace", "ReducedData"));
// Execute
TS_ASSERT_THROWS_NOTHING(alg.execute());
TS_ASSERT(alg.isExecuted());
// Get ouput
MatrixWorkspace_sptr outws = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve("ReducedData"));
TS_ASSERT(outws);
// Check output
TS_ASSERT_EQUALS(outws->getNumberHistograms(), 1);
// X, Y and E values
const Mantid::MantidVec &vecX = outws->readX(0);
const Mantid::MantidVec &vecY = outws->readY(0);
const Mantid::MantidVec &vecE = outws->readE(0);
TS_ASSERT_DELTA(vecX.front(), 0.0, 0.0001);
TS_ASSERT_DELTA(vecX.back(), 120.0, 0.0001);
// X around 80 belongs to the detetors that are excluded.
double y800 = vecY[800];
TS_ASSERT_DELTA(y800, 0, 0.0001);
double y1101 = vecY[1101];
double e1101 = vecE[1101];
TS_ASSERT_DELTA(y1101, 186.0716, 0.0001);
TS_ASSERT(e1101 > sqrt(y1101));
TS_ASSERT(e1101 < sqrt(y1101 * 1.05));
// Sample logs: temperature
TimeSeriesProperty<double> *tempbseries =
dynamic_cast<TimeSeriesProperty<double> *>(
outws->run().getProperty("temp_b"));
TS_ASSERT(tempbseries);
TS_ASSERT_EQUALS(tempbseries->size(), 61);
DateAndTime t0 = tempbseries->nthTime(0);
DateAndTime t3 = tempbseries->nthTime(3);
TS_ASSERT_EQUALS(
(t3.totalNanoseconds() - t0.totalNanoseconds()) / 1000000000, 90);
// Clean
AnalysisDataService::Instance().remove("ReducedData");
}
//----------------------------------------------------------------------------------------------
/** Clean the testing workspaces
*/
void test_Clean() {
AnalysisDataService::Instance().remove(m_dataMD->name());
AnalysisDataService::Instance().remove(m_monitorMD->name());
}
private:
IMDEventWorkspace_sptr m_dataMD;
IMDEventWorkspace_sptr m_monitorMD;
#endif /* MANTID_MDALGORITHMS_CONVERTCWPDMDTOSPECTRATEST_H_ */