Mantid
Loading...
Searching...
No Matches
LoadILLLagrange.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2023 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include "MantidAPI/Axis.h"
15#include "MantidKernel/Unit.h"
18#include "MantidNexus/H5Util.h"
21#include "MantidNexus/NexusFile.h"
22
23namespace Mantid::DataHandling {
24
25using namespace API;
26using namespace Geometry;
27using namespace Kernel;
28using namespace Nexus;
29using Types::Core::DateAndTime;
30
31// Register the algorithm into the AlgorithmFactory
33
34
35int LoadILLLagrange::confidence(Nexus::NexusDescriptor &descriptor) const {
36
37 // fields existent only at the ILL Diffraction
38 if (descriptor.isEntry("/entry0/IN1")) {
39 return 80;
40 } else {
41 return 0;
42 }
43}
44
46const std::string LoadILLLagrange::name() const { return "LoadILLLagrange"; }
47
49int LoadILLLagrange::version() const { return 1; }
50
52const std::string LoadILLLagrange::category() const { return "DataHandling\\Nexus;ILL\\Lagrange"; }
53
55const std::string LoadILLLagrange::summary() const { return "Loads ILL Lagrange instrument nexus files."; }
56
61
66 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, ".nxs"),
67 "File path of the data file to load");
68 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
69 "The output workspace.");
70 declareProperty("InitialEnergyOffset", 0.0, "Offset for the initial energy (meV)");
71}
72
77
78 Progress progress(this, 0, 1, 2);
79
80 progress.report("Loading the detector data");
81 loadData();
82
83 progress.report("Loading the metadata");
85
86 setProperty("OutputWorkspace", m_outputWorkspace);
87}
88
94
95 // open the H5 file
96 H5::H5File h5file(getPropertyValue("Filename"), H5F_ACC_RDONLY, Nexus::H5Util::defaultFileAcc());
97
98 H5::DataSet dataset = h5file.openDataSet("entry0/data_scan/detector_data/data");
99
100 // init the workspace with proper number of histograms and number of channels
101 initWorkspace(dataset);
102
103 // load the instrument
105
106 // load data from file
107 std::vector<int> dataInt(m_nScans);
108 dataset.read(dataInt.data(), dataset.getDataType());
109 dataset.close();
110
111 // get scanned variable names
112 H5::DataSet scanVarNames = h5file.openDataSet("entry0/data_scan/scanned_variables/variables_names/name");
113 H5::DataSpace scanVarNamesSpace = scanVarNames.getSpace();
114
115 int nDims = scanVarNamesSpace.getSimpleExtentNdims();
116 auto dimsSize = std::vector<hsize_t>(nDims);
117 scanVarNamesSpace.getSimpleExtentDims(dimsSize.data(), nullptr);
118
119 std::vector<char *> rdata(dimsSize[0]);
120 scanVarNames.read(rdata.data(), scanVarNames.getDataType());
121 size_t monitorIndex = 0;
122 while (monitorIndex < rdata.size()) {
123 if (std::string(rdata[monitorIndex]) == "Monitor1")
124 break;
125 monitorIndex++;
126 }
127 if (monitorIndex == rdata.size())
128 throw std::runtime_error("Monitor count not found. Please check your nexus file.");
129 scanVarNames.vlenReclaim(rdata.data(), scanVarNames.getDataType(), scanVarNamesSpace);
130 scanVarNames.close();
131
132 // get scanned variable values and extract monitor count
133 H5::DataSet scanVar = h5file.openDataSet("entry0/data_scan/scanned_variables/data");
134 H5::DataSpace scanVarSpace = scanVar.getSpace();
135
136 nDims = scanVarSpace.getSimpleExtentNdims();
137 std::vector<double> monitorData;
138 std::vector<double> scanVariableData;
139 dimsSize.resize(nDims);
140 if (dimsSize.size() != 2) {
141 throw std::runtime_error("Scanned variables are not formatted properly. Check you nexus file.");
142 } else {
143 scanVarSpace.getSimpleExtentDims(dimsSize.data(), nullptr);
144
145 if (dimsSize[1] != m_nScans)
146 throw std::runtime_error("Scanned variables are not formatted properly. Check you nexus file.");
147
148 std::vector<double> scanVarData(dimsSize[0] * dimsSize[1]);
149 scanVar.read(scanVarData.data(), scanVar.getDataType());
150 monitorData.resize(dimsSize[1]);
151 scanVariableData.resize(dimsSize[1]);
152 for (size_t i = 0; i < monitorData.size(); i++) {
153 monitorData[i] = scanVarData[monitorIndex * dimsSize[1] + i];
154 scanVariableData[i] = scanVarData[i];
155 }
156 }
157 scanVar.close();
158
159 double energyAxisOffset = getProperty("InitialEnergyOffset");
160 // fill the workspace
161 for (size_t j = 0; j < m_nScans; j++) {
162 double count = dataInt[j];
163 double error = sqrt(count);
164 m_outputWorkspace->mutableX(0)[j] = scanVariableData[j] - energyAxisOffset;
165 m_outputWorkspace->mutableY(0)[j] = count;
166 m_outputWorkspace->mutableE(0)[j] = error;
167 m_outputWorkspace->mutableX(1)[j] = scanVariableData[j] - energyAxisOffset;
168 m_outputWorkspace->mutableY(1)[j] = monitorData[j];
169 m_outputWorkspace->mutableE(1)[j] = sqrt(monitorData[j]);
170 }
171 h5file.close();
172}
173
178
179 // Open NeXus file
180 try {
181 Nexus::File nxHandle(getPropertyValue("Filename"), NXaccess::READ);
182 LoadHelper::addNexusFieldsToWsRun(nxHandle, m_outputWorkspace->mutableRun(), "entry0");
183 } catch (Nexus::Exception const &e) {
184 g_log.debug() << "Failed to open nexus file \"" << getPropertyValue("Filename") << "\" in read mode: " << e.what()
185 << "\n";
186 }
187
188 // Add scanned variable: energy to the sample logs so it can be used for merging workspaces as X axis
190 int index = 0;
191 for (auto energy : m_outputWorkspace->readX(0)) {
192 prop->addValue(index++, energy);
193 }
194 m_outputWorkspace->mutableRun().addProperty(prop);
195}
196
201void LoadILLLagrange::initWorkspace(const H5::DataSet &dataset) {
202 // The number of spectra is always 1 + 1 monitor, for consistency with ASCII data loader
203 const size_t nSpectra = 2;
204
205 // Set number of scans
206 H5::DataSpace detectorDataspace = dataset.getSpace();
207 int nDims = detectorDataspace.getSimpleExtentNdims();
208 std::vector<hsize_t> dimsSize(nDims);
209 detectorDataspace.getSimpleExtentDims(dimsSize.data(), NULL);
210 m_nScans = dimsSize[0];
211
213
214 // Set x axis units
215 m_outputWorkspace->getAxis(0)->unit() = UnitFactory::Instance().create("Energy");
216 // Set y axis unit
217 m_outputWorkspace->setYUnit("Counts");
218}
219
220} // namespace Mantid::DataHandling
double energy
Definition GetAllEi.cpp:157
double error
std::map< DeltaEMode::Type, std::string > index
int64_t nSpectra
int count
counter
Definition Matrix.cpp:37
#define DECLARE_NEXUS_FILELOADER_ALGORITHM(classname)
DECLARE_NEXUS_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro wh...
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
@ Load
allowed here which will be passed to the algorithm
Defines an interface to an algorithm that loads a file so that it can take part in the automatic sele...
Definition IFileLoader.h:19
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
LoadILLLagrange : Loads nexus files from ILL instrument LAGRANGE.
int version() const override
Algorithm's version for identification.
void exec() override
Executes the algorithm.
void loadMetaData()
Dumps the metadata from the file for each entry separately.
size_t m_nScans
number of scans in the file
void initWorkspace(const H5::DataSet &)
Initializes the output workspace for LAGRANGE.
API::MatrixWorkspace_sptr m_outputWorkspace
output workspace
void loadData()
Sets up the workspace, loads the mockup instrument, the data and scanned variables for proper data la...
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void init() override
Initialize the algorithm's properties.
const std::string name() const override
Algorithms name for identification.
const std::string category() const override
Algorithm's category for identification.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
A specialised Property class for holding a series of time-value pairs.
void addValue(const Types::Core::DateAndTime &time, const TYPE &value)
Add a value to the map using a DateAndTime object.
Class that provides for a standard Nexus exception.
void addNexusFieldsToWsRun(Nexus::File &filehandle, API::Run &runDetails, const std::string &entryName="", bool useFullAddress=false)
Add properties from a nexus file to the workspace run.
void loadEmptyInstrument(const API::MatrixWorkspace_sptr &ws, const std::string &instrumentName, const std::string &instrumentAddress="")
Loads empty instrument of chosen name into a provided workspace.
MANTID_NEXUS_DLL H5::FileAccPropList defaultFileAcc()
Default file access is H5F_CLOSE_STRONG.
Definition H5Util.cpp:119
@ Output
An output workspace.
Definition Property.h:54