Mantid
Loading...
Searching...
No Matches
DgsAbsoluteUnitsReduction.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 +
9#include "MantidKernel/Atom.h"
15
16using namespace Mantid::API;
17using namespace Mantid::Kernel;
18using namespace Mantid::PhysicalConstants;
19using namespace WorkflowAlgorithmHelpers;
20
22// Register the algorithm into the AlgorithmFactory
23DECLARE_ALGORITHM(DgsAbsoluteUnitsReduction)
24
25//----------------------------------------------------------------------------------------------
27const std::string DgsAbsoluteUnitsReduction::name() const { return "DgsAbsoluteUnitsReduction"; }
28
30int DgsAbsoluteUnitsReduction::version() const { return 1; }
31
33const std::string DgsAbsoluteUnitsReduction::category() const { return "Workflow\\Inelastic\\UsesPropertyManager"; }
34
35//----------------------------------------------------------------------------------------------
36
37//----------------------------------------------------------------------------------------------
41 this->declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
42 "The absolute units sample workspace.");
43 this->declareProperty(
44 std::make_unique<WorkspaceProperty<>>("InputMonitorWorkspace", "", Direction::Input, PropertyMode::Optional),
45 "A monitor workspace associated with the absolute "
46 "units sample workspace");
47 this->declareProperty(
48 std::make_unique<WorkspaceProperty<>>("DetectorVanadiumWorkspace", "", Direction::Input, PropertyMode::Optional),
49 "An absolute units detector vanadium workspace.");
50 this->declareProperty(std::make_unique<WorkspaceProperty<>>("DetectorVanadiumMonitorWorkspace", "", Direction::Input,
52 "A monitor workspace associated with the absolute units detector "
53 "vanadium workspace.");
54 this->declareProperty(
55 std::make_unique<WorkspaceProperty<>>("MaskWorkspace", "", Direction::Input, PropertyMode::Optional),
56 "A masking workspace to apply to the data.");
57 this->declareProperty(
58 std::make_unique<WorkspaceProperty<>>("GroupingWorkspace", "", Direction::Input, PropertyMode::Optional),
59 "A grouping workspace for the absolute units data.");
60 this->declareProperty("ReductionProperties", "__dgs_reduction_properties", Direction::Input);
61 this->declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
62 "The integrated absolute units workspace.");
63 this->declareProperty(std::make_unique<WorkspaceProperty<>>("OutputMaskWorkspace", "", Direction::Output),
64 "The diagnostic mask from the absolute units workspace");
65}
66
67//----------------------------------------------------------------------------------------------
71 g_log.notice() << "Starting DgsAbsoluteUnitsReduction\n";
72 // Get the reduction property manager
73 const std::string reductionManagerName = this->getProperty("ReductionProperties");
74 std::shared_ptr<PropertyManager> reductionManager;
75 if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
76 reductionManager = PropertyManagerDataService::Instance().retrieve(reductionManagerName);
77 } else {
78 throw std::runtime_error("DgsAbsoluteUnitsReduction cannot run without a "
79 "reduction PropertyManager.");
80 }
81
82 // Gather all of the input workspaces
83 MatrixWorkspace_sptr absSampleWS = this->getProperty("InputWorkspace");
84 MatrixWorkspace_sptr absSampleMonWS = this->getProperty("InputMonitorWorkspace");
85 MatrixWorkspace_sptr absDetVanWS = this->getProperty("DetectorVanadiumWorkspace");
86 MatrixWorkspace_sptr absDetVanMonWS = this->getProperty("DetectorVanadiumMonitorWorkspace");
87 MatrixWorkspace_sptr absGroupingWS = this->getProperty("GroupingWorkspace");
88 MatrixWorkspace_sptr maskWS = this->getProperty("MaskWorkspace");
89
90 // Process absolute units detector vanadium if necessary
91 MatrixWorkspace_sptr absIdetVanWS;
92 if (absDetVanWS) {
93 auto detVan = createChildAlgorithm("DgsProcessDetectorVanadium");
94 detVan->setProperty("InputWorkspace", absDetVanWS);
95 detVan->setProperty("InputMonitorWorkspace", absDetVanMonWS);
96 detVan->setProperty("ReductionProperties", reductionManagerName);
97 if (maskWS) {
98 detVan->setProperty("MaskWorkspace", maskWS);
99 }
100 detVan->executeAsChildAlg();
101 absIdetVanWS = detVan->getProperty("OutputWorkspace");
102 } else {
103 absIdetVanWS = absDetVanWS;
104 }
105
106 const std::string absWsName = absSampleWS->getName() + "_absunits";
107 auto etConv = createChildAlgorithm("DgsConvertToEnergyTransfer");
108 etConv->setProperty("InputWorkspace", absSampleWS);
109 etConv->setProperty("InputMonitorWorkspace", absSampleMonWS);
110 etConv->setProperty("OutputWorkspace", absWsName);
111 const double ei = reductionManager->getProperty("AbsUnitsIncidentEnergy");
112 etConv->setProperty("IncidentEnergyGuess", ei);
113 etConv->setProperty("IntegratedDetectorVanadium", absIdetVanWS);
114 etConv->setProperty("ReductionProperties", reductionManagerName);
115 if (maskWS) {
116 etConv->setProperty("MaskWorkspace", maskWS);
117 }
118 if (absGroupingWS) {
119 etConv->setProperty("GroupingWorkspace", absGroupingWS);
120 }
121 etConv->setProperty("AlternateGroupingTag", "AbsUnits");
122 etConv->executeAsChildAlg();
123 MatrixWorkspace_sptr outputWS = etConv->getProperty("OutputWorkspace");
124
125 const double vanadiumMass = getDblPropOrParam("VanadiumMass", reductionManager, "vanadium-mass", outputWS);
126
127 // Get the vanadium mass from the Mantid physical constants
128 Atom vanadium = getAtom("V");
129 const double vanadiumRmm = vanadium.mass;
130
131 outputWS /= (vanadiumMass / vanadiumRmm);
132
133 // Set integration range for absolute units sample
134 double eMin = getDblPropOrParam("AbsUnitsMinimumEnergy", reductionManager, "monovan-integr-min", outputWS);
135 double eMax = getDblPropOrParam("AbsUnitsMaximumEnergy", reductionManager, "monovan-integr-max", outputWS);
136
137 std::vector<double> params{eMin, eMax - eMin, eMax};
138
139 auto rebin = createChildAlgorithm("Rebin");
140 rebin->setProperty("InputWorkspace", outputWS);
141 rebin->setProperty("OutputWorkspace", outputWS);
142 rebin->setProperty("Params", params);
143 rebin->executeAsChildAlg();
144 outputWS = rebin->getProperty("OutputWorkspace");
145
146 auto cToMWs = createChildAlgorithm("ConvertToMatrixWorkspace");
147 cToMWs->setProperty("InputWorkspace", outputWS);
148 cToMWs->setProperty("OutputWorkspace", outputWS);
149 outputWS = cToMWs->getProperty("OutputWorkspace");
150
151 // Run diagnostics
152 const double huge = getDblPropOrParam("HighCounts", reductionManager, "diag_huge", outputWS);
153 const double tiny = getDblPropOrParam("LowCounts", reductionManager, "diag_tiny", outputWS);
154 const double vanOutLo = getDblPropOrParam("AbsUnitsLowOutlier", reductionManager, "monovan_lo_bound", outputWS);
155 const double vanOutHi = getDblPropOrParam("AbsUnitsHighOutlier", reductionManager, "monovan_hi_bound", outputWS);
156 const double vanLo = getDblPropOrParam("AbsUnitsMedianTestLow", reductionManager, "monovan_lo_frac", outputWS);
157 const double vanHi = getDblPropOrParam("AbsUnitsMedianTestHigh", reductionManager, "monovan_hi_frac", outputWS);
158 const double vanSigma = getDblPropOrParam("AbsUnitsErrorBarCriterion", reductionManager, "diag_samp_sig", outputWS);
159
160 auto diag = createChildAlgorithm("DetectorDiagnostic");
161 diag->setProperty("InputWorkspace", outputWS);
162 diag->setProperty("OutputWorkspace", "absUnitsDiagMask");
163 diag->setProperty("LowThreshold", tiny);
164 diag->setProperty("HighThreshold", huge);
165 diag->setProperty("LowOutlier", vanOutLo);
166 diag->setProperty("HighOutlier", vanOutHi);
167 diag->setProperty("LowThresholdFraction", vanLo);
168 diag->setProperty("HighThresholdFraction", vanHi);
169 diag->setProperty("SignificanceTest", vanSigma);
170 diag->executeAsChildAlg();
171 MatrixWorkspace_sptr absMaskWS = diag->getProperty("OutputWorkspace");
172
173 auto mask = createChildAlgorithm("MaskDetectors");
174 mask->setProperty("Workspace", outputWS);
175 mask->setProperty("MaskedWorkspace", absMaskWS);
176 mask->executeAsChildAlg();
177 outputWS = mask->getProperty("Workspace");
178
179 auto cFrmDist = createChildAlgorithm("ConvertFromDistribution");
180 cFrmDist->setProperty("Workspace", outputWS);
181 cFrmDist->executeAsChildAlg();
182 outputWS = cFrmDist->getProperty("Workspace");
183
184 auto wMean = createChildAlgorithm("WeightedMeanOfWorkspace");
185 wMean->setProperty("InputWorkspace", outputWS);
186 wMean->setProperty("OutputWorkspace", outputWS);
187 wMean->executeAsChildAlg();
188 outputWS = wMean->getProperty("OutputWorkspace");
189
190 // If the absolute units detector vanadium is used, do extra correction.
191 if (absIdetVanWS) {
192 NeutronAtom neutronVanadium = getNeutronAtom(vanadium.z_number);
193 double xsection = (neutronVanadium.inc_scatt_xs + neutronVanadium.coh_scatt_xs) * 1e3 / 4. /
194 M_PI; // cross section per steradian in millibarns
195
196 outputWS /= xsection;
197 const double sampleMass = reductionManager->getProperty("SampleMass");
198 const double sampleRmm = reductionManager->getProperty("SampleRmm");
199 outputWS *= (sampleMass / sampleRmm);
200 }
201
202 this->setProperty("OutputMaskWorkspace", absMaskWS);
203 this->setProperty("OutputWorkspace", outputWS);
204}
205
206} // 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
Kernel::Logger & g_log
Definition: Algorithm.h:451
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition: Logger.cpp:95
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
Struture to hold the common information for an atom.
Definition: Atom.h:20
const uint16_t z_number
The atomic number, or number of protons, for the atom.
Definition: Atom.h:34
const double mass
The atomic mass in units of 'u' (=1g/mol/Na).
Definition: Atom.h:47
DgsAbsoluteUnitsReduction : This is the algorithm responsible for processing and creating the absolut...
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
A namespace containing physical constants that are required by algorithms and unit routines.
Definition: Atom.h:14
MANTID_KERNEL_DLL const Atom & getAtom(const uint16_t z_number, const uint16_t a_number=0)
Definition: Atom.cpp:3167
MANTID_KERNEL_DLL NeutronAtom getNeutronAtom(const uint16_t z_number, const uint16_t a_number=0)
Retrieve a copy of NeutronAtom.
double getDblPropOrParam(const std::string &pmProp, Mantid::Kernel::PropertyManager_sptr &pm, const std::string &instParam, Mantid::API::MatrixWorkspace_sptr &ws, const double overrideValue=Mantid::EMPTY_DBL())
Function to get double property or instrument parameter value.
STL namespace.
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54
Structure to store neutronic scattering information for the various elements.
Definition: NeutronAtom.h:22
double inc_scatt_xs
The incoherent scattering cross section in barns.
Definition: NeutronAtom.h:66
double coh_scatt_xs
The coherent scattering cross section in barns.
Definition: NeutronAtom.h:63