Mantid
Loading...
Searching...
No Matches
EQSANSDarkCurrentSubtraction.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"
17#include "Poco/String.h"
18
19#include <filesystem>
20
22
23// Register the algorithm into the AlgorithmFactory
24DECLARE_ALGORITHM(EQSANSDarkCurrentSubtraction)
25
26using namespace Kernel;
27using namespace API;
28using namespace Geometry;
29using namespace DataObjects;
30
32 auto wsValidator = std::make_shared<WorkspaceUnitValidator>("Wavelength");
33 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input, wsValidator));
34
35 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Load, "_event.nxs"),
36 "The name of the input event Nexus file to load as dark current.");
37
38 declareProperty(std::make_unique<WorkspaceProperty<>>("OutputWorkspace", "", Direction::Output));
39 declareProperty("PersistentCorrection", true,
40 "If true, the algorithm will be persistent and re-used when "
41 "other data sets are processed");
42 declareProperty("ReductionProperties", "__sans_reduction_properties", Direction::Input);
43 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("OutputDarkCurrentWorkspace", "",
45 declareProperty("OutputMessage", "", Direction::Output);
46}
47
49 std::string output_message;
50 // Reduction property manager
51 const std::string reductionManagerName = getProperty("ReductionProperties");
52 std::shared_ptr<PropertyManager> reductionManager;
53 if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
54 reductionManager = PropertyManagerDataService::Instance().retrieve(reductionManagerName);
55 } else {
56 reductionManager = std::make_shared<PropertyManager>();
57 PropertyManagerDataService::Instance().addOrReplace(reductionManagerName, reductionManager);
58 }
59
60 // If the load algorithm isn't in the reduction properties, add it
61 const bool persistent = getProperty("PersistentCorrection");
62 if (!reductionManager->existsProperty("DarkCurrentAlgorithm") && persistent) {
63 auto algProp = std::make_unique<AlgorithmProperty>("DarkCurrentAlgorithm");
64 algProp->setValue(toString());
65 reductionManager->declareProperty(std::move(algProp));
66 }
67
68 Progress progress(this, 0.0, 1.0, 10);
69
70 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
71
72 const std::string fileName = getPropertyValue("Filename");
74
75 progress.report("Subtracting dark current");
76
77 // Look for an entry for the dark current in the reduction table
78 std::filesystem::path path(fileName);
79 const std::string entryName = "DarkCurrent" + path.stem().string();
80 std::string darkWSName = "__dark_current_" + path.stem().string();
81
82 if (reductionManager->existsProperty(entryName)) {
83 darkWS = reductionManager->getProperty(entryName);
84 darkWSName = reductionManager->getPropertyValue(entryName);
85 output_message += darkWSName + '\n';
86 } else {
87 // Load the dark current if we don't have it already
88 IAlgorithm_sptr loadAlg;
89 if (!reductionManager->existsProperty("LoadAlgorithm")) {
90 loadAlg = createChildAlgorithm("EQSANSLoad", 0.1, 0.3);
91 loadAlg->setProperty("Filename", fileName);
92 if (loadAlg->existsProperty("LoadMonitors"))
93 loadAlg->setProperty("LoadMonitors", false);
94 loadAlg->executeAsChildAlg();
95 darkWS = loadAlg->getProperty("OutputWorkspace");
96 } else {
97 // Get load algorithm as a string so that we can create a completely
98 // new proxy and ensure that we don't overwrite existing properties
99 IAlgorithm_sptr loadAlg0 = reductionManager->getProperty("LoadAlgorithm");
100 const std::string loadString = loadAlg0->toString();
101 loadAlg = Algorithm::fromString(loadString);
102 loadAlg->setChild(true);
103 loadAlg->setProperty("Filename", fileName);
104 if (loadAlg->existsProperty("LoadMonitors"))
105 loadAlg->setProperty("LoadMonitors", false);
106 loadAlg->setPropertyValue("OutputWorkspace", darkWSName);
107 loadAlg->execute();
108 darkWS = loadAlg->getProperty("OutputWorkspace");
109 }
110
111 output_message += "\n Loaded " + fileName + "\n";
112 if (loadAlg->existsProperty("OutputMessage")) {
113 std::string msg = loadAlg->getPropertyValue("OutputMessage");
114 output_message += " |" + Poco::replace(msg, "\n", "\n |") + "\n";
115 }
116
117 std::string darkWSOutputName = getPropertyValue("OutputDarkCurrentWorkspace");
118 if (!darkWSOutputName.empty())
119 setProperty("OutputDarkCurrentWorkspace", darkWS);
120 AnalysisDataService::Instance().addOrReplace(darkWSName, darkWS);
121 reductionManager->declareProperty(std::make_unique<WorkspaceProperty<>>(entryName, "", Direction::Output));
122 reductionManager->setPropertyValue(entryName, darkWSName);
123 reductionManager->setProperty(entryName, darkWS);
124 }
125 progress.report(3, "Loaded dark current");
126
127 // Normalize the dark current and data to counting time
128 double scaling_factor = 1.0;
129 if (inputWS->run().hasProperty("proton_charge")) {
130 auto dp = inputWS->run().getTimeSeriesProperty<double>("proton_charge");
131 double duration = dp->getStatistics().duration;
132
133 dp = darkWS->run().getTimeSeriesProperty<double>("proton_charge");
134 double dark_duration = dp->getStatistics().duration;
135 scaling_factor = duration / dark_duration;
136 } else if (inputWS->run().hasProperty("timer")) {
137 auto duration = inputWS->run().getPropertyValueAsType<double>("timer");
138 auto dark_duration = darkWS->run().getPropertyValueAsType<double>("timer");
139 ;
140 scaling_factor = duration / dark_duration;
141 } else {
142 output_message += "\n Could not find proton charge or duration in sample logs";
143 g_log.error() << "ERROR: Could not find proton charge or duration in sample logs\n";
144 };
145
146 progress.report("Scaling dark current");
147
148 // Scale the stored dark current by the counting time
149 auto rebinAlg = createChildAlgorithm("RebinToWorkspace", 0.4, 0.5);
150 rebinAlg->setProperty("WorkspaceToRebin", darkWS);
151 rebinAlg->setProperty("WorkspaceToMatch", inputWS);
152 rebinAlg->setProperty("OutputWorkspace", darkWS);
153 rebinAlg->executeAsChildAlg();
154 MatrixWorkspace_sptr scaledDarkWS = rebinAlg->getProperty("OutputWorkspace");
155
156 // Perform subtraction
157 auto scaleAlg = createChildAlgorithm("Scale", 0.5, 0.6);
158 scaleAlg->setProperty("InputWorkspace", scaledDarkWS);
159 scaleAlg->setProperty("Factor", scaling_factor);
160 scaleAlg->setProperty("OutputWorkspace", scaledDarkWS);
161 scaleAlg->setProperty("Operation", "Multiply");
162 scaleAlg->executeAsChildAlg();
163 scaledDarkWS = rebinAlg->getProperty("OutputWorkspace");
164
165 auto minusAlg = createChildAlgorithm("Minus", 0.6, 0.7);
166 minusAlg->setProperty("LHSWorkspace", inputWS);
167 minusAlg->setProperty("RHSWorkspace", scaledDarkWS);
168 const std::string outputWSname = getPropertyValue("OutputWorkspace");
169 minusAlg->setPropertyValue("OutputWorkspace", outputWSname);
170 minusAlg->executeAsChildAlg();
171 MatrixWorkspace_sptr outputWS = minusAlg->getProperty("OutputWorkspace");
172
173 setProperty("OutputWorkspace", outputWS);
174 setProperty("OutputMessage", "Dark current subtracted: " + output_message);
175
176 progress.report("Subtracted dark current");
177}
178
179} // 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.
Kernel::Logger & g_log
Definition Algorithm.h:422
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
@ 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.
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
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
@ Input
An input workspace.
Definition Property.h:53
@ Output
An output workspace.
Definition Property.h:54