Mantid
Loading...
Searching...
No Matches
LoadInstrumentFromRaw.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//----------------------------------------------------------------------
11#include "LoadRaw/isisraw.h"
20
21#include <fstream>
22
23namespace Mantid::DataHandling {
24
25DECLARE_ALGORITHM(LoadInstrumentFromRaw)
26
27using namespace Kernel;
28using namespace API;
29using Geometry::Instrument;
30
33
36 // When used as a Child Algorithm the workspace name is not used - hence the
37 // "Anonymous" to satisfy the validator
38 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("Workspace", "Anonymous", Direction::InOut),
39 "The name of the workspace in which to store the imported instrument.");
40
41 const std::vector<std::string> exts{".raw", ".s*"};
42 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
43 "The filename (including its full or relative path) of an ISIS RAW file. "
44 "The file extension must either be .raw or .s??");
45 declareProperty(std::make_unique<ArrayProperty<int>>("MonitorList", Direction::Output),
46 "List of detector ids of monitors loaded into the workspace");
47}
48
55 // Retrieve the filename from the properties
56 m_filename = getPropertyValue("Filename");
57
58 // Get the input workspace
59 const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
60
61 // open raw file
62 ISISRAW iraw(nullptr);
63 if (iraw.readFromFile(m_filename.c_str(), false) != 0) {
64 g_log.error("Unable to open file " + m_filename);
65 throw Exception::FileError("Unable to open File:", m_filename);
66 }
67
68 // Create a new Instrument with the right name and add it to the workspace
69 Geometry::Instrument_sptr instrument(new Instrument(iraw.i_inst));
70
71 // Add dummy source and samplepos to instrument
72 // The L2 and 2-theta values from Raw file assumed to be relative to sample
73 // position
74
75 Geometry::Component *samplepos = new Geometry::Component("Sample", instrument.get());
76 instrument->add(samplepos);
77 instrument->markAsSamplePos(samplepos);
78 samplepos->setPos(0.0, 0.0, 0.0);
79
80 Geometry::ObjComponent *source = new Geometry::ObjComponent("Source", instrument.get());
81 instrument->add(source);
82 instrument->markAsSource(source);
83
84 progress(0.5);
85 // If user has provided an L1, use that
86 auto l1ConfigValue = Kernel::ConfigService::Instance().getValue<double>("instrument.L1");
87 // Otherwise try and get it from the raw file
88 double l1 = l1ConfigValue.get_value_or(iraw.ivpb.i_l1);
89 // Default to 10 if the raw file doesn't have it set
90 if (l1 == 0)
91 l1 = 10.0;
92
93 source->setPos(0.0, 0.0, -1.0 * l1);
94
95 // add detectors
96 const int numDetector = iraw.i_det; // number of detectors
97 const int *const detID = iraw.udet; // detector IDs
98 const float *const r = iraw.len2; // distance from sample
99 const float *const angle = iraw.tthe; // angle between indicent beam and
100 // direction from sample to detector
101 // (two-theta)
102 const float *const phi = iraw.ut;
103 // Is ut01 (=phi) present? Sometimes an array is present but has wrong values
104 // e.g.all 1.0 or all 2.0
105 bool phiPresent = iraw.i_use > 0 && phi[0] != 1.0 && phi[0] != 2.0;
106 const int numMonitors = iraw.i_mon; // The number of monitors
107 const int *const monIndex = iraw.mdet; // Index into the udet array for each monitor
108
109 double prog = 0.5;
110 for (int i = 0; i < numDetector; ++i) {
111 // g_log.error() << " ## " << detID[i];
112 // Create a new detector. Instrument will take ownership of pointer so no
113 // need to delete.
114 Geometry::Detector *detector = new Geometry::Detector("det", detID[i], samplepos);
115 Kernel::V3D pos;
116
117 if (phiPresent)
118 pos.spherical(r[i], angle[i], phi[i]);
119 else
120 pos.spherical(r[i], angle[i], 0.0);
121
122 detector->setPos(pos);
123
124 // add to instrument
125 instrument->add(detector);
126
127 // Check monitor list to see if this is a monitor that should be marked as
128 // such
129 if (std::find(monIndex, monIndex + numMonitors, i + 1) != monIndex + numMonitors) {
130 instrument->markAsMonitor(detector);
131 g_log.information() << "Detector with ID " << detID[i] << " marked as a monitor.\n";
132 }
133 // otherwise mark as a detector
134 else {
135 instrument->markAsDetector(detector);
136 }
137
138 prog += (0.5 / numDetector);
139 progress(prog);
140 }
141 localWorkspace->setInstrument(instrument);
142
143 std::vector<detid_t> monitorList = instrument->getMonitors();
144 setProperty("MonitorList", monitorList);
145 // Information to the user about what info is extracted from raw file
146 g_log.information() << "SamplePos component added with position set to (0,0,0).\n"
147 << "Detector components added with position coordinates assumed to be "
148 "relative to the position of the sample; \n"
149 << "L2 and two-theta values were read from raw file and used to set the "
150 "r and theta spherical coordinates; \n"
151 << "the remaining spherical coordinate phi was set to zero.\n"
152 << "Source component added with position set to (0,0,-" << l1
153 << "). In standard configuration, with \n"
154 << "the beam along z-axis pointing from source to sample, this implies "
155 "the source is "
156 << l1 << "m in front \n"
157 << "of the sample. This value can be changed via the 'instrument.l1' "
158 "configuration property.\n";
159}
160
161} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
isis raw file.
Definition: isisraw.h:272
float * ut
nuse UT* user tables (total size NUSE*NDET) ut01=phi
Definition: isisraw.h:307
int i_use
number of user defined UTn tables NUSE
Definition: isisraw.h:298
char i_inst[8]
instrument name
Definition: isisraw.h:294
int i_det
number of detectors NDET
Definition: isisraw.h:296
int * mdet
detector number for monitors (size NMON)
Definition: isisraw.h:300
int readFromFile(const char *filename, bool read_data=true)
stuff
Definition: isisraw.cpp:923
int i_mon
number of monitors NMON
Definition: isisraw.h:297
IVPB_STRUCT ivpb
instrument parameter block (64*4 bytes)
Definition: isisraw.h:295
int * udet
user detector number for each detector (size NDET)
Definition: isisraw.h:322
float * tthe
2theta scattering angle (size NDET)
Definition: isisraw.h:306
float * len2
L2 table (size NDET)
Definition: isisraw.h:304
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
Kernel::Logger & g_log
Definition: Algorithm.h:451
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
A property class for workspaces.
void init() override
Overwrites Algorithm method. Does nothing at present.
std::string m_filename
The name and path of the input file.
void exec() override
Overwrites Algorithm method.
Component is a wrapper for a Component which can modify some of its parameters, e....
Definition: Component.h:41
void setPos(double, double, double) override
Set the IComponent position, x, y, z respective to parent (if present)
Definition: Component.cpp:204
This class represents a detector - i.e.
Definition: Detector.h:30
Base Instrument Class.
Definition: Instrument.h:47
Object Component class, this class brings together the physical attributes of the component to the po...
Definition: ObjComponent.h:33
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
Records the filename and the description of failure.
Definition: Exception.h:98
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
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Class for 3D vectors.
Definition: V3D.h:34
void spherical(const double R, const double theta, const double phi) noexcept
Sets the vector position based on spherical coordinates.
Definition: V3D.cpp:57
ISIS VMS raw file definitions.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Instrument > Instrument_sptr
Shared pointer to an instrument object.
float i_l1
L1 scattering length.
Definition: isisraw.h:113
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Output
An output workspace.
Definition: Property.h:54