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