/
LoadILL.cpp
579 lines (472 loc) · 19.3 KB
/
LoadILL.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
570
571
572
573
574
575
576
577
578
579
/*WIKI*
Loads an ILL TOF NeXus file into a [[Workspace2D]] with the given name.
This loader calculates the elastic peak position (EPP) on the fly.
In cases where the dispersion peak might be higher than the EPP, it is good practice to load a Vanadium file.
The property FilenameVanadium is optional. If it is present the EPP will be loaded from the Vanadium data.
To date this algorithm only supports: IN4, IN5 and IN6
*WIKI*/
//---------------------------------------------------
// Includes
//---------------------------------------------------
#include "MantidDataHandling/LoadILL.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/Progress.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/EmptyValues.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidDataHandling/LoadHelper.h"
#include <limits>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
namespace Mantid
{
namespace DataHandling
{
using namespace Kernel;
using namespace API;
using namespace NeXus;
DECLARE_NEXUS_FILELOADER_ALGORITHM(LoadILL);
;
/**
* tostring operator to print the contents of NXClassInfo
*
* TODO : This has to go somewhere else
*/
std::ostream& operator<<(std::ostream &strm, const NXClassInfo &c)
{
return strm << "NXClassInfo :: nxname: " << c.nxname << " , nxclass: "
<< c.nxclass;
}
/// Sets documentation strings for this algorithm
void LoadILL::initDocs()
{
this->setWikiSummary("Loads a ILL nexus file. ");
this->setOptionalMessage("Loads a ILL nexus file.");
}
/**
* 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 LoadILL::confidence(Kernel::NexusDescriptor & descriptor) const
{
// fields existent only at the ILL
if (descriptor.pathExists("/entry0/wavelength")
&& descriptor.pathExists("/entry0/experiment_identifier")
&& descriptor.pathExists("/entry0/mode")) {
return 80;
}
else
{
return 0;
}
}
//---------------------------------------------------
// Private member functions
//---------------------------------------------------
LoadILL::LoadILL() :
API::IFileLoader<Kernel::NexusDescriptor>()
{
m_instrumentName = "";
m_wavelength = 0;
m_channelWidth = 0;
m_numberOfChannels = 0;
m_numberOfHistograms = 0;
m_numberOfTubes = 0;
m_numberOfPixelsPerTube = 0;
m_monitorElasticPeakPosition = 0;
m_l1 = 0;
m_l2 = 0;
m_supportedInstruments.push_back("IN4");
m_supportedInstruments.push_back("IN5");
m_supportedInstruments.push_back("IN6");
}
/**
* Initialise the algorithm
*/
void LoadILL::init()
{
declareProperty(
new FileProperty("Filename", "", FileProperty::Load, ".nxs"),
"File path of the Data file to load");
declareProperty(
new FileProperty("FilenameVanadium", "", FileProperty::OptionalLoad, ".nxs"),
"File path of the Vanadium file to load (Optional)");
declareProperty(
new WorkspaceProperty<>("OutputWorkspace", "", Direction::Output),
"The name to use for the output workspace");
}
/**
* Execute the algorithm
*/
void LoadILL::exec()
{
// Retrieve filename
std::string filenameData = getPropertyValue("Filename");
std::string filenameVanadium = getPropertyValue("FilenameVanadium");
// open the root node
NeXus::NXRoot dataRoot(filenameData);
NXEntry dataFirstEntry = dataRoot.openFirstEntry();
loadInstrumentDetails(dataFirstEntry);
loadTimeDetails(dataFirstEntry);
initWorkSpace(dataFirstEntry);
runLoadInstrument(); // just to get IDF contents
initInstrumentSpecific();
int calculatedDetectorElasticPeakPosition = -1;
if (filenameVanadium != "") {
g_log.information() << "Calculating the elastic peak position from the Vanadium." << std::endl;
calculatedDetectorElasticPeakPosition = validateVanadium(filenameVanadium);
}
loadDataIntoTheWorkSpace(dataFirstEntry,calculatedDetectorElasticPeakPosition);
loadRunDetails(dataFirstEntry);
loadExperimentDetails(dataFirstEntry);
// load the instrument from the IDF if it exists
runLoadInstrument();
// Set the output workspace property
setProperty("OutputWorkspace", m_localWorkspace);
}
/**
*
*/
void LoadILL::loadInstrumentDetails(NeXus::NXEntry& firstEntry) {
m_instrumentPath = m_loader.findInstrumentNexusPath(firstEntry);
if (m_instrumentPath == "") {
throw std::runtime_error(
"Cannot set the instrument name from the Nexus file!");
}
m_instrumentName = m_loader.getStringFromNexusPath(firstEntry,
m_instrumentPath + "/name");
if (std::find(m_supportedInstruments.begin(), m_supportedInstruments.end(),
m_instrumentName) == m_supportedInstruments.end()) {
std::string message = "The instrument " + m_instrumentName + " is not valid for this loader!";
throw std::runtime_error(message);
}
g_log.debug() << "Instrument name set to: " + m_instrumentName << std::endl;
}
/**
* Creates the workspace and initialises member variables with
* the corresponding values
*
* @param entry :: The Nexus entry
*
*/
void LoadILL::initWorkSpace(NeXus::NXEntry& entry)
{
// read in the data
NXData dataGroup = entry.openNXData("data");
NXInt data = dataGroup.openIntData();
m_numberOfTubes = static_cast<size_t>(data.dim0());
m_numberOfPixelsPerTube = static_cast<size_t>(data.dim1());
m_numberOfChannels = static_cast<size_t>(data.dim2());
// dim0 * m_numberOfPixelsPerTube is the total number of detectors
m_numberOfHistograms = m_numberOfTubes * m_numberOfPixelsPerTube;
g_log.debug() << "NumberOfTubes: " << m_numberOfTubes << std::endl;
g_log.debug() << "NumberOfPixelsPerTube: " << m_numberOfPixelsPerTube
<< std::endl;
g_log.debug() << "NumberOfChannels: " << m_numberOfChannels << std::endl;
// Now create the output workspace
// Might need to get this value from the number of monitors in the Nexus file
// params:
// workspace type,
// total number of spectra + (number of monitors = 0),
// bin boundaries = m_numberOfChannels + 1
// Z/time dimension
m_localWorkspace = WorkspaceFactory::Instance().create("Workspace2D",
m_numberOfHistograms, m_numberOfChannels + 1, m_numberOfChannels);
m_localWorkspace->getAxis(0)->unit() = UnitFactory::Instance().create(
"TOF");
m_localWorkspace->setYUnitLabel("Counts");
}
/**
* Function to do specific instrument stuff
*
*/
void LoadILL::initInstrumentSpecific()
{
m_l1 = m_loader.getL1(m_localWorkspace);
// this will be mainly for IN5 (flat PSD detector)
m_l2 = m_loader.getInstrumentProperty(m_localWorkspace,"l2");
if (m_l2 == EMPTY_DBL()) {
g_log.debug("Calculating L2 from the IDF.");
m_l2 = m_loader.getL2(m_localWorkspace);
}
}
/**
* Load the time details from the nexus file.
* @param entry :: The Nexus entry
*/
void LoadILL::loadTimeDetails(NeXus::NXEntry& entry)
{
m_wavelength = entry.getFloat("wavelength");
// Monitor can be monitor (IN5) or monitor1 (IN6)
std::string monitorName;
if (entry.containsGroup("monitor"))
monitorName = "monitor";
else if (entry.containsGroup("monitor1"))
monitorName = "monitor1";
else
{
std::string message("Cannot find monitor[1] in the Nexus file!");
g_log.error(message);
throw std::runtime_error(message);
}
m_monitorElasticPeakPosition = entry.getInt(monitorName + "/elasticpeak");
NXFloat time_of_flight_data = entry.openNXFloat(
monitorName + "/time_of_flight");
time_of_flight_data.load();
// The entry "monitor/time_of_flight", has 3 fields:
// channel width , number of channels, Time of flight delay
m_channelWidth = time_of_flight_data[0];
// m_timeOfFlightDelay = time_of_flight_data[2];
g_log.debug("Nexus Data:");
g_log.debug() << " ChannelWidth: " << m_channelWidth << std::endl;
g_log.debug() << " Wavelength: " << m_wavelength << std::endl;
g_log.debug() << " ElasticPeakPosition: " << m_monitorElasticPeakPosition
<< std::endl;
}
/**
* Load information about the run.
* People from ISIs have this... Let's do the same for the ILL.
* TODO: They also have a lot of info in XML format!
*
* @param entry :: The Nexus entry
*/
void LoadILL::loadRunDetails(NXEntry & entry)
{
API::Run & runDetails = m_localWorkspace->mutableRun();
int runNum = entry.getInt("run_number");
std::string run_num = boost::lexical_cast<std::string>(runNum);
runDetails.addProperty("run_number", run_num);
std::string start_time = entry.getString("start_time");
start_time = m_loader.dateTimeInIsoFormat(start_time);
runDetails.addProperty("run_start", start_time);
std::string end_time = entry.getString("end_time");
end_time = m_loader.dateTimeInIsoFormat(end_time);
runDetails.addProperty("run_end", end_time);
//m_wavelength = entry.getFloat("wavelength");
std::string wavelength = boost::lexical_cast<std::string>(m_wavelength);
//runDetails.addProperty<double>("wavelength", m_wavelength);
runDetails.addProperty("wavelength", wavelength);
double ei = m_loader.calculateEnergy(m_wavelength);
runDetails.addProperty<double>("Ei", ei, true); //overwrite
//std::string ei_str = boost::lexical_cast<std::string>(ei);
//runDetails.addProperty("Ei", ei_str);
std::string duration = boost::lexical_cast<std::string>(
entry.getFloat("duration"));
runDetails.addProperty("duration", duration);
std::string preset = boost::lexical_cast<std::string>(
entry.getFloat("preset"));
runDetails.addProperty("preset", preset);
std::string mode = entry.getString("mode");
runDetails.addProperty("mode", mode);
std::string title = entry.getString("title");
runDetails.addProperty("title", title);
m_localWorkspace->setTitle(title);
// Below : This should belong to sample ???
std::string experiment_identifier = entry.getString(
"experiment_identifier");
runDetails.addProperty("experiment_identifier", experiment_identifier);
m_localWorkspace->mutableSample().setName(experiment_identifier);
std::string temperature = boost::lexical_cast<std::string>(
entry.getFloat("sample/temperature"));
runDetails.addProperty("temperature", temperature);
}
/*
* Load data about the Experiment.
*
* TODO: This is very incomplete. In ISIS they much more info in the nexus file than ILL.
*
* @param entry :: The Nexus entry
*/
void LoadILL::loadExperimentDetails(NXEntry & entry)
{
// TODO: Do the rest
// Pick out the geometry information
std::string description = boost::lexical_cast<std::string>(
entry.getFloat("sample/description"));
m_localWorkspace->mutableSample().setName(description);
// m_localWorkspace->mutableSample().setThickness(static_cast<double> (isis_raw->spb.e_thick));
// m_localWorkspace->mutableSample().setHeight(static_cast<double> (isis_raw->spb.e_height));
// m_localWorkspace->mutableSample().setWidth(static_cast<double> (isis_raw->spb.e_width));
}
/**
* Gets the experimental Elastic Peak Position in the dectector
* as the value parsed from the nexus file might be wrong.
*
* It gets a few spectra in the equatorial line of the detector,
* sum them up and finds the maximum = the Elastic peak
*
*
* @param data :: spectra data
* @return detector Elastic Peak Position
*/
int LoadILL::getDetectorElasticPeakPosition(const NeXus::NXInt &data)
{
// j = index in the equatorial line (256/2=128)
// both index 127 and 128 are in the equatorial line
size_t j = m_numberOfPixelsPerTube / 2;
// ignore the first tubes and the last ones to avoid the beamstop
//get limits in the m_numberOfTubes
size_t tubesToRemove = m_numberOfTubes / 7;
std::vector<int> cumulatedSumOfSpectras(m_numberOfChannels, 0);
for (size_t i = tubesToRemove; i < m_numberOfTubes - tubesToRemove; i++) {
int* data_p = &data(static_cast<int>(i), static_cast<int>(j), 0);
std::vector<int> thisSpectrum(data_p, data_p + m_numberOfChannels);
// sum spectras
std::transform(thisSpectrum.begin(), thisSpectrum.end(),
cumulatedSumOfSpectras.begin(), cumulatedSumOfSpectras.begin(),
std::plus<int>());
}
auto it = std::max_element(cumulatedSumOfSpectras.begin(),
cumulatedSumOfSpectras.end());
int calculatedDetectorElasticPeakPosition;
if (it == cumulatedSumOfSpectras.end())
{
g_log.warning()
<< "No Elastic peak position found! Assuming the EPP in the Nexus file: "
<< m_monitorElasticPeakPosition << std::endl;
calculatedDetectorElasticPeakPosition = m_monitorElasticPeakPosition;
}
else
{
//calculatedDetectorElasticPeakPosition = *it;
calculatedDetectorElasticPeakPosition = static_cast<int>(std::distance(
cumulatedSumOfSpectras.begin(), it));
if (calculatedDetectorElasticPeakPosition == 0)
{
g_log.warning()
<< "Elastic peak position is ZERO Assuming the EPP in the Nexus file: "
<< m_monitorElasticPeakPosition << std::endl;
calculatedDetectorElasticPeakPosition =
m_monitorElasticPeakPosition;
}
else
{
g_log.debug() << "Calculated Detector EPP: "
<< calculatedDetectorElasticPeakPosition;
g_log.debug() << " :: Read EPP from the nexus file: "
<< m_monitorElasticPeakPosition << std::endl;
}
}
return calculatedDetectorElasticPeakPosition;
}
/**
* It loads the vanadium nexus file and cross checks it against the
* data file already loaded (same wavelength and same instrument configuration).
* If matches looks for the elastic peak in the vanadium file and returns
* it position.
*
* @param filenameVanadium :: The path for the vanadium nexus file.
* @return The elastic peak position inside the tof channels.
*/
int LoadILL::validateVanadium(const std::string &filenameVanadium) {
NeXus::NXRoot vanaRoot(filenameVanadium);
NXEntry vanaFirstEntry = vanaRoot.openFirstEntry();
double wavelength = vanaFirstEntry.getFloat("wavelength");
// read in the data
NXData dataGroup = vanaFirstEntry.openNXData("data");
NXInt data = dataGroup.openIntData();
size_t numberOfTubes = static_cast<size_t>(data.dim0());
size_t numberOfPixelsPerTube = static_cast<size_t>(data.dim1());
size_t numberOfChannels = static_cast<size_t>(data.dim2());
if (wavelength != m_wavelength || numberOfTubes != m_numberOfTubes
|| numberOfPixelsPerTube != m_numberOfPixelsPerTube
|| numberOfChannels != m_numberOfChannels) {
throw std::runtime_error(
"Vanadium and Data were not collected in the same conditions!");
}
data.load();
int calculatedDetectorElasticPeakPosition = getDetectorElasticPeakPosition(
data);
return calculatedDetectorElasticPeakPosition;
}
/**
* Loads all the spectra into the workspace, including that from the monitor
*
* @param entry :: The Nexus entry
* @param vanaCalculatedDetectorElasticPeakPosition :: If -1 uses this value as the elastic peak position at the detector.
*/
void LoadILL::loadDataIntoTheWorkSpace(NeXus::NXEntry& entry, int vanaCalculatedDetectorElasticPeakPosition)
{
// read in the data
NXData dataGroup = entry.openNXData("data");
NXInt data = dataGroup.openIntData();
// load the counts from the file into memory
data.load();
/*
* Detector: Find real elastic peak in the detector.
* Looks for a few elastic peaks on the equatorial line of the detector.
*/
int calculatedDetectorElasticPeakPosition;
if (vanaCalculatedDetectorElasticPeakPosition == -1)
calculatedDetectorElasticPeakPosition = getDetectorElasticPeakPosition(data);
else
calculatedDetectorElasticPeakPosition = vanaCalculatedDetectorElasticPeakPosition;
double theoreticalElasticTOF = (m_loader.calculateTOF(m_l1,m_wavelength) + m_loader.calculateTOF(m_l2,m_wavelength))
* 1e6; //microsecs
// Calculate the real tof (t1+t2) put it in tof array
std::vector<double> detectorTofBins(m_numberOfChannels + 1);
for (size_t i = 0; i < m_numberOfChannels + 1; ++i)
{
detectorTofBins[i] = theoreticalElasticTOF
+ m_channelWidth
* static_cast<double>(static_cast<int>(i)
- calculatedDetectorElasticPeakPosition)
- m_channelWidth / 2; // to make sure the bin is in the middle of the elastic peak
}
//g_log.debug() << "Detector TOF bins: ";
//for (auto i : detectorTofBins) g_log.debug() << i << " ";
//g_log.debug() << "\n";
g_log.information() << "T1+T2 : Theoretical = " << theoreticalElasticTOF;
g_log.information() << " :: Calculated bin = ["
<< detectorTofBins[calculatedDetectorElasticPeakPosition] << ","
<< detectorTofBins[calculatedDetectorElasticPeakPosition + 1] << "]"
<< std::endl;
// Assign calculated bins to first X axis
m_localWorkspace->dataX(0).assign(detectorTofBins.begin(),
detectorTofBins.end());
Progress progress(this, 0, 1, m_numberOfTubes * m_numberOfPixelsPerTube);
size_t spec = 0;
for (size_t i = 0; i < m_numberOfTubes; ++i)
{
for (size_t j = 0; j < m_numberOfPixelsPerTube; ++j)
{
if (spec > 0)
{
// just copy the time binning axis to every spectra
m_localWorkspace->dataX(spec) = m_localWorkspace->readX(0);
}
// Assign Y
int* data_p = &data(static_cast<int>(i), static_cast<int>(j), 0);
m_localWorkspace->dataY(spec).assign(data_p,
data_p + m_numberOfChannels);
// Assign Error
MantidVec& E = m_localWorkspace->dataE(spec);
std::transform(data_p, data_p + m_numberOfChannels, E.begin(),
LoadILL::calculateError);
++spec;
progress.report();
}
}
}
/**
* Run the Child Algorithm LoadInstrument.
*/
void LoadILL::runLoadInstrument() {
IAlgorithm_sptr loadInst = createChildAlgorithm("LoadInstrument");
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
try {
// TODO: depending on the m_numberOfPixelsPerTube we might need to load a different IDF
loadInst->setPropertyValue("InstrumentName", m_instrumentName);
loadInst->setProperty<MatrixWorkspace_sptr>("Workspace",
m_localWorkspace);
loadInst->execute();
} catch (...) {
g_log.information("Cannot load the instrument definition.");
}
}
} // namespace DataHandling
} // namespace Mantid