Mantid
Loading...
Searching...
No Matches
SortPeaksWorkspace.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 +
9
10using namespace Mantid::Kernel;
11using namespace Mantid::API;
12using namespace Mantid::DataObjects;
13
14namespace Mantid::Crystal {
15
16// Register the algorithm into the AlgorithmFactory
17DECLARE_ALGORITHM(SortPeaksWorkspace)
18
19//----------------------------------------------------------------------------------------------
21const std::string SortPeaksWorkspace::name() const { return "SortPeaksWorkspace"; }
22
24int SortPeaksWorkspace::version() const { return 1; }
25
27const std::string SortPeaksWorkspace::category() const { return "Crystal\\Peaks;Utility\\Sorting"; }
28
29//----------------------------------------------------------------------------------------------
30
31//----------------------------------------------------------------------------------------------
35 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("InputWorkspace", "", Direction::Input),
36 "An input workspace.");
37 declareProperty(std::make_unique<WorkspaceProperty<IPeaksWorkspace>>("OutputWorkspace", "", Direction::Output),
38 "An output workspace.");
39
40 auto mustHave = std::make_shared<MandatoryValidator<std::string>>();
41 declareProperty("ColumnNameToSortBy", "", mustHave, "Column to sort by");
42
43 declareProperty("SortAscending", true, "Sort the OutputWorkspace by the target column in a Ascending fashion.");
44}
45
46//----------------------------------------------------------------------------------------------
50 const std::string columnToSortBy = getProperty("ColumnNameToSortBy");
51 const bool sortAscending = getProperty("SortAscending");
52 IPeaksWorkspace_sptr inputWS = getProperty("InputWorkspace");
53 IPeaksWorkspace_sptr outputWS = getProperty("OutputWorkspace");
54
55 // Try to get the column. This will throw if the column does not exist.
56 inputWS->getColumn(columnToSortBy);
57
58 if (inputWS != outputWS) {
59 outputWS = inputWS->clone();
60 }
61
62 std::vector<PeaksWorkspace::ColumnAndDirection> sortCriteria;
63 sortCriteria.emplace_back(columnToSortBy, sortAscending);
64 outputWS->sort(sortCriteria);
65 setProperty("OutputWorkspace", outputWS);
66}
67
68} // namespace Mantid::Crystal
#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.
SortPeaksWorkspace : Sort a PeaksWorkspace by a range of properties.
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
void init() override
Initialize the algorithm's properties.
void exec() override
Execute the algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< IPeaksWorkspace > IPeaksWorkspace_sptr
shared pointer to Mantid::API::IPeaksWorkspace
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54