/
ExtractSingleSpectrum.cpp
55 lines (46 loc) · 2.05 KB
/
ExtractSingleSpectrum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAlgorithms/ExtractSingleSpectrum.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/TextAxis.h"
#include "MantidKernel/BoundedValidator.h"
namespace Mantid {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ExtractSingleSpectrum)
using namespace Kernel;
using namespace API;
void ExtractSingleSpectrum::init() {
declareProperty(
make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
"The name of the input workspace.");
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output),
"The name under which to store the output workspace.");
auto mustBePositive = boost::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(0);
declareProperty("WorkspaceIndex", -1, mustBePositive,
"The workspace index number of the spectrum to extract.");
}
void ExtractSingleSpectrum::exec() {
// Get hold of the input workspace
MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
const int indexToExtract = getProperty("WorkspaceIndex");
const size_t numHist = inputWorkspace->getNumberHistograms();
if (static_cast<size_t>(indexToExtract) >= numHist) {
throw Exception::IndexError(
indexToExtract, inputWorkspace->getNumberHistograms(), this->name());
}
// Let crop do the rest
IAlgorithm_sptr cropper =
this->createChildAlgorithm("CropWorkspace", 0.0, 1.0);
cropper->setProperty("InputWorkspace", inputWorkspace);
cropper->setProperty("StartWorkspaceIndex", indexToExtract);
cropper->setProperty("EndWorkspaceIndex", indexToExtract);
cropper->executeAsChildAlg();
setProperty<MatrixWorkspace_sptr>("OutputWorkspace",
cropper->getProperty("OutputWorkspace"));
}
} // namespace Algorithms
} // namespace Mantid