Mantid
Loading...
Searching...
No Matches
ElasticWindow.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 +
7//----------------------------------------------------------------------
8// Includes
9//----------------------------------------------------------------------
11#include "MantidAPI/Axis.h"
15
16namespace Mantid::Algorithms {
17
18// Register the algorithm into the AlgorithmFactory
19DECLARE_ALGORITHM(ElasticWindow)
20
21using namespace Kernel;
22using namespace API;
23
25 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input,
26 std::make_shared<WorkspaceUnitValidator>("DeltaE")),
27 "The input workspace.");
28 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputInQ", "", Direction::Output),
29 "The name for output workspace with the X axis in units of Q");
30 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputInQSquared", "", Direction::Output),
31 "The name for output workspace with the X axis in units of Q^2.");
32 declareProperty("IntegrationRangeStart", EMPTY_DBL(), std::make_shared<MandatoryValidator<double>>(),
33 "Start Point of Range 1");
34 declareProperty("IntegrationRangeEnd", EMPTY_DBL(), std::make_shared<MandatoryValidator<double>>(),
35 "End Point of Range 1");
36 declareProperty("BackgroundRangeStart", EMPTY_DBL(), "Start Point of Range 2", Direction::Input);
37 declareProperty("BackgroundRangeEnd", EMPTY_DBL(), "End Point of Range 2.", Direction::Input);
38}
39
41 MatrixWorkspace_sptr inputWorkspace = getProperty("InputWorkspace");
42
43 double intRangeStart = getProperty("IntegrationRangeStart");
44 double intRangeEnd = getProperty("IntegrationRangeEnd");
45 double bgRangeStart = getProperty("BackgroundRangeStart");
46 double bgRangeEnd = getProperty("BackgroundRangeEnd");
47
48 // Create the output workspaces
50
52 MatrixWorkspace_sptr outputQSquared;
53
54 const bool childAlgLogging(true);
55 double startProgress(0.0), endProgress(0.0);
56
57 // Determine if we are converting from spectra number (red) or Q (Sqw)
58 const bool axisIsSpectrumNumber = inputWorkspace->getAxis(1)->isSpectra();
59 g_log.information() << "Axis is spectrum number: " << axisIsSpectrumNumber << '\n';
60
61 // Determine if we need to use the second time range...
62 const bool backgroundSubtraction = !((bgRangeStart == bgRangeEnd) && (bgRangeStart == EMPTY_DBL()));
63 g_log.information() << "Use background subtraction: " << backgroundSubtraction << '\n';
64
65 // Calculate number of steps
66 size_t numSteps = 4;
67 if (backgroundSubtraction)
68 numSteps += 1;
69 if (axisIsSpectrumNumber)
70 numSteps += 1;
71
72 double stepProgress = 1.0 / static_cast<double>(numSteps);
73
74 if (backgroundSubtraction) {
75 // ... CalculateFlatBackground, Minus, Integration...
76 auto flatBG = createChildAlgorithm("CalculateFlatBackground", startProgress, endProgress, childAlgLogging);
77 flatBG->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWorkspace);
78 flatBG->setProperty<double>("StartX", bgRangeStart);
79 flatBG->setProperty<double>("EndX", bgRangeEnd);
80 flatBG->setPropertyValue("Mode", "Mean");
81 flatBG->setPropertyValue("OutputWorkspace", "flatBG");
82 flatBG->execute();
83 startProgress += stepProgress;
84 endProgress += stepProgress;
85
86 MatrixWorkspace_sptr flatBGws = flatBG->getProperty("OutputWorkspace");
87
88 auto integ = createChildAlgorithm("Integration", startProgress, endProgress, childAlgLogging);
89 integ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", flatBGws);
90 integ->setProperty<double>("RangeLower", intRangeStart);
91 integ->setProperty<double>("RangeUpper", intRangeEnd);
92 integ->setPropertyValue("OutputWorkspace", "integ");
93 integ->execute();
94
95 integWS = integ->getProperty("OutputWorkspace");
96 } else {
97 // ... Just Integration ...
98 auto integ = createChildAlgorithm("Integration", startProgress, endProgress, childAlgLogging);
99 integ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", inputWorkspace);
100 integ->setProperty<double>("RangeLower", intRangeStart);
101 integ->setProperty<double>("RangeUpper", intRangeEnd);
102 integ->setPropertyValue("OutputWorkspace", "integ");
103 integ->execute();
104
105 integWS = integ->getProperty("OutputWorkspace");
106 }
107 startProgress += stepProgress;
108 endProgress += stepProgress;
109
110 if (axisIsSpectrumNumber) {
111 // Use ConvertSpectrumAxis v2 for correct result
112 const int version = 2;
113
114 // ... ConvertSpectrumAxis (Q) ...
115 auto csaQ = createChildAlgorithm("ConvertSpectrumAxis", startProgress, endProgress, childAlgLogging, version);
116 csaQ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", integWS);
117 csaQ->setPropertyValue("Target", "ElasticQ");
118 csaQ->setPropertyValue("EMode", "Indirect");
119 csaQ->setPropertyValue("OutputWorkspace", "csaQ");
120 csaQ->execute();
121 MatrixWorkspace_sptr csaQws = csaQ->getProperty("OutputWorkspace");
122 startProgress += stepProgress;
123 endProgress += stepProgress;
124
125 // ... ConvertSpectrumAxis (Q2) ...
126 auto csaQ2 = createChildAlgorithm("ConvertSpectrumAxis", startProgress, endProgress, childAlgLogging, version);
127 csaQ2->setProperty<MatrixWorkspace_sptr>("InputWorkspace", integWS);
128 csaQ2->setPropertyValue("Target", "ElasticQSquared");
129 csaQ2->setPropertyValue("EMode", "Indirect");
130 csaQ2->setPropertyValue("OutputWorkspace", "csaQ2");
131 csaQ2->execute();
132 MatrixWorkspace_sptr csaQ2ws = csaQ2->getProperty("OutputWorkspace");
133 startProgress += stepProgress;
134 endProgress += stepProgress;
135
136 // ... Transpose (Q) ...
137 auto tranQ = createChildAlgorithm("Transpose", startProgress, endProgress, childAlgLogging);
138 tranQ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", csaQws);
139 tranQ->setPropertyValue("OutputWorkspace", "outQ");
140 tranQ->execute();
141 outputQ = tranQ->getProperty("OutputWorkspace");
142 startProgress += stepProgress;
143 endProgress += stepProgress;
144
145 // ... Transpose (Q2) ...
146 auto tranQ2 = createChildAlgorithm("Transpose", startProgress, endProgress, childAlgLogging);
147 tranQ2->setProperty<MatrixWorkspace_sptr>("InputWorkspace", csaQ2ws);
148 tranQ2->setPropertyValue("OutputWorkspace", "outQSquared");
149 tranQ2->execute();
150 outputQSquared = tranQ2->getProperty("OutputWorkspace");
151 } else {
152 // ... Transpose (Q) ...
153 auto tranQ = createChildAlgorithm("Transpose", startProgress, endProgress, childAlgLogging);
154 tranQ->setProperty<MatrixWorkspace_sptr>("InputWorkspace", integWS);
155 tranQ->setPropertyValue("OutputWorkspace", "outQ");
156 tranQ->execute();
157 outputQ = tranQ->getProperty("OutputWorkspace");
158 startProgress += stepProgress;
159 endProgress += stepProgress;
160
161 // ... Convert to Histogram (Q2) ...
162 auto histQ2 = createChildAlgorithm("ConvertToHistogram", startProgress, endProgress, childAlgLogging);
163 histQ2->setProperty<MatrixWorkspace_sptr>("InputWorkspace", outputQ);
164 histQ2->setPropertyValue("OutputWorkspace", "outQ");
165 histQ2->execute();
166 MatrixWorkspace_sptr qHistWS = histQ2->getProperty("OutputWorkspace");
167 startProgress += stepProgress;
168 endProgress += stepProgress;
169
170 // ... Convert Units (Q2) ...
171 auto convUnitQ2 = createChildAlgorithm("ConvertUnits", startProgress, endProgress, childAlgLogging);
172 convUnitQ2->setProperty<MatrixWorkspace_sptr>("InputWorkspace", qHistWS);
173 convUnitQ2->setPropertyValue("Target", "QSquared");
174 convUnitQ2->setPropertyValue("EMode", "Indirect");
175 convUnitQ2->setPropertyValue("OutputWorkspace", "outQSquared");
176 convUnitQ2->execute();
177 outputQSquared = convUnitQ2->getProperty("OutputWorkspace");
178 }
179 auto yLabel = outputQSquared->YUnitLabel();
180 outputQSquared->setYUnitLabel("ln(" + yLabel + ")");
181
182 setProperty("OutputInQ", outputQ);
183 setProperty("OutputInQSquared", outputQSquared);
184}
185
186} // 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
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
void exec() override
Execution code.
void init() override
Initialisation code.
int version() const override
Algorithm's version.
Definition: ElasticWindow.h:37
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
Validator to check that a property is not left empty.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base 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