23const std::vector<std::string> DIFC_TABLE_COLUMN_NAMES{
"detid",
"difc",
"difa",
"tzero"};
24const std::vector<std::string> DIFC_TABLE_COLUMN_TYPES{
"int",
"double",
"double",
"double"};
26enum class OffsetMode { RELATIVE_OFFSET, ABSOLUTE_OFFSET, SIGNED_OFFSET, enum_count };
27const std::vector<std::string> offsetModeNames{
"Relative",
"Absolute",
"Signed"};
32const std::string
CALIB_WKSP(
"PreviousCalibration");
35const std::string
BINWIDTH(
"BinWidth");
76 "OffsetsWorkspace containing the calibration offsets.");
80 "A calibration table used as a cache for creating the OutputWorkspace. "
81 "Effectively, this algorithm applies partial updates to this table and "
82 "returns it as the OutputWorkspace");
85 std::make_shared<Mantid::Kernel::StringListValidator>(offsetModeNames),
86 "Optional: Whether to calculate a relative, absolute, or signed offset");
89 "Optional: The bin width of the X axis. If using 'Signed' OffsetMode, this value is mandatory");
93 "An output workspace.");
97 std::map<std::string, std::string> result;
101 std::string msg =
"Signed offset mode requires bin width to be specified.";
107 if (previous_calibration) {
109 std::vector<std::string> column_names = previous_calibration->getColumnNames();
110 if (column_names != DIFC_TABLE_COLUMN_NAMES) {
129 auto detIDs = offsetsWS->getSpectrum(
index).getDetectorIDs();
130 if (detIDs.size() != 1) {
131 std::stringstream msg;
132 msg <<
"Encountered spectrum with multiple detector ids (size=" << detIDs.size() <<
")";
133 throw std::logic_error(msg.str());
135 return (*(detIDs.begin()));
149 const detid_t detid = getDetID(offsetsWS,
index);
150 const double offset = offsetsWS->getValue(detid, 0.0);
154 }
catch (std::runtime_error &) {
160 double newDIFC = 0.0;
161 if (offsetMode == OffsetMode::SIGNED_OFFSET)
165 const double factor =
167 newDIFC = 1. / factor;
179double updateAbsoluteDIFC(
const double offset,
const double oldDIFC,
const double unused) {
182 std::stringstream msg;
183 msg <<
"Encountered offset of " << offset <<
" which converts data to negative d-spacing from old DIFC " << oldDIFC
185 throw std::logic_error(msg.str());
187 return oldDIFC / (1.0 + offset);
196double updateSignedDIFC(
const double offset,
const double oldDIFC,
const double binWidth) {
197 return oldDIFC * pow(1.0 +
fabs(binWidth), -1.0 * offset);
217 if (previous_calibration) {
218 configWksp = previous_calibration->clone();
221 configWksp = std::make_shared<TableWorkspace>();
222 for (
size_t i = 0; i < DIFC_TABLE_COLUMN_NAMES.size(); i++)
223 configWksp->addColumn(DIFC_TABLE_COLUMN_TYPES[i], DIFC_TABLE_COLUMN_NAMES[i]);
227 std::unordered_map<int, int> id_to_row;
228 if (previous_calibration) {
229 std::vector<int> previous_calibration_ids = previous_calibration->getColumn(0)->numeric_fill<
int>();
233 for (
auto id : previous_calibration_ids) {
234 id_to_row[id] = row++;
239 const size_t numberOfSpectra = offsetsWS->getNumberHistograms();
247 std::function<double(
const double,
const double,
const double)> newDIFC;
248 if (offsetMode == OffsetMode::SIGNED_OFFSET) {
249 newDIFC = updateSignedDIFC;
251 newDIFC = updateAbsoluteDIFC;
254 for (
size_t i = 0; i < numberOfSpectra; ++i) {
257 detid_t detector_id = getDetID(offsetsWS, i);
258 size_t internal_index = d_info.
indexOf(detector_id);
260 if (!d_info.
isMasked(internal_index)) {
262 double new_offset_value = offsetsWS->getValue(detector_id, 0.0);
265 auto iter = id_to_row.find(detector_id);
268 if (iter != id_to_row.end()) {
270 int row_to_update = iter->second;
272 double &difc_value_to_update = configWksp->cell<
double>(row_to_update, 1);
273 difc_value_to_update = newDIFC(new_offset_value, difc_value_to_update, binWidth);
279 newrow << static_cast<int>(detector_id);
280 newrow << calculateDIFC(offsetsWS, i, spectrumInfo, binWidth, offsetMode);
291 sortTable->setProperty(
"InputWorkspace", configWksp);
292 sortTable->setProperty(
"OutputWorkspace", configWksp);
293 sortTable->setPropertyValue(
"Columns",
"detid");
294 sortTable->executeAsChildAlg();
297 configWksp = sortTable->getProperty(
"OutputWorkspace");
#define DECLARE_ALGORITHM(classname)
std::map< DeltaEMode::Type, std::string > index
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
bool isDefault(const std::string &name) const
ITableWorkspace is an implementation of Workspace in which the data are organised in columns of same ...
Helper class for reporting progress from algorithms.
API::SpectrumInfo is an intermediate step towards a SpectrumInfo that is part of Instrument-2....
double twoTheta(const size_t index) const
Returns the scattering angle 2 theta in radians (angle w.r.t.
double l2(const size_t index) const
Returns L2 (distance from sample to spectrum).
double l1() const
Returns L1 (distance from source to sample).
TableRow represents a row in a TableWorkspace.
A property class for workspaces.
ConvertDiffCal : TODO: DESCRIPTION.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version for identification.
std::map< std::string, std::string > validateInputs() override
Method checking errors on ALL the inputs, before execution.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
An OffsetsWorkspace is a specialized Workspace2D where the Y value at each pixel is the offset to be ...
TableWorkspace is an implementation of Workspace in which the data are organised in columns of same s...
Geometry::DetectorInfo is an intermediate step towards a DetectorInfo that is part of Instrument-2....
bool isMasked(const size_t index) const
Returns true if the detector is masked.
size_t indexOf(const detid_t id) const
Returns the index of the detector with the given detector ID.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< const OffsetsWorkspace > OffsetsWorkspace_const_sptr
shared pointer to a const OffsetsWorkspace
std::shared_ptr< TableWorkspace > TableWorkspace_sptr
shared pointer to Mantid::DataObjects::TableWorkspace
MANTID_GEOMETRY_DLL double calculateDIFCCorrection(const double l1, const double l2, const double twoTheta, const double offset, const double binWidth)
MANTID_GEOMETRY_DLL double tofToDSpacingFactor(const double l1, const double l2, const double twoTheta, const double offset)
Calculate and return conversion factor from tof to d-spacing.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
const std::string BINWIDTH("BinWidth")
const std::string OFFSET_MODE("OffsetMode")
const std::string OFFSTS_WKSP("OffsetsWorkspace")
const std::string CALIB_WKSP("CalibrationWorkspace")
const std::string OUTPUT_WKSP("OutputWorkspace")
Helper class which provides the Collimation Length for SANS instruments.
int32_t detid_t
Typedef for a detector ID.
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Describes the direction (within an algorithm) of a Property.
@ Input
An input workspace.
@ Output
An output workspace.