Mantid
Loading...
Searching...
No Matches
LoadInstrumentFromNexus.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//----------------------------------------------------------------------
18
19#include <fstream>
20
21namespace Mantid::DataHandling {
22
23DECLARE_ALGORITHM(LoadInstrumentFromNexus)
24
25using namespace Kernel;
26using namespace API;
27using Geometry::Instrument;
28
31
34 // When used as a Child Algorithm the workspace name is not used - hence the
35 // "Anonymous" to satisfy the validator
36 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("Workspace", "Anonymous", Direction::InOut),
37 "The name of the workspace in which to attach the imported instrument");
38
39 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, ".nxs"),
40 "The name (including its full or relative path) of the Nexus file to "
41 "attempt to load the instrument from. The file extension must either be "
42 ".nxs or .NXS");
43}
44
51 // Retrieve the filename from the properties
52 m_filename = getPropertyValue("Filename");
53
54 // Get the input workspace
55 const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
56
57 // open Nexus file
58 MuonNexusReader nxload;
60 progress(0.5);
61 // Create a new Instrument with the right name and add it to the workspace
63
64 // Add dummy source and samplepos to instrument
65 // The L2 and 2-theta values from nexus file assumed to be relative to sample
66 // position
67
68 Geometry::Component *samplepos = new Geometry::Component("Unknown", instrument.get());
69 instrument->add(samplepos);
70 instrument->markAsSamplePos(samplepos);
71 samplepos->setPos(0.0, 0.0, 0.0);
72
73 Geometry::ObjComponent *source = new Geometry::ObjComponent("Unknown", instrument.get());
74 instrument->add(source);
75 instrument->markAsSource(source);
76 // If user has provided an L1, use that
77 auto l1ConfigVal = Kernel::ConfigService::Instance().getValue<double>("instrument.L1");
78 // Otherwise try and get it from the nexus file - but not there at present!
79 // l1 = nxload.ivpb.i_l1;
80 // Default to 10 if the file doesn't have it set
81 double l1 = l1ConfigVal.get_value_or(10.0);
82
83 source->setPos(0.0, -1.0 * l1, 0.0);
84 localWorkspace->setInstrument(instrument);
85 progress(1.0);
86}
87
88} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
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
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
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
MuunNexusReader opens a Nexus file and reads certain fields expected for a ISIS Muon data file (old f...
void readFromFile(const std::string &filename)
read histogram data
std::string getInstrumentName() const
return instrument name
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.
@ InOut
Both an input & output workspace.
Definition: Property.h:55