Mantid
Loading...
Searching...
No Matches
StepScan.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 +
13
15// Register the algorithm into the AlgorithmFactory
16DECLARE_ALGORITHM(StepScan)
17
18using namespace Kernel;
19using namespace API;
20
22const std::string StepScan::name() const { return "StepScan"; }
23
25int StepScan::version() const { return 1; }
26
28const std::string StepScan::category() const { return "Workflow\\Alignment"; }
29
31 // TODO: Validator to ensure that this is 'fresh' data???
33 "InputWorkspace", "", Direction::Input, std::make_shared<WorkspaceUnitValidator>("TOF")),
34 "The input workspace. Must hold 'raw' (unweighted) events.");
35 // Note that this algorithm may modify the input workspace (by masking and/or
36 // cropping)
37 declareProperty(std::make_unique<WorkspaceProperty<ITableWorkspace>>("OutputWorkspace", "", Direction::Output),
38 "The output table workspace.");
39
40 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("MaskWorkspace", "", Direction::Input,
42 "A workspace holding pixels to be masked.");
43
44 declareProperty("XMin", EMPTY_DBL(), "The minimum value of X for which an event will be counted.");
45 declareProperty("XMax", EMPTY_DBL(),
46 "The maximum value of X for which an "
47 "event will be counted. Must be greater "
48 "than XMin.");
49 // N.B. The choice of units is restricted by the upstream StepScan interface,
50 // but in fact any convertible unit will work so is allowed here
51 declareProperty("RangeUnit", "TOF", std::make_shared<StringListValidator>(UnitFactory::Instance().getKeys()),
52 "The units in which XMin and XMax is being given.");
53}
54
57 // Get hold of the input workspace
58 EventWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
59 // Get hold of the related monitors workspace, if it exists
60 EventWorkspace_sptr monitorWorkspace = getMonitorWorkspace(inputWorkspace);
61
62 // If any of the filtering properties have been set, clone the input workspace
63 MatrixWorkspace_sptr maskWS = getProperty("MaskWorkspace");
64 const double xmin = getProperty("XMin");
65 const double xmax = getProperty("XMax");
66 if (maskWS || !isEmpty(xmin) || !isEmpty(xmax)) {
67 inputWorkspace = cloneInputWorkspace(inputWorkspace);
68 }
69
70 // If the MaskWorkspace property has been set, run the MaskDetectors algorithm
71 if (maskWS) {
72 runMaskDetectors(inputWorkspace, maskWS);
73 }
74
75 // If a restricted X range has been set, handle that
76 if (!isEmpty(xmin) || !isEmpty(xmax)) {
77 runFilterByXValue(inputWorkspace, xmin, xmax);
78 // TODO: In what circumstances should we filter the monitors???
79 // if ( monitorWorkspace ) runFilterByXValue(monitorWorkspace, xmin, xmax);
80 }
81
82 // Run the SumEventsByLogValue algorithm with the log fixed to 'scan_index'
83 auto sumEvents = createChildAlgorithm("SumEventsByLogValue");
84 sumEvents->setProperty<EventWorkspace_sptr>("InputWorkspace", inputWorkspace);
85 if (monitorWorkspace) {
86 sumEvents->setProperty<EventWorkspace_sptr>("MonitorWorkspace", monitorWorkspace);
87 }
88 sumEvents->setProperty("LogName", "scan_index");
89 sumEvents->executeAsChildAlg();
90
91 Workspace_sptr outputWS = sumEvents->getProperty("OutputWorkspace");
92 auto table = Kernel::DynamicPointerCastHelper::dynamicPointerCastWithCheck<ITableWorkspace, API::Workspace>(
93 std::move(outputWS));
94 // Remove the scan_index=0 entry from the resulting table (unless it's the
95 // only one)
96 if (table->rowCount() > 1 && table->Int(0, 0) == 0) {
97 table->removeRow(0);
98 }
99
100 setProperty("OutputWorkspace", table);
101}
102
110 // See if there's a monitor workspace inside the input one
111 return std::dynamic_pointer_cast<DataObjects::EventWorkspace>(inputWS->monitorWorkspace());
112}
113
115 auto clone = createChildAlgorithm("CloneWorkspace");
116 clone->setProperty("InputWorkspace", inputWS);
117 clone->executeAsChildAlg();
118
119 Workspace_sptr temp = clone->getProperty("OutputWorkspace");
120 return std::static_pointer_cast<DataObjects::EventWorkspace>(temp);
121}
122
128 auto maskingAlg = createChildAlgorithm("MaskDetectors");
129 maskingAlg->setProperty<MatrixWorkspace_sptr>("Workspace", inputWS);
130 maskingAlg->setProperty<MatrixWorkspace_sptr>("MaskedWorkspace", maskWS);
131 maskingAlg->executeAsChildAlg();
132}
133
139void StepScan::runFilterByXValue(const MatrixWorkspace_sptr &inputWS, const double xmin, const double xmax) {
140 std::string rangeUnit = getProperty("RangeUnit");
141 // Run ConvertUnits on the input workspace if xmin/max were given in a
142 // different unit
143 if (rangeUnit != "TOF") {
144 auto convertUnits = createChildAlgorithm("ConvertUnits");
145 convertUnits->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWS);
146 convertUnits->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", inputWS);
147 convertUnits->setProperty("Target", rangeUnit);
148 // TODO: Emode/efixed?
149 convertUnits->executeAsChildAlg();
150 }
151
152 auto filter = createChildAlgorithm("FilterByXValue");
153 filter->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWS);
154 filter->setProperty<MatrixWorkspace_sptr>("OutputWorkspace", inputWS);
155 filter->setProperty("XMin", xmin);
156 filter->setProperty("XMax", xmax);
157 filter->executeAsChildAlg();
158}
159
160} // namespace Mantid::WorkflowAlgorithms
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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.
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.
static bool isEmpty(const NumT toCheck)
checks that the value was not set by users, uses the value in empty double/int.
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void exec() override
Virtual method - must be overridden by concrete algorithm.
Definition StepScan.cpp:55
DataObjects::EventWorkspace_sptr cloneInputWorkspace(const API::Workspace_sptr &inputWS)
Definition StepScan.cpp:114
int version() const override
Algorithm's version for identification.
Definition StepScan.cpp:25
const std::string category() const override
Algorithm's category for identification.
Definition StepScan.cpp:28
const std::string name() const override
Algorithm's name for identification.
Definition StepScan.cpp:22
void init() override
Virtual method - must be overridden by concrete algorithm.
Definition StepScan.cpp:30
void runFilterByXValue(const API::MatrixWorkspace_sptr &inputWS, const double xmin, const double xmax)
Runs FilterByXValue as a child algorithm on the given workspace.
Definition StepScan.cpp:139
DataObjects::EventWorkspace_sptr getMonitorWorkspace(const API::MatrixWorkspace_sptr &inputWS)
Tries to get hold of the workspace that holds the monitor data inside the input workspace.
Definition StepScan.cpp:109
void runMaskDetectors(const API::MatrixWorkspace_sptr &inputWS, const API::MatrixWorkspace_sptr &maskWS)
Runs MaskDetectors as a child algorithm on the input workspace.
Definition StepScan.cpp:127
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< EventWorkspace > EventWorkspace_sptr
shared pointer to the EventWorkspace class
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition EmptyValues.h:42
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54