49 std::string output_message;
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);
56 reductionManager = std::make_shared<PropertyManager>();
57 PropertyManagerDataService::Instance().addOrReplace(reductionManagerName, reductionManager);
61 const bool persistent =
getProperty(
"PersistentCorrection");
62 if (!reductionManager->existsProperty(
"DarkCurrentAlgorithm") && persistent) {
63 auto algProp = std::make_unique<AlgorithmProperty>(
"DarkCurrentAlgorithm");
65 reductionManager->declareProperty(std::move(algProp));
75 progress.report(
"Subtracting dark current");
78 std::filesystem::path path(fileName);
79 const std::string entryName =
"DarkCurrent" + path.stem().string();
80 std::string darkWSName =
"__dark_current_" + path.stem().string();
82 if (reductionManager->existsProperty(entryName)) {
83 darkWS = reductionManager->getProperty(entryName);
84 darkWSName = reductionManager->getPropertyValue(entryName);
85 output_message += darkWSName +
'\n';
89 if (!reductionManager->existsProperty(
"LoadAlgorithm")) {
91 loadAlg->setProperty(
"Filename", fileName);
92 if (loadAlg->existsProperty(
"LoadMonitors"))
93 loadAlg->setProperty(
"LoadMonitors",
false);
94 loadAlg->executeAsChildAlg();
95 darkWS = loadAlg->getProperty(
"OutputWorkspace");
99 IAlgorithm_sptr loadAlg0 = reductionManager->getProperty(
"LoadAlgorithm");
100 const std::string loadString = loadAlg0->toString();
102 loadAlg->setChild(
true);
103 loadAlg->setProperty(
"Filename", fileName);
104 if (loadAlg->existsProperty(
"LoadMonitors"))
105 loadAlg->setProperty(
"LoadMonitors",
false);
106 loadAlg->setPropertyValue(
"OutputWorkspace", darkWSName);
108 darkWS = loadAlg->getProperty(
"OutputWorkspace");
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";
117 std::string darkWSOutputName =
getPropertyValue(
"OutputDarkCurrentWorkspace");
118 if (!darkWSOutputName.empty())
120 AnalysisDataService::Instance().addOrReplace(darkWSName, darkWS);
122 reductionManager->setPropertyValue(entryName, darkWSName);
123 reductionManager->setProperty(entryName, darkWS);
125 progress.report(3,
"Loaded dark current");
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;
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");
140 scaling_factor = duration / dark_duration;
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";
146 progress.report(
"Scaling dark current");
150 rebinAlg->setProperty(
"WorkspaceToRebin", darkWS);
151 rebinAlg->setProperty(
"WorkspaceToMatch", inputWS);
152 rebinAlg->setProperty(
"OutputWorkspace", darkWS);
153 rebinAlg->executeAsChildAlg();
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");
166 minusAlg->setProperty(
"LHSWorkspace", inputWS);
167 minusAlg->setProperty(
"RHSWorkspace", scaledDarkWS);
169 minusAlg->setPropertyValue(
"OutputWorkspace", outputWSname);
170 minusAlg->executeAsChildAlg();
174 setProperty(
"OutputMessage",
"Dark current subtracted: " + output_message);
176 progress.report(
"Subtracted dark current");