-
Notifications
You must be signed in to change notification settings - Fork 122
/
SANSBeamFinder.cpp
296 lines (254 loc) · 12.4 KB
/
SANSBeamFinder.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidWorkflowAlgorithms/SANSBeamFinder.h"
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FileProperty.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/PropertyManager.h"
#include "MantidKernel/PropertyManagerDataService.h"
#include "MantidWorkflowAlgorithms/EQSANSInstrument.h"
#include "MantidWorkflowAlgorithms/HFIRInstrument.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Path.h"
#include "Poco/String.h"
namespace Mantid {
namespace WorkflowAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SANSBeamFinder)
using namespace Kernel;
using namespace API;
using namespace Geometry;
using namespace DataObjects;
void SANSBeamFinder::init() {
const std::vector<std::string> fileExts{"_event.nxs", ".xml"};
declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Load, fileExts),
"Data filed used to find beam center");
declareProperty("BeamCenterX", EMPTY_DBL(), "Beam position in X pixel coordinates");
declareProperty("BeamCenterY", EMPTY_DBL(), "Beam position in Y pixel coordinates");
declareProperty("UseDirectBeamMethod", true, "If true, the direct beam method will be used");
declareProperty("BeamRadius", 3.0, "Beam radius in pixels, used with the scattered beam method");
declareProperty("FoundBeamCenterX", EMPTY_DBL(), Direction::Output);
declareProperty("FoundBeamCenterY", EMPTY_DBL(), 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("OutputMessage", "", Direction::Output);
}
MatrixWorkspace_sptr SANSBeamFinder::loadBeamFinderFile(const std::string &beamCenterFile) {
Poco::Path path(beamCenterFile);
const std::string entryName = "SANSBeamFinder" + path.getBaseName();
const std::string reductionManagerName = getProperty("ReductionProperties");
MatrixWorkspace_sptr finderWS;
if (m_reductionManager->existsProperty(entryName)) {
finderWS = m_reductionManager->getProperty(entryName);
m_output_message += " |Using existing workspace: " + finderWS->getName() + '\n';
} else {
// Load the dark current if we don't have it already
std::string finderWSName = "__beam_finder_" + path.getBaseName();
if (!m_reductionManager->existsProperty("LoadAlgorithm")) {
IAlgorithm_sptr loadAlg = createChildAlgorithm("EQSANSLoad", 0.1, 0.3);
loadAlg->setProperty("Filename", beamCenterFile);
loadAlg->setProperty("NoBeamCenter", true);
loadAlg->setProperty("BeamCenterX", EMPTY_DBL());
loadAlg->setProperty("BeamCenterY", EMPTY_DBL());
loadAlg->setProperty("ReductionProperties", reductionManagerName);
loadAlg->executeAsChildAlg();
finderWS = loadAlg->getProperty("OutputWorkspace");
m_output_message += " |Loaded " + beamCenterFile + "\n";
std::string msg = loadAlg->getPropertyValue("OutputMessage");
m_output_message += " |" + Poco::replace(msg, "\n", "\n |") + "\n";
} else {
// Get load algorithm as a string so that we can create a completely
// new proxy and ensure that we don't overwrite existing properties
IAlgorithm_sptr loadAlg0 = m_reductionManager->getProperty("LoadAlgorithm");
const std::string loadString = loadAlg0->toString();
IAlgorithm_sptr loadAlg = Algorithm::fromString(loadString);
loadAlg->setProperty("Filename", beamCenterFile);
if (loadAlg->existsProperty("NoBeamCenter"))
loadAlg->setProperty("NoBeamCenter", true);
if (loadAlg->existsProperty("BeamCenterX"))
loadAlg->setProperty("BeamCenterX", EMPTY_DBL());
if (loadAlg->existsProperty("BeamCenterY"))
loadAlg->setProperty("BeamCenterY", EMPTY_DBL());
if (loadAlg->existsProperty("ReductionProperties"))
loadAlg->setProperty("ReductionProperties", reductionManagerName);
loadAlg->setPropertyValue("OutputWorkspace", finderWSName);
loadAlg->execute();
std::shared_ptr<Workspace> wks = AnalysisDataService::Instance().retrieve(finderWSName);
finderWS = std::dynamic_pointer_cast<MatrixWorkspace>(wks);
m_output_message += " |Loaded " + beamCenterFile + "\n";
if (loadAlg->existsProperty("OutputMessage")) {
std::string msg = loadAlg->getPropertyValue("OutputMessage");
m_output_message += " |" + Poco::replace(msg, "\n", "\n |") + "\n";
}
}
m_reductionManager->declareProperty(std::make_unique<WorkspaceProperty<>>(entryName, "", Direction::Output));
m_reductionManager->setPropertyValue(entryName, finderWSName);
m_reductionManager->setProperty(entryName, finderWS);
}
return finderWS;
}
void SANSBeamFinder::exec() {
// Reduction property manager
const std::string reductionManagerName = getProperty("ReductionProperties");
if (PropertyManagerDataService::Instance().doesExist(reductionManagerName)) {
m_reductionManager = PropertyManagerDataService::Instance().retrieve(reductionManagerName);
} else {
m_reductionManager = std::make_shared<PropertyManager>();
PropertyManagerDataService::Instance().addOrReplace(reductionManagerName, m_reductionManager);
}
const bool persistent = getProperty("PersistentCorrection");
if (!m_reductionManager->existsProperty("SANSBeamFinderAlgorithm") && persistent) {
auto algProp = std::make_unique<AlgorithmProperty>("SANSBeamFinderAlgorithm");
algProp->setValue(toString());
m_reductionManager->declareProperty(std::move(algProp));
}
m_output_message = "Beam center determination\n";
// Pixel coordinate to real-space coordinate mapping scheme
bool specialMapping = false;
if (m_reductionManager->existsProperty("InstrumentName")) {
const std::string instrumentName = m_reductionManager->getPropertyValue("InstrumentName");
specialMapping = instrumentName == "HFIRSANS";
}
// Find beam center
double center_x = getProperty("BeamCenterX");
double center_y = getProperty("BeamCenterY");
// Check whether we already know the position
const std::string beamCenterFile = getProperty("Filename");
Poco::Path path(beamCenterFile);
const std::string entryNameX = "SANSBeamFinder_X_" + path.getBaseName();
const std::string entryNameY = "SANSBeamFinder_Y_" + path.getBaseName();
// If the beam enter was provided, simply add it to the reduction
// property manager for other algorithms to find it
if (!isEmpty(center_x) && !isEmpty(center_y)) {
m_output_message += " |Using supplied beam center: ";
} else if (m_reductionManager->existsProperty(entryNameX) && m_reductionManager->existsProperty(entryNameY)) {
center_x = m_reductionManager->getProperty(entryNameX);
center_y = m_reductionManager->getProperty(entryNameY);
} else {
// Load the beam center file
MatrixWorkspace_sptr beamCenterWS = loadBeamFinderFile(beamCenterFile);
// HFIR reduction masks the first pixels on each edge of the detector
if (specialMapping)
// int high, int low, int left, int right
maskEdges(beamCenterWS, 1, 1, 1, 1);
IAlgorithm_sptr ctrAlg = createChildAlgorithm("FindCenterOfMassPosition");
ctrAlg->setProperty("InputWorkspace", beamCenterWS);
const bool directBeam = getProperty("UseDirectBeamMethod");
ctrAlg->setProperty("DirectBeam", directBeam);
double beamRadius = getProperty("BeamRadius");
if (!directBeam && !isEmpty(beamRadius)) {
std::vector<double> pars = beamCenterWS->getInstrument()->getNumberParameter("x-pixel-size");
if (pars.empty()) {
g_log.error() << "Could not read pixel size from instrument "
"parameters: using default\n";
} else {
ctrAlg->setProperty("BeamRadius", beamRadius * pars[0] / 1000.0);
}
}
ctrAlg->execute();
std::vector<double> centerOfMass = ctrAlg->getProperty("CenterOfMass");
if (specialMapping) {
HFIRInstrument::getPixelFromCoordinate(centerOfMass[0], centerOfMass[1], beamCenterWS, center_x, center_y);
} else {
EQSANSInstrument::getPixelFromCoordinate(centerOfMass[0], centerOfMass[1], beamCenterWS, center_x, center_y);
}
m_output_message += " |Found beam center: ";
}
// Store for later use
if (persistent) {
if (!m_reductionManager->existsProperty("LatestBeamCenterX"))
m_reductionManager->declareProperty(std::make_unique<PropertyWithValue<double>>("LatestBeamCenterX", center_x));
if (!m_reductionManager->existsProperty("LatestBeamCenterY"))
m_reductionManager->declareProperty(std::make_unique<PropertyWithValue<double>>("LatestBeamCenterY", center_y));
m_reductionManager->setProperty("LatestBeamCenterX", center_x);
m_reductionManager->setProperty("LatestBeamCenterY", center_y);
}
m_output_message +=
"[" + Poco::NumberFormatter::format(center_x, 3) + ", " + Poco::NumberFormatter::format(center_y, 3) + "]\n";
// Workflow algorithms can use the LatestBeamCenterX/Y entries, but to be
// compatible with the old ReductionSteps we also set output properties
// with the beam center position
setProperty("FoundBeamCenterX", center_x);
setProperty("FoundBeamCenterY", center_y);
setProperty("OutputMessage", m_output_message);
}
/*
* The standard HFIR reduction masks the edges of the detector
* This is here mostly to allow a direct comparison with old HFIR code
* and ensure that we reproduce the same results
*
* 2016/05/06 : this only works for RectangularDetector
*
*/
void SANSBeamFinder::maskEdges(const MatrixWorkspace_sptr &beamCenterWS, int high, int low, int left, int right,
const std::string &componentName) {
auto instrument = beamCenterWS->getInstrument();
std::shared_ptr<Mantid::Geometry::RectangularDetector> component;
try {
component = std::const_pointer_cast<Mantid::Geometry::RectangularDetector>(
std::dynamic_pointer_cast<const Mantid::Geometry::RectangularDetector>(
instrument->getComponentByName(componentName)));
} catch (std::exception &) {
g_log.warning("Expecting the component " + componentName + " to be a RectangularDetector. maskEdges not executed.");
return;
}
if (!component) {
g_log.warning("Expecting the component " + componentName + " to be a RectangularDetector. maskEdges not executed.");
return;
}
std::vector<int> IDs;
// right
for (int i = 0; i < right * component->idstep(); i++) {
IDs.emplace_back(component->idstart() + i);
}
// left
for (int i = component->maxDetectorID(); i > (component->maxDetectorID() - left * component->idstep()); i--) {
IDs.emplace_back(i);
}
// low
// 0,256,512,768,..,1,257,513
for (int row = 0; row < low; row++) { // 0,1
for (int i = row + component->idstart();
i < component->nelements() * component->idstep() - component->idstep() + low + component->idstart();
i += component->idstep()) {
IDs.emplace_back(i);
}
}
// high
// 255, 511, 767..
for (int row = 0; row < high; row++) {
for (int i = component->idstep() + component->idstart() - row - 1;
i < component->nelements() * component->idstep() + component->idstart(); i += component->idstep()) {
IDs.emplace_back(i);
}
}
g_log.debug() << "SANSBeamFinder::maskEdges Detector Ids to Mask:" << std::endl;
for (auto id : IDs) {
g_log.debug() << id << " ";
}
g_log.debug() << std::endl;
IAlgorithm_sptr maskAlg = createChildAlgorithm("MaskDetectors");
maskAlg->setChild(true);
maskAlg->setProperty("Workspace", beamCenterWS);
maskAlg->setProperty("DetectorList", IDs);
maskAlg->execute();
// HAcking to mask Wing Detector when finding the beam center
if (instrument->getComponentByName("wing_detector")) {
maskAlg = createChildAlgorithm("SANSMask");
maskAlg->setProperty("Workspace", beamCenterWS);
maskAlg->setPropertyValue("Facility", "HFIR");
maskAlg->setProperty("MaskedFullComponent", "wing_detector");
maskAlg->execute();
}
}
} // namespace WorkflowAlgorithms
} // namespace Mantid