Mantid
Loading...
Searching...
No Matches
HFIRDarkCurrentSubtraction.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 +
11#include "MantidAPI/Run.h"
15#include "Poco/String.h"
16
17#include <filesystem>
18
20
21// Register the algorithm into the AlgorithmFactory
22DECLARE_ALGORITHM(HFIRDarkCurrentSubtraction)
23
24using namespace Kernel;
25using namespace API;
26using namespace Geometry;
27
29 auto wsValidator = std::make_shared<WorkspaceUnitValidator>("Wavelength");
30 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator));
31
32 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Load, ".xml"),
33 "The name of the input event Nexus file to load as dark current.");
34
35 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output));
36 declareProperty("PersistentCorrection", true,
37 "If true, the algorithm will be persistent and re-used when "
38 "other data sets are processed");
39 declareProperty("ReductionProperties", "__sans_reduction_properties", Direction::Input);
40 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputDarkCurrentWorkspace", "",
42 declareProperty("OutputMessage", "", Direction::Output);
43}
44
46 std::string output_message;
47 // Reduction property manager
48 const std::string reductionManagerName = getProperty("ReductionProperties");
49 std::shared_ptr<PropertyManager> reductionManager;
50 if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
51 reductionManager = PropertyManagerDataService::Instance().retrieve(reductionManagerName);
52 } else {
53 reductionManager = std::make_shared<PropertyManager>();
54 PropertyManagerDataService::Instance().addOrReplace(reductionManagerName, reductionManager);
55 }
56
57 // If the load algorithm isn't in the reduction properties, add it
58 const bool persistent = getProperty("PersistentCorrection");
59 if (!reductionManager->existsProperty("DarkCurrentAlgorithm") && persistent) {
60 auto algProp = std::make_unique<AlgorithmProperty>("DarkCurrentAlgorithm");
61 algProp->setValue(toString());
62 reductionManager->declareProperty(std::move(algProp));
63 }
64
65 Progress progress(this, 0.0, 1.0, 10);
66
67 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
68 const std::string fileName = getPropertyValue("Filename");
70 std::string darkWSName = getPropertyValue("OutputDarkCurrentWorkspace");
71
72 progress.report("Subtracting dark current");
73
74 // Look for an entry for the dark current in the reduction table
75 std::filesystem::path path(fileName);
76 const std::string entryName = "DarkCurrent" + path.stem().string();
77
78 if (reductionManager->existsProperty(entryName)) {
79 darkWS = reductionManager->getProperty(entryName);
80 darkWSName = reductionManager->getPropertyValue(entryName);
81 output_message += darkWSName + '\n';
82 } else {
83 // Load the dark current if we don't have it already
84 if (darkWSName.empty()) {
85 darkWSName = "__dark_current_" + path.stem().string();
86 setPropertyValue("OutputDarkCurrentWorkspace", darkWSName);
87 }
88
89 IAlgorithm_sptr loadAlg;
90 if (!reductionManager->existsProperty("LoadAlgorithm")) {
91 loadAlg = createChildAlgorithm("HFIRLoad", 0.1, 0.3);
92 loadAlg->setProperty("Filename", fileName);
93 loadAlg->setProperty("ReductionProperties", reductionManagerName);
94 loadAlg->executeAsChildAlg();
95 } else {
96 IAlgorithm_sptr loadAlg0 = reductionManager->getProperty("LoadAlgorithm");
97 const std::string loadString = loadAlg0->toString();
98 loadAlg = Algorithm::fromString(loadString);
99 loadAlg->setChild(true);
100 loadAlg->setProperty("Filename", fileName);
101 loadAlg->setProperty("ReductionProperties", reductionManagerName);
102 loadAlg->setPropertyValue("OutputWorkspace", darkWSName);
103 loadAlg->execute();
104 }
105 darkWS = loadAlg->getProperty("OutputWorkspace");
106 output_message += "\n Loaded " + fileName + "\n";
107 if (loadAlg->existsProperty("OutputMessage")) {
108 std::string msg = loadAlg->getPropertyValue("OutputMessage");
109 output_message += " |" + Poco::replace(msg, "\n", "\n |") + "\n";
110 }
111
112 setProperty("OutputDarkCurrentWorkspace", darkWS);
113 reductionManager->declareProperty(std::make_unique<WorkspaceProperty<>>(entryName, "", Direction::Output));
114 reductionManager->setPropertyValue(entryName, darkWSName);
115 reductionManager->setProperty(entryName, darkWS);
116 }
117 progress.report(3, "Loaded dark current");
118
119 // Perform subtraction
120 double darkTimer = getCountingTime(darkWS);
121 double dataTimer = getCountingTime(inputWS);
122 auto scaleAlg = createChildAlgorithm("Scale", 0.3, 0.5);
123 scaleAlg->setProperty("InputWorkspace", darkWS);
124 scaleAlg->setProperty("Factor", dataTimer / darkTimer);
125 scaleAlg->setProperty("Operation", "Multiply");
126 scaleAlg->executeAsChildAlg();
127 MatrixWorkspace_sptr scaledDarkWS = scaleAlg->getProperty("OutputWorkspace");
128
129 // Zero out timer and monitor so that we don't subtract them out
130 for (size_t i = 0; i < scaledDarkWS->dataY(0).size(); i++) {
131 scaledDarkWS->dataY(DEFAULT_TIMER_ID)[i] = 0.0;
132 scaledDarkWS->dataE(DEFAULT_TIMER_ID)[i] = 0.0;
133 scaledDarkWS->dataY(DEFAULT_MONITOR_ID)[i] = 0.0;
134 scaledDarkWS->dataE(DEFAULT_MONITOR_ID)[i] = 0.0;
135 }
136 auto minusAlg = createChildAlgorithm("Minus", 0.5, 0.7);
137 minusAlg->setProperty("LHSWorkspace", inputWS);
138 minusAlg->setProperty("RHSWorkspace", scaledDarkWS);
139 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
140 minusAlg->setProperty("OutputWorkspace", outputWS);
141 minusAlg->executeAsChildAlg();
142 MatrixWorkspace_sptr correctedWS = minusAlg->getProperty("OutputWorkspace");
143 setProperty("OutputWorkspace", correctedWS);
144 setProperty("OutputMessage", "Dark current subtracted: " + output_message);
145
146 progress.report("Subtracted dark current");
147}
148
152 // First, look whether we have the information in the log
153 if (inputWS->run().hasProperty("timer")) {
154 return inputWS->run().getPropertyValueAsType<double>("timer");
155 } else {
156 // If we don't have the information in the log, use the default timer
157 // spectrum
158 const MantidVec &timer = inputWS->dataY(DEFAULT_TIMER_ID);
159 return timer[0];
160 }
161}
162
163} // 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.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
std::string toString() const override
Serialize an object to a string.
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 IAlgorithm_sptr fromString(const std::string &input)
De-serialize an object from a string.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
@ Load
allowed here which will be passed to the algorithm
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
double getCountingTime(const API::MatrixWorkspace_sptr &inputWS) const
Get the counting time from a workspace.
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::vector< double > MantidVec
typedef for the data storage used in Mantid matrix workspaces
Definition cow_ptr.h:172
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54