Mantid
Loading...
Searching...
No Matches
LoadMuonNexus.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
12#include "MantidAPI/Progress.h"
13#include "MantidAPI/TableRow.h"
24
27#include <Poco/Path.h>
28#include <cmath>
29#include <limits>
30#include <memory>
31// clang-format off
32#include <nexus/NeXusFile.hpp>
33#include <nexus/NeXusException.hpp>
34// clang-format on
35
36namespace Mantid::DataHandling {
37
38using namespace Kernel;
39using namespace API;
40using Geometry::Instrument;
41using namespace Mantid::NeXus;
42
45 : m_filename(), m_entrynumber(0), m_numberOfSpectra(0), m_numberOfPeriods(0), m_list(false), m_interval(false),
46 m_spec_list(), m_spec_min(0), m_spec_max(EMPTY_INT()) {}
47
50 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, ".nxs"),
51 "The name of the Nexus file to load");
52
53 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
54 "The name of the workspace to be created as the output of the\n"
55 "algorithm. For multiperiod files, one workspace will be\n"
56 "generated for each period");
57
58 auto mustBePositive = std::make_shared<BoundedValidator<specnum_t>>();
59 mustBePositive->setLower(0);
60 declareProperty("SpectrumMin", static_cast<specnum_t>(0), mustBePositive,
61 "Index number of the first spectrum to read\n");
62 declareProperty("SpectrumMax", static_cast<specnum_t>(EMPTY_INT()), mustBePositive,
63 "Index of last spectrum to read\n"
64 "(default the last spectrum)");
65
66 declareProperty(std::make_unique<ArrayProperty<specnum_t>>("SpectrumList"),
67 "Array, or comma separated list, of indexes of spectra to\n"
68 "load. If a range and a list of spectra are both supplied,\n"
69 "all the specified spectra will be loaded.");
70 declareProperty("AutoGroup", false,
71 "Determines whether the spectra are automatically grouped\n"
72 "together based on the groupings in the NeXus file, only\n"
73 "for single period data (default no). Version 1 only.");
74
75 auto mustBeNonNegative = std::make_shared<BoundedValidator<int64_t>>();
76 mustBeNonNegative->setLower(0);
77 declareProperty("EntryNumber", static_cast<int64_t>(0), mustBeNonNegative,
78 "0 indicates that every entry is loaded, into a separate "
79 "workspace within a group. "
80 "A positive number identifies one entry to be loaded, into "
81 "one workspace");
82
83 std::vector<std::string> FieldOptions{"Transverse", "Longitudinal"};
84 declareProperty("MainFieldDirection", "Transverse", std::make_shared<StringListValidator>(FieldOptions),
85 "Output the main field direction if specified in Nexus file "
86 "(run/instrument/detector/orientation, default "
87 "longitudinal). Version 1 only.",
89
90 declareProperty("TimeZero", 0.0, "Time zero in units of micro-seconds (default to 0.0)", Direction::Output);
91 declareProperty("FirstGoodData", 0.0, "First good data in units of micro-seconds (default to 0.0)",
93 declareProperty("LastGoodData", 0.0, "Last good data in the OutputWorkspace's spectra", Kernel::Direction::Output);
94
95 declareProperty(std::make_unique<ArrayProperty<double>>("TimeZeroList", Direction::Output),
96 "A vector of time zero values");
97
99 std::make_unique<WorkspaceProperty<Workspace>>("TimeZeroTable", "", Direction::Output, PropertyMode::Optional),
100 "TableWorkspace containing time zero values per spectra.");
101
102 declareProperty("CorrectTime", true, "Boolean flag controlling whether time should be corrected by timezero.",
104
106 std::make_unique<WorkspaceProperty<Workspace>>("DeadTimeTable", "", Direction::Output, PropertyMode::Optional),
107 "Table or a group of tables containing detector dead times. Version 1 "
108 "only.");
109
110 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("DetectorGroupingTable", "", Direction::Output,
112 "Table or a group of tables with information about the "
113 "detector grouping stored in the file (if any). Version 1 only.");
114}
115
118 // read in the settings passed to the algorithm
119 m_spec_list = getProperty("SpectrumList");
120 m_spec_max = getProperty("SpectrumMax");
121 // Are we using a list of spectra or all the spectra in a range?
122 m_list = !m_spec_list.empty();
124 if (m_spec_max == EMPTY_INT())
125 m_spec_max = 0;
126
127 // Check validity of spectra list property, if set
128 if (m_list) {
129 const specnum_t minlist = *min_element(m_spec_list.begin(), m_spec_list.end());
130 const specnum_t maxlist = *max_element(m_spec_list.begin(), m_spec_list.end());
131 if (maxlist > m_numberOfSpectra || minlist == 0) {
132 g_log.error("Invalid list of spectra");
133 throw std::invalid_argument("Inconsistent properties defined");
134 }
135 }
136
137 // Check validity of spectra range, if set
138 if (m_interval) {
139 m_spec_min = getProperty("SpectrumMin");
140 if (m_spec_max < m_spec_min || m_spec_max > m_numberOfSpectra) {
141 g_log.error("Invalid Spectrum min/max properties");
142 throw std::invalid_argument("Inconsistent properties defined");
143 }
144 }
145}
146
149
150 auto loadInst = createChildAlgorithm("LoadInstrument");
151
152 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
153 try {
154 loadInst->setPropertyValue("InstrumentName", m_instrument_name);
155 loadInst->setProperty<MatrixWorkspace_sptr>("Workspace", localWorkspace);
156 loadInst->setProperty("RewriteSpectraMap", Mantid::Kernel::OptionalBool(false));
157 loadInst->execute();
158 } catch (std::invalid_argument &) {
159 g_log.information("Invalid argument to LoadInstrument Child Algorithm");
160 } catch (std::runtime_error &) {
161 g_log.information("Unable to successfully run LoadInstrument Child Algorithm");
162 }
163
164 // If loading instrument definition file fails,
165 // we may get instrument by some other means yet to be decided upon
166 // at present just create a dummy instrument with the correct name.
167 if (!loadInst->isExecuted()) {
168 auto inst = std::make_shared<Geometry::Instrument>();
169 inst->setName(m_instrument_name);
170 localWorkspace->setInstrument(inst);
171 }
172}
173
181 UNUSED_ARG(descriptor);
182 return 0; // Not to be used but LoadMuonNexus2, which inherits from this will
183}
184
189 Mantid::API::Algorithm_sptr logAlg = createChildAlgorithm("AddSampleLog");
190 logAlg->setProperty("Workspace", ws);
191 return logAlg;
192}
193
197void LoadMuonNexus::addToSampleLog(const std::string &logName, const int logNumber, DataObjects::Workspace2D_sptr &ws) {
198 auto alg = createSampleLogAlgorithm(ws);
199 alg->setProperty("LogType", "Number");
200 alg->setProperty("NumberType", "Int");
201 alg->setProperty("LogName", logName);
202 alg->setProperty("LogText", std::to_string(logNumber));
203 alg->executeAsChildAlg();
204}
205
209void LoadMuonNexus::addToSampleLog(const std::string &logName, const std::string &logString,
211 auto alg = createSampleLogAlgorithm(ws);
212 alg->setProperty("LogType", "String");
213 alg->setProperty("LogName", logName);
214 alg->setProperty("LogText", logString);
215 alg->executeAsChildAlg();
216}
217} // namespace Mantid::DataHandling
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
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
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
A property class for workspaces.
void init() override
Overwrites Algorithm method.
specnum_t m_spec_min
The value of the spectrum_min property.
void runLoadInstrument(const DataObjects::Workspace2D_sptr &)
Run the Child Algorithm LoadInstrument.
std::string m_instrument_name
The instrument name from Nexus.
Definition: LoadMuonNexus.h:85
bool m_interval
Have the spectrum_min/max properties been set?
Definition: LoadMuonNexus.h:97
bool m_list
Has the spectrum_list property been set?
Definition: LoadMuonNexus.h:95
specnum_t m_spec_max
The value of the spectrum_max property.
void checkOptionalProperties()
Validates the optional 'spectra to read' properties, if they have been set.
std::vector< specnum_t > m_spec_list
The value of the spectrum_list property.
Definition: LoadMuonNexus.h:99
Mantid::API::Algorithm_sptr createSampleLogAlgorithm(DataObjects::Workspace2D_sptr &ws)
Create Algorithm to add a sample log to a workspace.
LoadMuonNexus()
Default constructor.
int confidence(Kernel::NexusDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
void addToSampleLog(const std::string &logName, const int logNumber, DataObjects::Workspace2D_sptr &ws)
Function to add a single int as a sample log to a workspace.
specnum_t m_numberOfSpectra
The number of spectra in the raw file.
Definition: LoadMuonNexus.h:91
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
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.
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
Definition: Algorithm.h:61
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54