11#include "MantidHistogramData/Histogram.h"
33 return "Clones the input MatrixWorkspace(s) and orders the x-axis in an "
34 "ascending or descending fashion.";
42 "Sorted Output Workspace");
44 auto orderingValues = std::vector<std::string>({
"Ascending",
"Descending"});
45 auto orderingValidator = std::make_shared<StringListValidator>(orderingValues);
46 declareProperty(
"Ordering", orderingValues[0], orderingValidator,
"Ascending or descending sorting",
58 const auto sizeOfX = inputWorkspace->x(0).size();
61 for (
int specNum = 0u; specNum < (int)inputWorkspace->getNumberHistograms(); specNum++) {
67 copyToOutputWorkspace(workspaceIndices, *inputWorkspace, *outputWorkspace, specNum, isAProperHistogram);
82 std::vector<std::size_t> workspaceIndices;
83 workspaceIndices.reserve(sizeOfX);
84 for (
auto workspaceIndex = 0u; workspaceIndex < sizeOfX; workspaceIndex++) {
85 workspaceIndices.emplace_back(workspaceIndex);
87 return workspaceIndices;
100template <
typename Comparator>
102 unsigned int specNum, Comparator
const &
compare) {
103 std::sort(workspaceIndices.begin(), workspaceIndices.end(), [&](std::size_t lhs, std::size_t
rhs) ->
bool {
104 return compare(inputWorkspace.x(specNum)[lhs], inputWorkspace.x(specNum)[rhs]);
110 if (order ==
"Ascending") {
111 sortByXValue(workspaceIndices, inputWorkspace, specNum, std::less<double>());
112 }
else if (order ==
"Descending") {
113 sortByXValue(workspaceIndices, inputWorkspace, specNum, std::greater<double>());
131 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
x(specNum).size(); workspaceIndex++) {
132 outputWorkspace.
mutableX(specNum)[workspaceIndex] = inputWorkspace.
x(specNum)[workspaceIndices[workspaceIndex]];
137 if (inputWorkspace.
hasDx(specNum)) {
138 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
dx(specNum).size(); workspaceIndex++) {
139 outputWorkspace.
mutableDx(specNum)[workspaceIndex] = inputWorkspace.
dx(specNum)[workspaceIndices[workspaceIndex]];
159 bool isAProperHistogram) {
162 if (isAProperHistogram) {
163 auto lastIndexIt = std::find(workspaceIndices.begin(), workspaceIndices.end(), inputWorkspace.
y(specNum).size());
164 workspaceIndices.erase(lastIndexIt);
167 const auto &inSpaceY = inputWorkspace.
y(specNum);
168 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
y(specNum).size(); workspaceIndex++) {
169 outputWorkspace.
mutableY(specNum)[workspaceIndex] = inSpaceY[workspaceIndices[workspaceIndex]];
172 const auto &inSpaceE = inputWorkspace.
e(specNum);
173 for (
auto workspaceIndex = 0u; workspaceIndex < inputWorkspace.
e(specNum).size(); workspaceIndex++) {
174 outputWorkspace.
mutableE(specNum)[workspaceIndex] = inSpaceE[workspaceIndices[workspaceIndex]];
181 bool isAProperHistogram) {
197template <
typename Comparator>
200 if (!std::is_sorted(inputWorkspace.
x(specNum).begin(), inputWorkspace.
x(specNum).end(),
201 [&](
double lhs,
double rhs) ->
bool { return compare(lhs, rhs); })) {
218 if (inputWorkspace.
x(0).size() != inputWorkspace.
y(0).size()) {
221 if (!
isItSorted(std::greater<double>(), inputWorkspace)) {
222 if (!
isItSorted(std::less<double>(), inputWorkspace)) {
223 throw std::runtime_error(
"The data entered contains an invalid histogram: histogram has an "
224 "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.