Mantid
Loading...
Searching...
No Matches
SeqDomainSpectrumCreator.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 +
10#include "MantidAPI/Workspace.h"
17#include "MantidKernel/Matrix.h"
18
19namespace Mantid::CurveFitting {
20
21using namespace API;
22
35 const std::string &workspacePropertyName)
36 : IDomainCreator(manager, std::vector<std::string>(1, workspacePropertyName), SeqDomainSpectrumCreator::Sequential),
37 m_workspacePropertyName(m_workspacePropertyNames.front()), m_matrixWorkspace() {}
38
50void SeqDomainSpectrumCreator::createDomain(std::shared_ptr<FunctionDomain> &domain,
51 std::shared_ptr<FunctionValues> &values, size_t i0) {
53
54 if (!m_matrixWorkspace) {
55 throw std::invalid_argument("No matrix workspace assigned - can not create domain.");
56 }
57
59
60 size_t numberOfHistograms = m_matrixWorkspace->getNumberHistograms();
61 for (size_t i = 0; i < numberOfHistograms; ++i) {
62 if (histogramIsUsable(i)) {
63 auto spectrumDomain = new FunctionDomain1DSpectrumCreator;
65 spectrumDomain->setWorkspaceIndex(i);
66
67 seqDomain->addCreator(IDomainCreator_sptr(spectrumDomain));
68 }
69 }
70
71 domain.reset(seqDomain);
72
73 if (!values) {
74 values.reset(new FunctionValues(*domain));
75 } else {
76 values->expand(i0 + domain->size());
77 }
78}
79
101 std::shared_ptr<FunctionDomain> domain,
102 std::shared_ptr<FunctionValues> values,
103 const std::string &outputWorkspacePropertyName) {
104 // don't need values, since the values need to be calculated spectrum by
105 // spectrum (see loop below).
106 UNUSED_ARG(values);
107
108 std::shared_ptr<SeqDomain> seqDomain = std::dynamic_pointer_cast<SeqDomain>(domain);
109
110 if (!seqDomain) {
111 throw std::invalid_argument("CreateOutputWorkspace requires SeqDomain.");
112 }
113
114 if (!m_matrixWorkspace) {
115 throw std::invalid_argument("No MatrixWorkspace assigned. Cannot construct "
116 "proper output workspace.");
117 }
118
119 MatrixWorkspace_sptr outputWs =
120 std::dynamic_pointer_cast<MatrixWorkspace>(WorkspaceFactory::Instance().create(m_matrixWorkspace));
121
122 // Assign y-values, taking into account masked detectors
123 for (size_t i = 0; i < seqDomain->getNDomains(); ++i) {
124 FunctionDomain_sptr localDomain;
125 FunctionValues_sptr localValues;
126
127 seqDomain->getDomainAndValues(i, localDomain, localValues);
128 function->function(*localDomain, *localValues);
129
130 std::shared_ptr<FunctionDomain1DSpectrum> spectrumDomain =
131 std::dynamic_pointer_cast<FunctionDomain1DSpectrum>(localDomain);
132
133 if (spectrumDomain) {
134 size_t wsIndex = spectrumDomain->getWorkspaceIndex();
135
136 auto &yValues = outputWs->mutableY(wsIndex);
137 for (size_t j = 0; j < yValues.size(); ++j) {
138 yValues[j] = localValues->getCalculated(j);
139 }
140 }
141 }
142
143 // Assign x-values on all histograms
144 for (size_t i = 0; i < m_matrixWorkspace->getNumberHistograms(); ++i) {
145 outputWs->setSharedX(i, m_matrixWorkspace->sharedX(i));
146 }
147
148 if (m_manager && !outputWorkspacePropertyName.empty()) {
150 "Result workspace");
151
152 m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspace");
153 m_manager->setProperty(outputWorkspacePropertyName, outputWs);
154 }
155
156 // If the input is a not an EventWorkspace and is a distrubution, then convert
157 // the output also to a distribution
158 if (!std::dynamic_pointer_cast<Mantid::API::IEventWorkspace>(m_matrixWorkspace)) {
159 if (m_matrixWorkspace->isDistribution()) {
160 outputWs->setDistribution(true);
161 }
162 }
163
164 return outputWs;
165}
166
173 if (!m_matrixWorkspace) {
174 throw std::invalid_argument("No matrix workspace assigned.");
175 }
176
177 size_t nHist = m_matrixWorkspace->getNumberHistograms();
178 size_t totalSize = 0;
179
180 for (size_t i = 0; i < nHist; ++i) {
181 totalSize += m_matrixWorkspace->y(i).size();
182 }
183
184 return totalSize;
185}
186
189 if (m_manager) {
191
192 setMatrixWorkspace(std::dynamic_pointer_cast<MatrixWorkspace>(workspace));
193 }
194}
195
198 if (!matrixWorkspace) {
199 throw std::invalid_argument("InputWorkspace must be a valid MatrixWorkspace.");
200 }
201
202 m_matrixWorkspace = matrixWorkspace;
203}
204
208 if (!m_matrixWorkspace) {
209 throw std::invalid_argument("No matrix workspace assigned.");
210 }
211
212 const auto &spectrumInfo = m_matrixWorkspace->spectrumInfo();
213
214 if (!spectrumInfo.hasDetectors(i)) {
215 return true;
216 }
217 return !spectrumInfo.isMasked(i);
218}
219
220} // namespace Mantid::CurveFitting
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
A class to store values calculated by a function.
An base class for domain creators for use in Fit.
Kernel::IPropertyManager * m_manager
Pointer to a property manager.
void declareProperty(Kernel::Property *prop, const std::string &doc)
Declare a property to the algorithm.
DomainType m_domainType
Domain type.
A property class for workspaces.
void setMatrixWorkspace(API::MatrixWorkspace_sptr matrixWorkspace)
Sets the matrix workspace this creator is working with.
size_t getDomainSize() const override
Returns the domain size.
void setParametersFromPropertyManager()
Tries to extract a workspace from the assigned property manager.
API::Workspace_sptr createOutputWorkspace(const std::string &baseName, API::IFunction_sptr function, std::shared_ptr< API::FunctionDomain > domain, std::shared_ptr< API::FunctionValues > values, const std::string &outputWorkspacePropertyName="OutputWorkspace") override
Creates an output workspace using the given function and domain.
SeqDomainSpectrumCreator(Kernel::IPropertyManager *manager, const std::string &workspacePropertyName)
Constructor.
void setMatrixWorkspace(const API::MatrixWorkspace_sptr &matrixWorkspace)
Sets the MatrixWorkspace the created domain is based on.
bool histogramIsUsable(size_t i) const
Determines whether a spectrum is masked, in case there is no instrument this always returns true.
void createDomain(std::shared_ptr< API::FunctionDomain > &domain, std::shared_ptr< API::FunctionValues > &values, size_t i0=0) override
Creates a sequential domain corresponding to the assigned MatrixWorkspace.
An implementation of CompositeDomain.
Definition: SeqDomain.h:30
void addCreator(const API::IDomainCreator_sptr &creator)
Add new domain creator.
Definition: SeqDomain.cpp:48
static SeqDomain * create(API::IDomainCreator::DomainType type)
Create an instance of SeqDomain in one of two forms: either SeqDomain for sequential domain creation ...
Definition: SeqDomain.cpp:60
Interface to PropertyManager.
virtual void setPropertyValue(const std::string &name, const std::string &value)=0
Sets property value from a string.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
virtual TypedValue getProperty(const std::string &name) const =0
Get the value of a property.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< FunctionValues > FunctionValues_sptr
typedef for a shared pointer
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< IFunction > IFunction_sptr
shared pointer to the function base class
Definition: IFunction.h:732
std::shared_ptr< IDomainCreator > IDomainCreator_sptr
Typedef for a shared pointer to IDomainCreator.
std::shared_ptr< FunctionDomain > FunctionDomain_sptr
typedef for a shared pointer
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::unique_ptr< T > create(const P &parent, const IndexArg &indexArg, const HistArg &histArg)
This is the create() method that all the other create() methods call.
STL namespace.
@ Output
An output workspace.
Definition: Property.h:54