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
13#include "MantidKernel/Logger.h"
15
17
18namespace {
19Mantid::Kernel::Logger g_log("FunctionQDepends");
20}
21
23
24/* ===========
25 Public
26 ===========*/
27
36 this->declareAttribute("Q", Attr(EMPTY_DBL()));
37 this->declareAttribute("WorkspaceIndex", Attr(EMPTY_INT()));
38}
39
59void FunctionQDepends::setAttribute(const std::string &attName, const Attr &attValue) {
60 // Q value is tied to WorkspaceIndex if we have a list of Q values
61 if (attName == "WorkspaceIndex") {
62 size_t wi{static_cast<size_t>(attValue.asInt())}; // ah!, the "joys" of C++ strong typing.
63 if (!m_vQ.empty() && wi < m_vQ.size()) {
64 Mantid::API::IFunction::setAttribute(attName, attValue);
65 auto qAttr = getAttribute("Q");
66 qAttr.setDouble(m_vQ.at(wi));
67 // Can't call setAttributeValue, as it will recurse back into here
69 }
70 }
71 // Q can be manually changed by user only if list of Q values is empty
72 else if (attName == "Q") {
73 if (m_vQ.empty()) {
74 Mantid::API::IFunction::setAttribute(attName, attValue);
75 }
76 } else {
77 Mantid::API::IFunction::setAttribute(attName, attValue);
78 }
79}
80
89void FunctionQDepends::setMatrixWorkspace(std::shared_ptr<const Mantid::API::MatrixWorkspace> workspace, size_t wi,
90 double startX, double endX) {
91 UNUSED_ARG(startX);
92 UNUSED_ARG(endX);
93 // reset attributes if new workspace is passed
94 if (!m_vQ.empty()) {
97 }
98 // Obtain Q values from the passed workspace, if possible. m_vQ will be
99 // cleared if unsuccessful.
100 if (workspace) {
101 m_vQ = this->extractQValues(*workspace);
102 }
103 if (!m_vQ.empty()) {
104 this->setAttributeValue("WorkspaceIndex", static_cast<int>(wi));
105 }
106}
107
108/* ===========
109 Private
110 ===========*/
111
118 std::vector<double> qs;
119 // Check if the vertical axis has units of momentum transfer, then extract Q
120 // values...
121 auto axis_ptr = dynamic_cast<Mantid::API::NumericAxis *>(workspace.getAxis(1));
122 if (axis_ptr) {
123 const std::shared_ptr<Kernel::Unit> &unit_ptr = axis_ptr->unit();
124 if (unit_ptr->unitID() == "MomentumTransfer") {
125 qs = axis_ptr->getValues();
126 }
127 }
128 // ...otherwise, compute the momentum transfer for each spectrum, if possible
129 else {
130 const auto &spectrumInfo = workspace.spectrumInfo();
131 size_t numHist = workspace.getNumberHistograms();
132 for (size_t wi = 0; wi < numHist; wi++) {
133 if (spectrumInfo.hasDetectors(wi)) {
134 const auto detID = spectrumInfo.detector(wi).getID();
135 double efixed = workspace.getEFixed(detID);
136 double usignTheta = 0.5 * spectrumInfo.twoTheta(wi);
138 qs.emplace_back(q);
139 } else {
140 g_log.debug("Cannot populate Q values from workspace");
141 qs.clear();
142 break;
143 }
144 }
145 }
146 return qs;
147}
148
149} // namespace Mantid::CurveFitting::Functions
Mantid::API::IFunction::Attribute Attr
IPeaksWorkspace_sptr workspace
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
Definition System.h:44
const Kernel::Unit_sptr & unit() const
The unit for this axis.
Definition Axis.cpp:28
Attribute is a non-fitting parameter.
Definition IFunction.h:285
int asInt() const
Returns int value if attribute is a int, throws exception otherwise.
virtual Attribute getAttribute(const std::string &name) const
Return a value of attribute attName.
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
virtual void setAttribute(const std::string &name, const Attribute &)
Set a value to attribute attName.
void setAttributeValue(const std::string &attName, const T &value)
Set an attribute value.
Definition IFunction.h:601
Base MatrixWorkspace Abstract Class.
Class to represent a numeric axis of a workspace.
Definition NumericAxis.h:30
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:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
static double convertToElasticQ(const double theta, const double efixed)
Convert to ElasticQ from Energy.
Kernel::Logger g_log("ExperimentInfo")
static logger object
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition EmptyValues.h:24
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42