Mantid
Loading...
Searching...
No Matches
DgsPreprocessData.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 +
14#include "MantidAPI/Run.h"
21#include "MantidKernel/System.h"
24#include "Poco/Path.h"
25
26using namespace Mantid::Kernel;
27using namespace Mantid::API;
28using namespace WorkflowAlgorithmHelpers;
29
31
32// Register the algorithm into the AlgorithmFactory
33DECLARE_ALGORITHM(DgsPreprocessData)
34
35
36const std::string DgsPreprocessData::name() const { return "DgsPreprocessData"; }
37
39int DgsPreprocessData::version() const { return 1; }
40
42const std::string DgsPreprocessData::category() const { return "Workflow\\Inelastic\\UsesPropertyManager"; }
43
47 this->declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
48 "An input workspace.");
49 this->declareProperty(
50 std::make_unique<WorkspaceProperty<>>("InputMonitorWorkspace", "", Direction::Input, PropertyMode::Optional),
51 "A monitor workspace associated with the input workspace.");
52 this->declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output),
53 "The name for the output workspace.");
54 this->declareProperty("TofRangeOffset", 0.0, "An addition to the TOF axis for monitor integration.");
55 this->declareProperty("ReductionProperties", "__dgs_reduction_properties", Direction::Input);
56}
57
61 g_log.notice() << "Starting DgsPreprocessData\n";
62 // Get the reduction property manager
63 const std::string reductionManagerName = this->getProperty("ReductionProperties");
64 std::shared_ptr<PropertyManager> reductionManager;
65 if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
66 reductionManager = PropertyManagerDataService::Instance().retrieve(reductionManagerName);
67 } else {
68 throw std::runtime_error("DgsPreprocessData cannot run without a reduction PropertyManager.");
69 }
70
71 // Log name that will indicate if the preprocessing has been done.
72 const std::string doneLog = "DirectInelasticReductionNormalisedBy";
73
74 MatrixWorkspace_sptr inputWS = this->getProperty("InputWorkspace");
75 MatrixWorkspace_sptr outputWS = this->getProperty("OutputWorkspace");
76
77 std::string incidentBeamNorm = reductionManager->getProperty("IncidentBeamNormalisation");
78 g_log.notice() << "Incident beam norm method = " << incidentBeamNorm << '\n';
79
80 // Check to see if preprocessing has already been done.
81 bool normAlreadyDone = inputWS->run().hasProperty(doneLog);
82
83 if ("None" != incidentBeamNorm && !normAlreadyDone) {
84 const std::string facility = ConfigService::Instance().getFacility().name();
85 // SNS hard-wired to current normalisation
86 if ("SNS" == facility) {
87 incidentBeamNorm = "ByCurrent";
88 }
89 const std::string normAlg = "Normalise" + incidentBeamNorm;
90 auto norm = createChildAlgorithm(normAlg);
91 norm->setProperty("InputWorkspace", inputWS);
92 norm->setProperty("OutputWorkspace", outputWS);
93 if ("ToMonitor" == incidentBeamNorm) {
94 // Perform extra setup for monitor normalisation
95 double rangeOffset = this->getProperty("TofRangeOffset");
96 double rangeMin = getDblPropOrParam("MonitorIntRangeLow", reductionManager, "norm-mon1-min", inputWS);
97 rangeMin += rangeOffset;
98
99 double rangeMax = getDblPropOrParam("MonitorIntRangeHigh", reductionManager, "norm-mon1-max", inputWS);
100 rangeMax += rangeOffset;
101
102 specnum_t monSpec = static_cast<specnum_t>(inputWS->getInstrument()->getNumberParameter("norm-mon1-spec")[0]);
103 if ("ISIS" == facility) {
104 norm->setProperty("MonitorSpectrum", monSpec);
105 }
106 // Do SNS
107 else {
108 MatrixWorkspace_const_sptr monitorWS = this->getProperty("MonitorWorkspace");
109 if (!monitorWS) {
110 throw std::runtime_error("SNS instruments require monitor workspaces "
111 "for monitor normalisation.");
112 }
113 std::size_t monIndex = monitorWS->getIndexFromSpectrumNumber(monSpec);
114 norm->setProperty("MonitorWorkspace", monitorWS);
115 norm->setProperty("MonitorWorkspaceIndex", monIndex);
116 }
117 norm->setProperty("IntegrationRangeMin", rangeMin);
118 norm->setProperty("IntegrationRangeMax", rangeMax);
119 norm->setProperty("IncludePartialBins", true);
120 }
121 norm->executeAsChildAlg();
122
123 outputWS = norm->getProperty("OutputWorkspace");
124
125 auto addLog = createChildAlgorithm("AddSampleLog");
126 addLog->setProperty("Workspace", outputWS);
127 addLog->setProperty("LogName", doneLog);
128 addLog->setProperty("LogText", normAlg);
129 addLog->executeAsChildAlg();
130 } else {
131 if (normAlreadyDone) {
132 g_log.information() << "Preprocessing already done on " << inputWS->getName() << '\n';
133 }
134 outputWS = inputWS;
135 }
136
137 this->setProperty("OutputWorkspace", outputWS);
138}
139
140} // 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
void information(const std::string &msg)
Logs at information level.
Definition: Logger.cpp:105
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
DgsPreprocessData : This algorithm is responsible for normalising the data to current (proton charge)...
int version() const override
Algorithm's version for identification.
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
int32_t specnum_t
Typedef for a spectrum Number.
Definition: IDTypes.h:16
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