11#include "MantidHistogramData/Histogram.h"
32 return "Clones the input MatrixWorkspace(s) and orders the x-axis in an "
33 "ascending or descending fashion.";
41 "Sorted Output Workspace");
43 auto orderingValues = std::vector<std::string>({
"Ascending",
"Descending"});
44 auto orderingValidator = std::make_shared<StringListValidator>(orderingValues);
45 declareProperty(
"Ordering", orderingValues[0], orderingValidator,
"Ascending or descending sorting",
57 const auto sizeOfX = inputWorkspace->x(0).size();
60 for (
int specNum = 0u; specNum < (int)inputWorkspace->getNumberHistograms(); specNum++) {
66 copyToOutputWorkspace(workspaceIndices, *inputWorkspace, *outputWorkspace, specNum, isAProperHistogram);
81 std::vector<std::size_t> workspaceIndices;
82 workspaceIndices.reserve(sizeOfX);
83 for (
auto workspaceIndex = 0u; workspaceIndex < sizeOfX; workspaceIndex++) {
84 workspaceIndices.emplace_back(workspaceIndex);
86 return workspaceIndices;
99template <
typename Comparator>
101 unsigned int specNum, Comparator
const &
compare) {
102 std::sort(workspaceIndices.begin(), workspaceIndices.end(), [&](std::size_t lhs, std::size_t
rhs) ->
bool {
103 return compare(inputWorkspace.x(specNum)[lhs], inputWorkspace.x(specNum)[rhs]);
109 if (order ==
"Ascending") {
110 sortByXValue(workspaceIndices, inputWorkspace, specNum, std::less<double>());
111 }
else if (order ==
"Descending") {
112 sortByXValue(workspaceIndices, inputWorkspace, specNum, std::greater<double>());
130 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
x(specNum).size(); workspaceIndex++) {
131 outputWorkspace.
mutableX(specNum)[workspaceIndex] = inputWorkspace.
x(specNum)[workspaceIndices[workspaceIndex]];
136 if (inputWorkspace.
hasDx(specNum)) {
137 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
dx(specNum).size(); workspaceIndex++) {
138 outputWorkspace.
mutableDx(specNum)[workspaceIndex] = inputWorkspace.
dx(specNum)[workspaceIndices[workspaceIndex]];
158 bool isAProperHistogram) {
161 if (isAProperHistogram) {
162 auto lastIndexIt = std::find(workspaceIndices.begin(), workspaceIndices.end(), inputWorkspace.
y(specNum).size());
163 workspaceIndices.erase(lastIndexIt);
166 const auto &inSpaceY = inputWorkspace.
y(specNum);
167 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
y(specNum).size(); workspaceIndex++) {
168 outputWorkspace.
mutableY(specNum)[workspaceIndex] = inSpaceY[workspaceIndices[workspaceIndex]];
171 const auto &inSpaceE = inputWorkspace.
e(specNum);
172 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
e(specNum).size(); workspaceIndex++) {
173 outputWorkspace.
mutableE(specNum)[workspaceIndex] = inSpaceE[workspaceIndices[workspaceIndex]];
180 bool isAProperHistogram) {
196template <
typename Comparator>
199 if (!std::is_sorted(inputWorkspace.
x(specNum).begin(), inputWorkspace.
x(specNum).end(),
200 [&](
double lhs,
double rhs) ->
bool { return compare(lhs, rhs); })) {
217 if (inputWorkspace.
x(0).size() != inputWorkspace.
y(0).size()) {
220 if (!
isItSorted(std::greater<double>(), inputWorkspace)) {
221 if (!
isItSorted(std::less<double>(), inputWorkspace)) {
222 throw std::runtime_error(
"The data entered contains an invalid histogram: histogram has an "
223 "unordered x-axis.");
#define DECLARE_ALGORITHM(classname)
const std::vector< double > & rhs
#define PARALLEL_START_INTERRUPT_REGION
Begins a block to skip processing is the algorithm has been interupted Note the end of the block if n...
#define PARALLEL_END_INTERRUPT_REGION
Ends a block to skip processing is the algorithm has been interupted Note the start of the block if n...
#define PARALLEL_FOR_IF(condition)
Empty definitions - to enable set your complier to enable openMP.
#define PARALLEL_CHECK_INTERRUPT_REGION
Adds a check after a Parallel region to see if it was interupted.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Base MatrixWorkspace Abstract Class.
const HistogramData::HistogramE & e(const size_t index) const
HistogramData::HistogramX & mutableX(const size_t index) &
const HistogramData::HistogramDx & dx(const size_t index) const
virtual std::size_t getNumberHistograms() const =0
Returns the number of histograms in the workspace.
const HistogramData::HistogramX & x(const size_t index) const
virtual bool hasDx(const std::size_t index) const
Probes if DX (X Error) values were set on a particular spectrum.
HistogramData::HistogramE & mutableE(const size_t index) &
HistogramData::HistogramDx & mutableDx(const size_t index) &
HistogramData::HistogramY & mutableY(const size_t index) &
const HistogramData::HistogramY & y(const size_t index) const
A property class for workspaces.
SortXAxis will take Histogram or Point data and reorder it based on the X Axis' values,...
std::vector< std::size_t > createIndexes(const size_t)
Gets a vector of numbers from 0 to the sizeOfX-1 and returns it.
void init() override
Virtual method - must be overridden by concrete algorithm.
bool determineIfHistogramIsValid(const Mantid::API::MatrixWorkspace &inputWorkspace)
Determines whether it is a valid histogram or not.
void exec() override
Virtual method - must be overridden by concrete algorithm.
int version() const override
function to return a version of the algorithm, must be overridden in all algorithms
const std::string summary() const override
function returns a summary message that will be displayed in the default GUI, and in the help.
void copyYandEToOutputWorkspace(std::vector< std::size_t > &workspaceIndices, const Mantid::API::MatrixWorkspace &inputWorkspace, Mantid::API::MatrixWorkspace &outputWorkspace, unsigned int SpecNum, bool isAProperHistogram)
Copies the sorted inputworkspace into the output workspace without using clone because of how histogr...
void copyXandDxToOutputWorkspace(const std::vector< std::size_t > &workspaceIndices, const Mantid::API::MatrixWorkspace &inputWorkspace, Mantid::API::MatrixWorkspace &outputWorkspace, unsigned int specNum)
Copies the sorted inputworkspace into the output workspace without using clone because of how histogr...
void sortIndicesByX(std::vector< std::size_t > &workspaceIndices, const std::string &order, const Mantid::API::MatrixWorkspace &inputWorkspace, unsigned int specNum)
void copyToOutputWorkspace(std::vector< std::size_t > &workspaceIndices, const Mantid::API::MatrixWorkspace &inputWorkspace, Mantid::API::MatrixWorkspace &outputWorkspace, unsigned int specNum, bool isAProperHistogram)
const std::string category() const override
function to return a category of the algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
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
bool isItSorted(Comparator const &compare, const Mantid::API::MatrixWorkspace &inputWorkspace)
determines whether or not a given spectrum is sorted based on a passed comparator
void sortByXValue(std::vector< std::size_t > &workspaceIndices, const Mantid::API::MatrixWorkspace &inputWorkspace, unsigned int specNum, Comparator const &compare)
A template for sorting the values given a comparator.
bool compare(const mypair &left, const mypair &right)
std::enable_if< std::is_pointer< Arg >::value, bool >::type threadSafe(Arg workspace)
Thread-safety check Checks the workspace to ensure it is suitable for multithreaded access.
Helper class which provides the Collimation Length for SANS instruments.
@ Input
An input workspace.
@ Output
An output workspace.