-
Notifications
You must be signed in to change notification settings - Fork 122
/
CreateSimulationWorkspace.cpp
411 lines (349 loc) · 14.9 KB
/
CreateSimulationWorkspace.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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidDataHandling/CreateSimulationWorkspace.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataHandling/StartAndEndTimeFromNexusFileExtractor.h"
#include "MantidDataHandling/LoadRawHelper.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/OptionalBool.h"
#include "MantidKernel/RebinParamsValidator.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/VectorHelper.h"
#include "LoadRaw/isisraw2.h"
// clang-format off
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
// clang-format on
#include <Poco/File.h>
#include <boost/algorithm/string/predicate.hpp>
namespace {
struct StartAndEndTime {
Mantid::Types::Core::DateAndTime startTime;
Mantid::Types::Core::DateAndTime endTime;
};
StartAndEndTime getStartAndEndTimesFromRawFile(const std::string &filename) {
FILE *rawFile = fopen(filename.c_str(), "rb");
if (!rawFile)
throw std::runtime_error("Cannot open RAW file for reading: " + filename);
ISISRAW2 isisRaw;
const bool fromFile(true), readData(false);
isisRaw.ioRAW(rawFile, fromFile, readData);
StartAndEndTime startAndEndTime;
startAndEndTime.startTime = Mantid::DataHandling::LoadRawHelper::extractStartTime(isisRaw);
startAndEndTime.endTime = Mantid::DataHandling::LoadRawHelper::extractEndTime(isisRaw);
fclose(rawFile);
return startAndEndTime;
}
StartAndEndTime getStartAndEndTimesFromNexusFile(const std::string &filename,
const Mantid::Types::Core::DateAndTime &startTimeDefault,
const Mantid::Types::Core::DateAndTime &endTimeDefault) {
StartAndEndTime startAndEndTime;
try {
startAndEndTime.startTime = Mantid::DataHandling::extractStartTime(filename);
startAndEndTime.endTime = Mantid::DataHandling::extractEndTime(filename);
} catch (...) {
startAndEndTime.startTime = startTimeDefault;
startAndEndTime.endTime = endTimeDefault;
}
return startAndEndTime;
}
} // namespace
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CreateSimulationWorkspace)
using namespace API;
using namespace HistogramData;
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string CreateSimulationWorkspace::name() const { return "CreateSimulationWorkspace"; }
/// Algorithm's version for identification. @see Algorithm::version
int CreateSimulationWorkspace::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string CreateSimulationWorkspace::category() const { return "Utility\\Workspaces"; }
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CreateSimulationWorkspace::init() {
using namespace Kernel;
declareProperty("Instrument", "", std::make_shared<MandatoryValidator<std::string>>(),
"An instrument name or filename ( a full path or string "
"containing an xml extension).",
Direction::Input);
declareProperty(
std::make_unique<ArrayProperty<double>>("BinParams", std::make_shared<RebinParamsValidator>(), Direction::Input),
"A comma separated list of first bin boundary, width, last "
"bin boundary. See Rebin for more details");
declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output), "The new workspace");
auto knownUnits = UnitFactory::Instance().getKeys();
declareProperty("UnitX", "DeltaE", std::make_shared<ListValidator<std::string>>(knownUnits),
"The unit to assign to the X axis", Direction::Input);
declareProperty(
std::make_unique<FileProperty>("DetectorTableFilename", "", FileProperty::OptionalLoad, "", Direction::Input),
"An optional filename (currently RAW or ISIS NeXus) that "
"contains UDET & SPEC tables to access hardware grouping");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CreateSimulationWorkspace::exec() {
createInstrument();
createOutputWorkspace();
setProperty("OutputWorkspace", m_outputWS);
}
//-----------------------------------------------------------------------------------------------
// Private methods
//-----------------------------------------------------------------------------------------------
/**
* Create the instrument from the name/file. Runs LoadInstrument with a fake
* workspace.
*/
void CreateSimulationWorkspace::createInstrument() {
const bool enableLogging(false);
IAlgorithm_sptr loadInstrument = createChildAlgorithm("LoadInstrument", 0.0, 0.5, enableLogging);
MatrixWorkspace_sptr tempWS = WorkspaceFactory::Instance().create("Workspace2D", 1, 1, 1);
// We need to set the correct start date for this workspace
// else we might be pulling an inadequate IDF
setStartDate(tempWS);
loadInstrument->setProperty("Workspace", tempWS);
const std::string instrProp = getProperty("Instrument");
if (boost::algorithm::ends_with(instrProp, ".xml")) {
loadInstrument->setPropertyValue("Filename", instrProp);
} else {
loadInstrument->setPropertyValue("InstrumentName", instrProp);
}
loadInstrument->setProperty("RewriteSpectraMap", Kernel::OptionalBool(true));
loadInstrument->executeAsChildAlg();
tempWS = loadInstrument->getProperty("Workspace");
m_instrument = tempWS->getInstrument();
}
/**
* Creates the output workspace attaching the instrument
*/
void CreateSimulationWorkspace::createOutputWorkspace() {
const size_t nhistograms = createDetectorMapping();
const auto binBoundaries = createBinBoundaries();
const size_t xlength = binBoundaries.size();
const size_t ylength = xlength - 1;
m_outputWS = WorkspaceFactory::Instance().create("Workspace2D", nhistograms, xlength, ylength);
m_outputWS->setInstrument(m_instrument);
m_outputWS->populateInstrumentParameters();
m_outputWS->getAxis(0)->setUnit(getProperty("UnitX"));
m_outputWS->setYUnit("SpectraNumber");
m_progress = std::make_shared<Progress>(this, 0.5, 0.75, nhistograms);
PARALLEL_FOR_IF(Kernel::threadSafe(*m_outputWS))
for (int64_t i = 0; i < static_cast<int64_t>(nhistograms); ++i) {
m_outputWS->setBinEdges(i, binBoundaries);
m_outputWS->mutableY(i) = 1.0;
m_progress->report("Setting X values");
}
applyDetectorMapping();
// Update the instrument from the file if necessary
const std::string detTableFile = getProperty("DetectorTableFilename");
if (boost::algorithm::ends_with(detTableFile, ".raw") || boost::algorithm::ends_with(detTableFile, ".RAW") ||
boost::algorithm::ends_with(detTableFile, ".nxs") || boost::algorithm::ends_with(detTableFile, ".NXS")) {
adjustInstrument(detTableFile);
}
}
/**
* Sets up the detector map. By default a 1:1 map is ensured, however a file can
* be given to use as a map
* @returns The number of spectra that are required
*/
size_t CreateSimulationWorkspace::createDetectorMapping() {
const std::string detTableFile = getProperty("DetectorTableFilename");
if (detTableFile.empty()) {
createOneToOneMapping();
} else {
loadMappingFromFile(detTableFile);
}
return m_detGroups.size();
}
/**
* Create a one to one mapping from the spectrum numbers to detector IDs
*/
void CreateSimulationWorkspace::createOneToOneMapping() {
const std::vector<detid_t> detids = m_instrument->getDetectorIDs(true);
const size_t nhist = detids.size();
m_detGroups.clear();
for (size_t i = 0; i < nhist; ++i) {
std::set<detid_t> group;
group.insert(detids[i]);
m_detGroups.emplace(static_cast<specnum_t>(i + 1), group);
}
}
/**
* Load the detector mapping from a file
* @param filename :: The name of the file to pull the UDET/SPEC tables from
*/
void CreateSimulationWorkspace::loadMappingFromFile(const std::string &filename) {
if (boost::algorithm::ends_with(filename, ".raw") || boost::algorithm::ends_with(filename, ".RAW")) {
loadMappingFromRAW(filename);
} else if (boost::algorithm::ends_with(filename, ".nxs") || boost::algorithm::ends_with(filename, ".NXS")) {
loadMappingFromISISNXS(filename);
}
}
/**
* Load the detector mapping from a RAW file
* @param filename :: The name of the RAW file to pull the UDET/SPEC tables from
*/
void CreateSimulationWorkspace::loadMappingFromRAW(const std::string &filename) {
FILE *rawFile = fopen(filename.c_str(), "rb");
if (!rawFile)
throw std::runtime_error("Cannot open RAW file for reading: " + filename);
ISISRAW2 isisRaw;
const bool fromFile(true), readData(false);
isisRaw.ioRAW(rawFile, fromFile, readData);
int ndet = isisRaw.i_det;
int *specTable = isisRaw.spec;
int *udetTable = isisRaw.udet;
createGroupingsFromTables(specTable, udetTable, ndet);
fclose(rawFile);
}
/**
* Load the detector mapping from a NeXus file. Throws if the file does not
* provide the mapping tables
* @param filename :: The name of the ISIS raw NeXus file to pull the UDET/SPEC
* tables from
*/
void CreateSimulationWorkspace::loadMappingFromISISNXS(const std::string &filename) {
::NeXus::File nxsFile(filename);
try {
nxsFile.openPath("/raw_data_1/isis_vms_compat");
} catch (::NeXus::Exception &) {
throw std::runtime_error("Cannot find path to isis_vms_compat. Is the file an ISIS NeXus file?");
}
using NXIntArray = std::unique_ptr<std::vector<int32_t>>;
nxsFile.openData("NDET");
NXIntArray ndets(nxsFile.getData<int32_t>());
nxsFile.closeData();
nxsFile.openData("SPEC");
NXIntArray specTable(nxsFile.getData<int32_t>());
nxsFile.closeData();
nxsFile.openData("UDET");
NXIntArray udetTable(nxsFile.getData<int32_t>());
nxsFile.closeData();
createGroupingsFromTables(specTable->data(), udetTable->data(), (*ndets)[0]);
}
/**
* Create the grouping map from the tables
* @param specTable :: An array of spectrum numbers
* @param udetTable :: An array of detector IDs
* @param ndets :: The size of the two arrays
*/
void CreateSimulationWorkspace::createGroupingsFromTables(int *specTable, int *udetTable, int ndets) {
m_detGroups.clear();
for (int i = 0; i < ndets; ++i) {
int specNo = specTable[i];
int detID = udetTable[i];
if (m_instrument->isMonitor(detID))
continue; // Skip monitors
auto iter = m_detGroups.find(specNo);
if (iter != m_detGroups.end()) {
iter->second.insert(detID);
} else {
std::set<detid_t> group;
group.insert(static_cast<detid_t>(detID));
m_detGroups.emplace(specNo, group);
}
}
}
/**
* @returns The bin bounadries for the new workspace
*/
BinEdges CreateSimulationWorkspace::createBinBoundaries() const {
const std::vector<double> rbparams = getProperty("BinParams");
MantidVec newBins;
const int numBoundaries = Mantid::Kernel::VectorHelper::createAxisFromRebinParams(rbparams, newBins);
if (numBoundaries <= 2) {
throw std::invalid_argument("Error in BinParams - Gave invalid number of bin boundaries: " +
std::to_string(numBoundaries));
}
return BinEdges(std::move(newBins));
}
/**
* Apply the created mapping to the workspace
*/
void CreateSimulationWorkspace::applyDetectorMapping() {
size_t wsIndex(0);
for (auto &detGroup : m_detGroups) {
auto &spectrum = m_outputWS->getSpectrum(wsIndex);
spectrum.setSpectrumNo(static_cast<specnum_t>(wsIndex + 1)); // Ensure a contiguous mapping
spectrum.clearDetectorIDs();
spectrum.addDetectorIDs(detGroup.second);
++wsIndex;
}
}
/**
* Apply any instrument adjustments from the file
* @param filename :: The file to take the positions
*/
void CreateSimulationWorkspace::adjustInstrument(const std::string &filename) {
// If requested update the instrument to positions in the raw file
const auto &pmap = m_outputWS->constInstrumentParameters();
Geometry::Instrument_const_sptr instrument = m_outputWS->getInstrument();
std::shared_ptr<Geometry::Parameter> updateDets = pmap.get(instrument->getComponentID(), "det-pos-source");
if (!updateDets)
return; // No tag, use IDF
std::string value = updateDets->value<std::string>();
if (value.substr(0, 8) == "datafile") {
IAlgorithm_sptr updateInst = createChildAlgorithm("UpdateInstrumentFromFile", 0.75, 1.0);
updateInst->setProperty<MatrixWorkspace_sptr>("Workspace", m_outputWS);
updateInst->setPropertyValue("Filename", filename);
if (value == "datafile-ignore-phi") {
updateInst->setProperty("IgnorePhi", true);
g_log.information("Detector positions in IDF updated with positions in "
"the data file except for the phi values");
} else {
g_log.information("Detector positions in IDF updated with positions in the data file");
}
// We want this to throw if it fails to warn the user that the information
// is not correct.
updateInst->execute();
}
}
/**
* Sets the start date on a dummy workspace. If there is a detector table file
* available we update the dummy workspace with the start date from this file.
* @param workspace: dummy workspace
*/
void CreateSimulationWorkspace::setStartDate(const API::MatrixWorkspace_sptr &workspace) {
const std::string detTableFile = getProperty("DetectorTableFilename");
auto hasDetTableFile = !detTableFile.empty();
auto &run = workspace->mutableRun();
Types::Core::DateAndTime startTime;
Types::Core::DateAndTime endTime;
try {
// The start and end times might not be valid, and hence can throw
startTime = run.startTime();
endTime = run.endTime();
} catch (std::runtime_error &) {
startTime = Types::Core::DateAndTime::getCurrentTime();
endTime = Types::Core::DateAndTime::getCurrentTime();
}
if (hasDetTableFile) {
if (boost::algorithm::ends_with(detTableFile, ".raw") || boost::algorithm::ends_with(detTableFile, ".RAW")) {
auto startAndEndTime = getStartAndEndTimesFromRawFile(detTableFile);
startTime = startAndEndTime.startTime;
endTime = startAndEndTime.endTime;
} else if (boost::algorithm::ends_with(detTableFile, ".nxs") || boost::algorithm::ends_with(detTableFile, ".NXS")) {
auto startAndEndTime = getStartAndEndTimesFromNexusFile(detTableFile, startTime, endTime);
startTime = startAndEndTime.startTime;
endTime = startAndEndTime.endTime;
}
}
run.setStartAndEndTime(startTime, endTime);
}
} // namespace DataHandling
} // namespace Mantid