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 
42 namespace REST_Units {
43 
44 // We use more common physics units instead of SI unit
45 enum 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 
81 bool IsBasicUnit(std::string in);
82 bool IsUnit(std::string in);
83 
84 double GetScaleToStandardUnit(std::string unitsdef);
85 std::string GetStandardUnitDefinition(std::string unitsdef);
86 std::string FindRESTUnitsInString(std::string InString);
87 std::string RemoveUnitsFromString(std::string s);
88 Double_t ConvertValueToRESTUnits(Double_t value, std::string unitsStr);
89 Double_t ConvertRESTUnitsValueToCustomUnits(Double_t value, std::string unitsStr);
90 
91 Double_t GetDblValueInString(std::string in);
92 TVector2 Get2DVectorValueInString(std::string in);
93 TVector3 Get3DVectorValueInString(std::string in);
94 
95 Double_t GetValueInRESTUnits(std::string in);
96 Double_t GetDblValueInRESTUnits(std::string in);
97 TVector2 Get2DVectorInRESTUnits(std::string in);
98 TVector3 Get3DVectorInRESTUnits(std::string in);
99 
100 double _AddUnit(std::string name, int type, double scale);
101 
102 // energy unit multiplier
103 AddUnit(meV, REST_Units::Energy, 1e6);
104 AddUnit(eV, REST_Units::Energy, 1e3);
105 AddUnit(keV, REST_Units::Energy, 1);
106 AddUnit(MeV, REST_Units::Energy, 1e-3);
107 AddUnit(GeV, REST_Units::Energy, 1e-6);
108 AddUnit(J, REST_Units::Energy, 1.60e-19);
109 AddUnit(kJ, REST_Units::Energy, 1.60e-22);
110 
111 // time unit multiplier
112 AddUnit(ns, REST_Units::Time, 1.e3);
113 AddUnit(us, REST_Units::Time, 1.);
114 AddUnit(ms, REST_Units::Time, 1.e-3);
115 AddUnit(s, REST_Units::Time, 1.e-6);
116 AddUnit(Hz, REST_Units::Time, 1.e6);
117 AddUnit(minu, REST_Units::Time, 1.e-6 / 60.);
118 AddUnit(minutes, REST_Units::Time, 1.e-6 / 60.);
119 AddUnit(hr, REST_Units::Time, 1e-6 / 3600.);
120 AddUnit(hours, REST_Units::Time, 1e-6 / 3600.);
121 AddUnit(day, REST_Units::Time, 1e-6 / 3600. / 24.);
122 AddUnit(days, REST_Units::Time, 1e-6 / 3600. / 24.);
123 AddUnit(mon, REST_Units::Time, 1e-6 / 3600. / 24. / 30);
124 AddUnit(months, REST_Units::Time, 1e-6 / 3600. / 24. / 30);
125 AddUnit(yr, REST_Units::Time, 1e-6 / 3600. / 24. / 365.25);
126 AddUnit(years, REST_Units::Time, 1e-6 / 3600. / 24. / 365.25);
127 
128 // length unit multiplier
129 AddUnit(nm, REST_Units::Length, 1e6);
130 AddUnit(um, REST_Units::Length, 1e3);
131 AddUnit(mm, REST_Units::Length, 1.);
132 AddUnit(cm, REST_Units::Length, 1e-1);
133 AddUnit(m, REST_Units::Length, 1e-3);
134 
135 // mass unit multiplier
136 AddUnit(mg, REST_Units::Mass, 1e6);
137 AddUnit(gram, REST_Units::Mass, 1e3);
138 AddUnit(g, REST_Units::Mass, 1e3);
139 AddUnit(kg, REST_Units::Mass, 1.);
140 AddUnit(ton, REST_Units::Mass, 1e-3);
141 
142 // voltage unit multiplier
143 AddUnit(mV, REST_Units::Voltage, 1.e3);
144 AddUnit(V, REST_Units::Voltage, 1.);
145 AddUnit(kV, REST_Units::Voltage, 1.e-3);
146 
147 // magnetic field unit multiplier
148 AddUnit(mT, REST_Units::MagneticField, 1.e3);
149 AddUnit(T, REST_Units::MagneticField, 1.);
150 AddUnit(G, REST_Units::MagneticField, 1.e4);
151 
152 // pressure field unit multiplier
153 AddUnit(bar, REST_Units::Pressure, 1.);
154 AddUnit(mbar, REST_Units::Pressure, 1.e3);
155 AddUnit(atm, REST_Units::Pressure, 1.013);
156 AddUnit(torr, REST_Units::Pressure, 760);
157 AddUnit(MPa, REST_Units::Pressure, 0.101325);
158 AddUnit(kPa, REST_Units::Pressure, 101.325);
159 AddUnit(Pa, REST_Units::Pressure, 101325);
160 AddUnit(mPa, REST_Units::Pressure, 10132500);
161 
162 // angle unit multiplier
163 AddUnit(rad, REST_Units::Angle, 1.);
164 AddUnit(radian, REST_Units::Angle, 1.);
165 AddUnit(radians, REST_Units::Angle, 1.);
166 AddUnit(deg, REST_Units::Angle, TMath::RadToDeg());
167 AddUnit(degree, REST_Units::Angle, TMath::RadToDeg());
168 AddUnit(degrees, REST_Units::Angle, TMath::RadToDeg());
169 AddUnit(arcmin, REST_Units::Angle, TMath::RadToDeg() * 60.);
170 AddUnit(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.