Mantid
Loading...
Searching...
No Matches
ConvertTableToMatrixWorkspace.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//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Axis.h"
17#include "MantidKernel/Unit.h"
19
20namespace Mantid::Algorithms {
21
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(ConvertTableToMatrixWorkspace)
24
25using namespace Kernel;
26using namespace API;
27using namespace HistogramData;
28using namespace DataObjects;
29
31 declareProperty(std::make_unique<WorkspaceProperty<API::ITableWorkspace>>("InputWorkspace", "", Direction::Input),
32 "An input TableWorkspace.");
33 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
34 "An output Workspace2D.");
35 declareProperty("ColumnX", "", std::make_shared<MandatoryValidator<std::string>>(),
36 "The column name for the X vector.");
37 declareProperty("ColumnY", "", std::make_shared<MandatoryValidator<std::string>>(),
38 "The column name for the Y vector.");
39 declareProperty("ColumnE", "", "The column name for the E vector (optional).");
40}
41
43 ITableWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
44 std::string columnX = getProperty("ColumnX");
45 std::string columnY = getProperty("ColumnY");
46 std::string columnE = getProperty("ColumnE");
47
48 const size_t nrows = inputWorkspace->rowCount();
49 if (nrows == 0) {
50 throw std::runtime_error("The input table is empty");
51 }
52
53 const auto X = inputWorkspace->getColumn(columnX)->numeric_fill<>();
54 const auto Y = inputWorkspace->getColumn(columnY)->numeric_fill<>();
55
56 MatrixWorkspace_sptr outputWorkspace = create<Workspace2D>(1, Points(nrows));
57
58 outputWorkspace->mutableX(0).assign(X.cbegin(), X.cend());
59 outputWorkspace->mutableY(0).assign(Y.cbegin(), Y.cend());
60
61 if (!columnE.empty()) {
62 outputWorkspace->mutableE(0) = inputWorkspace->getColumn(columnE)->numeric_fill<>();
63 }
64
65 auto labelX = std::dynamic_pointer_cast<Units::Label>(UnitFactory::Instance().create("Label"));
66 labelX->setLabel(columnX);
67 outputWorkspace->getAxis(0)->unit() = labelX;
68
69 outputWorkspace->setYUnitLabel(columnY);
70
71 setProperty("OutputWorkspace", outputWorkspace);
72}
73
74} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Validator to check that a property is not left empty.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< const ITableWorkspace > ITableWorkspace_const_sptr
shared pointer to Mantid::API::ITableWorkspace (const version)
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.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54