13#include "MantidBeamline/ComponentType.h"
26#include <boost/math/special_functions/round.hpp>
34using namespace CurveFitting;
36using namespace Kernel;
42Logger
g_log(
"SCDPanelErrors");
80 if (detname.compare(
"none") == 0.0)
84 Kernel::DynamicPointerCastHelper::dynamicPointerCastWithCheck<DataObjects::PeaksWorkspace, API::Workspace>(
86 if (inputP->getInstrumentName().compare(
"CORELLI") == 0.0 && detname !=
"moderator")
87 detname.append(
"/sixteenpack");
89 if (
x != 0.0 ||
y != 0.0 ||
z != 0.0) {
90 auto alg1 = Mantid::API::AlgorithmFactory::Instance().create(
"MoveInstrumentComponent", -1);
93 alg1->setLogging(
false);
95 alg1->setPropertyValue(
"ComponentName", detname);
97 alg1->setProperty(
"X",
x);
98 alg1->setProperty(
"Y",
y);
99 alg1->setProperty(
"Z",
z);
100 alg1->setPropertyValue(
"RelativePosition",
"1");
105 auto algx = Mantid::API::AlgorithmFactory::Instance().create(
"RotateInstrumentComponent", -1);
107 algx->setChild(
true);
108 algx->setLogging(
false);
110 algx->setPropertyValue(
"ComponentName", detname);
111 algx->setProperty(
"X", 1.0);
112 algx->setProperty(
"Y", 0.0);
113 algx->setProperty(
"Z", 0.0);
114 algx->setProperty(
"Angle", rotx);
115 algx->setPropertyValue(
"RelativeRotation",
"1");
120 auto algy = Mantid::API::AlgorithmFactory::Instance().create(
"RotateInstrumentComponent", -1);
122 algy->setChild(
true);
123 algy->setLogging(
false);
125 algy->setPropertyValue(
"ComponentName", detname);
126 algy->setProperty(
"X", 0.0);
127 algy->setProperty(
"Y", 1.0);
128 algy->setProperty(
"Z", 0.0);
129 algy->setProperty(
"Angle", roty);
130 algy->setPropertyValue(
"RelativeRotation",
"1");
135 auto algz = Mantid::API::AlgorithmFactory::Instance().create(
"RotateInstrumentComponent", -1);
137 algz->setChild(
true);
138 algz->setLogging(
false);
140 algz->setPropertyValue(
"ComponentName", detname);
141 algz->setProperty(
"X", 0.0);
142 algz->setProperty(
"Y", 0.0);
143 algz->setProperty(
"Z", 1.0);
144 algz->setProperty(
"Angle", rotz);
145 algz->setPropertyValue(
"RelativeRotation",
"1");
149 auto &componentInfo = inputP->mutableComponentInfo();
153 bankIndex = componentInfo.indexOfAny(detname);
154 }
catch (
const std::invalid_argument &) {
158 componentInfo.componentType(bankIndex) == Beamline::ComponentType::Rectangular) {
160 auto oldscalex =
pmap.getDouble(detname,
"scalex");
161 auto oldscaley =
pmap.getDouble(detname,
"scaley");
162 double relscalex =
scalex;
163 double relscaley =
scaley;
164 if (!oldscalex.empty())
165 relscalex /= oldscalex[0];
166 if (!oldscaley.empty())
167 relscaley /= oldscaley[0];
168 componentInfo.addDouble(bankIndex,
"scalex",
scalex);
169 componentInfo.addDouble(bankIndex,
"scaley",
scaley);
176void SCDPanelErrors::eval(
double xshift,
double yshift,
double zshift,
double xrotate,
double yrotate,
double zrotate,
177 double scalex,
double scaley,
double *out,
const double *xValues,
const size_t nData,
178 double tShift)
const {
185 std::shared_ptr<API::Workspace> cloned =
m_workspace->clone();
189 Kernel::DynamicPointerCastHelper::dynamicPointerCastWithCheck<DataObjects::PeaksWorkspace, API::Workspace>(
191 auto inst = inputP->getInstrument();
193 for (
int i = 0; i < inputP->getNumberPeaks(); i++) {
195 V3D hkl =
V3D(boost::math::iround(peak.
getH()), boost::math::iround(peak.
getK()), boost::math::iround(peak.
getL()));
198 if (hkl ==
V3D(0, 0, 0))
199 throw std::runtime_error(
"unindexed peak");
203 wl.
initialize(peak2.
getL1(), 0, {{UnitParams::l2, peak2.getL2()}, {UnitParams::twoTheta, peak2.getScattering()}});
204 peak2.setWavelength(wl.singleFromTOF(peak.
getTOF() + tShift));
205 V3D Q3 = peak2.getQSampleFrame();
206 out[i * 3] = Q3[0] - Q2[0];
207 out[i * 3 + 1] = Q3[1] - Q2[1];
208 out[i * 3 + 2] = Q3[2] - Q2[2];
209 }
catch (std::runtime_error &) {
212 out[i * 3 + 1] = 0.15;
213 out[i * 3 + 2] = 0.15;
224void SCDPanelErrors::function1D(
double *out,
const double *xValues,
const size_t nData)
const {
225 const double xshift = getParameter(
"XShift");
226 const double yshift = getParameter(
"YShift");
227 const double zshift = getParameter(
"ZShift");
228 const double xrotate = getParameter(
"XRotate");
229 const double yrotate = getParameter(
"YRotate");
230 const double zrotate = getParameter(
"ZRotate");
231 const double scalex = getParameter(
"ScaleWidth");
232 const double scaley = getParameter(
"ScaleHeight");
233 const double tShift = getParameter(
"T0Shift");
234 eval(xshift, yshift, zshift, xrotate, yrotate, zrotate,
scalex,
scaley, out, xValues, nData, tShift);
244void SCDPanelErrors::functionDeriv1D(
API::Jacobian *out,
const double *xValues,
const size_t nData) {
246 this->calNumericalDeriv(domain, *out);
250void SCDPanelErrors::clear()
const { m_setupFinished =
false; }
257 if (attName ==
"FileName") {
258 std::string fileName =
value.asUnquotedString();
259 if (fileName.empty()) {
260 storeAttributeValue(
"FileName",
Attribute(
"",
true));
264 std::string
error = fval.isValid(fileName);
266 storeAttributeValue(attName,
Attribute(fileName,
true));
267 storeAttributeValue(
"Workspace",
Attribute(
""));
273 }
else if (attName ==
"Workspace") {
274 std::string wsName =
value.asString();
275 if (!wsName.empty()) {
276 storeAttributeValue(attName,
value);
277 storeAttributeValue(
"FileName",
Attribute(
"",
true));
278 loadWorkspace(wsName);
282 m_setupFinished =
false;
290void SCDPanelErrors::load(
const std::string &fname) {
291 auto loadAlg = Mantid::API::AlgorithmFactory::Instance().create(
"Load", -1);
292 loadAlg->initialize();
293 loadAlg->setChild(
true);
294 loadAlg->setLogging(
false);
296 loadAlg->setPropertyValue(
"Filename", fname);
297 loadAlg->setPropertyValue(
"OutputWorkspace",
"_SCDPanelErrors_fit_data_");
299 }
catch (std::runtime_error &) {
300 throw std::runtime_error(
"Unable to load Nexus file for SCDPanelErrors function.");
305 loadWorkspace(resData);
312void SCDPanelErrors::loadWorkspace(
const std::string &wsName)
const {
313 auto ws = AnalysisDataService::Instance().retrieveWS<
API::Workspace>(wsName);
321void SCDPanelErrors::loadWorkspace(std::shared_ptr<API::Workspace> ws)
const {
322 m_workspace = std::move(ws);
323 m_setupFinished =
false;
329void SCDPanelErrors::setupData()
const {
330 if (m_setupFinished) {
331 g_log.
debug() <<
"Re-setting isn't required.";
336 std::string wsName = getAttribute(
"Workspace").asString();
338 throw std::invalid_argument(
"Data not set for function " + this->
name());
340 loadWorkspace(wsName);
343 m_bank = getAttribute(
"Bank").asString();
345 g_log.
debug() <<
"Setting up " << m_workspace->getName() <<
" bank " << m_bank <<
'\n';
347 m_setupFinished =
true;
double value
The value of the point.
#define DECLARE_FUNCTION(classname)
Macro for declaring a new type of function to be used with the FunctionFactory.
Mantid::API::IFunction::Attribute Attribute
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
1D domain - a wrapper around an array of doubles.
Attribute is a non-fitting parameter.
void declareAttribute(const std::string &name, const API::IFunction::Attribute &defaultValue)
Declare a single attribute.
virtual void setAttribute(const std::string &name, const Attribute &)
Set a value to attribute attName.
Represents the Jacobian in IFitFunction::functionDeriv.
void declareParameter(const std::string &name, double initValue=0, const std::string &description="") override
Declare a new parameter.
Base Workspace Abstract Class.
void moveDetector(double x, double y, double z, double rotx, double roty, double rotz, double scalex, double scaley, std::string detname, const API::Workspace_sptr &inputW) const
Move detectors with parameters.
std::string m_bank
Stores bank.
static const int defaultIndexValue
The default value for the workspace index.
std::shared_ptr< API::Workspace > m_workspace
Temporary workspace holder.
SCDPanelErrors()
Constructor.
void setupData() const
Fill in the workspace and bank names.
void 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
Evaluate the function for a list of arguments and given scaling factor.
double getL() const override
Get the L index of the peak.
double getK() const override
Get the K index of the peak.
double getH() const override
Get the H index of the peak.
Mantid::Kernel::Matrix< double > getGoniometerMatrix() const override
Get the goniometer rotation matrix at which this peak was measured.
Structure describing a single-crystal peak.
double getL1() const override
Return the L1 flight path length (source to sample), in meters.
int getDetectorID() const override
Get the ID of the detector at the center of the peak
double getWavelength() const override
Calculate the neutron wavelength (in angstroms) at the peak (Note for inelastic scattering - it is th...
double getTOF() const override
Calculate the time of flight (in microseconds) of the neutrons for this peak, using the geometry of t...
static constexpr size_t invalidIndex
Returned by indexOfOrInvalid() for a component ID that is not part of this instrument.
Class to implement UB matrix.
Kernel::V3D qFromHKL(const Kernel::V3D &hkl) const
Calculate the hkl corresponding to a given Q-vector.
Parameter map iterator typedef.
Records the filename and the description of failure.
FileValidator is a validator that checks that a filepath is valid.
void debug(const std::string &msg)
Logs at debug level.
void initialize(const double &_l1, const int &_emode, const UnitParametersMap ¶ms)
Initialize the unit to perform conversion using singleToTof() and singleFromTof()
MANTID_API_DLL void applyRectangularDetectorScaleToComponentInfo(Geometry::ComponentInfo &componentInfo, const size_t componentIndex, const double scaleX, const double scaleY)
Helpers for resizing RectangularDetectors.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< PeaksWorkspace > PeaksWorkspace_sptr
Typedef for a shared pointer to a peaks workspace.
Generate a tableworkspace to store the calibration results.
adjust instrument component position and orientation
: detector size scale at y-direction