Mantid
Loading...
Searching...
No Matches
FunctionDomain1DSpectrumCreator.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#include <utility>
8
10#include "MantidHistogramData/BinEdges.h"
11#include "MantidHistogramData/Points.h"
12
14
15namespace Mantid::CurveFitting {
16
17using namespace API;
18using namespace HistogramData;
23 : IDomainCreator(nullptr, std::vector<std::string>(), FunctionDomain1DSpectrumCreator::Simple), m_matrixWorkspace(),
24 m_workspaceIndex(0), m_workspaceIndexIsSet(false) {}
25
31 m_matrixWorkspace = std::move(matrixWorkspace);
32}
33
40 m_workspaceIndex = workspaceIndex;
41
43}
44
60void FunctionDomain1DSpectrumCreator::createDomain(std::shared_ptr<FunctionDomain> &domain,
61 std::shared_ptr<FunctionValues> &values, size_t i0) {
63
64 auto points = m_matrixWorkspace->points(m_workspaceIndex);
65 domain.reset(new FunctionDomain1DSpectrum(m_workspaceIndex, points.mutableRawData()));
66
67 if (!values) {
68 values.reset(new FunctionValues(*domain));
69 } else {
70 values->expand(i0 + domain->size());
71 }
72
73 const auto &yData = m_matrixWorkspace->y(m_workspaceIndex);
74 const auto &eData = m_matrixWorkspace->e(m_workspaceIndex);
75
76 for (size_t i = 0; i < yData.size(); ++i) {
77 values->setFitData(i, yData[i]);
78 values->setFitWeight(i, 1.0 / (eData[i] != 0 ? eData[i] : 1.0));
79 }
80}
81
93
94 size_t numberOfXValues = m_matrixWorkspace->readX(m_workspaceIndex).size();
95
96 if (m_matrixWorkspace->isHistogramData()) {
97 return numberOfXValues - 1;
98 }
99
100 return numberOfXValues;
101}
102
105 if (!m_matrixWorkspace) {
106 throw std::invalid_argument("No matrix workspace assigned or does not "
107 "contain histogram data - cannot create "
108 "domain.");
109 }
110
111 if (!m_workspaceIndexIsSet || m_workspaceIndex >= m_matrixWorkspace->getNumberHistograms()) {
112 throw std::invalid_argument("Workspace index has not been set or is invalid.");
113 }
114}
115
116} // namespace Mantid::CurveFitting
Specialization of FunctionDomain1DVector for spectra of MatrixWorkspaces.
A class to store values calculated by a function.
An base class for domain creators for use in Fit.
void createDomain(std::shared_ptr< API::FunctionDomain > &domain, std::shared_ptr< API::FunctionValues > &values, size_t i0=0) override
Creates the domain according to the stored MatrixWorkspace and workspace index.
void throwIfWorkspaceInvalid() const
Checks the assigned MatrixWorkspace and workspace index.
size_t getDomainSize() const override
Returns the domain sizes.
void setWorkspaceIndex(size_t workspaceIndex)
Sets the workspace index for the created domain.
void setMatrixWorkspace(API::MatrixWorkspace_sptr matrixWorkspace)
Sets the matrix workspace this creator is working with.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
STL namespace.