Mantid
Loading...
Searching...
No Matches
LoadEmptyInstrument.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 +
17#include "MantidIndexing/IndexInfo.h"
21#include "MantidNexusGeometry/NexusGeometryParser.h"
22
23namespace Mantid::DataHandling {
24// Register the algorithm into the algorithm factory as a file loading algorithm
25DECLARE_FILELOADER_ALGORITHM(LoadEmptyInstrument)
26
27using namespace Kernel;
28using namespace API;
29using namespace Geometry;
30using namespace DataObjects;
31using namespace HistogramData;
32
40 const std::string &filePath = descriptor.filename();
41
42 int confidence(0);
43 if (descriptor.isAscii()) // Only consider an Ascii file
44 {
45 // Filename must contain "Definition"
46 std::string::size_type stripPath = filePath.find_last_of("\\/");
47 if (stripPath == std::string::npos)
48 stripPath = 0;
49 if (filePath.find("Definition", stripPath) != std::string::npos) {
50 // We have some confidence and it depends on the filetype.
51 if (descriptor.extension() == "xml") {
52 confidence = 80;
53 } else {
54 confidence = 20;
55 }
56 } // Has "Definition"
57 } // Ascii file
58 return confidence;
59}
60
64 std::make_unique<FileProperty>("Filename", "", FileProperty::OptionalLoad, LoadGeometry::validExtensions()),
65 "The filename (including its full or relative path) of an instrument "
66 "definition file. The file extension must either be .xml or .XML when "
67 "specifying an instrument definition file. Files can also be .hdf5 or "
68 ".nxs for usage with NeXus Geometry files. Note Filename or "
69 "InstrumentName must be specified but not both.");
70 declareProperty("InstrumentName", "",
71 "Name of instrument. Can be used instead of Filename to "
72 "specify an IDF");
73 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
74 "The name of the workspace in which to store the imported instrument");
75
76 auto mustBePositive = std::make_shared<BoundedValidator<double>>();
77 mustBePositive->setLower(0.0);
78 declareProperty("DetectorValue", 1.0, mustBePositive,
79 "This value affects the colour of the detectors in the instrument\n"
80 "display window (default 1)");
81 declareProperty("MonitorValue", 2.0, mustBePositive,
82 "This value affects the colour of the monitors in the instrument\n"
83 "display window (default 2)");
84
85 declareProperty(std::make_unique<PropertyWithValue<bool>>("MakeEventWorkspace", false),
86 "Set to True to create an EventWorkspace (with no events) "
87 "instead of a Workspace2D.");
88}
89
101 // load the instrument into this workspace
102 const std::string filename = getPropertyValue("Filename");
103 const std::string instrumentname = getPropertyValue("InstrumentName");
104 Instrument_const_sptr instrument;
105 Progress prog(this, 0.0, 1.0, 10);
106
107 // Call LoadIstrument as a child algorithm
108 MatrixWorkspace_sptr ws = this->runLoadInstrument(filename, instrumentname);
109 instrument = ws->getInstrument();
110
111 // Get number of detectors stored in instrument
112 const size_t number_spectra = instrument->getNumberDetectors();
113
114 // Check that we have some spectra for the workspace
115 if (number_spectra == 0) {
116 g_log.error("Instrument has no detectors, unable to create workspace for it");
117 throw Kernel::Exception::InstrumentDefinitionError("No detectors found in instrument");
118 }
119
120 Indexing::IndexInfo indexInfo(number_spectra);
121 bool MakeEventWorkspace = getProperty("MakeEventWorkspace");
122 prog.reportIncrement(5, "Creating Data");
123 if (MakeEventWorkspace) {
124 setProperty("OutputWorkspace",
125 create<EventWorkspace>(instrument, indexInfo, BinEdges{0.0, std::numeric_limits<double>::min()}));
126 } else {
127 const double detector_value = getProperty("DetectorValue");
128 const double monitor_value = getProperty("MonitorValue");
129 auto ws2D = create<Workspace2D>(
130 instrument, indexInfo,
131 Histogram(BinEdges{0.0, 1.0}, Counts(1, detector_value), CountStandardDeviations(1, detector_value)));
132
133 Counts v_monitor_y(1, monitor_value);
134 CountStandardDeviations v_monitor_e(1, monitor_value);
135
136 const auto &spectrumInfo = ws2D->spectrumInfo();
137 const auto size = static_cast<int64_t>(spectrumInfo.size());
138#pragma omp parallel for
139 for (int64_t i = 0; i < size; i++) {
140 if (spectrumInfo.isMonitor(i)) {
141 ws2D->setCounts(i, v_monitor_y);
142 ws2D->setCountStandardDeviations(i, v_monitor_e);
143 }
144 }
145 setProperty("OutputWorkspace", std::move(ws2D));
146 }
147}
148
151 const std::string &instrumentname) {
152 auto loadInst = createChildAlgorithm("LoadInstrument", 0, 0.5);
153 loadInst->setPropertyValue("Filename", filename);
154 loadInst->setPropertyValue("InstrumentName", instrumentname);
155 loadInst->setProperty("RewriteSpectraMap", OptionalBool(true));
156 auto ws = WorkspaceFactory::Instance().create("Workspace2D", 1, 2, 1);
157 loadInst->setProperty<MatrixWorkspace_sptr>("Workspace", ws);
158
159 loadInst->execute();
160
161 return ws;
162}
163
164} // namespace Mantid::DataHandling
#define DECLARE_FILELOADER_ALGORITHM(classname)
DECLARE_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro when wri...
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
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
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
Definition: FileProperty.h:53
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void init() override
Overwrites Algorithm method.
int confidence(Kernel::FileDescriptor &descriptor) const override
Returns a confidence value that this algorithm can load a file.
API::MatrixWorkspace_sptr runLoadInstrument(const std::string &filename, const std::string &instrumentname)
Run the Child Algorithm LoadInstrument (or LoadInstrumentFromRaw)
void exec() override
Overwrites Algorithm method.
Exception for errors associated with the instrument definition.
Definition: Exception.h:220
Defines a wrapper around an open file.
const std::string & filename() const
Access the filename.
static bool isAscii(const std::string &filename, const size_t nbytes=256)
Returns true if the file is considered ascii.
const std::string & extension() const
Access the file extension.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
OptionalBool : Tri-state bool.
Definition: OptionalBool.h:25
void reportIncrement(int inc, const std::string &msg="")
Sends the progress notification and increment the loop counter by more than one.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
const std::vector< std::string > validExtensions()
List allowed file extensions for geometry.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
@ Output
An output workspace.
Definition: Property.h:54