Mantid
Loading...
Searching...
No Matches
GetQsInQENSData.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 +
8
13#include "MantidKernel/Unit.h"
15
16#include <stdexcept>
17
18namespace {
19Mantid::Kernel::Logger g_log("ConvolutionFitSequential");
20}
21
22namespace Mantid::Algorithms {
23
24using namespace API;
25using namespace Kernel;
26using namespace Geometry;
27
28// Register the Algorithm into the AlgorithmFactory
29DECLARE_ALGORITHM(GetQsInQENSData)
30
31// Initializes the Algorithm
32void GetQsInQENSData::init() {
33 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
34 "Input QENS data as MatrixWorkspace");
35
36 declareProperty("RaiseMode", false,
37 "Set to True if an Exception, instead of "
38 "any empty list of Q values, is "
39 "desired.");
40
41 declareProperty(std::make_unique<ArrayProperty<double>>("Qvalues", Direction::Output));
42}
43
44/*
45 * Validates the input properties
46 */
47std::map<std::string, std::string> GetQsInQENSData::validateInputs() {
48 std::map<std::string, std::string> issues;
49 MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
50
51 // Check whether the input workspace could be found
52 if (!inputWs) {
53 issues["InputWorkspace"] = "InputWorkspace is not a MatrixWorkspace";
54 }
55
56 return issues;
57}
58
59/*
60 * Executes the Algorithm
61 */
63 MatrixWorkspace_sptr inputWs = getProperty("InputWorkspace");
64
65 try {
66 setProperty("Qvalues", extractQValues(inputWs));
67 } catch (std::exception &e) {
68 g_log.error(e.what());
69
70 // If the RaiseMode property has been set to true, raise any
71 // exception which is thrown.
72 bool inRaiseMode = getProperty("RaiseMode");
73 if (inRaiseMode) {
74 throw;
75 }
76
77 setProperty("Qvalues", std::vector<double>());
78 }
79}
80
81/*
82 * Extracts Q-values from the specified workspace.
83 *
84 * @param workspace The workspace from which to extract Q-values.
85 * @return The extracted Q-values as a vector.
86 */
88 size_t numSpectra = workspace->getNumberHistograms();
89 Axis *qAxis;
90
91 try {
92 qAxis = workspace->getAxis(1);
93 } catch (std::exception &) {
94 throw std::runtime_error("Vertical axis is empty");
95 }
96
97 MantidVec qValues(qAxis->length());
98
99 // Check if the specified workspace is already in Q-space.
100 if (qAxis->unit()->unitID() == "MomentumTransfer") {
101
102 // Add axis values to vector of Q-values
103 for (size_t i = 0; i < qAxis->length(); i++) {
104 qValues[i] = qAxis->getValue(i);
105 }
106
107 // Check if the Q-values are stored as histogram data.
108 if (qValues.size() == numSpectra + 1) {
109 // Convert Q-values to point values.
110 qValues.pop_back();
111 qValues.erase(qValues.begin());
112 using std::placeholders::_1;
113 std::transform(qValues.begin(), qValues.end(), qValues.begin(), std::bind(std::divides<double>(), _1, 2.0));
114 }
115 } else {
116
117 // Iterate over all spectrum in the specified workspace.
118 try {
119
120 for (size_t i = 0; i < numSpectra; i++) {
121 IDetector_const_sptr detector = workspace->getDetector(i);
122 double efixed = workspace->getEFixed(detector);
123 double theta = 0.5 * workspace->detectorTwoTheta(*detector);
124 qValues[i] = UnitConversion::convertToElasticQ(theta, efixed);
125 }
126 } catch (std::exception &) {
127 throw std::runtime_error("Detectors are missing from the input workspace");
128 }
129 }
130
131 return qValues;
132}
133} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
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
Class to represent the axis of a workspace.
Definition: Axis.h:30
virtual std::size_t length() const =0
Get the length of the axis.
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
Definition: Axis.cpp:28
double getValue(const std::size_t &index, const std::size_t &verticalIndex=0) const
Gets the value at the specified index.
Definition: Axis.cpp:51
A property class for workspaces.
Extracts Q-values from a workspace containing QENS data.
MantidVec extractQValues(const Mantid::API::MatrixWorkspace_sptr &workspace)
Extracts Q-values from the specified matrix workspace.
std::map< std::string, std::string > validateInputs() override
Cross-check properties with each other.
void exec() override
Execution code.
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
static double convertToElasticQ(const double theta, const double efixed)
Convert to ElasticQ from Energy.
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< const Mantid::Geometry::IDetector > IDetector_const_sptr
Shared pointer to IDetector (const version)
Definition: IDetector.h:102
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition: cow_ptr.h:172
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54