-
Notifications
You must be signed in to change notification settings - Fork 121
/
ConvertTableToMatrixWorkspace.cpp
81 lines (65 loc) · 2.78 KB
/
ConvertTableToMatrixWorkspace.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/ConvertTableToMatrixWorkspace.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitFactory.h"
#include <boost/mpl/if.hpp>
#include <boost/type_traits.hpp>
#include <sstream>
namespace Mantid {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ConvertTableToMatrixWorkspace)
using namespace Kernel;
using namespace API;
using namespace HistogramData;
void ConvertTableToMatrixWorkspace::init() {
declareProperty(make_unique<WorkspaceProperty<API::ITableWorkspace>>(
"InputWorkspace", "", Direction::Input),
"An input TableWorkspace.");
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output),
"An output Workspace2D.");
declareProperty("ColumnX", "",
boost::make_shared<MandatoryValidator<std::string>>(),
"The column name for the X vector.");
declareProperty("ColumnY", "",
boost::make_shared<MandatoryValidator<std::string>>(),
"The column name for the Y vector.");
declareProperty("ColumnE", "",
"The column name for the E vector (optional).");
}
void ConvertTableToMatrixWorkspace::exec() {
ITableWorkspace_const_sptr inputWorkspace = getProperty("InputWorkspace");
std::string columnX = getProperty("ColumnX");
std::string columnY = getProperty("ColumnY");
std::string columnE = getProperty("ColumnE");
size_t nrows = inputWorkspace->rowCount();
if (nrows == 0) {
throw std::runtime_error("The input table is empty");
}
auto X = inputWorkspace->getColumn(columnX)->numeric_fill<>();
auto Y = inputWorkspace->getColumn(columnY)->numeric_fill<>();
MatrixWorkspace_sptr outputWorkspace =
WorkspaceFactory::Instance().create("Workspace2D", 1, nrows, nrows);
outputWorkspace->mutableX(0).assign(X.begin(), X.end());
outputWorkspace->mutableY(0).assign(Y.begin(), Y.end());
if (!columnE.empty()) {
outputWorkspace->mutableE(0) =
inputWorkspace->getColumn(columnE)->numeric_fill<>();
}
auto labelX = boost::dynamic_pointer_cast<Units::Label>(
UnitFactory::Instance().create("Label"));
labelX->setLabel(columnX);
outputWorkspace->getAxis(0)->unit() = labelX;
outputWorkspace->setYUnitLabel(columnY);
setProperty("OutputWorkspace", outputWorkspace);
}
} // namespace Algorithms
} // namespace Mantid