REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestSystemOfUnits.h
1/*************************************************************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5 * For more information see http://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see http://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
23#ifndef RestCore_TRestSystemOfUnits
24#define RestCore_TRestSystemOfUnits
25
26#include <TMath.h>
27#include <TString.h>
28#include <TVector2.h>
29#include <TVector3.h>
30
31#include <iostream>
32#include <map>
33#include <string>
34
35#ifdef REST_UnitsAdd_Caller
36#define AddUnit(name, type, scale) double name = _AddUnit(#name, type, scale)
37#else
38#define AddUnit(name, type, scale) constexpr double name = scale
39#endif
40
42namespace REST_Units {
43
44// We use more common physics units instead of SI unit
45enum Physical_Unit { Energy, Time, Length, Mass, Voltage, MagneticField, Pressure, Angle, NOT_A_UNIT = -1 };
46
48 private:
49 // stores a list of base units for composite units
50 std::vector<int> fComponents;
51 // stores a list of units order for composite units
52 std::vector<double> fComponentOrder;
53
54 Bool_t fZombie;
55
56 double fScaleCombined;
57
59 int GetUnitType(std::string singleUnit);
61 double GetUnitScale(std::string singleUnit);
62
63 public:
65 TRestSystemOfUnits(std::string unitsStr);
67 bool IsZombie() const { return fZombie; }
68 std::string ToStandardDefinition();
69
70 friend Double_t operator*(const Double_t& val, const TRestSystemOfUnits& units) {
71 if (units.fZombie) return val;
72 return val * units.fScaleCombined;
73 }
74
75 friend Double_t operator/(const Double_t& val, const TRestSystemOfUnits& units) {
76 if (units.fZombie) return val;
77 return val / units.fScaleCombined;
78 }
79};
80
81bool IsBasicUnit(std::string in);
82bool IsUnit(std::string in);
83
84double GetScaleToStandardUnit(std::string unitsdef);
85std::string GetStandardUnitDefinition(std::string unitsdef);
86std::string FindRESTUnitsInString(std::string InString);
87std::string RemoveUnitsFromString(std::string s);
88Double_t ConvertValueToRESTUnits(Double_t value, std::string unitsStr);
89Double_t ConvertRESTUnitsValueToCustomUnits(Double_t value, std::string unitsStr);
90
91Double_t GetDblValueInString(std::string in);
92TVector2 Get2DVectorValueInString(std::string in);
93TVector3 Get3DVectorValueInString(std::string in);
94
95Double_t GetValueInRESTUnits(std::string in);
96Double_t GetDblValueInRESTUnits(std::string in);
97TVector2 Get2DVectorInRESTUnits(std::string in);
98TVector3 Get3DVectorInRESTUnits(std::string in);
99
100double _AddUnit(std::string name, int type, double scale);
101
102// energy unit multiplier
103AddUnit(meV, REST_Units::Energy, 1e6);
104AddUnit(eV, REST_Units::Energy, 1e3);
105AddUnit(keV, REST_Units::Energy, 1);
106AddUnit(MeV, REST_Units::Energy, 1e-3);
107AddUnit(GeV, REST_Units::Energy, 1e-6);
108AddUnit(J, REST_Units::Energy, 1.60e-19);
109AddUnit(kJ, REST_Units::Energy, 1.60e-22);
110
111// time unit multiplier
112AddUnit(ns, REST_Units::Time, 1.e3);
113AddUnit(us, REST_Units::Time, 1.);
114AddUnit(ms, REST_Units::Time, 1.e-3);
115AddUnit(s, REST_Units::Time, 1.e-6);
116AddUnit(Hz, REST_Units::Time, 1.e6);
117AddUnit(minu, REST_Units::Time, 1.e-6 / 60.);
118AddUnit(minutes, REST_Units::Time, 1.e-6 / 60.);
119AddUnit(hr, REST_Units::Time, 1e-6 / 3600.);
120AddUnit(hours, REST_Units::Time, 1e-6 / 3600.);
121AddUnit(day, REST_Units::Time, 1e-6 / 3600. / 24.);
122AddUnit(days, REST_Units::Time, 1e-6 / 3600. / 24.);
123AddUnit(mon, REST_Units::Time, 1e-6 / 3600. / 24. / 30);
124AddUnit(months, REST_Units::Time, 1e-6 / 3600. / 24. / 30);
125AddUnit(yr, REST_Units::Time, 1e-6 / 3600. / 24. / 365.25);
126AddUnit(years, REST_Units::Time, 1e-6 / 3600. / 24. / 365.25);
127
128// length unit multiplier
129AddUnit(nm, REST_Units::Length, 1e6);
130AddUnit(um, REST_Units::Length, 1e3);
131AddUnit(mm, REST_Units::Length, 1.);
132AddUnit(cm, REST_Units::Length, 1e-1);
133AddUnit(m, REST_Units::Length, 1e-3);
134
135// mass unit multiplier
136AddUnit(mg, REST_Units::Mass, 1e6);
137AddUnit(gram, REST_Units::Mass, 1e3);
138AddUnit(g, REST_Units::Mass, 1e3);
139AddUnit(kg, REST_Units::Mass, 1.);
140AddUnit(ton, REST_Units::Mass, 1e-3);
141
142// voltage unit multiplier
143AddUnit(mV, REST_Units::Voltage, 1.e3);
144AddUnit(V, REST_Units::Voltage, 1.);
145AddUnit(kV, REST_Units::Voltage, 1.e-3);
146
147// magnetic field unit multiplier
148AddUnit(mT, REST_Units::MagneticField, 1.e3);
149AddUnit(T, REST_Units::MagneticField, 1.);
150AddUnit(G, REST_Units::MagneticField, 1.e4);
151
152// pressure field unit multiplier
153AddUnit(bar, REST_Units::Pressure, 1.);
154AddUnit(mbar, REST_Units::Pressure, 1.e3);
155AddUnit(atm, REST_Units::Pressure, 1.013);
156AddUnit(torr, REST_Units::Pressure, 760);
157AddUnit(MPa, REST_Units::Pressure, 0.101325);
158AddUnit(kPa, REST_Units::Pressure, 101.325);
159AddUnit(Pa, REST_Units::Pressure, 101325);
160AddUnit(mPa, REST_Units::Pressure, 10132500);
161
162// angle unit multiplier
163AddUnit(rad, REST_Units::Angle, 1.);
164AddUnit(radian, REST_Units::Angle, 1.);
165AddUnit(radians, REST_Units::Angle, 1.);
166AddUnit(deg, REST_Units::Angle, TMath::RadToDeg());
167AddUnit(degree, REST_Units::Angle, TMath::RadToDeg());
168AddUnit(degrees, REST_Units::Angle, TMath::RadToDeg());
169AddUnit(arcmin, REST_Units::Angle, TMath::RadToDeg() * 60.);
170AddUnit(arcsec, REST_Units::Angle, TMath::RadToDeg() * 3600.);
171} // namespace REST_Units
172
174
175#endif
double GetUnitScale(std::string singleUnit)
Get the scale of the unit to convert to the REST standard units.
TRestSystemOfUnits(std::string unitsStr)
Constructor from a unit std::string.
bool IsZombie() const
Whether this unit is zombie(invalid)
int GetUnitType(std::string singleUnit)
Get the type of the units.
This namespace defines the unit conversion for different units which are understood by REST.
bool IsBasicUnit(std::string in)
Checks if the string is a REST basic unit.
Double_t GetValueInRESTUnits(std::string in)
It scales a physics measurement with its units into a REST default units value.
TVector3 Get3DVectorInRESTUnits(std::string in)
It scales a physics measurement with its units into a REST default units value.
Double_t GetDblValueInString(std::string in)
It scales a physics measurement with its units into a REST default units value.
double GetScaleToStandardUnit(std::string unitsdef)
Get the scale to REST standard unit. scale (unitsdef) = 1 (standard unit)
Double_t ConvertRESTUnitsValueToCustomUnits(Double_t value, std::string unitsStr)
Convert value with REST units into the given custom units.
std::string FindRESTUnitsInString(std::string InString)
Find and return the units definition in a string.
Double_t GetDblValueInRESTUnits(std::string in)
It scales a physics measurement with its units into a REST default units value.
std::string GetStandardUnitDefinition(std::string unitsdef)
Get standard form of this unit definition.
bool IsUnit(std::string in)
Checks if the string is a REST supported unit.
TVector2 Get2DVectorInRESTUnits(std::string in)
It scales a physics measurement with its units into a REST default units value.
Double_t ConvertValueToRESTUnits(Double_t value, std::string unitsStr)
Convert value into REST units.
TVector3 Get3DVectorValueInString(std::string in)
It scales a physics measurement with its units into a REST default units value.
TVector2 Get2DVectorValueInString(std::string in)
It scales a physics measurement with its units into a REST default units value.
std::string RemoveUnitsFromString(std::string s)
It should remove all units found inside the input string.
double _AddUnit(std::string name, int type, double scale)
Add a unit with given name, type and scale.