Mantid
Loading...
Searching...
No Matches
WorkspaceFactory.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 +
13#include "MantidAPI/Run.h"
14#include "MantidAPI/TextAxis.h"
15#include "MantidAPI/Workspace.h"
18
19namespace Mantid::API {
20namespace {
22Kernel::Logger g_log("WorkspaceFactory");
23} // namespace
24
25using std::size_t;
26
29 g_log.debug() << "WorkspaceFactory created.\n";
30}
31
58 size_t XLength, size_t YLength) const {
59 bool differentSize(true);
60 // Use the parent sizes if new ones are not specified
61 if (NVectors == size_t(-1))
62 NVectors = parent->getNumberHistograms();
63 if (XLength == size_t(-1))
64 XLength = parent->dataX(0).size();
65 if (YLength == size_t(-1)) {
66 differentSize = false;
67 YLength = parent->blocksize();
68 }
69
70 // If the parent is an EventWorkspace, we want it to spawn a Workspace2D (or
71 // managed variant) as a child
72 std::string id(parent->id());
73 if (id == "EventWorkspace")
74 id = "Workspace2D";
75
76 // Create an 'empty' workspace of the appropriate type and size
77 MatrixWorkspace_sptr ws = create(id, NVectors, XLength, YLength);
78
79 // Copy over certain parent data members
80 initializeFromParent(*parent, *ws, differentSize);
81
82 return ws;
83}
84
96 const bool differentSize) const {
97 child.setTitle(parent.getTitle());
98 child.setComment(parent.getComment());
99 child.copyExperimentInfoFrom(&parent);
100 child.setYUnit(parent.m_YUnit);
101 child.setYUnitLabel(parent.m_YUnitLabel);
102 child.setDistribution(parent.isDistribution());
103
104 // Only copy the axes over if new sizes are not given
105 if (!differentSize) {
106 // Only copy mask map if same size for now. Later will need to check
107 // continued validity.
108 child.m_masks = parent.m_masks;
109 }
110
111 // Same number of histograms = copy over the spectra data
112 if (parent.getNumberHistograms() == child.getNumberHistograms()) {
113 child.m_isInitialized = false;
114 for (size_t i = 0; i < parent.getNumberHistograms(); ++i)
115 child.getSpectrum(i).copyInfoFrom(parent.getSpectrum(i));
116 child.m_isInitialized = true;
117 // We use this variant without ISpectrum update to avoid costly rebuilds
118 // triggered by setIndexInfo(). ISpectrum::copyInfoFrom sets invalid flags
119 // for spectrum definitions, so it is important to call this *afterwards*,
120 // since it clears the flags:
122 }
123
124 // deal with axis
125 for (size_t i = 0; i < parent.m_axes.size(); ++i) {
126 if (parent.m_axes[i]->isSpectra()) {
127 // By default the child already has a spectra axis which
128 // does not need to get cloned from the parent.
129 continue;
130 }
131 const bool isBinEdge = dynamic_cast<const BinEdgeAxis *const>(parent.m_axes[i].get()) != nullptr;
132 const size_t newAxisLength = child.m_axes[i]->length() + (isBinEdge ? 1 : 0);
133 const size_t oldAxisLength = parent.m_axes[i]->length();
134
135 // Need to delete the existing axis created in init above
136
137 child.m_axes[i] = nullptr;
138 if (newAxisLength == oldAxisLength) {
139 // Now set to a copy of the parent workspace's axis
140 child.m_axes[i] = std::unique_ptr<Axis>(parent.m_axes[i]->clone(&child));
141 } else {
142 // Call the 'different length' clone variant
143 child.m_axes[i] = std::unique_ptr<Axis>(parent.m_axes[i]->clone(newAxisLength, &child));
144 }
145 }
146}
147
163MatrixWorkspace_sptr WorkspaceFactoryImpl::create(const std::string &className, const size_t &NVectors,
164 const size_t &XLength, const size_t &YLength) const {
165 MatrixWorkspace_sptr ws = std::dynamic_pointer_cast<MatrixWorkspace>(this->create(className));
166
167 if (!ws) {
168 g_log.error("Workspace was not created");
169 throw std::runtime_error("Workspace was not created");
170 }
171
172 ws->initialize(NVectors, XLength, YLength);
173 return ws;
174}
175
177ITableWorkspace_sptr WorkspaceFactoryImpl::createTable(const std::string &className) const {
179 try {
180 ws = std::dynamic_pointer_cast<ITableWorkspace>(this->create(className));
181 if (!ws) {
182 throw std::runtime_error("Class " + className + " cannot be cast to ITableWorkspace");
183 }
185 throw;
186 }
187 return ws;
188}
189
191IPeaksWorkspace_sptr WorkspaceFactoryImpl::createPeaks(const std::string &className) const {
193 try {
194 ws = std::dynamic_pointer_cast<IPeaksWorkspace>(this->create(className));
195 if (!ws) {
196 throw std::runtime_error("Class " + className + " cannot be cast to IPeaksWorkspace");
197 }
199 throw;
200 }
201 return ws;
202}
203
204} // namespace Mantid::API
Stores numeric values that are assumed to be bin edge values.
Definition: BinEdgeAxis.h:20
void copyExperimentInfoFrom(const ExperimentInfo *other)
Copy everything from the given experiment object.
void copyInfoFrom(const ISpectrum &other)
Copy spectrum number and detector IDs, but not X vector, from another ISpectrum.
Definition: ISpectrum.cpp:24
Base MatrixWorkspace Abstract Class.
virtual ISpectrum & getSpectrum(const size_t index)=0
Return the underlying ISpectrum ptr at the given workspace index.
std::vector< std::unique_ptr< Axis > > m_axes
A vector of pointers to the axes for this workspace.
bool m_isInitialized
Has this workspace been initialised?
std::string m_YUnit
The unit for the data values (e.g. Counts)
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
void setTitle(const std::string &) override
Sets MatrixWorkspace title.
std::map< int64_t, MaskList > m_masks
The set of masked bins in a map keyed on workspace index.
bool isDistribution() const
Are the Y-values dimensioned?
const std::string getTitle() const override
Gets MatrixWorkspace title (same as Run object run_title property)
void setDistribution(bool newValue)
Set the flag for whether the Y-values are dimensioned.
const Indexing::IndexInfo & indexInfo() const
Returns a const reference to the IndexInfo object of the workspace.
void setIndexInfoWithoutISpectrumUpdate(const Indexing::IndexInfo &indexInfo)
Variant of setIndexInfo, used by WorkspaceFactoryImpl.
void setYUnitLabel(const std::string &newLabel)
Sets a new caption for the data (Y axis) in the workspace.
void setYUnit(const std::string &newUnit)
Sets a new unit for the data (Y axis) in the workspace.
std::string m_YUnitLabel
A text label for use when plotting spectra.
void initializeFromParent(const MatrixWorkspace &parent, MatrixWorkspace &child, const bool differentSize) const
Initialize a workspace from its parent This sets values such as title, instrument,...
std::shared_ptr< ITableWorkspace > createTable(const std::string &className="TableWorkspace") const
Create a ITableWorkspace.
WorkspaceFactoryImpl()
Private Constructor for singleton class.
std::shared_ptr< IPeaksWorkspace > createPeaks(const std::string &className="PeaksWorkspace") const
Create a IPeaksWorkspace.
MatrixWorkspace_sptr create(const MatrixWorkspace_const_sptr &parent, size_t NVectors=size_t(-1), size_t XLength=size_t(-1), size_t YLength=size_t(-1)) const
Create a new instance of the same type of workspace as that given as argument.
Base Workspace Abstract Class.
Definition: Workspace.h:30
const std::string & getComment() const
Get the workspace comment.
Definition: Workspace.cpp:52
void setComment(const std::string &)
Set the comment field of the workspace.
Definition: Workspace.cpp:34
The dynamic factory is a base dynamic factory for serving up objects in response to requests from oth...
Exception for when an item is not found in a collection.
Definition: Exception.h:145
void debug(const std::string &msg)
Logs at debug level.
Definition: Logger.cpp:114
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
Helper class which provides the Collimation Length for SANS instruments.