-
Notifications
You must be signed in to change notification settings - Fork 121
/
LoadTOFRawNexus.cpp
569 lines (502 loc) · 19.3 KB
/
LoadTOFRawNexus.cpp
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
#include "MantidDataHandling/LoadTOFRawNexus.h"
#include "MantidDataHandling/LoadEventNexus.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/Strings.h"
#include "MantidKernel/cow_ptr.h"
#include <nexus/NeXusFile.hpp>
#include <boost/algorithm/string/detail/classification.hpp>
#include <boost/algorithm/string/split.hpp>
namespace Mantid {
namespace DataHandling {
DECLARE_NEXUS_FILELOADER_ALGORITHM(LoadTOFRawNexus)
using namespace Kernel;
using namespace API;
using namespace DataObjects;
LoadTOFRawNexus::LoadTOFRawNexus()
: m_numPixels(0), m_signalNo(0), pulseTimes(0), m_numBins(0), m_spec_min(0),
m_spec_max(0), m_dataField(""), m_axisField(""), m_xUnits(""),
m_fileMutex(), m_assumeOldFile(false) {}
/// Initialisation method.
void LoadTOFRawNexus::init() {
declareProperty(
make_unique<FileProperty>("Filename", "", FileProperty::Load, ".nxs"),
"The name of the NeXus file to load");
declareProperty(make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The name of the Workspace2D to create.");
declareProperty("Signal", 1,
"Number of the signal to load from the file. Default is 1 = "
"time_of_flight.\n"
"Some NXS files have multiple data fields giving binning in "
"other units (e.g. d-spacing or momentum).\n"
"Enter the right signal number for your desired field.");
auto mustBePositive = boost::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(1);
declareProperty(
make_unique<PropertyWithValue<specnum_t>>("SpectrumMin", 1,
mustBePositive),
"The index number of the first spectrum to read. Only used if\n"
"spectrum_max is set.");
declareProperty(
make_unique<PropertyWithValue<specnum_t>>(
"SpectrumMax", Mantid::EMPTY_INT(), mustBePositive),
"The number of the last spectrum to read. Only used if explicitly\n"
"set.");
}
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not
* be used
*/
int LoadTOFRawNexus::confidence(Kernel::NexusDescriptor &descriptor) const {
int confidence(0);
if (descriptor.pathOfTypeExists("/entry", "NXentry") ||
descriptor.pathOfTypeExists("/entry-state0", "NXentry")) {
const bool hasEventData = descriptor.classTypeExists("NXevent_data");
const bool hasData = descriptor.classTypeExists("NXdata");
if (hasData && hasEventData)
// Event data = this is event NXS
confidence = 20;
else if (hasData && !hasEventData)
// No event data = this is the one
confidence = 80;
else
// No data ?
confidence = 10;
}
return confidence;
}
/** Goes thoguh a histogram NXS file and counts the number of pixels.
* It also determines the name of the data field and axis to load
*
* @param nexusfilename :: nxs file path
* @param entry_name :: name of the entry
* @param bankNames :: returns the list of bank names
*/
void LoadTOFRawNexus::countPixels(const std::string &nexusfilename,
const std::string &entry_name,
std::vector<std::string> &bankNames) {
m_numPixels = 0;
m_numBins = 0;
m_dataField = "";
m_axisField = "";
bankNames.clear();
// Create the root Nexus class
auto file = new ::NeXus::File(nexusfilename);
// Open the default data group 'entry'
file->openGroup(entry_name, "NXentry");
// Also pop into the instrument
file->openGroup("instrument", "NXinstrument");
// Look for all the banks
std::map<std::string, std::string> entries = file->getEntries();
std::map<std::string, std::string>::iterator it;
for (it = entries.begin(); it != entries.end(); ++it) {
std::string name = it->first;
if (name.size() > 4) {
if (name.substr(0, 4) == "bank") {
// OK, this is some bank data
file->openGroup(name, it->second);
// -------------- Find the data field name ----------------------------
if (m_dataField.empty()) {
std::map<std::string, std::string> entries = file->getEntries();
std::map<std::string, std::string>::iterator it;
for (it = entries.begin(); it != entries.end(); ++it) {
if (it->second == "SDS") {
file->openData(it->first);
if (file->hasAttr("signal")) {
int signal = 0;
file->getAttr("signal", signal);
if (signal == m_signalNo) {
// That's the right signal!
m_dataField = it->first;
// Find the corresponding X axis
std::string axes;
m_assumeOldFile = false;
if (!file->hasAttr("axes")) {
if (1 != m_signalNo) {
throw std::runtime_error(
"Your chosen signal number, " +
Strings::toString(m_signalNo) +
", corresponds to the data field '" + m_dataField +
"' has no 'axes' attribute specifying.");
} else {
m_assumeOldFile = true;
axes = "x_pixel_offset,y_pixel_offset,time_of_flight";
}
}
if (!m_assumeOldFile) {
file->getAttr("axes", axes);
}
std::vector<std::string> allAxes;
boost::split(allAxes, axes,
boost::algorithm::detail::is_any_ofF<char>(","));
if (allAxes.size() != 3)
throw std::runtime_error(
"Your chosen signal number, " +
Strings::toString(m_signalNo) +
", corresponds to the data field '" + m_dataField +
"' which has only " +
Strings::toString(allAxes.size()) +
" dimension. Expected 3 dimensions.");
m_axisField = allAxes.back();
g_log.information() << "Loading signal " << m_signalNo << ", "
<< m_dataField << " with axis "
<< m_axisField << '\n';
file->closeData();
break;
} // Data has a 'signal' attribute
} // Yes, it is a data field
file->closeData();
} // each entry in the group
}
}
file->closeGroup();
} // bankX name
}
} // each entry
if (m_dataField.empty())
throw std::runtime_error("Your chosen signal number, " +
Strings::toString(m_signalNo) +
", was not found in any of the data fields of any "
"'bankX' group. Cannot load file.");
for (it = entries.begin(); it != entries.end(); ++it) {
std::string name = it->first;
if (name.size() > 4) {
if (name.substr(0, 4) == "bank") {
// OK, this is some bank data
file->openGroup(name, it->second);
std::map<std::string, std::string> entries = file->getEntries();
if (entries.find("pixel_id") != entries.end()) {
bankNames.push_back(name);
// Count how many pixels in the bank
file->openData("pixel_id");
std::vector<int64_t> dims = file->getInfo().dims;
file->closeData();
if (!dims.empty()) {
size_t newPixels = 1;
for (auto dim : dims)
newPixels *= dim;
m_numPixels += newPixels;
}
} else {
bankNames.push_back(name);
// Get the number of pixels from the offsets arrays
file->openData("x_pixel_offset");
std::vector<int64_t> xdim = file->getInfo().dims;
file->closeData();
file->openData("y_pixel_offset");
std::vector<int64_t> ydim = file->getInfo().dims;
file->closeData();
if (!xdim.empty() && !ydim.empty()) {
m_numPixels += (xdim[0] * ydim[0]);
}
}
if (entries.find(m_axisField) != entries.end()) {
// Get the size of the X vector
file->openData(m_axisField);
std::vector<int64_t> dims = file->getInfo().dims;
// Find the units, if available
if (file->hasAttr("units"))
file->getAttr("units", m_xUnits);
else
m_xUnits = "microsecond"; // use default
file->closeData();
if (!dims.empty())
m_numBins = dims[0] - 1;
}
file->closeGroup();
} // bankX name
}
} // each entry
file->close();
delete file;
}
/*for (std::vector<uint32_t>::iterator it = pixel_id.begin(); it !=
pixel_id.end();)
{
detid_t pixelID = *it;
specnum_t wi = static_cast<specnum_t>((*id_to_wi)[pixelID]);
// spectrum is just wi+1
if (wi+1 < m_spec_min || wi+1 > m_spec_max) pixel_id.erase(it);
else ++it;
}*/
// Function object for remove_if STL algorithm
namespace {
// Check the numbers supplied are not in the range and erase the ones that are
struct range_check {
range_check(specnum_t min, specnum_t max, detid2index_map id_to_wi)
: m_min(min), m_max(max), m_id_to_wi(id_to_wi) {}
bool operator()(specnum_t x) {
specnum_t wi = static_cast<specnum_t>((m_id_to_wi)[x]);
return (wi + 1 < m_min || wi + 1 > m_max);
}
private:
specnum_t m_min;
specnum_t m_max;
detid2index_map m_id_to_wi;
};
}
/** Load a single bank into the workspace
*
* @param nexusfilename :: file to open
* @param entry_name :: NXentry name
* @param bankName :: NXdata bank name
* @param WS :: workspace to modify
* @param id_to_wi :: det ID to workspace index mapping
*/
void LoadTOFRawNexus::loadBank(const std::string &nexusfilename,
const std::string &entry_name,
const std::string &bankName,
API::MatrixWorkspace_sptr WS,
const detid2index_map &id_to_wi) {
g_log.debug() << "Loading bank " << bankName << '\n';
// To avoid segfaults on RHEL5/6 and Fedora
m_fileMutex.lock();
// Navigate to the point in the file
auto file = new ::NeXus::File(nexusfilename);
file->openGroup(entry_name, "NXentry");
file->openGroup("instrument", "NXinstrument");
file->openGroup(bankName, "NXdetector");
size_t m_numPixels = 0;
std::vector<uint32_t> pixel_id;
if (!m_assumeOldFile) {
// Load the pixel IDs
file->readData("pixel_id", pixel_id);
m_numPixels = pixel_id.size();
if (m_numPixels == 0) {
file->close();
m_fileMutex.unlock();
g_log.warning() << "Invalid pixel_id data in " << bankName << '\n';
return;
}
} else {
// Load the x and y pixel offsets
std::vector<float> xoffsets;
std::vector<float> yoffsets;
file->readData("x_pixel_offset", xoffsets);
file->readData("y_pixel_offset", yoffsets);
m_numPixels = xoffsets.size() * yoffsets.size();
if (0 == m_numPixels) {
file->close();
m_fileMutex.unlock();
g_log.warning() << "Invalid (x,y) offsets in " << bankName << '\n';
return;
}
size_t bankNum = 0;
if (bankName.size() > 4) {
if (bankName.substr(0, 4) == "bank") {
bankNum = boost::lexical_cast<size_t>(bankName.substr(4));
bankNum--;
} else {
file->close();
m_fileMutex.unlock();
g_log.warning() << "Invalid bank number for " << bankName << '\n';
return;
}
}
// All good, so construct the pixel ID listing
size_t numX = xoffsets.size();
size_t numY = yoffsets.size();
for (size_t i = 0; i < numX; i++) {
for (size_t j = 0; j < numY; j++) {
pixel_id.push_back(
static_cast<uint32_t>(j + numY * (i + numX * bankNum)));
}
}
}
size_t iPart = 0;
if (m_spec_max != Mantid::EMPTY_INT()) {
uint32_t ifirst = pixel_id[0];
range_check out_range(m_spec_min, m_spec_max, id_to_wi);
auto newEnd = std::remove_if(pixel_id.begin(), pixel_id.end(), out_range);
pixel_id.erase(newEnd, pixel_id.end());
// check if beginning or end of array was erased
if (ifirst != pixel_id[0])
iPart = m_numPixels - pixel_id.size();
m_numPixels = pixel_id.size();
if (m_numPixels == 0) {
file->close();
m_fileMutex.unlock();
g_log.warning() << "No pixels from " << bankName << '\n';
return;
};
}
// Load the TOF vector
std::vector<float> tof;
file->readData(m_axisField, tof);
size_t m_numBins = tof.size() - 1;
if (tof.size() <= 1) {
file->close();
m_fileMutex.unlock();
g_log.warning() << "Invalid " << m_axisField << " data in " << bankName
<< '\n';
return;
}
HistogramData::BinEdges X(tof.begin(), tof.end());
// Load the data. Coerce ints into double.
std::string errorsField;
std::vector<double> data;
file->openData(m_dataField);
file->getDataCoerce(data);
if (file->hasAttr("errors"))
file->getAttr("errors", errorsField);
file->closeData();
// Load the errors
bool hasErrors = !errorsField.empty();
std::vector<double> errors;
if (hasErrors) {
try {
file->openData(errorsField);
file->getDataCoerce(errors);
file->closeData();
} catch (...) {
g_log.information() << "Error loading the errors field, '" << errorsField
<< "' for bank " << bankName
<< ". Will use sqrt(counts). \n";
hasErrors = false;
}
}
/*if (data.size() != m_numBins * m_numPixels)
{ file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid size of '"
<< m_dataField << "' data in " << bankName << '\n'; return; }
if (hasErrors && (errors.size() != m_numBins * m_numPixels))
{ file->close(); m_fileMutex.unlock(); g_log.warning() << "Invalid size of '"
<< errorsField << "' errors in " << bankName << '\n'; return; }
*/
// Have all the data I need
m_fileMutex.unlock();
file->close();
for (size_t i = iPart; i < iPart + m_numPixels; i++) {
// Find the workspace index for this detector
detid_t pixelID = pixel_id[i - iPart];
size_t wi = id_to_wi.find(pixelID)->second;
// Set the basic info of that spectrum
auto &spec = WS->getSpectrum(wi);
spec.setSpectrumNo(specnum_t(wi + 1));
spec.setDetectorID(pixel_id[i - iPart]);
// Set the shared X pointer
spec.setBinEdges(X);
// Extract the Y
MantidVec &Y = spec.dataY();
Y.assign(data.begin() + i * m_numBins, data.begin() + (i + 1) * m_numBins);
MantidVec &E = spec.dataE();
if (hasErrors) {
// Copy the errors from the loaded document
E.assign(errors.begin() + i * m_numBins,
errors.begin() + (i + 1) * m_numBins);
} else {
// Now take the sqrt(Y) to give E
E = MantidVec();
std::transform(Y.begin(), Y.end(), std::back_inserter(E),
static_cast<double (*)(double)>(sqrt));
}
}
// Done!
}
/** @return the name of the entry that we will load */
std::string LoadTOFRawNexus::getEntryName(const std::string &filename) {
std::string entry_name = "entry";
auto file = new ::NeXus::File(filename);
std::map<std::string, std::string> entries = file->getEntries();
file->close();
delete file;
if (entries.empty())
throw std::runtime_error("No entries in the NXS file!");
// name "entry" is normal, but "entry-state0" is the name of the real state
// for live nexus files.
if (entries.find(entry_name) == entries.end())
entry_name = "entry-state0";
// If that doesn't exist, just take the first entry.
if (entries.find(entry_name) == entries.end())
entry_name = entries.begin()->first;
// // Tell the user
// if (entries.size() > 1)
// g_log.notice() << "There are " << entries.size() << " NXentry's in the
// file. Loading entry '" << entry_name << "' only.\n";
return entry_name;
}
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*
* @throw Exception::FileError If the Nexus file cannot be found/opened
* @throw std::invalid_argument If the optional properties are set to invalid
*values
*/
void LoadTOFRawNexus::exec() {
// The input properties
std::string filename = getPropertyValue("Filename");
m_signalNo = getProperty("Signal");
m_spec_min = getProperty("SpectrumMin");
m_spec_max = getProperty("SpectrumMax");
// Find the entry name we want.
std::string entry_name = LoadTOFRawNexus::getEntryName(filename);
// Count pixels and other setup
auto prog = new Progress(this, 0.0, 1.0, 10);
prog->doReport("Counting pixels");
std::vector<std::string> bankNames;
countPixels(filename, entry_name, bankNames);
g_log.debug() << "Workspace found to have " << m_numPixels << " pixels and "
<< m_numBins << " bins\n";
prog->setNumSteps(bankNames.size() + 5);
prog->doReport("Creating workspace");
// Start with a dummy WS just to hold the logs and load the instrument
MatrixWorkspace_sptr WS = WorkspaceFactory::Instance().create(
"Workspace2D", m_numPixels, m_numBins + 1, m_numBins);
// Load the logs
prog->doReport("Loading DAS logs");
g_log.debug() << "Loading DAS logs\n";
int nPeriods = 1; // Unused
auto periodLog =
make_unique<const TimeSeriesProperty<int>>("period_log"); // Unused
LoadEventNexus::runLoadNexusLogs<MatrixWorkspace_sptr>(
filename, WS, *this, false, nPeriods, periodLog);
// Load the instrument
prog->report("Loading instrument");
g_log.debug() << "Loading instrument\n";
LoadEventNexus::runLoadInstrument<MatrixWorkspace_sptr>(filename, WS,
entry_name, this);
// Load the meta data, but don't stop on errors
prog->report("Loading metadata");
g_log.debug() << "Loading metadata\n";
try {
LoadEventNexus::loadEntryMetadata(filename, WS, entry_name);
} catch (std::exception &e) {
g_log.warning() << "Error while loading meta data: " << e.what() << '\n';
}
// Set the spectrum number/detector ID at each spectrum. This is consistent
// with LoadEventNexus for non-ISIS files.
prog->report("Building Spectra Mapping");
g_log.debug() << "Building Spectra Mapping\n";
WS->rebuildSpectraMapping(false);
// And map ID to WI
g_log.debug() << "Mapping ID to WI\n";
const auto id_to_wi = WS->getDetectorIDToWorkspaceIndexMap();
// Load each bank sequentially
for (const auto &bankName : bankNames) {
prog->report("Loading bank " + bankName);
g_log.debug() << "Loading bank " << bankName << '\n';
loadBank(filename, entry_name, bankName, WS, id_to_wi);
}
// Set some units
if (m_xUnits == "Ang")
WS->getAxis(0)->setUnit("dSpacing");
else if (m_xUnits == "invAng")
WS->getAxis(0)->setUnit("MomentumTransfer");
else
// Default to TOF for any other string
WS->getAxis(0)->setUnit("TOF");
WS->setYUnit("Counts");
// Set to the output
setProperty("OutputWorkspace", WS);
delete prog;
}
} // namespace DataHandling
} // namespace Mantid