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"
14#include "MantidAPI/Sample.h"
21
22#include <algorithm>
23#include <cmath>
24#include <limits>
25#include <vector>
26
27namespace Mantid::DataHandling {
28
29using namespace Kernel;
30using namespace API;
31using namespace Nexus;
32
34
35//----------------------------------------------------------------------------------------------
39 : m_supportedInstruments{"FOCUS"}, m_numberOfTubes{0}, m_numberOfPixelsPerTube{0}, m_numberOfChannels{0},
40 m_numberOfHistograms{0} {
41
42 this->useAlgorithm("LoadSINQ");
43 this->deprecatedDate("2013-10-28");
44}
45
46//----------------------------------------------------------------------------------------------
48const std::string LoadSINQFocus::name() const { return "LoadSINQFocus"; }
49
51int LoadSINQFocus::version() const { return 1; }
52
54const std::string LoadSINQFocus::category() const { return "DataHandling\\Nexus"; }
55
56//----------------------------------------------------------------------------------------------
57
65
66 // fields existent only at the SINQ (to date Loader only valid for focus)
67 if (descriptor.isEntry("/entry1/FOCUS/SINQ")) {
68 return 80;
69 } else {
70 return 0;
71 }
72}
73
74//-----------------------------------------1-----------------------------------------------------
78 const std::vector<std::string> exts{".nxs", ".hdf"};
79 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
80 "The name of the Nexus file to load");
81 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
82 "The name to use for the output workspace");
83}
84
85//----------------------------------------------------------------------------------------------
89
90 std::string filename = getPropertyValue("Filename");
91 NXRoot root(filename);
92 NXEntry entry = root.openFirstEntry();
93 setInstrumentName(entry);
94
95 initWorkSpace(entry);
96
98
99 loadRunDetails(entry);
101
103
104 setProperty("OutputWorkspace", m_localWorkspace);
105}
106
107/*
108 * Set global variables:
109 * m_instrumentAddress
110 * m_instrumentName
111 * Note that the instrument in the nexus file is of the form "FOCUS at SINQ"
112 *
113 */
115
117
118 if (m_instrumentAddress.empty()) {
119 throw std::runtime_error("Cannot set the instrument name from the Nexus file!");
120 }
122 size_t pos = m_instrumentName.find(' ');
123 m_instrumentName.erase(pos + 1, m_instrumentName.size());
124}
125
127
128 // read in the data
129 NXData dataGroup = entry.openNXData("merged");
130 NXInt data = dataGroup.openIntData();
131
132 m_numberOfTubes = static_cast<size_t>(data.dim0());
134 m_numberOfChannels = static_cast<size_t>(data.dim1());
135
136 // dim0 * m_numberOfPixelsPerTube is the total number of detectors
138
139 g_log.debug() << "NumberOfTubes: " << m_numberOfTubes << '\n';
140 g_log.debug() << "NumberOfPixelsPerTube: " << m_numberOfPixelsPerTube << '\n';
141 g_log.debug() << "NumberOfChannels: " << m_numberOfChannels << '\n';
142
143 // Now create the output workspace
144 // Might need to get this value from the number of monitors in the Nexus file
145 // params:
146 // workspace type,
147 // total number of spectra + (number of monitors = 0),
148 // bin boundaries = m_numberOfChannels + 1
149 // Z/time dimension
152 m_localWorkspace->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
153 m_localWorkspace->setYUnitLabel("Counts");
154}
155
157
158 // read in the data
159 NXData dataGroup = entry.openNXData("merged");
160 NXInt data = dataGroup.openIntData();
161 data.load();
162
163 std::vector<double> timeBinning = LoadHelper::getTimeBinningFromNexusAddress(entry, "merged/time_binning");
164 auto &x = m_localWorkspace->mutableX(0);
165 x.assign(timeBinning.begin(), timeBinning.end());
166
168 size_t spec = 0;
169 for (size_t i = 0; i < m_numberOfTubes; ++i) {
170 for (size_t j = 0; j < m_numberOfPixelsPerTube; ++j) {
171 if (spec > 0) {
172 // just copy the time binning axis to every spectra
173 m_localWorkspace->setSharedX(spec, m_localWorkspace->sharedX(0));
174 }
175 // Assign Y
176 int *data_p = &data(static_cast<int>(i), static_cast<int>(j));
177 m_localWorkspace->mutableY(spec).assign(data_p, data_p + m_numberOfChannels);
178 // Assign Error
179 auto &E = m_localWorkspace->mutableE(spec);
180 std::transform(data_p, data_p + m_numberOfChannels, E.begin(), LoadSINQFocus::calculateError);
181 ++spec;
182 progress.report();
183 }
184 }
185 g_log.debug() << "Data loading into WS done....\n";
186}
187
189
190 API::Run &runDetails = m_localWorkspace->mutableRun();
191
192 // int runNum = entry.getInt("run_number");
193 // std::string run_num = boost::lexical_cast<std::string>(runNum);
194 // runDetails.addProperty("run_number", run_num);
195
196 std::string start_time = entry.getString("start_time");
197 // start_time = getDateTimeInIsoFormat(start_time);
198 runDetails.addProperty("run_start", start_time);
199
200 std::string end_time = entry.getString("end_time");
201 // end_time = getDateTimeInIsoFormat(end_time);
202 runDetails.addProperty("run_end", end_time);
203
204 double wavelength = entry.getFloat(m_instrumentAddress + "/monochromator/lambda");
205 runDetails.addProperty<double>("wavelength", wavelength);
206
207 double energy = entry.getFloat(m_instrumentAddress + "/monochromator/energy");
208 runDetails.addProperty<double>("Ei", energy, true); // overwrite
209
210 std::string title = entry.getString("title");
211 runDetails.addProperty("title", title);
212 m_localWorkspace->setTitle(title);
213}
214
215/*
216 * Load data about the Experiment.
217 *
218 * TODO: This is very incomplete. In ISIS they much more info in the nexus file
219 *than ILL.
220 *
221 * @param entry :: The Nexus entry
222 */
224
225 std::string sampleName = boost::lexical_cast<std::string>(entry.getFloat("sample/name"));
226 m_localWorkspace->mutableSample().setName(sampleName);
227}
228
233
234 auto loadInst = createChildAlgorithm("LoadInstrument");
235
236 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
237 try {
238
239 // TODO: depending on the m_numberOfPixelsPerTube we might need to load a
240 // different IDF
241
242 loadInst->setPropertyValue("InstrumentName", m_instrumentName);
243 loadInst->setProperty("RewriteSpectraMap", Mantid::Kernel::OptionalBool(true));
244 loadInst->setProperty<MatrixWorkspace_sptr>("Workspace", m_localWorkspace);
245 loadInst->execute();
246 } catch (...) {
247 g_log.information("Cannot load the instrument definition.");
248 }
249}
250
251} // 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.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
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.
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
void addProperty(Kernel::Property *prop, bool overwrite=false)
Add data to the object in the form of a property.
Definition LogManager.h:91
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:35
A property class for workspaces.
Loads an PSI nexus file into a Mantid workspace.
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 initWorkSpace(const Nexus::NXEntry &)
void runLoadInstrument()
Run the Child Algorithm LoadInstrument.
API::MatrixWorkspace_sptr m_localWorkspace
void setInstrumentName(const Nexus::NXEntry &entry)
int confidence(Nexus::NexusDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
static double calculateError(double in)
Calculate error for y.
void loadDataIntoTheWorkSpace(Nexus::NXEntry &)
void loadRunDetails(const Nexus::NXEntry &)
void loadExperimentDetails(const Nexus::NXEntry &)
void exec() override
Execute the algorithm.
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
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
OptionalBool : Tri-state bool.
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.
void load()
Read all of the datablock in.
dimsize_t dim0() const
Returns the number of elements along the first dimension.
dimsize_t dim1() const
Returns the number of elements along the second dimension.
Implements NXdata Nexus class.
NXInt openIntData()
Opens data of int type.
Implements NXentry Nexus class.
NXData openNXData(const std::string &name) const
Opens a NXData.
Implements NXroot Nexus class.
NXEntry openFirstEntry()
Open the first NXentry in the file.
bool isEntry(const std::string &entryName, const std::string &groupClass) const noexcept
Checks if a full-address entry exists for a particular groupClass in a Nexus dataset.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string getStringFromNexusAddress(const Mantid::Nexus::NXEntry &, const std::string &)
std::vector< double > getTimeBinningFromNexusAddress(const Mantid::Nexus::NXEntry &, const std::string &)
Gets the time binning from a Nexus float array Adds an extra bin at the end.
std::string findInstrumentNexusAddress(const Mantid::Nexus::NXEntry &)
Finds the address for the instrument name in the nexus file Usually of the form: entry0/<NXinstrument...
@ Output
An output workspace.
Definition Property.h:54