16#include <unordered_map>
22#include "tbb/concurrent_unordered_map.h"
42class MANTID_KERNEL_DLL
Unit {
58 virtual const std::string
unitID()
const = 0;
61 virtual const std::string
caption()
const = 0;
76 bool quickConversion(
const Unit &destination,
double &factor,
double &power)
const;
77 bool quickConversion(std::string destUnitName,
double &factor,
double &power)
const;
92 void toTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
93 std::initializer_list<std::pair<const UnitParams, double>> params);
94 void toTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
112 template <
typename Iterator>
113 void toTOF(Iterator
const xbegin, Iterator
const xend,
double const l1,
int const emode,
115 this->initialize(l1, emode, params);
116 std::transform(xbegin, xend, xbegin, [
this](
double const x) {
return this->singleToTOF(
x); });
120 double convertSingleToTOF(
const double xvalue,
const double &l1,
const int &emode,
const UnitParametersMap ¶ms);
135 void fromTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
136 std::initializer_list<std::pair<const UnitParams, double>> params);
138 void fromTOF(std::vector<double> &xdata, std::vector<double>
const &ydata,
const double &_l1,
const int &_emode,
156 template <
typename Iterator>
157 void fromTOF(Iterator
const xbegin, Iterator
const xend,
double const l1,
int const emode,
159 this->initialize(l1, emode, params);
160 std::transform(xbegin, xend, xbegin, [
this](
double const tof) {
return this->singleFromTOF(tof); });
164 double convertSingleFromTOF(
const double xvalue,
const double &l1,
const int &emode,
const UnitParametersMap ¶ms);
167 void initialize(
const double &_l1,
const int &_emode,
const UnitParametersMap ¶ms);
199 virtual std::pair<double, double> conversionRange()
const;
203 void addConversion(std::string to,
const double &factor,
const double &power = 1.0)
const;
208 virtual void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms);
233 using ConversionsMap = tbb::concurrent_unordered_map<std::string, UnitConversions>;
254 const std::string
unitID()
const override;
255 const std::string
caption()
const override {
return ""; }
259 double singleToTOF(
const double x)
const override;
260 double singleFromTOF(
const double tof)
const override;
261 void init()
override;
262 Unit *clone()
const override;
264 double conversionTOFMin()
const override;
265 double conversionTOFMax()
const override;
275 const std::string
unitID()
const override;
276 const std::string
caption()
const override {
return m_caption; }
280 Label(
const std::string &caption,
const std::string &label);
282 Unit *clone()
const override;
295 const std::string
unitID()
const override;
296 const std::string
caption()
const override {
return "Time-of-flight"; }
300 void init()
override;
301 double singleToTOF(
const double x)
const override;
302 double singleFromTOF(
const double tof)
const override;
303 Unit *clone()
const override;
305 double conversionTOFMin()
const override;
307 double conversionTOFMax()
const override;
314 const std::string
unitID()
const override;
315 const std::string
caption()
const override {
return "Wavelength"; }
318 double singleToTOF(
const double x)
const override;
319 double singleFromTOF(
const double tof)
const override;
320 void init()
override;
321 Unit *clone()
const override;
323 double conversionTOFMin()
const override;
324 double conversionTOFMax()
const override;
330 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
343 const std::string
unitID()
const override;
344 const std::string
caption()
const override {
return "Energy"; }
347 double singleToTOF(
const double x)
const override;
348 double singleFromTOF(
const double tof)
const override;
349 void init()
override;
350 Unit *clone()
const override;
352 double conversionTOFMin()
const override;
353 double conversionTOFMax()
const override;
359 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
368 const std::string
unitID()
const override;
369 const std::string
caption()
const override {
return "Energy"; }
372 double singleToTOF(
const double x)
const override;
373 double singleFromTOF(
const double tof)
const override;
374 void init()
override;
375 Unit *clone()
const override;
376 double conversionTOFMin()
const override;
377 double conversionTOFMax()
const override;
383 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
389 const double offset);
392 const double offset,
const double binWidth);
398 const std::string
unitID()
const override;
399 const std::string
caption()
const override {
return "d-Spacing"; }
401 double singleToTOF(
const double x)
const override;
402 double singleFromTOF(
const double tof)
const override;
403 void init()
override;
404 Unit *clone()
const override;
405 double conversionTOFMin()
const override;
406 double conversionTOFMax()
const override;
407 double calcTofMin(
const double difc,
const double difa,
const double tzero,
const double tofmin = 0.);
408 double calcTofMax(
const double difc,
const double difa,
const double tzero,
const double tofmax = 0.);
414 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
425 const std::string
unitID()
const override;
426 const std::string
caption()
const override {
return "d-SpacingPerpendicular"; }
429 double singleToTOF(
const double x)
const override;
430 double singleFromTOF(
const double tof)
const override;
431 void init()
override;
432 Unit *clone()
const override;
433 double conversionTOFMin()
const override;
434 double conversionTOFMax()
const override;
440 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
452 const std::string
unitID()
const override;
453 const std::string
caption()
const override {
return "q"; }
456 double singleToTOF(
const double x)
const override;
457 double singleFromTOF(
const double tof)
const override;
458 void init()
override;
459 Unit *clone()
const override;
460 double conversionTOFMin()
const override;
461 double conversionTOFMax()
const override;
466 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
474 const std::string
unitID()
const override;
475 const std::string
caption()
const override {
return "Q2"; }
478 double singleToTOF(
const double x)
const override;
479 double singleFromTOF(
const double tof)
const override;
480 Unit *clone()
const override;
481 double conversionTOFMin()
const override;
482 double conversionTOFMax()
const override;
492 const std::string
unitID()
const override;
493 const std::string
caption()
const override {
return "Energy transfer"; }
496 double singleToTOF(
const double x)
const override;
497 double singleFromTOF(
const double tof)
const override;
498 void init()
override;
499 Unit *clone()
const override;
501 double conversionTOFMin()
const override;
502 double conversionTOFMax()
const override;
508 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
521 const std::string
unitID()
const override;
524 void init()
override;
525 Unit *clone()
const override;
534 const std::string
unitID()
const override;
537 void init()
override;
538 Unit *clone()
const override;
547 const std::string
unitID()
const override;
548 const std::string
caption()
const override {
return "Momentum"; }
551 double singleToTOF(
const double ki)
const override;
552 double singleFromTOF(
const double tof)
const override;
553 void init()
override;
554 Unit *clone()
const override;
555 double conversionTOFMin()
const override;
556 double conversionTOFMax()
const override;
562 void validateUnitParams(
const int emode,
const UnitParametersMap ¶ms)
override;
575 const std::string
unitID()
const override;
576 const std::string
caption()
const override {
return "Spin Echo Length"; }
579 double singleToTOF(
const double x)
const override;
580 double singleFromTOF(
const double tof)
const override;
581 void init()
override;
582 Unit *clone()
const override;
583 double conversionTOFMin()
const override;
584 double conversionTOFMax()
const override;
597 const std::string
unitID()
const override;
598 const std::string
caption()
const override {
return "Spin Echo Time"; }
601 double singleToTOF(
const double x)
const override;
602 double singleFromTOF(
const double tof)
const override;
603 void init()
override;
604 Unit *clone()
const override;
605 double conversionTOFMin()
const override;
606 double conversionTOFMax()
const override;
619 const std::string
unitID()
const override;
620 const std::string
caption()
const override {
return "t"; }
624 double singleToTOF(
const double x)
const override;
625 double singleFromTOF(
const double tof)
const override;
626 double conversionTOFMax()
const override;
627 double conversionTOFMin()
const override;
628 void init()
override;
629 Unit *clone()
const override;
644 const std::string
unitID()
const override;
645 const std::string
caption()
const override {
return "Scattering angle"; }
648 Unit *clone()
const override;
650 double singleToTOF(
const double x)
const override;
651 double singleFromTOF(
const double tof)
const override;
660 const std::string
unitID()
const override;
661 const std::string
caption()
const override {
return "Phi"; }
670 const std::string
unitID()
const override;
671 const std::string
caption()
const override {
return "Temperature"; }
674 Unit *clone()
const override;
676 double singleToTOF(
const double x)
const override;
677 double singleFromTOF(
const double tof)
const override;
688 const std::string
unitID()
const override;
689 const std::string
caption()
const override {
return "Atomic Distance"; }
692 Unit *clone()
const override;
694 double singleToTOF(
const double x)
const override;
695 double singleFromTOF(
const double tof)
const override;
703MANTID_KERNEL_DLL
double timeConversionValue(
const std::string &input_unit,
const std::string &output_unit);
709 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
std::shared_ptr< const Unit > Unit_const_sptr
Shared pointer to the Unit base class (const version)
std::shared_ptr< Unit > Unit_sptr
Shared pointer to the Unit base class.
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)