Mantid
Loading...
Searching...
No Matches
ExtractSingleSpectrum.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 +
11
12namespace Mantid::Algorithms {
13
14// Register the algorithm into the AlgorithmFactory
15DECLARE_ALGORITHM(ExtractSingleSpectrum)
16
17using namespace Kernel;
18using namespace API;
19
21 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
22 "The name of the input workspace.");
23 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
24 "The name under which to store the output workspace.");
25
26 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
27 mustBePositive->setLower(0);
28 declareProperty("WorkspaceIndex", -1, mustBePositive, "The workspace index number of the spectrum to extract.");
29}
30
32 // Get hold of the input workspace
33 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
34 const int indexToExtract = getProperty("WorkspaceIndex");
35 const size_t numHist = inputWorkspace->getNumberHistograms();
36 if (static_cast<size_t>(indexToExtract) >= numHist) {
37 throw Exception::IndexError(indexToExtract, inputWorkspace->getNumberHistograms(), this->name());
38 }
39
40 // Let crop do the rest
41 auto cropper = this->createChildAlgorithm("CropWorkspace", 0.0, 1.0);
42 cropper->setProperty("InputWorkspace", inputWorkspace);
43 cropper->setProperty("StartWorkspaceIndex", indexToExtract);
44 cropper->setProperty("EndWorkspaceIndex", indexToExtract);
45 cropper->executeAsChildAlg();
46
47 setProperty<MatrixWorkspace_sptr>("OutputWorkspace", cropper->getProperty("OutputWorkspace"));
48}
49
50} // namespace Mantid::Algorithms
#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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
A property class for workspaces.
void init() override
Initialisation code.
Exception for index errors.
Definition: Exception.h:284
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54