From 93e7923b7b18fe04e71e808df80c76f7453848ba Mon Sep 17 00:00:00 2001 From: Javier Galan Date: Thu, 14 Jul 2022 11:14:18 +0200 Subject: [PATCH] TRestStringHelper adding UnitConversion method (WIP) --- .../framework/tools/inc/TRestStringHelper.h | 2 ++ .../framework/tools/src/TRestStringHelper.cxx | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/source/framework/tools/inc/TRestStringHelper.h b/source/framework/tools/inc/TRestStringHelper.h index 2123fdd9f..e1ca6124f 100644 --- a/source/framework/tools/inc/TRestStringHelper.h +++ b/source/framework/tools/inc/TRestStringHelper.h @@ -20,6 +20,7 @@ #include #include "TRestStringOutput.h" +#include "TRestSystemOfUnits.h" /// String helper classes. Declared static to be able to have direct access to the methods namespace REST_StringHelper { @@ -34,6 +35,7 @@ Double_t StringToDouble(std::string in); Int_t StringToInteger(std::string in); std::string IntegerToString(Int_t n, std::string format = "%d"); std::string DoubleToString(Double_t d, std::string format = "%4.2lf"); +std::string UnitConversion(std::string in); Bool_t StringToBool(std::string in); Long64_t StringToLong(std::string in); TVector3 StringTo3DVector(std::string in); diff --git a/source/framework/tools/src/TRestStringHelper.cxx b/source/framework/tools/src/TRestStringHelper.cxx index 14f7550a2..db1e05e2a 100644 --- a/source/framework/tools/src/TRestStringHelper.cxx +++ b/source/framework/tools/src/TRestStringHelper.cxx @@ -607,6 +607,26 @@ string REST_StringHelper::IntegerToString(Int_t n, std::string format) { return /// string REST_StringHelper::DoubleToString(Double_t d, std::string format) { return Form(format.c_str(), d); } +/////////////////////////////////////////////// +/// \brief It will transform a string containing units("UNIT"), where UNIT is any REST valid +/// physical unit, to its corresponding scaling factor. +/// +std::string REST_StringHelper::UnitConversion(std::string in) { + std::string inIn = RemoveWhiteSpaces(in); + size_t pos1 = inIn.find("units(\""); + if (pos1 != 0) { + pos1 = inIn.find("units(\'"); + if (pos1 != 0) return in; + } + + size_t pos2 = in.find(")"); + + std::string unit = inIn.substr(7, pos2 - 8); + // std::cout << unit << std::endl; + + return DoubleToString(1. * units(unit)); +} + Bool_t REST_StringHelper::StringToBool(std::string in) { return (ToUpper(in) == "TRUE" || ToUpper(in) == "ON"); }