Mantid
Loading...
Searching...
No Matches
IMDWorkspace.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 +
12#include "MantidKernel/VMD.h"
13
14#include <sstream>
15#include <utility>
16
18
19namespace Mantid::API {
20//-----------------------------------------------------------------------------------------------
22IMDWorkspace::IMDWorkspace(const Parallel::StorageMode storageMode)
23 : Workspace(storageMode), Mantid::API::MDGeometry(),
24 m_convention(Kernel::ConfigService::Instance().getString("Q.convention")) {}
25
34std::unique_ptr<IMDIterator> IMDWorkspace::createIterator(Mantid::Geometry::MDImplicitFunction *function) const {
35 std::vector<std::unique_ptr<IMDIterator>> iterators = this->createIterators(1, function);
36 if (iterators.empty())
37 throw std::runtime_error("IMDWorkspace::createIterator(): iterator "
38 "creation was not successful. No iterators "
39 "returned by " +
40 this->id());
41 return std::move(iterators[0]);
42}
43
44//---------------------------------------------------------------------------------------------
47std::string IMDWorkspace::getConvention() const { return m_convention; }
48
49//---------------------------------------------------------------------------------------------
52void IMDWorkspace::setConvention(std::string convention) { m_convention = std::move(convention); }
53
54//---------------------------------------------------------------------------------------------
58 if (this->getConvention() == "Crystallography")
59 m_convention = "Inelastic";
60 else
61 m_convention = "Crystallography";
62 return m_convention;
63}
64
65//-------------------------------------------------------------------------------------------
73 const Mantid::API::MDNormalization &normalization) const {
74 return this->getSignalAtCoord(coords.getBareArray(), normalization);
75}
76
77//-------------------------------------------------------------------------------------------
86 const Mantid::API::MDNormalization &normalization) const {
87 return this->getSignalWithMaskAtCoord(coords.getBareArray(), normalization);
88}
89
90//-----------------------------------------------------------------------------------------------
91
92const std::string IMDWorkspace::toString() const {
93 std::ostringstream os;
94 os << id() << "\n"
95 << "Title: " + getTitle() << "\n";
96 for (size_t i = 0; i < getNumDims(); i++) {
97 const auto &dim = getDimension(i);
98 os << "Dim " << i << ": (" << dim->getName() << ") " << dim->getMinimum() << " to " << dim->getMaximum() << " in "
99 << dim->getNBins() << " bins";
100 // Also show the dimension ID string, if different than name
101 if (dim->getDimensionId() != dim->getName())
102 os << ". Id=" << dim->getDimensionId();
103 os << "\n";
104 }
105 if (hasOriginalWorkspace()) {
106 os << "Binned from '" << getOriginalWorkspace()->getName();
107 }
108 os << "\n";
109 if (this->getConvention() == "Crystallography")
110 os << "Crystallography: kf-ki";
111 else
112 os << "Inelastic: ki-kf";
113 os << "\n";
114
115 return os.str();
116}
117
118//----------------------------------------------------------------------------------------------
126void IMDWorkspace::makeSinglePointWithNaN(std::vector<coord_t> &x, std::vector<signal_t> &y,
127 std::vector<signal_t> &e) const {
128 x.emplace_back(0.f);
129 y.emplace_back(std::numeric_limits<signal_t>::quiet_NaN());
130 e.emplace_back(std::numeric_limits<signal_t>::quiet_NaN());
131}
132
133//-----------------------------------------------------------------------------------------------
149 // TODO: Don't use a fixed number of points later
150 size_t numPoints = 200;
151
152 VMD step = (end - start) / double(numPoints);
153 double stepLength = step.norm();
154
155 // This will be the curve as plotted
156 LinePlot line;
157 for (size_t i = 0; i < numPoints; i++) {
158 // Coordinate along the line
159 VMD coord = start + step * double(i);
160 // Record the position along the line
161 line.x.emplace_back(static_cast<coord_t>(stepLength * double(i)));
162
163 signal_t yVal = this->getSignalAtCoord(coord.getBareArray(), normalize);
164 line.y.emplace_back(yVal);
165 line.e.emplace_back(0.0);
166 }
167 // And the last point
168 line.x.emplace_back((end - start).norm());
169 return line;
170}
171
177
183} // namespace Mantid::API
184
185namespace Mantid::Kernel {
188template <>
190IPropertyManager::getValue<Mantid::API::IMDWorkspace_sptr>(const std::string &name) const {
191 auto *prop = dynamic_cast<PropertyWithValue<Mantid::API::IMDWorkspace_sptr> *>(getPointerToProperty(name));
192 if (prop) {
193 return *prop;
194 } else {
195 std::string message =
196 "Attempt to assign property " + name + " to incorrect type. Expected shared_ptr<IMDWorkspace>.";
197 throw std::runtime_error(message);
198 }
199}
200
203template <>
205IPropertyManager::getValue<Mantid::API::IMDWorkspace_const_sptr>(const std::string &name) const {
206 auto *prop = dynamic_cast<PropertyWithValue<Mantid::API::IMDWorkspace_sptr> *>(getPointerToProperty(name));
207 if (prop) {
208 return prop->operator()();
209 } else {
210 std::string message =
211 "Attempt to assign property " + name + " to incorrect type. Expected const shared_ptr<IMDWorkspace>.";
212 throw std::runtime_error(message);
213 }
214}
215
216} // namespace Mantid::Kernel
signal_t getSignalWithMaskAtVMD(const Mantid::Kernel::VMD &coords, const Mantid::API::MDNormalization &normalization=Mantid::API::VolumeNormalization) const
Returns the signal (normalized by volume) at a given coordinates or 0 if masked.
virtual signal_t getSignalAtCoord(const coord_t *coords, const Mantid::API::MDNormalization &normalization) const =0
Returns the (normalized) signal at a given coordinates.
std::string changeQConvention()
IMDWorkspace(const Parallel::StorageMode storageMode=Parallel::StorageMode::Cloned)
Default constructor.
virtual signal_t getSignalWithMaskAtCoord(const coord_t *coords, const Mantid::API::MDNormalization &normalization) const =0
Returns the (normalized) signal at a given coordinates or 0 if the value.
const std::string toString() const override
Serializes the object to a string.
virtual MDNormalization displayNormalizationHisto() const
std::string getConvention() const
std::unique_ptr< IMDIterator > createIterator(Mantid::Geometry::MDImplicitFunction *function=nullptr) const
Creates a single iterator and returns it.
signal_t getSignalAtVMD(const Mantid::Kernel::VMD &coords, const Mantid::API::MDNormalization &normalization=Mantid::API::VolumeNormalization) const
Returns the signal (normalized by volume) at a given coordinates.
virtual MDNormalization displayNormalization() const
virtual LinePlot getLinePlot(const Mantid::Kernel::VMD &start, const Mantid::Kernel::VMD &end, Mantid::API::MDNormalization normalize) const
Method to generate a line plot through a MD-workspace.
virtual std::vector< std::unique_ptr< IMDIterator > > createIterators(size_t suggestedNumCores=1, Mantid::Geometry::MDImplicitFunction *function=nullptr) const =0
Creates a new iterator pointing to the first cell in the workspace.
void makeSinglePointWithNaN(std::vector< coord_t > &x, std::vector< signal_t > &y, std::vector< signal_t > &e) const
Make a single point with NaN as the signal and error This can be returned when there would otherwise ...
void setConvention(std::string convention)
Describes the geometry (i.e.
Definition: MDGeometry.h:37
virtual std::shared_ptr< const Mantid::Geometry::IMDDimension > getDimension(size_t index) const
Get a dimension.
Definition: MDGeometry.cpp:161
virtual size_t getNumDims() const
Definition: MDGeometry.cpp:148
bool hasOriginalWorkspace(size_t index=0) const
Definition: MDGeometry.cpp:320
std::shared_ptr< Workspace > getOriginalWorkspace(size_t index=0) const
Get the "original" workspace (the workspace that was the source for a binned MDWorkspace).
Definition: MDGeometry.cpp:340
Base Workspace Abstract Class.
Definition: Workspace.h:30
virtual const std::string getTitle() const
Get the workspace title.
Definition: Workspace.cpp:46
An "ImplicitFunction" defining a hyper-cuboid-shaped region in N dimensions.
virtual const std::string id() const =0
A string ID for the class.
The concrete, templated class for properties.
Manage the lifetime of a class intended to be a singleton.
const TYPE * getBareArray() const
Definition: VMD.cpp:248
TYPE norm() const
Definition: VMD.cpp:420
std::shared_ptr< const IMDWorkspace > IMDWorkspace_const_sptr
Shared pointer to the IMDWorkspace base class (const version)
Definition: IMDWorkspace.h:148
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
Definition: IMDWorkspace.h:146
MDNormalization
Enum describing different ways to normalize the signal in a MDWorkspace.
Definition: IMDIterator.h:25
@ NoNormalization
Don't normalize = return raw counts.
Definition: IMDIterator.h:27
MANTID_KERNEL_DLL V3D normalize(V3D v)
Normalizes a V3D.
Definition: V3D.h:341
Helper class which provides the Collimation Length for SANS instruments.
float coord_t
Typedef for the data type to use for coordinate axes in MD objects such as MDBox, MDEventWorkspace,...
Definition: MDTypes.h:27
double signal_t
Typedef for the signal recorded in a MDBox, etc.
Definition: MDTypes.h:36
Holds X, Y, E for a line plot.
Definition: IMDWorkspace.h:48
std::vector< signal_t > y
Definition: IMDWorkspace.h:50
std::vector< signal_t > e
Definition: IMDWorkspace.h:51