-
Notifications
You must be signed in to change notification settings - Fork 122
/
SortPeaksWorkspace.cpp
101 lines (84 loc) · 3.48 KB
/
SortPeaksWorkspace.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "MantidCrystal/SortPeaksWorkspace.h"
#include "MantidKernel/MandatoryValidator.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
namespace Mantid {
namespace Crystal {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SortPeaksWorkspace)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string SortPeaksWorkspace::name() const {
return "SortPeaksWorkspace";
}
/// Algorithm's version for identification. @see Algorithm::version
int SortPeaksWorkspace::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SortPeaksWorkspace::category() const {
return "Crystal\\Peaks;Utility\\Sorting";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SortPeaksWorkspace::init() {
declareProperty(make_unique<WorkspaceProperty<IPeaksWorkspace>>(
"InputWorkspace", "", Direction::Input),
"An input workspace.");
declareProperty(make_unique<WorkspaceProperty<IPeaksWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"An output workspace.");
auto mustHave = boost::make_shared<MandatoryValidator<std::string>>();
declareProperty("ColumnNameToSortBy", "", mustHave, "Column to sort by");
declareProperty(
"SortAscending", true,
"Sort the OutputWorkspace by the target column in a Ascending fashion.");
}
PeaksWorkspace_sptr SortPeaksWorkspace::tryFetchOutputWorkspace() const {
IPeaksWorkspace_sptr temp = getProperty("OutputWorkspace");
PeaksWorkspace_sptr outputWS;
if (temp != nullptr) {
outputWS = boost::dynamic_pointer_cast<PeaksWorkspace>(temp);
if (outputWS == nullptr) {
throw std::invalid_argument("OutputWorkspace is not a PeaksWorkspace.");
}
}
return outputWS;
}
PeaksWorkspace_sptr SortPeaksWorkspace::tryFetchInputWorkspace() const {
IPeaksWorkspace_sptr temp = getProperty("InputWorkspace");
PeaksWorkspace_sptr inputWS =
boost::dynamic_pointer_cast<PeaksWorkspace>(temp);
if (inputWS == nullptr) {
throw std::invalid_argument("InputWorkspace is not a PeaksWorkspace.");
}
return inputWS;
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SortPeaksWorkspace::exec() {
const std::string columnToSortBy = getProperty("ColumnNameToSortBy");
const bool sortAscending = getProperty("SortAscending");
PeaksWorkspace_sptr inputWS = tryFetchInputWorkspace();
PeaksWorkspace_sptr outputWS = tryFetchOutputWorkspace();
try {
// Try to get the column. This will throw if the column does not exist.
inputWS->getColumn(columnToSortBy);
if (inputWS != outputWS) {
outputWS = inputWS->clone();
}
// Perform the sorting.
std::vector<std::pair<std::string, bool>> sortCriteria;
sortCriteria.push_back(
std::pair<std::string, bool>(columnToSortBy, sortAscending));
outputWS->sort(sortCriteria);
setProperty("OutputWorkspace", outputWS);
} catch (std::invalid_argument &) {
this->g_log.error("Specified ColumnToSortBy does not exist");
throw;
}
}
} // namespace Crystal
} // namespace Mantid