Mantid
Loading...
Searching...
No Matches
ExtractSpectra2.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 +
8#include "MantidAPI/Algorithm.tcc"
11#include "MantidAPI/TextAxis.h"
15#include "MantidIndexing/IndexInfo.h"
16
17namespace Mantid {
18using namespace API;
19using namespace DataObjects;
20using namespace Kernel;
21namespace Algorithms {
22
23// Currently we DO NOT REGISTER the algorithm into the AlgorithmFactory. The API
24// is different from version 1 and thus cannot replace it without breaking
25// scripts. It can be used internally directly without being registered.
26
28const std::string ExtractSpectra2::name() const { return "ExtractSpectra2"; }
29
31int ExtractSpectra2::version() const { return 2; }
32
34const std::string ExtractSpectra2::category() const { return "Transforms\\Splitting"; }
35
37const std::string ExtractSpectra2::summary() const {
38 return "Extracts a list of spectra from a workspace and places them in a new "
39 "workspace.";
40}
41
44 declareWorkspaceInputProperties<MatrixWorkspace, static_cast<int>(IndexType::SpectrumNum) |
45 static_cast<int>(IndexType::WorkspaceIndex)>(
46 "InputWorkspace", "The input workspace");
47 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
48 "Name of the output workspace");
49}
50
53 std::shared_ptr<MatrixWorkspace> inputWS;
54 Indexing::SpectrumIndexSet indexSet;
55 std::tie(inputWS, indexSet) = getWorkspaceAndIndices<MatrixWorkspace>("InputWorkspace");
56
57 auto outputWS = create<MatrixWorkspace>(
58 *inputWS, dynamic_cast<IndexProperty *>(getPointerToProperty("InputWorkspaceIndexSet"))->getFilteredIndexInfo(),
59 HistogramData::BinEdges(2));
60
61 Axis *inAxis1(nullptr);
62 TextAxis *outTxtAxis(nullptr);
63 NumericAxis *outNumAxis(nullptr);
64 bool isBinEdgeAxis(false);
65 if (inputWS->axes() > 1) {
66 inAxis1 = inputWS->getAxis(1);
67 auto outAxis1 = outputWS->getAxis(1);
68 outTxtAxis = dynamic_cast<TextAxis *>(outAxis1);
69 if (!outTxtAxis) {
70 outNumAxis = dynamic_cast<NumericAxis *>(outAxis1);
71 isBinEdgeAxis = dynamic_cast<BinEdgeAxis *>(inAxis1) != nullptr;
72 }
73 }
74
75 Progress prog(this, 0.0, 1.0, indexSet.size());
76 for (size_t j = 0; j < indexSet.size(); ++j) {
77 // Rely on Indexing::IndexSet preserving index order.
78 const size_t i = indexSet[j];
79 // Copy spectrum data, automatically setting up sharing for histogram.
80 outputWS->getSpectrum(j).copyDataFrom(inputWS->getSpectrum(i));
81
82 // Copy axis entry, SpectraAxis is implicit in workspace creation
83 if (outTxtAxis)
84 outTxtAxis->setLabel(j, inAxis1->label(i));
85 else if (outNumAxis)
86 outNumAxis->setValue(j, inAxis1->operator()(i));
87
88 // Copy bin masking if it exists.
89 if (inputWS->hasMaskedBins(i))
90 outputWS->setMaskedBins(j, inputWS->maskedBins(i));
91
92 prog.report();
93 }
94
95 if (isBinEdgeAxis) {
96 if (!indexSet.isContiguous()) {
97 throw std::invalid_argument("Cannot extract non-contiguous set of "
98 "spectra when the vertical axis has bin "
99 "edges.");
100 }
101 const auto outIndex = indexSet.size();
102 const auto inIndex = indexSet[indexSet.size() - 1] + 1;
103 outNumAxis->setValue(outIndex, inAxis1->operator()(inIndex));
104 }
105
106 setProperty("OutputWorkspace", std::move(outputWS));
107}
108
109} // namespace Algorithms
110} // namespace Mantid
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
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
Definition: Algorithm.cpp:2033
Class to represent the axis of a workspace.
Definition: Axis.h:30
virtual std::string label(const std::size_t &index) const =0
Returns a text label of for a value Note that the index here is not the index of a value,...
Stores numeric values that are assumed to be bin edge values.
Definition: BinEdgeAxis.h:20
IndexProperty : Implementation of a property type which returns a SpectrumIndexSet provided an input ...
Definition: IndexProperty.h:30
Class to represent a numeric axis of a workspace.
Definition: NumericAxis.h:29
void setValue(const std::size_t &index, const double &value) override
Set the value at a specific index.
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
Class to represent a text axis of a workspace.
Definition: TextAxis.h:36
void setLabel(const std::size_t &index, const std::string &lbl)
Set the label at the given index.
Definition: TextAxis.cpp:104
A property class for workspaces.
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void init() override
Initialize the algorithm's properties.
const std::string name() const override
Algorithms name for identification.
void exec() override
Executes the algorithm.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
Helper class which provides the Collimation Length for SANS instruments.
@ Output
An output workspace.
Definition: Property.h:54