26 #include "TRestStringHelper.h"
37 UnitsStruct(
string _name,
int _type,
double _scale) {
47 bool operator>(
const UnitsStruct& compare)
const {
return name > compare.name; }
49 bool operator<(
const UnitsStruct& compare)
const {
return name < compare.name; }
51 bool operator==(
const UnitsStruct& compare)
const {
return name == compare.name; }
54 map<string, pair<int, double>> __ListOfRESTUnits;
56 #define REST_UnitsAdd_Caller
58 #include "TRestSystemOfUnits.h"
98 bool IsBasicUnit(
string unitsStr) {
return (__ListOfRESTUnits.count(unitsStr) == 1); }
124 string unitDef = s.substr(l, -1);
144 string valstr1 = s.substr(0, s.find_first_not_of(
"1234567890(),.-eE/: "));
146 if (valstr1.size() == 0) {
151 if (valstr1[valstr1.size() - 1] ==
'e' || valstr1[valstr1.size() - 1] ==
'E') {
152 return valstr1.substr(0, valstr1.size() - 1);
193 return TVector2(valueX, valueY);
214 return TVector3(valueX, valueY, valueZ);
255 double _AddUnit(
string name,
int type,
double scale) {
256 __ListOfRESTUnits[name] = {type, scale};
280 TRestSystemOfUnits::TRestSystemOfUnits(
string unitsStr) {
281 unitsStr =
Trim(unitsStr);
284 if (unitsStr.find(
"{") != string::npos || unitsStr.find(
"}") != string::npos) {
289 if (unitsStr ==
"") {
295 for (
size_t pos = 0; pos >= 0 && pos < unitsStr.size();) {
296 if (isalpha(unitsStr[pos])) {
298 while (pos < unitsStr.size() && isalpha(unitsStr[pos])) {
302 string singleunit = unitsStr.substr(pos1, pos2 - pos1);
307 if (unitsStr[pos1 - 1] ==
'/') {
309 }
else if (unitsStr[pos1 - 1] ==
'-' || unitsStr[pos1 - 1] ==
'*') {
311 RESTWarning <<
"illegal unit combiner \"" << unitsStr[pos1 - 1] <<
"\"" << RESTendl;
316 if (pos2 < unitsStr.size() - 1) {
317 if (unitsStr[pos2] ==
'^') {
318 int pos3 = unitsStr.find_first_not_of(
"-0123456789.", pos2 + 1);
319 string orderstr = unitsStr.substr(pos2 + 1, pos3 - pos2 - 1);
325 int _type = GetUnitType(singleunit);
326 double _scale = GetUnitScale(singleunit);
327 double _order = ordernum * orderprefix;
329 fComponents.push_back(_type);
330 fComponentOrder.push_back(_order);
332 fScaleCombined *= pow(_scale, _order);
340 if (pos == unitsStr.size() - 1) {
341 RESTWarning <<
"Last character inside \"" << unitsStr <<
"\" \"" << unitsStr[pos]
342 <<
"\" unrecognized in unit definition!" << RESTendl;
344 std::string lastChar = unitsStr.substr(pos, 1);
347 std::string tmpStr = unitsStr;
348 tmpStr.insert(pos,
"^");
349 RESTWarning <<
"Perhaps you meant: " << tmpStr << RESTendl;
356 if (fComponents.size() == 0) {
363 int TRestSystemOfUnits::GetUnitType(
string singleUnit) {
365 return __ListOfRESTUnits[singleUnit].first;
370 double TRestSystemOfUnits::GetUnitScale(
string singleUnit) {
372 return __ListOfRESTUnits[singleUnit].second;
377 string TRestSystemOfUnits::ToStandardDefinition() {
379 for (
unsigned int i = 0; i < fComponents.size(); i++) {
380 if (fComponentOrder[i] < 0) {
386 for (
auto iter : __ListOfRESTUnits) {
387 if (iter.second.first == fComponents[i] && iter.second.second == 1) {
388 result += iter.first;
392 if (abs(fComponentOrder[i]) != 1) {
393 result +=
"^" + ToString(abs(fComponentOrder[i]));
bool IsZombie() const
Whether this unit is zombie(invalid)
Double_t StringToDouble(std::string in)
Gets a double from a string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.
std::string Trim(std::string s, const char *t=" \t\n\r\f\v")
Removes all white spaces found at the beginning and the end of the string (https://stackoverflow....
Int_t isANumber(std::string in)
Returns 1 only if a valid number is found in the string in. If not it returns 0.
TVector3 StringTo3DVector(std::string in)
Gets a 3D-vector from a string. Format should be : (X,Y,Z).
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.