/
HFIRDarkCurrentSubtraction.cpp
170 lines (150 loc) · 7.08 KB
/
HFIRDarkCurrentSubtraction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidWorkflowAlgorithms/HFIRDarkCurrentSubtraction.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceUnitValidator.h"
#include "MantidAPI/FileProperty.h"
#include "Poco/Path.h"
#include "Poco/String.h"
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidKernel/PropertyManagerDataService.h"
#include "MantidKernel/PropertyManager.h"
namespace Mantid {
namespace WorkflowAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(HFIRDarkCurrentSubtraction)
using namespace Kernel;
using namespace API;
using namespace Geometry;
void HFIRDarkCurrentSubtraction::init() {
auto wsValidator = boost::make_shared<WorkspaceUnitValidator>("Wavelength");
declareProperty(make_unique<WorkspaceProperty<>>(
"InputWorkspace", "", Direction::Input, wsValidator));
declareProperty(
make_unique<API::FileProperty>("Filename", "", API::FileProperty::Load,
".xml"),
"The name of the input event Nexus file to load as dark current.");
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output));
declareProperty("PersistentCorrection", true,
"If true, the algorithm will be persistent and re-used when "
"other data sets are processed");
declareProperty("ReductionProperties", "__sans_reduction_properties",
Direction::Input);
declareProperty(make_unique<WorkspaceProperty<MatrixWorkspace>>(
"OutputDarkCurrentWorkspace", "", Direction::Output,
PropertyMode::Optional));
declareProperty("OutputMessage", "", Direction::Output);
}
void HFIRDarkCurrentSubtraction::exec() {
std::string output_message = "";
// Reduction property manager
const std::string reductionManagerName = getProperty("ReductionProperties");
boost::shared_ptr<PropertyManager> reductionManager;
if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
reductionManager =
PropertyManagerDataService::Instance().retrieve(reductionManagerName);
} else {
reductionManager = boost::make_shared<PropertyManager>();
PropertyManagerDataService::Instance().addOrReplace(reductionManagerName,
reductionManager);
}
// If the load algorithm isn't in the reduction properties, add it
const bool persistent = getProperty("PersistentCorrection");
if (!reductionManager->existsProperty("DarkCurrentAlgorithm") && persistent) {
auto algProp = make_unique<AlgorithmProperty>("DarkCurrentAlgorithm");
algProp->setValue(toString());
reductionManager->declareProperty(std::move(algProp));
}
Progress progress(this, 0.0, 1.0, 10);
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
const std::string fileName = getPropertyValue("Filename");
MatrixWorkspace_sptr darkWS;
std::string darkWSName = getPropertyValue("OutputDarkCurrentWorkspace");
progress.report("Subtracting dark current");
// Look for an entry for the dark current in the reduction table
Poco::Path path(fileName);
const std::string entryName = "DarkCurrent" + path.getBaseName();
if (reductionManager->existsProperty(entryName)) {
darkWS = reductionManager->getProperty(entryName);
darkWSName = reductionManager->getPropertyValue(entryName);
output_message += darkWSName + '\n';
} else {
// Load the dark current if we don't have it already
if (darkWSName.size() == 0) {
darkWSName = "__dark_current_" + path.getBaseName();
setPropertyValue("OutputDarkCurrentWorkspace", darkWSName);
}
IAlgorithm_sptr loadAlg;
if (!reductionManager->existsProperty("LoadAlgorithm")) {
loadAlg = createChildAlgorithm("HFIRLoad", 0.1, 0.3);
loadAlg->setProperty("Filename", fileName);
loadAlg->setProperty("ReductionProperties", reductionManagerName);
loadAlg->executeAsChildAlg();
} else {
IAlgorithm_sptr loadAlg0 = reductionManager->getProperty("LoadAlgorithm");
const std::string loadString = loadAlg0->toString();
loadAlg = Algorithm::fromString(loadString);
loadAlg->setChild(true);
loadAlg->setProperty("Filename", fileName);
loadAlg->setProperty("ReductionProperties", reductionManagerName);
loadAlg->setPropertyValue("OutputWorkspace", darkWSName);
loadAlg->execute();
}
darkWS = loadAlg->getProperty("OutputWorkspace");
output_message += "\n Loaded " + fileName + "\n";
if (loadAlg->existsProperty("OutputMessage")) {
std::string msg = loadAlg->getPropertyValue("OutputMessage");
output_message += " |" + Poco::replace(msg, "\n", "\n |") + "\n";
}
setProperty("OutputDarkCurrentWorkspace", darkWS);
reductionManager->declareProperty(Kernel::make_unique<WorkspaceProperty<>>(
entryName, "", Direction::Output));
reductionManager->setPropertyValue(entryName, darkWSName);
reductionManager->setProperty(entryName, darkWS);
}
progress.report(3, "Loaded dark current");
// Perform subtraction
double darkTimer = getCountingTime(darkWS);
double dataTimer = getCountingTime(inputWS);
IAlgorithm_sptr scaleAlg = createChildAlgorithm("Scale", 0.3, 0.5);
scaleAlg->setProperty("InputWorkspace", darkWS);
scaleAlg->setProperty("Factor", dataTimer / darkTimer);
scaleAlg->setProperty("Operation", "Multiply");
scaleAlg->executeAsChildAlg();
MatrixWorkspace_sptr scaledDarkWS = scaleAlg->getProperty("OutputWorkspace");
// Zero out timer and monitor so that we don't subtract them out
for (size_t i = 0; i < scaledDarkWS->dataY(0).size(); i++) {
scaledDarkWS->dataY(DEFAULT_TIMER_ID)[i] = 0.0;
scaledDarkWS->dataE(DEFAULT_TIMER_ID)[i] = 0.0;
scaledDarkWS->dataY(DEFAULT_MONITOR_ID)[i] = 0.0;
scaledDarkWS->dataE(DEFAULT_MONITOR_ID)[i] = 0.0;
}
IAlgorithm_sptr minusAlg = createChildAlgorithm("Minus", 0.5, 0.7);
minusAlg->setProperty("LHSWorkspace", inputWS);
minusAlg->setProperty("RHSWorkspace", scaledDarkWS);
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
minusAlg->setProperty("OutputWorkspace", outputWS);
minusAlg->executeAsChildAlg();
MatrixWorkspace_sptr correctedWS = minusAlg->getProperty("OutputWorkspace");
setProperty("OutputWorkspace", correctedWS);
setProperty("OutputMessage", "Dark current subtracted: " + output_message);
progress.report("Subtracted dark current");
}
/// Get the counting time from a workspace
/// @param inputWS :: workspace to read the counting time from
double
HFIRDarkCurrentSubtraction::getCountingTime(MatrixWorkspace_sptr inputWS) {
// First, look whether we have the information in the log
if (inputWS->run().hasProperty("timer")) {
return inputWS->run().getPropertyValueAsType<double>("timer");
} else {
// If we don't have the information in the log, use the default timer
// spectrum
MantidVec &timer = inputWS->dataY(DEFAULT_TIMER_ID);
return timer[0];
}
}
} // namespace WorkflowAlgorithms
} // namespace Mantid