Newer
Older
Ronald Fowler
committed
#ifndef LOADMUONNEXUSTEST_H_
#define LOADMUONNEXUSTEST_H_
//This test does not compile on Windows64 as is does not support HDF4 files
#ifndef _WIN64
Ronald Fowler
committed
// These includes seem to make the difference between initialization of the
// workspace names (workspace2D/1D etc), instrument classes and not for this test case.
Ronald Fowler
committed
#include "MantidDataObjects/WorkspaceSingleValue.h"
Ronald Fowler
committed
#include "MantidDataHandling/LoadInstrument.h"
Ronald Fowler
committed
//
#include <fstream>
#include <cxxtest/TestSuite.h>
#include "MantidNexus/LoadMuonNexus.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/ManagedWorkspace2D.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include "MantidAPI/SpectraDetectorMap.h"
Gigg, Martyn Anthony
committed
#include "Poco/Path.h"
Ronald Fowler
committed
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::NeXus;
Ronald Fowler
committed
using namespace Mantid::DataHandling;
Ronald Fowler
committed
using namespace Mantid::DataObjects;
class LoadMuonNexusTest : public CxxTest::TestSuite
{
public:
void testInit()
{
TS_ASSERT_THROWS_NOTHING(nxLoad.initialize());
TS_ASSERT( nxLoad.isInitialized() );
}
void testExec()
{
if ( !nxLoad.isInitialized() ) nxLoad.initialize();
// Should fail because mandatory parameter has not been set
TS_ASSERT_THROWS(nxLoad.execute(),std::runtime_error);
// Now set required filename and output workspace name
Gigg, Martyn Anthony
committed
inputFile = Poco::Path(Poco::Path::current()).resolve("../../../../Test/Nexus/emu00006473.nxs").toString();
Ronald Fowler
committed
nxLoad.setPropertyValue("FileName", inputFile);
outputSpace="outer";
nxLoad.setPropertyValue("OutputWorkspace", outputSpace);
std::string result;
TS_ASSERT_THROWS_NOTHING( result = nxLoad.getPropertyValue("Filename") )
TS_ASSERT( ! result.compare(inputFile));
//
// Test execute to read file and populate workspace
//
TS_ASSERT_THROWS_NOTHING(nxLoad.execute());
TS_ASSERT( nxLoad.isExecuted() );
//
// Test workspace data (copied from LoadRawTest.h)
//
Roman Tolchenov
committed
MatrixWorkspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace)));
Ronald Fowler
committed
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
// Should be 32 for file inputFile = "../../../../Test/Nexus/emu00006473.nxs";
TS_ASSERT_EQUALS( output2D->getNumberHistograms(), 32);
// Check two X vectors are the same
TS_ASSERT( (output2D->dataX(3)) == (output2D->dataX(31)) );
// Check two Y arrays have the same number of elements
TS_ASSERT_EQUALS( output2D->dataY(5).size(), output2D->dataY(17).size() );
// Check one particular value
TS_ASSERT_EQUALS( output2D->dataY(11)[686], 81);
// Check that the error on that value is correct
TS_ASSERT_EQUALS( output2D->dataE(11)[686], 9);
// Check that the time is as expected from bin boundary update
TS_ASSERT_DELTA( output2D->dataX(11)[687], 10.738,0.001);
// Check the unit has been set correctly
TS_ASSERT_EQUALS( output->getAxis(0)->unit()->unitID(), "TOF" )
TS_ASSERT( ! output-> isDistribution() )
Ronald Fowler
committed
/* - other tests from LoadRawTest - These test data not in current Nexus files
Ronald Fowler
committed
//----------------------------------------------------------------------
// Tests taken from LoadInstrumentTest to check sub-algorithm is running properly
//----------------------------------------------------------------------
boost::shared_ptr<Instrument> i = output->getInstrument();
Mantid::Geometry::Component* source = i->getSource();
TS_ASSERT_EQUALS( source->getName(), "undulator");
TS_ASSERT_DELTA( source->getPos().Y(), 0.0,0.01);
Mantid::Geometry::Component* samplepos = i->getSample();
TS_ASSERT_EQUALS( samplepos->getName(), "nickel-holder");
TS_ASSERT_DELTA( samplepos->getPos().Y(), 10.0,0.01);
Mantid::Geometry::Detector *ptrDet103 = dynamic_cast<Mantid::Geometry::Detector*>(i->getDetector(103));
TS_ASSERT_EQUALS( ptrDet103->getID(), 103);
TS_ASSERT_EQUALS( ptrDet103->getName(), "pixel");
TS_ASSERT_DELTA( ptrDet103->getPos().X(), 0.4013,0.01);
TS_ASSERT_DELTA( ptrDet103->getPos().Z(), 2.4470,0.01);
Ronald Fowler
committed
*/
Ronald Fowler
committed
//----------------------------------------------------------------------
// Test code copied from LoadLogTest to check sub-algorithm is running properly
//----------------------------------------------------------------------
Sofia Antony
committed
//boost::shared_ptr<Sample> sample = output->getSample();
Property *l_property = output->sample().getLogData( std::string("beamlog_current") );
Ronald Fowler
committed
TimeSeriesProperty<double> *l_timeSeriesDouble = dynamic_cast<TimeSeriesProperty<double>*>(l_property);
std::string timeSeriesString = l_timeSeriesDouble->value();
Ronald Fowler
committed
TS_ASSERT_EQUALS( timeSeriesString.substr(0,27), "2006-Nov-21 07:03:08 182.8" );
Ronald Fowler
committed
//check that sample name has been set correctly
Sofia Antony
committed
TS_ASSERT_EQUALS(output->sample().getName(), "Cr2.7Co0.3Si")
Ronald Fowler
committed
Ronald Fowler
committed
/*
Ronald Fowler
committed
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
//----------------------------------------------------------------------
// Tests to check that Loading SpectraDetectorMap is done correctly
//----------------------------------------------------------------------
map= output->getSpectraMap();
// Check the total number of elements in the map for HET
TS_ASSERT_EQUALS(map->nElements(),24964);
// Test one to one mapping, for example spectra 6 has only 1 pixel
TS_ASSERT_EQUALS(map->ndet(6),1);
// Test one to many mapping, for example 10 pixels contribute to spectra 2084
TS_ASSERT_EQUALS(map->ndet(2084),10);
// Check the id number of all pixels contributing
std::vector<Mantid::Geometry::IDetector*> detectorgroup;
detectorgroup=map->getDetectors(2084);
std::vector<Mantid::Geometry::IDetector*>::iterator it;
int pixnum=101191;
for (it=detectorgroup.begin();it!=detectorgroup.end();it++)
TS_ASSERT_EQUALS((*it)->getID(),pixnum++);
// Test with spectra that does not exist
// Test that number of pixel=0
TS_ASSERT_EQUALS(map->ndet(5),0);
// Test that trying to get the Detector throws.
boost::shared_ptr<Mantid::Geometry::IDetector> test;
TS_ASSERT_THROWS(test=map->getDetector(5),std::runtime_error);
*/
}
// void testWithManagedWorkspace()
// {
// ConfigService::Instance().loadConfig("UseManagedWS.properties");
// //LoadRaw loader4;
// //loader4.initialize();
// //loader4.setPropertyValue("Filename", inputFile);
// //loader4.setPropertyValue("OutputWorkspace", "managedws");
// // TS_ASSERT_THROWS_NOTHING( loader4.execute() )
// //TS_ASSERT( loader4.isExecuted() )
//
// // Get back workspace and check it really is a ManagedWorkspace2D
Roman Tolchenov
committed
// MatrixWorkspace_sptr output;
Ronald Fowler
committed
// TS_ASSERT_THROWS_NOTHING( output = AnalysisDataService::Instance().retrieve("managedws") );
// TS_ASSERT( dynamic_cast<ManagedWorkspace2D*>(output.get()) )
// }
void testExec2()
{
Sofia Antony
committed
//test for multi period
// Now set required filename and output workspace name
inputFile2 = Poco::Path(Poco::Path::current()).resolve("../../../../Test/Nexus/emu00006475.nxs").toString();
Sofia Antony
committed
nxLoad.setPropertyValue("FileName", inputFile2);
outputSpace="outer2";
nxLoad.setPropertyValue("OutputWorkspace", outputSpace);
nxLoad.setPropertyValue("EntryNumber", "1");
int entryNumber=nxLoad.getProperty("EntryNumber");
Ronald Fowler
committed
Sofia Antony
committed
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
std::string result;
TS_ASSERT_THROWS_NOTHING( result = nxLoad.getPropertyValue("Filename") )
TS_ASSERT( ! result.compare(inputFile2));
//
// Test execute to read file and populate workspace
//
TS_ASSERT_THROWS_NOTHING(nxLoad.execute());
TS_ASSERT( nxLoad.isExecuted() );
//
// Test workspace data - should be 4 separate workspaces for this 4 period file
//
if(entryNumber==0)
{
WorkspaceGroup_sptr outGrp;
TS_ASSERT_THROWS_NOTHING(outGrp = boost::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve(outputSpace)));
}
//if entry number is given
if(entryNumber==1)
{
MatrixWorkspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace)));
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
//Workspace2D_sptr output2D2 = boost::dynamic_pointer_cast<Workspace2D>(output2);
// Should be 32 for file inputFile = "../../../../Test/Nexus/emu00006475.nxs";
TS_ASSERT_EQUALS( output2D->getNumberHistograms(), 32);
// Check two X vectors are the same
TS_ASSERT( (output2D->dataX(3)) == (output2D->dataX(31)) );
// Check two Y arrays have the same number of elements
TS_ASSERT_EQUALS( output2D->dataY(5).size(), output2D->dataY(17).size() );
// Check that the time is as expected from bin boundary update
TS_ASSERT_DELTA( output2D->dataX(11)[687], 10.738,0.001);
// Check the unit has been set correctly
TS_ASSERT_EQUALS( output->getAxis(0)->unit()->unitID(), "TOF" )
TS_ASSERT( ! output-> isDistribution() )
//check that sample name has been set correctly
Sofia Antony
committed
//boost::shared_ptr<Sample> sample,sample2;
//sample = output->getSample();
Sofia Antony
committed
//sample2 = output2->getSample();
//TS_ASSERT_EQUALS(sample->getName(), sample2->getName());
Sofia Antony
committed
TS_ASSERT_EQUALS(output->sample().getName(), "ptfe test")
Sofia Antony
committed
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
}
MatrixWorkspace_sptr output,output2,output3,output4;
WorkspaceGroup_sptr outGrp;
//if no entry number load the group workspace
if(entryNumber==0)
{
TS_ASSERT_THROWS_NOTHING(outGrp = boost::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve(outputSpace)));
TS_ASSERT_THROWS_NOTHING(output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_1")));
TS_ASSERT_THROWS_NOTHING(output2 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_2")));
TS_ASSERT_THROWS_NOTHING(output3 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_3")));
TS_ASSERT_THROWS_NOTHING(output4 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_4")));
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
Workspace2D_sptr output2D2 = boost::dynamic_pointer_cast<Workspace2D>(output2);
// Should be 32 for file inputFile = "../../../../Test/Nexus/emu00006475.nxs";
TS_ASSERT_EQUALS( output2D->getNumberHistograms(), 32);
// Check two X vectors are the same
TS_ASSERT( (output2D->dataX(3)) == (output2D->dataX(31)) );
// Check two Y arrays have the same number of elements
TS_ASSERT_EQUALS( output2D->dataY(5).size(), output2D->dataY(17).size() );
// Check one particular value
TS_ASSERT_EQUALS( output2D2->dataY(8)[502], 121);
// Check that the error on that value is correct
TS_ASSERT_EQUALS( output2D2->dataE(8)[502], 11);
// Check that the time is as expected from bin boundary update
TS_ASSERT_DELTA( output2D->dataX(11)[687], 10.738,0.001);
// Check the unit has been set correctly
TS_ASSERT_EQUALS( output->getAxis(0)->unit()->unitID(), "TOF" )
TS_ASSERT( ! output-> isDistribution() )
//check that sample name has been set correctly
Sofia Antony
committed
// boost::shared_ptr<Sample> sample,sample2;
//sample = output->getSample();
// sample2 = output2->getSample();
TS_ASSERT_EQUALS(output->sample().getName(), output2->sample().getName());
TS_ASSERT_EQUALS(output->sample().getName(), "ptfe test")
Sofia Antony
committed
}
}
void testExec2withZeroEntryNumber()
{
//test for multi period
Ronald Fowler
committed
// Now set required filename and output workspace name
inputFile2 = Poco::Path(Poco::Path::current()).resolve("../../../../Test/Nexus/emu00006475.nxs").toString();
Ronald Fowler
committed
nxLoad.setPropertyValue("FileName", inputFile2);
outputSpace="outer2";
Sofia Antony
committed
nxLoad.setPropertyValue("OutputWorkspace", outputSpace);
nxLoad.setPropertyValue("EntryNumber", "0");
int entryNumber=nxLoad.getProperty("EntryNumber");
Ronald Fowler
committed
std::string result;
TS_ASSERT_THROWS_NOTHING( result = nxLoad.getPropertyValue("Filename") )
TS_ASSERT( ! result.compare(inputFile2));
//
// Test execute to read file and populate workspace
//
TS_ASSERT_THROWS_NOTHING(nxLoad.execute());
TS_ASSERT( nxLoad.isExecuted() );
//
// Test workspace data - should be 4 separate workspaces for this 4 period file
Ronald Fowler
committed
//
Sofia Antony
committed
WorkspaceGroup_sptr outGrp;
TS_ASSERT_THROWS_NOTHING(outGrp = boost::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve(outputSpace)));
Roman Tolchenov
committed
MatrixWorkspace_sptr output,output2,output3,output4;
Sofia Antony
committed
//WorkspaceGroup_sptr outGrp;
//if no entry number load the group workspace
if(entryNumber==0)
{
//TS_ASSERT_THROWS_NOTHING(outGrp = boost::dynamic_pointer_cast<WorkspaceGroup>(AnalysisDataService::Instance().retrieve(outputSpace)));
TS_ASSERT_THROWS_NOTHING(output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_1")));
TS_ASSERT_THROWS_NOTHING(output2 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_2")));
TS_ASSERT_THROWS_NOTHING(output3 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_3")));
TS_ASSERT_THROWS_NOTHING(output4 = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve(outputSpace+"_4")));
Ronald Fowler
committed
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
Workspace2D_sptr output2D2 = boost::dynamic_pointer_cast<Workspace2D>(output2);
// Should be 32 for file inputFile = "../../../../Test/Nexus/emu00006475.nxs";
TS_ASSERT_EQUALS( output2D->getNumberHistograms(), 32);
Ronald Fowler
committed
// Check two X vectors are the same
TS_ASSERT( (output2D->dataX(3)) == (output2D->dataX(31)) );
Ronald Fowler
committed
// Check two Y arrays have the same number of elements
TS_ASSERT_EQUALS( output2D->dataY(5).size(), output2D->dataY(17).size() );
// Check one particular value
TS_ASSERT_EQUALS( output2D2->dataY(8)[502], 121);
Ronald Fowler
committed
// Check that the error on that value is correct
TS_ASSERT_EQUALS( output2D2->dataE(8)[502], 11);
Ronald Fowler
committed
// Check that the time is as expected from bin boundary update
TS_ASSERT_DELTA( output2D->dataX(11)[687], 10.738,0.001);
// Check the unit has been set correctly
TS_ASSERT_EQUALS( output->getAxis(0)->unit()->unitID(), "TOF" )
TS_ASSERT( ! output-> isDistribution() )
Ronald Fowler
committed
//check that sample name has been set correctly
Sofia Antony
committed
//boost::shared_ptr<Sample> sample,sample2;
// sample = output->getSample();
// sample2 = output2->getSample();
TS_ASSERT_EQUALS(output->sample().getName(), output2->sample().getName());
TS_ASSERT_EQUALS(output->sample().getName(), "ptfe test")
Sofia Antony
committed
}
Ronald Fowler
committed
}
Ronald Fowler
committed
void testarrayin()
{
if ( !nxload3.isInitialized() ) nxload3.initialize();
nxload3.setPropertyValue("Filename", inputFile);
nxload3.setPropertyValue("OutputWorkspace", "outWS");
nxload3.setPropertyValue("SpectrumList", "29,30,31");
nxload3.setPropertyValue("SpectrumMin", "5");
nxload3.setPropertyValue("SpectrumMax", "10");
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(nxload3.execute());
TS_ASSERT( nxload3.isExecuted() );
// Get back the saved workspace
Roman Tolchenov
committed
MatrixWorkspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("outWS")));
Ronald Fowler
committed
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
// Should be 6 for selected input
TS_ASSERT_EQUALS( output2D->getNumberHistograms(), 9);
// Check two X vectors are the same
TS_ASSERT( (output2D->dataX(1)) == (output2D->dataX(5)) );
// Check two Y arrays have the same number of elements
TS_ASSERT_EQUALS( output2D->dataY(2).size(), output2D->dataY(7).size() );
// Check one particular value
TS_ASSERT_EQUALS( output2D->dataY(8)[479], 144);
// Check that the error on that value is correct
TS_ASSERT_EQUALS( output2D->dataE(8)[479], 12);
// Check that the error on that value is correct
TS_ASSERT_DELTA( output2D->dataX(8)[479], 7.410, 0.0001);
}
Ronald Fowler
committed
private:
Ronald Fowler
committed
LoadMuonNexus nxLoad,nxload2,nxload3;
Ronald Fowler
committed
std::string outputSpace;
std::string entryName;
std::string inputFile;
std::string inputFile2;
boost::shared_ptr<SpectraDetectorMap> map;
};
Ronald Fowler
committed
#endif /*LOADMUONNEXUSTEST_H_*/