Skip to content

Commit

Permalink
TRestStringHelper adding UnitConversion method (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgalan committed Jul 14, 2022
1 parent 13150b5 commit 93e7923
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions source/framework/tools/inc/TRestStringHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <sstream>

#include "TRestStringOutput.h"
#include "TRestSystemOfUnits.h"

/// String helper classes. Declared static to be able to have direct access to the methods
namespace REST_StringHelper {
Expand All @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions source/framework/tools/src/TRestStringHelper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit 93e7923

Please sign in to comment.