Mantid
Loading...
Searching...
No Matches
FunctionQDepends.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// Mantid Coding standars <http://www.mantidproject.org/Coding_Standards>
8
9// Main Module Header
11// Mantid Headers from the same project
12// N/A
13// Mantid headers from other projects
19// third party libraries
20// N/A
21// standard library
22// N/A
23
25
26namespace {
27Mantid::Kernel::Logger g_log("FunctionQDepends");
28}
29
31
32/* ===========
33 Public
34 ===========*/
35
44 this->declareAttribute("Q", Attr(EMPTY_DBL()));
45 this->declareAttribute("WorkspaceIndex", Attr(EMPTY_INT()));
46}
47
67void FunctionQDepends::setAttribute(const std::string &attName, const Attr &attValue) {
68 // Q value is tied to WorkspaceIndex if we have a list of Q values
69 if (attName == "WorkspaceIndex") {
70 size_t wi{static_cast<size_t>(attValue.asInt())}; // ah!, the "joys" of C++ strong typing.
71 if (!m_vQ.empty() && wi < m_vQ.size()) {
72 Mantid::API::IFunction::setAttribute(attName, attValue);
73 auto qAttr = getAttribute("Q");
74 qAttr.setDouble(m_vQ.at(wi));
75 // Can't call setAttributeValue, as it will recurse back into here
77 }
78 }
79 // Q can be manually changed by user only if list of Q values is empty
80 else if (attName == "Q") {
81 if (m_vQ.empty()) {
82 Mantid::API::IFunction::setAttribute(attName, attValue);
83 }
84 } else {
85 Mantid::API::IFunction::setAttribute(attName, attValue);
86 }
87}
88
97void FunctionQDepends::setMatrixWorkspace(std::shared_ptr<const Mantid::API::MatrixWorkspace> workspace, size_t wi,
98 double startX, double endX) {
99 UNUSED_ARG(startX);
100 UNUSED_ARG(endX);
101 // reset attributes if new workspace is passed
102 if (!m_vQ.empty()) {
105 }
106 // Obtain Q values from the passed workspace, if possible. m_vQ will be
107 // cleared if unsuccessful.
108 if (workspace) {
109 m_vQ = this->extractQValues(*workspace);
110 }
111 if (!m_vQ.empty()) {
112 this->setAttributeValue("WorkspaceIndex", static_cast<int>(wi));
113 }
114}
115
116/* ===========
117 Private
118 ===========*/
119
126 std::vector<double> qs;
127 // Check if the vertical axis has units of momentum transfer, then extract Q
128 // values...
129 auto axis_ptr = dynamic_cast<Mantid::API::NumericAxis *>(workspace.getAxis(1));
130 if (axis_ptr) {
131 const std::shared_ptr<Kernel::Unit> &unit_ptr = axis_ptr->unit();
132 if (unit_ptr->unitID() == "MomentumTransfer") {
133 qs = axis_ptr->getValues();
134 }
135 }
136 // ...otherwise, compute the momentum transfer for each spectrum, if possible
137 else {
138 const auto &spectrumInfo = workspace.spectrumInfo();
139 size_t numHist = workspace.getNumberHistograms();
140 for (size_t wi = 0; wi < numHist; wi++) {
141 if (spectrumInfo.hasDetectors(wi)) {
142 const auto detID = spectrumInfo.detector(wi).getID();
143 double efixed = workspace.getEFixed(detID);
144 double usignTheta = 0.5 * spectrumInfo.twoTheta(wi);
146 qs.emplace_back(q);
147 } else {
148 g_log.debug("Cannot populate Q values from workspace");
149 qs.clear();
150 break;
151 }
152 }
153 }
154 return qs;
155}
156
157} // namespace Mantid::CurveFitting::Functions
Mantid::API::IFunction::Attribute Attr
IPeaksWorkspace_sptr workspace
Definition: IndexPeaks.cpp:114
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition: System.h:64
const std::shared_ptr< Kernel::Unit > & unit() const
The unit for this axis.
Definition: Axis.cpp:28
static Kernel::Logger g_log
Logger instance.
Definition: IFunction1D.h:74
Attribute is a non-fitting parameter.
Definition: IFunction.h:282
int asInt() const
Returns int value if attribute is a int, throws exception otherwise.
Definition: IFunction.cpp:726
virtual Attribute getAttribute(const std::string &name) const
Return a value of attribute attName.
Definition: IFunction.cpp:1394
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
Definition: IFunction.cpp:1418
virtual void setAttribute(const std::string &name, const Attribute &)
Set a value to attribute attName.
Definition: IFunction.cpp:1409
void setAttributeValue(const std::string &attName, const T &value)
Set an attribute value.
Definition: IFunction.h:597
Base MatrixWorkspace Abstract Class.
Class to represent a numeric axis of a workspace.
Definition: NumericAxis.h:29
std::vector< double > extractQValues(const Mantid::API::MatrixWorkspace &workspace)
Extract Q values from vertical dimension of the workspace, or compute them.
void setMatrixWorkspace(std::shared_ptr< const Mantid::API::MatrixWorkspace > workspace, size_t wi, double startX, double endX) override
Learn the Q values from the workspace, if possible, and update attribute Q accordingly.
virtual void setAttribute(const std::string &attName, const Mantid::API::IFunction::Attribute &attValue) override
Update attributes WorkspaceIndex and Q according to certain precedence rules.
virtual void declareAttributes() override
declare commonattributes Q and WorkspaceIndex.
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
static double convertToElasticQ(const double theta, const double efixed)
Convert to ElasticQ from Energy.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43