-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadDetectorInfoTest.h
450 lines (369 loc) · 15.2 KB
/
LoadDetectorInfoTest.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
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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#ifndef LOADDETECTORINFOTEST_H_
#define LOADDETECTORINFOTEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidDataHandling/LoadDetectorInfo.h"
#include "MantidDataHandling/LoadRaw3.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/DetectorGroup.h"
#include "MantidGeometry/Instrument/ObjComponent.h"
#include "MantidAPI/SpectraDetectorMap.h"
#include <Poco/Path.h>
#include <Poco/File.h>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <fstream>
#include <vector>
#include <iostream>
using namespace Mantid::DataHandling;
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::Geometry;
using namespace Mantid::DataObjects;
/* choose an instrument to test, we could test all instruments
* every time but I think a detailed test on the smallest workspace
* is enough as the other workspaces take a long time to process (Steve Williams)
*/
//MARI
static const std::string RAWFILE = "MAR11001.raw";
static const double TIMEOFF = 3.9;
static const int MONITOR = 2;
static const int NUMRANDOM = 7;
static const int DETECTS[NUMRANDOM]={4101,4804,1323,1101,3805,1323,3832};
namespace SmallTestDatFile
{
const int NDETECTS = 6;
}
class LoadDetectorIndoTestHelper : public LoadDetectorInfo
{
public:
void readNXS(const std::string &fName,bool moveDetectors = true)
{
this->m_moveDets = moveDetectors;
LoadDetectorInfo::readNXS(fName);
}
};
namespace
{
std::string delta[] = {"4", "4.500", "4.500", "4.500", "-6.00", "0.000"};
std::string pressure[] = {"10.0000", "10.0000", "10.0000", "10.0001", "10.000", "10.0001"};
std::string wallThick[] = {"0.00080", "0.00080", "0.00080", "-0.00080", "0.00080", "9.500"};
std::string code[] = {"3", "1", "3", "3", "3", "3"};
std::string det_l2[] = {"1.5", "1.5", "1.5", "1.5", "1.5", "1.5"};
std::string det_theta[] = {"30", "35", "40", "45", "50", "55"};
std::string det_phi[] = {"-105", "-110", "-115", "-120", "-125", "-130"};
void writeSmallDatFile(const std::string & filename)
{
std::ofstream file(filename.c_str());
const int NOTUSED = -123456;
file << "DETECTOR.DAT writen by LoadDetectorInfoTest" << std::endl;
file << 165888 << " " << 14 << std::endl;
file << "det no. offset l2 code theta phi w_x w_y w_z f_x f_y f_z a_x a_y a_z det_1 det_2 det_3 det4" << std::endl;
for( int i = 0; i < SmallTestDatFile::NDETECTS; ++i )
{
file << i << "\t" << delta[i]<< "\t" << det_l2[i] << "\t" << code[i]<< "\t" << det_theta[i]<< "\t" << det_phi[i]<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED << "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << pressure[i]<< "\t" << wallThick[i]<< "\t" << NOTUSED << std::endl;
}
file.close();
}
void writeLargeTestDatFile(const std::string & filename, const int ndets)
{
// The array is the same value across all spectra as this is meant as a
// performance test not a validation test
std::ofstream file(filename.c_str());
const int NOTUSED = -123456;
file << "DETECTOR.DAT writen by LoadDetectorInfoTest" << std::endl;
file << 165888 << " " << 14 << std::endl;
file << "det no. offset l2 code theta phi w_x w_y w_z f_x f_y f_z a_x a_y a_z det_1 det_2 det_3 det4" << std::endl;
for( int i = 0; i < ndets ; ++i )
{
file << i << "\t" << delta[0]<< "\t" << det_l2[0] << "\t" << code[0]<< "\t" << det_theta[0]<< "\t" << det_phi[0]<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED << "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << NOTUSED<< "\t" << pressure[0]<< "\t" << wallThick[0]<< "\t" << NOTUSED << std::endl;
}
file.close();
}
// Set up a small workspace for testing
void makeTestWorkspace(const int ndets, const int nbins, const std::string & ads_name)
{
MatrixWorkspace_sptr space = WorkspaceFactory::Instance().create("Workspace2D", ndets, nbins+1, nbins);
space->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
Workspace2D_sptr space2D = boost::dynamic_pointer_cast<Workspace2D>(space);
Mantid::MantidVecPtr xs, errors;
std::vector<Mantid::MantidVecPtr> data(ndets);
xs.access().resize(nbins+1, 0.0);
errors.access().resize(nbins, 1.0);
std::vector<int> detIDs(ndets);
std::vector<int> specNums(ndets);
for (int j = 0; j < ndets; ++j)
{
space2D->setX(j,xs);
data[j].access().resize(nbins, j + 1); // the y values will be different for each spectra (1+index_number) but the same for each bin
space2D->setData(j, data[j], errors);
space2D->getAxis(1)->spectraNo(j) = j+1; // spectra numbers are also 1 + index_numbers because this is the tradition
detIDs[j] = j;
specNums[j] = j+1;
}
Instrument_sptr instr(new Instrument);
space->setInstrument(instr);
ObjComponent *samplePos = new ObjComponent("sample-pos", instr.get());
instr->markAsSamplePos(samplePos);
for( int i = 0; i < ndets; ++i)
{
std::ostringstream os;
os << "det-" << i;
Detector *d = new Detector(os.str(),i,0);
instr->markAsDetector(d);
}
// Populate the spectraDetectorMap with fake data to make spectrum number = detector id = workspace index
space->replaceSpectraMap(new SpectraDetectorMap(&specNums[0], &detIDs[0], ndets));
// Register the workspace in the data service
AnalysisDataService::Instance().add(ads_name, space);
}
}
class LoadDetectorInfoTest : public CxxTest::TestSuite
{
public:
static LoadDetectorInfoTest *createSuite() { return new LoadDetectorInfoTest(); }
static void destroySuite(LoadDetectorInfoTest *suite) { delete suite; }
LoadDetectorInfoTest() :
m_InoutWS("loaddetectorinfotest_input_workspace"),
m_DatFile("loaddetectorinfotest_filename.dat"),
m_MariWS("MARfromRaw")
{
// create a .dat file in the current directory that we'll load later
writeSmallDatFile(m_DatFile);
m_rawFile = RAWFILE;
}
~LoadDetectorInfoTest()
{
Poco::File(m_DatFile).remove();
}
void testLoadDat()
{// also tests changing X-values with -same bins, different offsets
LoadDetectorInfo grouper;
TS_ASSERT_EQUALS( grouper.name(), "LoadDetectorInfo" );
TS_ASSERT_EQUALS( grouper.version(), 1 );
TS_ASSERT_THROWS_NOTHING( grouper.initialize() );
TS_ASSERT( grouper.isInitialized() );
// Set up a small workspace for testing
makeTestWorkspace(SmallTestDatFile::NDETECTS, NBINS, m_InoutWS);
grouper.setPropertyValue("Workspace", m_InoutWS);
grouper.setPropertyValue("DataFilename", m_DatFile);
grouper.setPropertyValue("RelocateDets", "1");
grouper.setRethrows(true);
TS_ASSERT_THROWS_NOTHING(grouper.execute());
TS_ASSERT( grouper.isExecuted() );
MatrixWorkspace_const_sptr WS = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve(m_InoutWS));
const ParameterMap& pmap = WS->instrumentParameters();
for ( int j = 0; j < SmallTestDatFile::NDETECTS; ++j)
{
boost::shared_ptr<const IDetector> detector =WS->getInstrument()->getDetector(j);
const IComponent* baseComp = detector->getComponentID();
Parameter_sptr par = pmap.get(baseComp,"3He(atm)");
// this is only for PSD detectors, code 3
if ( code[j] == "3" )
{
TS_ASSERT(par);
TS_ASSERT_EQUALS(par->asString(), castaround(pressure[j]));
par = pmap.get(baseComp,"wallT(m)");
TS_ASSERT(par);
TS_ASSERT_EQUALS(par->asString(), castaround(wallThick[j]).substr(0,par->asString().length()));
}
else TS_ASSERT ( ! par );
const V3D pos = detector->getPos();
V3D expected;
if( j == 1 ) // Monitors are fixed and unaffected
{
expected = V3D(0,0,0);
}
else
{
expected.spherical(boost::lexical_cast<double>(det_l2[j]),
boost::lexical_cast<double>(det_theta[j]),
boost::lexical_cast<double>(det_phi[j]));
}
TS_ASSERT_EQUALS(expected, pos);
}
// ensure that the loops below are entered into
TS_ASSERT( WS->getNumberHistograms() > 0 );
TS_ASSERT( WS->readX(0).size() > 0);
// test sharing X-value arrays
const double *previous = (&(WS->dataX(0)[0]));
for (std::size_t k = 1; k < WS->getNumberHistograms(); ++k)
{
if ( k == 3 )// the third and fourth times are the same so their array should be shared
{
TS_ASSERT_EQUALS( previous, &(WS->dataX(k)[0]) );
}
else TS_ASSERT_DIFFERS( previous, &(WS->dataX(k)[0]) );
previous = &(WS->dataX(k)[0]);
}
// test x offsets
for (std::size_t x = 0; x < WS->getNumberHistograms(); x++ )
{
for (int j = 0; j < static_cast<int>(WS->readX(0).size()); j++ )
{
if ( x == DAT_MONTOR_IND )
{
TS_ASSERT_DELTA( WS->readX(x)[j], boost::lexical_cast<double>(delta[x]), 1e-6 );
}
else
{
TS_ASSERT_DELTA( WS->readX(x)[j], -boost::lexical_cast<double>(delta[x]), 1e-6 );
}
}
}
AnalysisDataService::Instance().remove(m_InoutWS);
}
void testDifferentBinsDifferentOffsets()
{
LoadDetectorInfo info;
info.initialize();
info.isInitialized();
// Set up a small workspace for testing
makeTestWorkspace(SmallTestDatFile::NDETECTS, NBINS, m_InoutWS);
info.setPropertyValue("Workspace", m_InoutWS);
info.setPropertyValue("DataFilename", m_DatFile);
MatrixWorkspace_sptr WS = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve(m_InoutWS));
//change a bin boundary, so they're not common anymore, only difference from the last test
const int alteredHist = 4, alteredBin = 1;
const double alteredAmount = 1e-4;
WS->dataX(alteredHist)[alteredBin] = WS->dataX(alteredHist)[alteredBin] + alteredAmount;
TS_ASSERT_THROWS_NOTHING(info.execute());
TS_ASSERT( info.isExecuted() );
// test x offsets
TS_ASSERT( WS->getNumberHistograms() > 0 );
for (int x = 0; x < static_cast<int>(WS->getNumberHistograms()); x++ )
{
for (int j = 0; j < static_cast<int>(WS->readX(0).size()); j++ )
{
if ( x == alteredHist && j == alteredBin)
{
TS_ASSERT_DELTA( WS->readX(x)[j], -boost::lexical_cast<double>(delta[x])+alteredAmount, 1e-6 );
continue;
}
if ( x == DAT_MONTOR_IND )
{
TS_ASSERT_DELTA( WS->readX(x)[j], boost::lexical_cast<double>(delta[x]), 1e-6 );
}
else
{
TS_ASSERT_DELTA( WS->readX(x)[j], -boost::lexical_cast<double>(delta[x]), 1e-6 );
}
}
}
AnalysisDataService::Instance().remove(m_InoutWS);
}
void testFromRaw()
{
LoadDetectorInfo grouper;
TS_ASSERT_THROWS_NOTHING( grouper.initialize() );
TS_ASSERT( grouper.isInitialized() );
loadRawFile();
MatrixWorkspace_sptr WS = boost::dynamic_pointer_cast<MatrixWorkspace>(
AnalysisDataService::Instance().retrieve(m_MariWS));
// check the X-values for a sample of spectra avoiding the monitors
const int firstIndex = 5, lastIndex = 690;
grouper.setPropertyValue("Workspace", m_MariWS);
grouper.setPropertyValue("DataFilename", m_rawFile);
grouper.setPropertyValue("RelocateDets", "1");
TS_ASSERT_THROWS_NOTHING( grouper.execute());
TS_ASSERT( grouper.isExecuted() );
ParameterMap& pmap = WS->instrumentParameters();
// read the parameters from some random detectors, they're parameters are all set to the same thing
for ( int i = 0; i < NUMRANDOM; ++i)
{
int detID = DETECTS[i];
IDetector_const_sptr detector =WS->getInstrument()->getDetector(detID);
Parameter_sptr par = pmap.get(detector.get(),"3He(atm)");
TS_ASSERT(par);
TS_ASSERT_EQUALS(par->asString(), castaround("10.0"));
par = pmap.get(detector.get(),"wallT(m)");
TS_ASSERT(par);
TS_ASSERT_EQUALS(par->asString(), castaround("0.0008").substr(0,6));
}
// Test that a random detector has been moved
IDetector_const_sptr det = WS->getInstrument()->getDetector(DETECTS[0]);
TS_ASSERT_EQUALS(V3D(0,0.2406324,4.014795), det->getPos());
// all non-monitors should share the same array
/**/ const double *first = &WS->readX(firstIndex)[0];
for (int i = firstIndex+1; i < lastIndex+1; ++i)
{
TS_ASSERT_EQUALS( first, &(WS->readX(i)[0]) );
}
// the code above proves that the X-values for each histogram are the same so just check one histogram
TS_ASSERT( WS->readX(1).size() > 0 );
// the time of flight values that matter are the differences between the detector values and the monitors
for (int j = 0; j < static_cast<int>(WS->readX(firstIndex).size()); j++ )
{// we're assuming here that the second spectrum (index 1) is a monitor
TS_ASSERT_DELTA( WS->readX(firstIndex)[j] - WS->readX(MONITOR)[j], -TIMEOFF, 1e-6 );
}
AnalysisDataService::Instance().remove(m_MariWS);
}
void testLoadNXS()
{
LoadDetectorIndoTestHelper loader;
TS_ASSERT_THROWS(loader.readNXS("NonExistingFile"),std::invalid_argument);
TS_ASSERT_THROWS_NOTHING(loader.readNXS("detector_121.nxs"));
}
void loadRawFile()
{
LoadRaw3 loader;
loader.initialize();
loader.setPropertyValue("Filename", m_rawFile);
loader.setPropertyValue("OutputWorkspace", m_MariWS);
TS_ASSERT_THROWS_NOTHING(loader.execute());
}
private:
const std::string m_InoutWS, m_DatFile, m_MariWS;
std::string m_rawFile;
enum constants { NBINS = 4, DAT_MONTOR_IND = 1};
std::string castaround(std::string floatNum)
{
return boost::lexical_cast<std::string>(boost::lexical_cast<double>(floatNum));
}
};
//-------------------------------------------------------------------------------------------------------------------------
// Performance test
//-------------------------------------------------------------------------------------------------------------------------
namespace
{
}
class LoadDetectorInfoTestPerformance : public CxxTest::TestSuite
{
public:
LoadDetectorInfoTestPerformance()
: m_testfile("LoadDetectorInfoTestPerformance_largefile.dat"),
m_wsName("LoadDetectorInfoTestPerformance")
{
}
void setUp()
{
// 100,000 histograms
const int ndets(100000);
writeLargeTestDatFile(m_testfile, ndets);
// 1000 bins
makeTestWorkspace(100000, 1000, m_wsName); //Adds it to the ADS
}
void tearDown()
{
Poco::File(m_testfile).remove();
AnalysisDataService::Instance().remove(m_wsName);
}
void test_performance_on_large_data_set()
{
LoadDetectorInfo alg;
alg.initialize();
alg.setPropertyValue("Workspace", m_wsName);
alg.setPropertyValue("DataFilename", m_testfile);
alg.setPropertyValue("RelocateDets", "1");
alg.execute();
}
private:
std::string m_testfile;
std::string m_wsName;
};
#endif /*LOADDETECTORINFOTEST_H_*/