17#include <unordered_map>
20#include "tbb/concurrent_unordered_map.h"
40class MANTID_KERNEL_DLL
Unit {
56 virtual const std::string
unitID()
const = 0;
59 virtual const std::string
caption()
const = 0;
74 bool quickConversion(
const Unit &destination,
double &factor,
double &power)
const;
75 bool quickConversion(std::string destUnitName,
double &factor,
double &power)
const;
90 void toTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
91 std::initializer_list<std::pair<const UnitParams, double>> params);
92 void toTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
110 template <
typename Iterator>
111 void toTOF(Iterator
const xbegin, Iterator
const xend,
double const l1,
int const emode,
113 this->initialize(l1, emode, params);
114 std::transform(xbegin, xend, xbegin, [
this](
double const x) {
return this->singleToTOF(
x); });
118 double convertSingleToTOF(
const double xvalue,
const double &l1,
const int &emode,
const UnitParametersMap ¶ms);
133 void fromTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
134 std::initializer_list<std::pair<const UnitParams, double>> params);
136 void fromTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
154 template <
typename Iterator>
155 void fromTOF(Iterator
const xbegin, Iterator
const xend,
double const l1,
int const emode,
157 this->initialize(l1, emode, params);
158 std::transform(xbegin, xend, xbegin, [
this](
double const tof) {
return this->singleFromTOF(tof); });
162 double convertSingleFromTOF(
const double xvalue,
const double &l1,
const int &emode,
const UnitParametersMap ¶ms);
165 void initialize(
const double &_l1,
const int &_emode,
const UnitParametersMap ¶ms);
197 virtual std::pair<double, double> conversionRange()
const;
201 void addConversion(std::string to,
const double &factor,
const double &power = 1.0)
const;
206 virtual void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms);
231 using ConversionsMap = tbb::concurrent_unordered_map<std::string, UnitConversions>;
247 const std::string
unitID()
const override;
248 const std::string
caption()
const override {
return ""; }
252 double singleToTOF(
const double x)
const override;
253 double singleFromTOF(
const double tof)
const override;
254 void init()
override;
255 Unit *clone()
const override;
257 double conversionTOFMin()
const override;
258 double conversionTOFMax()
const override;
268 const std::string
unitID()
const override;
269 const std::string
caption()
const override {
return m_caption; }
273 Label(
const std::string &caption,
const std::string &label);
275 Unit *clone()
const override;
288 const std::string
unitID()
const override;
289 const std::string
caption()
const override {
return "Time-of-flight"; }
293 void init()
override;
294 double singleToTOF(
const double x)
const override;
295 double singleFromTOF(
const double tof)
const override;
296 Unit *clone()
const override;
298 double conversionTOFMin()
const override;
300 double conversionTOFMax()
const override;
307 const std::string
unitID()
const override;
308 const std::string
caption()
const override {
return "Wavelength"; }
311 double singleToTOF(
const double x)
const override;
312 double singleFromTOF(
const double tof)
const override;
313 void init()
override;
314 Unit *clone()
const override;
316 double conversionTOFMin()
const override;
317 double conversionTOFMax()
const override;
323 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
336 const std::string
unitID()
const override;
337 const std::string
caption()
const override {
return "Energy"; }
340 double singleToTOF(
const double x)
const override;
341 double singleFromTOF(
const double tof)
const override;
342 void init()
override;
343 Unit *clone()
const override;
345 double conversionTOFMin()
const override;
346 double conversionTOFMax()
const override;
352 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
361 const std::string
unitID()
const override;
362 const std::string
caption()
const override {
return "Energy"; }
365 double singleToTOF(
const double x)
const override;
366 double singleFromTOF(
const double tof)
const override;
367 void init()
override;
368 Unit *clone()
const override;
369 double conversionTOFMin()
const override;
370 double conversionTOFMax()
const override;
376 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
382 const double offset);
385 const double offset,
const double binWidth);
391 const std::string
unitID()
const override;
392 const std::string
caption()
const override {
return "d-Spacing"; }
394 double singleToTOF(
const double x)
const override;
395 double singleFromTOF(
const double tof)
const override;
396 void init()
override;
397 Unit *clone()
const override;
398 double conversionTOFMin()
const override;
399 double conversionTOFMax()
const override;
400 double calcTofMin(
const double difc,
const double difa,
const double tzero,
const double tofmin = 0.);
401 double calcTofMax(
const double difc,
const double difa,
const double tzero,
const double tofmax = 0.);
407 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
418 const std::string
unitID()
const override;
419 const std::string
caption()
const override {
return "d-SpacingPerpendicular"; }
422 double singleToTOF(
const double x)
const override;
423 double singleFromTOF(
const double tof)
const override;
424 void init()
override;
425 Unit *clone()
const override;
426 double conversionTOFMin()
const override;
427 double conversionTOFMax()
const override;
433 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
445 const std::string
unitID()
const override;
446 const std::string
caption()
const override {
return "q"; }
449 double singleToTOF(
const double x)
const override;
450 double singleFromTOF(
const double tof)
const override;
451 void init()
override;
452 Unit *clone()
const override;
453 double conversionTOFMin()
const override;
454 double conversionTOFMax()
const override;
459 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
467 const std::string
unitID()
const override;
468 const std::string
caption()
const override {
return "Q2"; }
471 double singleToTOF(
const double x)
const override;
472 double singleFromTOF(
const double tof)
const override;
473 Unit *clone()
const override;
474 double conversionTOFMin()
const override;
475 double conversionTOFMax()
const override;
485 const std::string
unitID()
const override;
486 const std::string
caption()
const override {
return "Energy transfer"; }
489 double singleToTOF(
const double x)
const override;
490 double singleFromTOF(
const double tof)
const override;
491 void init()
override;
492 Unit *clone()
const override;
494 double conversionTOFMin()
const override;
495 double conversionTOFMax()
const override;
501 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
514 const std::string
unitID()
const override;
517 void init()
override;
518 Unit *clone()
const override;
527 const std::string
unitID()
const override;
530 void init()
override;
531 Unit *clone()
const override;
540 const std::string
unitID()
const override;
541 const std::string
caption()
const override {
return "Momentum"; }
544 double singleToTOF(
const double ki)
const override;
545 double singleFromTOF(
const double tof)
const override;
546 void init()
override;
547 Unit *clone()
const override;
548 double conversionTOFMin()
const override;
549 double conversionTOFMax()
const override;
555 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
568 const std::string
unitID()
const override;
569 const std::string
caption()
const override {
return "Spin Echo Length"; }
572 double singleToTOF(
const double x)
const override;
573 double singleFromTOF(
const double tof)
const override;
574 void init()
override;
575 Unit *clone()
const override;
576 double conversionTOFMin()
const override;
577 double conversionTOFMax()
const override;
590 const std::string
unitID()
const override;
591 const std::string
caption()
const override {
return "Spin Echo Time"; }
594 double singleToTOF(
const double x)
const override;
595 double singleFromTOF(
const double tof)
const override;
596 void init()
override;
597 Unit *clone()
const override;
598 double conversionTOFMin()
const override;
599 double conversionTOFMax()
const override;
612 const std::string
unitID()
const override;
613 const std::string
caption()
const override {
return "t"; }
617 double singleToTOF(
const double x)
const override;
618 double singleFromTOF(
const double tof)
const override;
619 double conversionTOFMax()
const override;
620 double conversionTOFMin()
const override;
621 void init()
override;
622 Unit *clone()
const override;
637 const std::string
unitID()
const override;
638 const std::string
caption()
const override {
return "Scattering angle"; }
641 Unit *clone()
const override;
643 double singleToTOF(
const double x)
const override;
644 double singleFromTOF(
const double tof)
const override;
653 const std::string
unitID()
const override;
654 const std::string
caption()
const override {
return "Phi"; }
663 const std::string
unitID()
const override;
664 const std::string
caption()
const override {
return "Temperature"; }
667 Unit *clone()
const override;
669 double singleToTOF(
const double x)
const override;
670 double singleFromTOF(
const double tof)
const override;
681 const std::string
unitID()
const override;
682 const std::string
caption()
const override {
return "Atomic Distance"; }
685 Unit *clone()
const override;
687 double singleToTOF(
const double x)
const override;
688 double singleFromTOF(
const double tof)
const override;
696MANTID_KERNEL_DLL
double timeConversionValue(
const std::string &input_unit,
const std::string &output_unit);
702 std::transform(
vec.begin(),
vec.end(),
vec.begin(), [factor](T
x) -> T { return x * static_cast<T>(factor); });
std::vector< T > const * vec
A base-class for the a class that is able to return unit labels in different representations.
The base units (abstract) class.
virtual Unit * clone() const =0
const UnitParametersMap * m_params
additional parameters l2 :: distance from sample to detector (in metres) twoTheta :: scattering angle...
virtual void init()=0
Finalize the initialization.
virtual ~Unit()=default
Virtual destructor.
virtual const UnitLabel label() const =0
A label for the unit to be printed on axes,.
void toTOF(Iterator const xbegin, Iterator const xend, double const l1, int const emode, UnitParametersMap const ¶ms)
Convert a range of X values in place from the concrete unit to time-of-flight.
virtual double conversionTOFMin() const =0
some units can be converted from TOF only in the range of TOF ; This function returns minimal TOF val...
virtual double conversionTOFMax() const =0
This function returns maximal TOF value still reversibly convertible into the unit.
virtual double singleToTOF(const double x) const =0
Convert a single X value to TOF.
virtual bool isConvertible() const
Returns if the unit can be used in conversions.
bool initialized
The unit values have been initialized.
virtual const std::string caption() const =0
The full name of the unit.
tbb::concurrent_unordered_map< std::string, UnitConversions > ConversionsMap
The possible 'quick conversions' are held in a map with the starting unit as the key.
virtual const std::string unitID() const =0
The name of the unit.
std::pair< double, double > ConstantAndPower
A 'quick conversion' requires the constant by which to multiply the input and the power to which to r...
virtual double singleFromTOF(const double tof) const =0
Convert a single tof value to this unit.
tbb::concurrent_unordered_map< std::string, ConstantAndPower > UnitConversions
Lists, for a given starting unit, the units to which a 'quick conversion' can be made.
void fromTOF(Iterator const xbegin, Iterator const xend, double const l1, int const emode, UnitParametersMap const ¶ms)
Convert a range of X values in place from time-of-flight to the concrete unit.
double l1
l1 :: The source-sample distance (in metres)
static ConversionsMap s_conversionFactors
The table of possible 'quick conversions'.
int emode
emode :: The energy mode (0=elastic, 1=direct geometry, 2=indirect geometry)
bool isInitialized() const
Atomic Distance in Angstroms.
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"AtomicDistance"
Degrees that has degrees as unit and "Scattering angle" as title.
const std::string unitID() const override
The name of the unit.
const std::string caption() const override
< Degrees
Energy transfer in units of frequency (MHz)
const std::string unitID() const override
"DeltaE_inFrequency"
Energy transfer in units of wavenumber (cm^-1)
const std::string unitID() const override
"DeltaE_inWavenumber"
Energy transfer in milli-electronvolts.
const std::string unitID() const override
"DeltaE"
const std::string caption() const override
The full name of the unit.
double factorFrom
Constant factor for from conversion.
double factorTo
Constant factor for to conversion.
double t_otherFrom
Energy mode dependent factor in from conversion.
double unitScaling
Apply unit scaling to energy value.
double t_other
Energy mode dependent factor in to conversion.
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"Empty"
bool isConvertible() const override
Returns if the unit can be used in conversions.
Absolute energy in units of wavenumber (cm^-1)
const std::string unitID() const override
"Energy_inWavenumber"
double factorFrom
Constant factor for from conversion.
const std::string caption() const override
The full name of the unit.
double factorTo
Constant factor for to conversion.
Energy in milli-electronvolts.
const std::string unitID() const override
"Energy"
const std::string caption() const override
The full name of the unit.
double factorTo
Constant factor for to conversion.
double factorFrom
Constant factor for from conversion.
const std::string caption() const override
The full name of the unit.
std::string m_caption
Caption.
const std::string unitID() const override
"Label"
Momentum Transfer in Angstrom^-1.
const std::string unitID() const override
"MomentumTransfer"
const std::string caption() const override
The full name of the unit.
double sfpTo
Extra correction factor in to conversion.
const std::string unitID() const override
"Momentum"
const std::string caption() const override
The full name of the unit.
double sfpFrom
Extra correction factor in from conversion.
bool do_sfpFrom
Apply the sfpFrom value.
double factorTo
Constant factor for to conversion.
double factorFrom
Constant factor for from conversion.
Phi that has degrees as unit and "Phi" as title.
const std::string caption() const override
< Degrees
Unit * clone() const override
const std::string unitID() const override
The name of the unit.
Momentum transfer squared in Angstrom^-2.
const std::string unitID() const override
"QSquared"
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"SpinEchoLength"
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"SpinEchoTime"
const std::string caption() const override
The full name of the unit.
Time of flight in microseconds.
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"TOF"
const std::string unitID() const override
"Temperature"
const std::string caption() const override
The full name of the unit.
double factorFrom
Constant factor for from conversion.
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"Time"
bool isConvertible() const override
Returns if the unit can be used in conversions.
double factorTo
Constant factor for to conversion.
const std::string caption() const override
The full name of the unit.
const std::string unitID() const override
"Wavelength"
bool do_sfpFrom
Apply the sfpFrom value.
double sfpFrom
Extra correction factor in from conversion.
double factorTo
Constant factor for to conversion.
double sfpTo
Extra correction factor in to conversion.
double factorFrom
Constant factor for from conversion.
d-SpacingPerpendicular in Angstrom
const std::string caption() const override
The full name of the unit.
double sfpFrom
Extra correction factor in to conversion.
double factorFrom
Constant factor for from conversion.
double factorTo
Constant factor for to conversion.
const std::string unitID() const override
"dSpacingPerpendicular"
double sfpTo
Extra correction factor in to conversion.
std::string toDSpacingError
const std::string unitID() const override
"dSpacing"
const std::string caption() const override
The full name of the unit.
void timeConversionVector(std::vector< T > &vec, const std::string &input_unit, const std::string &output_unit)
MANTID_KERNEL_DLL double calculateDIFCCorrection(const double l1, const double l2, const double twotheta, const double offset, const double binWidth)
Calculate DIFC in case of logarithmic binning, used in CalculateDIFC with Signed mode.
MANTID_KERNEL_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.
MANTID_KERNEL_DLL double timeConversionValue(const std::string &input_unit, const std::string &output_unit)
std::unordered_map< UnitParams, double > UnitParametersMap
Helper class which provides the Collimation Length for SANS instruments.
constexpr bool operator==(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)
constexpr bool operator!=(const wide_integer< Bits, Signed > &lhs, const wide_integer< Bits2, Signed2 > &rhs)