18#include "Poco/String.h"
25using namespace Kernel;
27using namespace Geometry;
28using namespace DataObjects;
31 auto wsValidator = std::make_shared<WorkspaceUnitValidator>(
"Wavelength");
35 "The name of the input event Nexus file to load as dark current.");
39 "If true, the algorithm will be persistent and re-used when "
40 "other data sets are processed");
48 std::string output_message;
50 const std::string reductionManagerName =
getProperty(
"ReductionProperties");
51 std::shared_ptr<PropertyManager> reductionManager;
55 reductionManager = std::make_shared<PropertyManager>();
60 const bool persistent =
getProperty(
"PersistentCorrection");
61 if (!reductionManager->existsProperty(
"DarkCurrentAlgorithm") && persistent) {
62 auto algProp = std::make_unique<AlgorithmProperty>(
"DarkCurrentAlgorithm");
64 reductionManager->declareProperty(std::move(algProp));
74 progress.report(
"Subtracting dark current");
77 Poco::Path path(fileName);
78 const std::string entryName =
"DarkCurrent" + path.getBaseName();
79 std::string darkWSName =
"__dark_current_" + path.getBaseName();
81 if (reductionManager->existsProperty(entryName)) {
82 darkWS = reductionManager->getProperty(entryName);
83 darkWSName = reductionManager->getPropertyValue(entryName);
84 output_message += darkWSName +
'\n';
88 if (!reductionManager->existsProperty(
"LoadAlgorithm")) {
90 loadAlg->setProperty(
"Filename", fileName);
91 if (loadAlg->existsProperty(
"LoadMonitors"))
92 loadAlg->setProperty(
"LoadMonitors",
false);
93 loadAlg->executeAsChildAlg();
94 darkWS = loadAlg->getProperty(
"OutputWorkspace");
98 IAlgorithm_sptr loadAlg0 = reductionManager->getProperty(
"LoadAlgorithm");
99 const std::string loadString = loadAlg0->toString();
101 loadAlg->setChild(
true);
102 loadAlg->setProperty(
"Filename", fileName);
103 if (loadAlg->existsProperty(
"LoadMonitors"))
104 loadAlg->setProperty(
"LoadMonitors",
false);
105 loadAlg->setPropertyValue(
"OutputWorkspace", darkWSName);
107 darkWS = loadAlg->getProperty(
"OutputWorkspace");
110 output_message +=
"\n Loaded " + fileName +
"\n";
111 if (loadAlg->existsProperty(
"OutputMessage")) {
112 std::string msg = loadAlg->getPropertyValue(
"OutputMessage");
113 output_message +=
" |" + Poco::replace(msg,
"\n",
"\n |") +
"\n";
116 std::string darkWSOutputName =
getPropertyValue(
"OutputDarkCurrentWorkspace");
117 if (!darkWSOutputName.empty())
121 reductionManager->setPropertyValue(entryName, darkWSName);
122 reductionManager->setProperty(entryName, darkWS);
124 progress.report(3,
"Loaded dark current");
127 double scaling_factor = 1.0;
128 if (inputWS->run().hasProperty(
"proton_charge")) {
129 auto dp = inputWS->run().getTimeSeriesProperty<
double>(
"proton_charge");
130 double duration = dp->getStatistics().duration;
132 dp = darkWS->run().getTimeSeriesProperty<
double>(
"proton_charge");
133 double dark_duration = dp->getStatistics().duration;
134 scaling_factor = duration / dark_duration;
135 }
else if (inputWS->run().hasProperty(
"timer")) {
136 auto duration = inputWS->run().getPropertyValueAsType<
double>(
"timer");
137 auto dark_duration = darkWS->run().getPropertyValueAsType<
double>(
"timer");
139 scaling_factor = duration / dark_duration;
141 output_message +=
"\n Could not find proton charge or duration in sample logs";
142 g_log.
error() <<
"ERROR: Could not find proton charge or duration in sample logs\n";
145 progress.report(
"Scaling dark current");
149 rebinAlg->setProperty(
"WorkspaceToRebin", darkWS);
150 rebinAlg->setProperty(
"WorkspaceToMatch", inputWS);
151 rebinAlg->setProperty(
"OutputWorkspace", darkWS);
152 rebinAlg->executeAsChildAlg();
157 scaleAlg->setProperty(
"InputWorkspace", scaledDarkWS);
158 scaleAlg->setProperty(
"Factor", scaling_factor);
159 scaleAlg->setProperty(
"OutputWorkspace", scaledDarkWS);
160 scaleAlg->setProperty(
"Operation",
"Multiply");
161 scaleAlg->executeAsChildAlg();
162 scaledDarkWS = rebinAlg->getProperty(
"OutputWorkspace");
165 minusAlg->setProperty(
"LHSWorkspace", inputWS);
166 minusAlg->setProperty(
"RHSWorkspace", scaledDarkWS);
168 minusAlg->setPropertyValue(
"OutputWorkspace", outputWSname);
169 minusAlg->executeAsChildAlg();
173 setProperty(
"OutputMessage",
"Dark current subtracted: " + output_message);
175 progress.report(
"Subtracted dark current");
#define DECLARE_ALGORITHM(classname)
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.
@ Load
allowed here which will be passed to the algorithm
Helper class for reporting progress from algorithms.
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.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
void init() override
Initialisation code.
void exec() override
Execution code.
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.
@ Output
An output workspace.