/
SCDPanelErrors.cpp
346 lines (314 loc) · 12.5 KB
/
SCDPanelErrors.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// 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 "MantidCrystal/SCDPanelErrors.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidAPI/ResizeRectangularDetectorHelper.h"
#include "MantidAPI/Sample.h"
#include "MantidCrystal/IndexPeaks.h"
#include "MantidDataObjects/Peak.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Component.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/FileValidator.h"
#include <algorithm>
#include <boost/math/special_functions/round.hpp>
#include <cmath>
#include <fstream>
#include <sstream>
#include <utility>
namespace Mantid::Crystal {
using namespace CurveFitting;
using namespace Kernel;
using namespace API;
namespace {
/// static logger
Logger g_log("SCDPanelErrors");
} // namespace
DECLARE_FUNCTION(SCDPanelErrors)
const int SCDPanelErrors::defaultIndexValue = 0;
/// Constructor
SCDPanelErrors::SCDPanelErrors() : m_setupFinished(false) {
declareParameter("XShift", 0.0, "Shift factor in X");
declareParameter("YShift", 0.0, "Shift factor in Y");
declareParameter("ZShift", 0.0, "Shift factor in Z");
declareParameter("XRotate", 0.0, "Rotate angle in X");
declareParameter("YRotate", 0.0, "Rotate angle in Y");
declareParameter("ZRotate", 0.0, "Rotate angle in Z");
declareParameter("ScaleWidth", 1.0, "Scale width of detector");
declareParameter("ScaleHeight", 1.0, "Scale height of detector");
declareParameter("T0Shift", 0.0, "Shift for TOF");
declareAttribute("FileName", Attribute("", true));
declareAttribute("Workspace", Attribute(""));
declareAttribute("Bank", Attribute(""));
}
/**
* The movedetector function changes detector position and angles
* @param x :: The shift along the X-axis
* @param y :: The shift along the Y-axis
* @param z :: The shift along the Z-axis
* @param rotx :: The rotation around the X-axis
* @param roty :: The rotation around the Y-axis
* @param rotz :: The rotation around the Z-axis
* @param scalex :: Scale width of rectangular detector
* @param scaley :: Scale height of rectangular detector
* @param detname :: The detector name
* @param inputW :: The workspace
*/
void SCDPanelErrors::moveDetector(double x, double y, double z, double rotx, double roty, double rotz, double scalex,
double scaley, std::string detname, const Workspace_sptr &inputW) const {
if (detname.compare("none") == 0.0)
return;
// CORELLI has sixteenpack under bank
DataObjects::PeaksWorkspace_sptr inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(inputW);
Geometry::Instrument_sptr inst = std::const_pointer_cast<Geometry::Instrument>(inputP->getInstrument());
if (inst->getName().compare("CORELLI") == 0.0 && detname != "moderator")
detname.append("/sixteenpack");
if (x != 0.0 || y != 0.0 || z != 0.0) {
auto alg1 = Mantid::API::AlgorithmFactory::Instance().create("MoveInstrumentComponent", -1);
alg1->initialize();
alg1->setChild(true);
alg1->setLogging(false);
alg1->setProperty<Workspace_sptr>("Workspace", inputW);
alg1->setPropertyValue("ComponentName", detname);
// Move in m
alg1->setProperty("X", x);
alg1->setProperty("Y", y);
alg1->setProperty("Z", z);
alg1->setPropertyValue("RelativePosition", "1");
alg1->execute();
}
if (rotx != 0.0) {
auto algx = Mantid::API::AlgorithmFactory::Instance().create("RotateInstrumentComponent", -1);
algx->initialize();
algx->setChild(true);
algx->setLogging(false);
algx->setProperty<Workspace_sptr>("Workspace", inputW);
algx->setPropertyValue("ComponentName", detname);
algx->setProperty("X", 1.0);
algx->setProperty("Y", 0.0);
algx->setProperty("Z", 0.0);
algx->setProperty("Angle", rotx);
algx->setPropertyValue("RelativeRotation", "1");
algx->execute();
}
if (roty != 0.0) {
auto algy = Mantid::API::AlgorithmFactory::Instance().create("RotateInstrumentComponent", -1);
algy->initialize();
algy->setChild(true);
algy->setLogging(false);
algy->setProperty<Workspace_sptr>("Workspace", inputW);
algy->setPropertyValue("ComponentName", detname);
algy->setProperty("X", 0.0);
algy->setProperty("Y", 1.0);
algy->setProperty("Z", 0.0);
algy->setProperty("Angle", roty);
algy->setPropertyValue("RelativeRotation", "1");
algy->execute();
}
if (rotz != 0.0) {
auto algz = Mantid::API::AlgorithmFactory::Instance().create("RotateInstrumentComponent", -1);
algz->initialize();
algz->setChild(true);
algz->setLogging(false);
algz->setProperty<Workspace_sptr>("Workspace", inputW);
algz->setPropertyValue("ComponentName", detname);
algz->setProperty("X", 0.0);
algz->setProperty("Y", 0.0);
algz->setProperty("Z", 1.0);
algz->setProperty("Angle", rotz);
algz->setPropertyValue("RelativeRotation", "1");
algz->execute();
}
if (scalex != 1.0 || scaley != 1.0) {
Geometry::IComponent_const_sptr comp = inst->getComponentByName(detname);
std::shared_ptr<const Geometry::RectangularDetector> rectDet =
std::dynamic_pointer_cast<const Geometry::RectangularDetector>(comp);
if (rectDet) {
Geometry::ParameterMap &pmap = inputP->instrumentParameters();
auto oldscalex = pmap.getDouble(rectDet->getName(), "scalex");
auto oldscaley = pmap.getDouble(rectDet->getName(), "scaley");
double relscalex = scalex;
double relscaley = scaley;
if (!oldscalex.empty())
relscalex /= oldscalex[0];
if (!oldscaley.empty())
relscaley /= oldscaley[0];
pmap.addDouble(rectDet.get(), "scalex", scalex);
pmap.addDouble(rectDet.get(), "scaley", scaley);
applyRectangularDetectorScaleToComponentInfo(inputP->mutableComponentInfo(), rectDet->getComponentID(), relscalex,
relscaley);
}
}
}
/// Evaluate the function for a list of arguments and given scaling factor
void SCDPanelErrors::eval(double xshift, double yshift, double zshift, double xrotate, double yrotate, double zrotate,
double scalex, double scaley, double *out, const double *xValues, const size_t nData,
double tShift) const {
UNUSED_ARG(xValues);
if (nData == 0)
return;
setupData();
std::shared_ptr<API::Workspace> cloned = m_workspace->clone();
moveDetector(xshift, yshift, zshift, xrotate, yrotate, zrotate, scalex, scaley, m_bank, cloned);
auto inputP = std::dynamic_pointer_cast<DataObjects::PeaksWorkspace>(cloned);
// IAlgorithm_sptr alg =
// Mantid::API::AlgorithmFactory::Instance().create("IndexPeaks", -1);
// alg->initialize();
// alg->setChild(true);
// alg->setLogging(false);
// alg->setProperty("PeaksWorkspace", inputP);
// alg->setProperty("Tolerance", 0.15);
// alg->execute();
auto inst = inputP->getInstrument();
Geometry::OrientedLattice lattice = inputP->mutableSample().getOrientedLattice();
for (int i = 0; i < inputP->getNumberPeaks(); i++) {
const DataObjects::Peak &peak = inputP->getPeak(i);
V3D hkl = V3D(boost::math::iround(peak.getH()), boost::math::iround(peak.getK()), boost::math::iround(peak.getL()));
V3D Q2 = lattice.qFromHKL(hkl);
try {
if (hkl == V3D(0, 0, 0))
throw std::runtime_error("unindexed peak");
DataObjects::Peak peak2(inst, peak.getDetectorID(), peak.getWavelength(), hkl, peak.getGoniometerMatrix());
Units::Wavelength wl;
wl.initialize(peak2.getL1(), 0, {{UnitParams::l2, peak2.getL2()}, {UnitParams::twoTheta, peak2.getScattering()}});
peak2.setWavelength(wl.singleFromTOF(peak.getTOF() + tShift));
V3D Q3 = peak2.getQSampleFrame();
out[i * 3] = Q3[0] - Q2[0];
out[i * 3 + 1] = Q3[1] - Q2[1];
out[i * 3 + 2] = Q3[2] - Q2[2];
} catch (std::runtime_error &) {
// set penalty for unindexed peaks greater than tolerance
out[i * 3] = 0.15;
out[i * 3 + 1] = 0.15;
out[i * 3 + 2] = 0.15;
}
}
}
/**
* Calculate the function values.
* @param out :: The output buffer for the calculated values.
* @param xValues :: The array of x-values.
* @param nData :: The size of the data.
*/
void SCDPanelErrors::function1D(double *out, const double *xValues, const size_t nData) const {
const double xshift = getParameter("XShift");
const double yshift = getParameter("YShift");
const double zshift = getParameter("ZShift");
const double xrotate = getParameter("XRotate");
const double yrotate = getParameter("YRotate");
const double zrotate = getParameter("ZRotate");
const double scalex = getParameter("ScaleWidth");
const double scaley = getParameter("ScaleHeight");
const double tShift = getParameter("T0Shift");
eval(xshift, yshift, zshift, xrotate, yrotate, zrotate, scalex, scaley, out, xValues, nData, tShift);
}
/**
* function derivatives
* @param out :: The output Jacobian matrix: function derivatives over its
* parameters.
* @param xValues :: The function arguments
* @param nData :: The size of xValues.
*/
void SCDPanelErrors::functionDeriv1D(API::Jacobian *out, const double *xValues, const size_t nData) {
FunctionDomain1DView domain(xValues, nData);
this->calNumericalDeriv(domain, *out);
}
/// Clear all data
void SCDPanelErrors::clear() const { m_setupFinished = false; }
/** Set a value to attribute attName
* @param attName :: The attribute name
* @param value :: The new value
*/
void SCDPanelErrors::setAttribute(const std::string &attName, const IFunction::Attribute &value) {
if (attName == "FileName") {
std::string fileName = value.asUnquotedString();
if (fileName.empty()) {
storeAttributeValue("FileName", Attribute("", true));
return;
}
FileValidator fval;
std::string error = fval.isValid(fileName);
if (error.empty()) {
storeAttributeValue(attName, Attribute(fileName, true));
storeAttributeValue("Workspace", Attribute(""));
} else {
// file not found
throw Kernel::Exception::FileError(error, fileName);
}
load(fileName);
} else if (attName == "Workspace") {
std::string wsName = value.asString();
if (!wsName.empty()) {
storeAttributeValue(attName, value);
storeAttributeValue("FileName", Attribute("", true));
loadWorkspace(wsName);
}
} else {
IFunction::setAttribute(attName, value);
m_setupFinished = false;
}
}
/**
* Load input file as a Nexus file.
* @param fname :: The file name
*/
void SCDPanelErrors::load(const std::string &fname) {
auto loadAlg = Mantid::API::AlgorithmFactory::Instance().create("Load", -1);
loadAlg->initialize();
loadAlg->setChild(true);
loadAlg->setLogging(false);
try {
loadAlg->setPropertyValue("Filename", fname);
loadAlg->setPropertyValue("OutputWorkspace", "_SCDPanelErrors_fit_data_");
loadAlg->execute();
} catch (std::runtime_error &) {
throw std::runtime_error("Unable to load Nexus file for SCDPanelErrors function.");
}
Workspace_sptr ws = loadAlg->getProperty("OutputWorkspace");
API::Workspace_sptr resData = std::dynamic_pointer_cast<Mantid::API::Workspace>(ws);
loadWorkspace(resData);
}
/**
* Load the points from a PeaksWorkspace
* @param wsName :: The workspace to load from
*/
void SCDPanelErrors::loadWorkspace(const std::string &wsName) const {
auto ws = AnalysisDataService::Instance().retrieveWS<API::Workspace>(wsName);
loadWorkspace(ws);
}
/**
* Load the points from a PeaksWorkspace
* @param ws :: The workspace to load from
*/
void SCDPanelErrors::loadWorkspace(std::shared_ptr<API::Workspace> ws) const {
m_workspace = std::move(ws);
m_setupFinished = false;
}
/**
* Fill in the workspace name and bank
*/
void SCDPanelErrors::setupData() const {
if (m_setupFinished) {
g_log.debug() << "Re-setting isn't required.";
return;
}
if (!m_workspace) {
std::string wsName = getAttribute("Workspace").asString();
if (wsName.empty())
throw std::invalid_argument("Data not set for function " + this->name());
else
loadWorkspace(wsName);
}
m_bank = getAttribute("Bank").asString();
g_log.debug() << "Setting up " << m_workspace->getName() << " bank " << m_bank << '\n';
m_setupFinished = true;
}
} // namespace Mantid::Crystal