Mantid
Loading...
Searching...
No Matches
LoadSINQFocus.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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"
12#include "MantidAPI/Progress.h"
19
20#include <algorithm>
21#include <cmath>
22#include <limits>
23#include <vector>
24
25namespace Mantid::DataHandling {
26
27using namespace Kernel;
28using namespace API;
29using namespace NeXus;
30
32
33//----------------------------------------------------------------------------------------------
37 : m_supportedInstruments{"FOCUS"}, m_numberOfTubes{0}, m_numberOfPixelsPerTube{0}, m_numberOfChannels{0},
38 m_numberOfHistograms{0} {
39
40 this->useAlgorithm("LoadSINQ");
41 this->deprecatedDate("2013-10-28");
42}
43
44//----------------------------------------------------------------------------------------------
46const std::string LoadSINQFocus::name() const { return "LoadSINQFocus"; }
47
49int LoadSINQFocus::version() const { return 1; }
50
52const std::string LoadSINQFocus::category() const { return "DataHandling\\Nexus"; }
53
54//----------------------------------------------------------------------------------------------
55
63
64 // fields existent only at the SINQ (to date Loader only valid for focus)
65 if (descriptor.pathExists("/entry1/FOCUS/SINQ")) {
66 return 80;
67 } else {
68 return 0;
69 }
70}
71
72//-----------------------------------------1-----------------------------------------------------
76 const std::vector<std::string> exts{".nxs", ".hdf"};
77 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
78 "The name of the Nexus file to load");
79 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
80 "The name to use for the output workspace");
81}
82
83//----------------------------------------------------------------------------------------------
87
88 std::string filename = getPropertyValue("Filename");
89 NXRoot root(filename);
90 NXEntry entry = root.openFirstEntry();
91 setInstrumentName(entry);
92
93 initWorkSpace(entry);
94
96
97 loadRunDetails(entry);
99
101
102 setProperty("OutputWorkspace", m_localWorkspace);
103}
104
105/*
106 * Set global variables:
107 * m_instrumentPath
108 * m_instrumentName
109 * Note that the instrument in the nexus file is of the form "FOCUS at SINQ"
110 *
111 */
113
115
116 if (m_instrumentPath.empty()) {
117 throw std::runtime_error("Cannot set the instrument name from the Nexus file!");
118 }
120 size_t pos = m_instrumentName.find(' ');
121 m_instrumentName = m_instrumentName.substr(0, pos);
122}
123
125
126 // read in the data
127 NXData dataGroup = entry.openNXData("merged");
128 NXInt data = dataGroup.openIntData();
129
130 m_numberOfTubes = static_cast<size_t>(data.dim0());
132 m_numberOfChannels = static_cast<size_t>(data.dim1());
133
134 // dim0 * m_numberOfPixelsPerTube is the total number of detectors
136
137 g_log.debug() << "NumberOfTubes: " << m_numberOfTubes << '\n';
138 g_log.debug() << "NumberOfPixelsPerTube: " << m_numberOfPixelsPerTube << '\n';
139 g_log.debug() << "NumberOfChannels: " << m_numberOfChannels << '\n';
140
141 // Now create the output workspace
142 // Might need to get this value from the number of monitors in the Nexus file
143 // params:
144 // workspace type,
145 // total number of spectra + (number of monitors = 0),
146 // bin boundaries = m_numberOfChannels + 1
147 // Z/time dimension
150 m_localWorkspace->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
151 m_localWorkspace->setYUnitLabel("Counts");
152}
153
155
156 // read in the data
157 NXData dataGroup = entry.openNXData("merged");
158 NXInt data = dataGroup.openIntData();
159 data.load();
160
161 std::vector<double> timeBinning = LoadHelper::getTimeBinningFromNexusPath(entry, "merged/time_binning");
162 auto &x = m_localWorkspace->mutableX(0);
163 x.assign(timeBinning.begin(), timeBinning.end());
164
166 size_t spec = 0;
167 for (size_t i = 0; i < m_numberOfTubes; ++i) {
168 for (size_t j = 0; j < m_numberOfPixelsPerTube; ++j) {
169 if (spec > 0) {
170 // just copy the time binning axis to every spectra
171 m_localWorkspace->setSharedX(spec, m_localWorkspace->sharedX(0));
172 }
173 // Assign Y
174 int *data_p = &data(static_cast<int>(i), static_cast<int>(j));
175 m_localWorkspace->mutableY(spec).assign(data_p, data_p + m_numberOfChannels);
176 // Assign Error
177 auto &E = m_localWorkspace->mutableE(spec);
178 std::transform(data_p, data_p + m_numberOfChannels, E.begin(), LoadSINQFocus::calculateError);
179 ++spec;
180 progress.report();
181 }
182 }
183 g_log.debug() << "Data loading into WS done....\n";
184}
185
187
188 API::Run &runDetails = m_localWorkspace->mutableRun();
189
190 // int runNum = entry.getInt("run_number");
191 // std::string run_num = boost::lexical_cast<std::string>(runNum);
192 // runDetails.addProperty("run_number", run_num);
193
194 std::string start_time = entry.getString("start_time");
195 // start_time = getDateTimeInIsoFormat(start_time);
196 runDetails.addProperty("run_start", start_time);
197
198 std::string end_time = entry.getString("end_time");
199 // end_time = getDateTimeInIsoFormat(end_time);
200 runDetails.addProperty("run_end", end_time);
201
202 double wavelength = entry.getFloat(m_instrumentPath + "/monochromator/lambda");
203 runDetails.addProperty<double>("wavelength", wavelength);
204
205 double energy = entry.getFloat(m_instrumentPath + "/monochromator/energy");
206 runDetails.addProperty<double>("Ei", energy, true); // overwrite
207
208 std::string title = entry.getString("title");
209 runDetails.addProperty("title", title);
210 m_localWorkspace->setTitle(title);
211}
212
213/*
214 * Load data about the Experiment.
215 *
216 * TODO: This is very incomplete. In ISIS they much more info in the nexus file
217 *than ILL.
218 *
219 * @param entry :: The Nexus entry
220 */
222
223 std::string name = boost::lexical_cast<std::string>(entry.getFloat("sample/name"));
224 m_localWorkspace->mutableSample().setName(name);
225}
226
231
232 auto loadInst = createChildAlgorithm("LoadInstrument");
233
234 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
235 try {
236
237 // TODO: depending on the m_numberOfPixelsPerTube we might need to load a
238 // different IDF
239
240 loadInst->setPropertyValue("InstrumentName", m_instrumentName);
241 loadInst->setProperty("RewriteSpectraMap", Mantid::Kernel::OptionalBool(true));
242 loadInst->setProperty<MatrixWorkspace_sptr>("Workspace", m_localWorkspace);
243 loadInst->execute();
244 } catch (...) {
245 g_log.information("Cannot load the instrument definition.");
246 }
247}
248
249} // namespace Mantid::DataHandling
double energy
Definition: GetAllEi.cpp:157
#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.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
void addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
Definition: LogManager.h:79
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
A property class for workspaces.
Loads an PSI nexus file into a Mantid workspace.
Definition: LoadSINQFocus.h:31
const std::string category() const override
Algorithm's category for identification.
const std::string name() const override
Algorithm's name for identification.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
void runLoadInstrument()
Run the Child Algorithm LoadInstrument.
API::MatrixWorkspace_sptr m_localWorkspace
Definition: LoadSINQFocus.h:60
void loadExperimentDetails(const NeXus::NXEntry &)
void initWorkSpace(NeXus::NXEntry &)
void loadRunDetails(const NeXus::NXEntry &)
static double calculateError(double in)
Calculate error for y.
Definition: LoadSINQFocus.h:52
void loadDataIntoTheWorkSpace(NeXus::NXEntry &)
void setInstrumentName(const NeXus::NXEntry &entry)
void exec() override
Execute the algorithm.
int confidence(Kernel::NexusDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
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:114
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
Defines a wrapper around a file whose internal structure can be accessed using the NeXus API.
bool pathExists(const std::string &path) const
Query if a path exists.
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
float getFloat(const std::string &name) const
Returns a float.
std::string getString(const std::string &name) const
Returns a string.
Templated class implementation of NXDataSet.
Definition: NexusClasses.h:203
void load(const int blocksize=1, int i=-1, int j=-1, int k=-1, int l=-1) override
Implementation of the virtual NXDataSet::load(...) method.
Definition: NexusClasses.h:289
int dim0() const
Returns the number of elements along the first dimension.
int dim1() const
Returns the number of elements along the second dimension.
Implements NXdata Nexus class.
Definition: NexusClasses.h:795
NXInt openIntData()
Opens data of int type.
Definition: NexusClasses.h:828
Implements NXentry Nexus class.
Definition: NexusClasses.h:898
NXData openNXData(const std::string &name) const
Opens a NXData.
Definition: NexusClasses.h:912
Implements NXroot Nexus class.
Definition: NexusClasses.h:922
NXEntry openFirstEntry()
Open the first NXentry in the file.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string getStringFromNexusPath(const Mantid::NeXus::NXEntry &, const std::string &)
Definition: LoadHelper.cpp:49
std::vector< double > getTimeBinningFromNexusPath(const Mantid::NeXus::NXEntry &, const std::string &)
Gets the time binning from a Nexus float array Adds an extra bin at the end.
Definition: LoadHelper.cpp:61
std::string findInstrumentNexusPath(const Mantid::NeXus::NXEntry &)
Finds the path for the instrument name in the nexus file Usually of the form: entry0/<NXinstrument cl...
Definition: LoadHelper.cpp:37
@ Output
An output workspace.
Definition: Property.h:54