REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Functions
REST_StringHelper Namespace Reference

String helper classes. Declared static to be able to have direct access to the methods. More...

Functions

Int_t Count (std::string s, std::string sbstring)
 Counts the number of occurences of substring inside the input string in. More...
 
TF1 * CreateTF1FromString (std::string func, double init, double end)
 Reads a function with parameter options from string and returns it as TF1*. More...
 
std::string CropWithPrecision (std::string in, Int_t precision)
 It crops a floating number given inside the string in with the given precision. I.e. CropWithPrecision("3.48604", 2) will return "3.48". More...
 
std::string DataMemberNameToParameterName (std::string name)
 Convert data member name to parameter name, following REST parameter naming convention. More...
 
Int_t DiffString (const std::string &source, const std::string &target)
 Returns the number of different characters between two strings. More...
 
std::string DoubleToString (Double_t d, std::string format="%8.6e")
 Gets a string from a double. More...
 
std::string EscapeSpecialLetters (std::string in)
 
std::string EvaluateExpression (std::string exp)
 Evaluates a complex numerical expression and returns the resulting value using TFormula. More...
 
Int_t FindNthStringPosition (const std::string &in, size_t pos, const std::string &strToFind, size_t nth)
 Returns the position of the nth occurence of the string strToFind inside the string in. More...
 
std::string FirstToUpper (std::string s)
 Convert the first character of a string to upper case. More...
 
Int_t GetChar (std::string hint="Press a KEY to continue ...")
 Helps to pause the program, printing a message before pausing. More...
 
std::vector< int > IntegerToBinary (int number, size_t dimension=0)
 It returns an integer vector with the binary digits decomposed. More...
 
std::string IntegerToString (Int_t n, std::string format="%d")
 Gets a string from an integer. More...
 
Int_t isAExpression (const std::string &in)
 Returns 1 only if valid mathematical expression keywords (or numbers) are found in the string in. If not it returns 0. More...
 
Int_t isANumber (std::string in)
 Returns 1 only if a valid number is found in the string in. If not it returns 0. More...
 
std::string LeftTrim (std::string s, const char *t=" \t\n\r\f\v")
 Removes all white spaces found at the beginning of the string (https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) More...
 
Bool_t MatchString (std::string str, std::string matcher)
 This method matches a string with certain matcher. Returns true if matched. Supports wildcard characters. More...
 
std::string ParameterNameToDataMemberName (std::string name)
 Convert parameter name to datamember name, following REST parameter naming convention. More...
 
std::string RemoveDelimiters (std::string in)
 Returns the input string removing any delimiters ({[]}) More...
 
std::string RemoveWhiteSpaces (std::string in)
 Returns the input string removing all white spaces. More...
 
std::string Replace (std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
 Replace any occurences of thisSring by byThisString inside string in. More...
 
std::string ReplaceMathematicalExpressions (std::string buffer, Int_t precision=0, std::string errorMessage="")
 Evaluates and replaces valid mathematical expressions found in the input string buffer. More...
 
std::string RightTrim (std::string s, const char *t=" \t\n\r\f\v")
 Removes all white spaces found at the end of the string (https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring) More...
 
std::vector< std::string > Split (std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
 Split the input string according to the given separator. Returning a vector of fragments. More...
 
TVector2 StringTo2DVector (std::string in)
 Gets a 2D-vector from a string. More...
 
TVector3 StringTo3DVector (std::string in)
 Gets a 3D-vector from a string. Format should be : (X,Y,Z). More...
 
Bool_t StringToBool (std::string booleanString)
 
Double_t StringToDouble (std::string in)
 Gets a double from a string. More...
 
std::vector< double > StringToElements (std::string in, std::string headChar, std::string separator, std::string tailChar)
 Convert the input string in into a vector of double elements. More...
 
std::vector< double > StringToElements (std::string in, std::string separator)
 Convert the input string into a vector of double elements. More...
 
Float_t StringToFloat (std::string in)
 Gets a float from a string. More...
 
Int_t StringToInteger (std::string in)
 Gets an integer from a string. More...
 
Long64_t StringToLong (std::string in)
 
time_t StringToTimeStamp (std::string time)
 A method to convert a date/time formatted string to a timestamp. More...
 
TRestStringOutput::REST_Verbose_Level StringToVerboseLevel (std::string in)
 
template<class T >
std::string StringWithPrecision (T &value, int precision)
 
std::string ToDateTimeString (time_t time)
 Format time_t into string. More...
 
constexpr ULong64_t ToHash (const char *str, ULong64_t last_value=0xCBF29CE484222325ull)
 
ULong64_t ToHash (std::string str)
 
std::string ToLower (std::string in)
 Convert string to its lower case. Alternative of TString::ToLower. More...
 
template<class T >
std::string ToString (T source, int length=-1, char fill=' ')
 
std::string ToUpper (std::string in)
 Convert string to its upper case. Alternative of TString::ToUpper. More...
 
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.com/questions/216823/whats-the-best-way-to-trim-stdstring) More...
 
std::string TrimAndLower (std::string s)
 It trims and lowers the string. More...
 
std::string TrimAndUpper (std::string s)
 It trims and uppers the string. More...
 
template<class T1 , class T2 >
std::vector< T2 > Vector_cast (std::vector< T1 > vecstring)
 

Detailed Description

String helper classes. Declared static to be able to have direct access to the methods.

Function Documentation

◆ Count()

Int_t REST_StringHelper::Count ( std::string  s,
std::string  sbstring 
)

Counts the number of occurences of substring inside the input string in.

Definition at line 342 of file TRestStringHelper.cxx.

◆ CreateTF1FromString()

TF1 * REST_StringHelper::CreateTF1FromString ( std::string  func,
double  init,
double  end 
)

Reads a function with parameter options from string and returns it as TF1*.

‍The function is defined as a string following ROOT::TFormula conventions for parameters.

Inside the square brackets we allow parameter initialization, constant parameter and ranges. This has been created for fitting functions, where each parameter has its own restrictions.

Examples: – Initial value: [0=3.5] – Fixed value: [0==3.5] – Range: [0=3.5(1,5)] The parameter 0 begin at 3.5 and it can move between 1 and 5.

All parameters should be initialized.

Input arguments:

  • String with function and parameter options.
  • Two doubles with the range of the function.

Output: TF1* with the interpreted fuction. It contains all restrictions, ranges, etc.

Warning
This object will create a new TF1 object in memory, so the code creating a TF1 object using this method as a helper will be responsible to delete this object from memory.
TF1* ff = CreateTF1FromString(xxx);
...
ff->DoSomething();
...
delete ff;
TF1 * CreateTF1FromString(std::string func, double init, double end)
Reads a function with parameter options from string and returns it as TF1*.

Definition at line 882 of file TRestStringHelper.cxx.

◆ CropWithPrecision()

string REST_StringHelper::CropWithPrecision ( std::string  in,
Int_t  precision 
)

It crops a floating number given inside the string in with the given precision. I.e. CropWithPrecision("3.48604", 2) will return "3.48".

It will not round the number. Perhaps on the next update of this method.

Definition at line 78 of file TRestStringHelper.cxx.

◆ DataMemberNameToParameterName()

string REST_StringHelper::DataMemberNameToParameterName ( std::string  name)

Convert data member name to parameter name, following REST parameter naming convention.

‍The name of class data member, if starts from “f” and have the second character in

capital form, will be linked to a parameter. The linked parameter will strip the first “f” and have the first letter in lowercase. For example, data member “fTargetName” is linked to parameter “targetName”.

Definition at line 821 of file TRestStringHelper.cxx.

◆ DiffString()

Int_t REST_StringHelper::DiffString ( const std::string &  source,
const std::string &  target 
)

Returns the number of different characters between two strings.

This algorithm is case insensitive. It matches the two strings in pieces after inserting proper blanks, then counts the unmatched part(just a guess). e.g. "woll " "world" 00101 --> 2

" torgae" "Storage" 1000110 --> 3

"fi le" "title" 10100 --> 2

Source code from https://blog.csdn.net/baidu_23086307/article/details/53020566

Definition at line 443 of file TRestStringHelper.cxx.

◆ DoubleToString()

string REST_StringHelper::DoubleToString ( Double_t  d,
std::string  format = "%8.6e" 
)

Gets a string from a double.

Definition at line 670 of file TRestStringHelper.cxx.

◆ EscapeSpecialLetters()

string REST_StringHelper::EscapeSpecialLetters ( std::string  in)

Definition at line 501 of file TRestStringHelper.cxx.

◆ EvaluateExpression()

string REST_StringHelper::EvaluateExpression ( std::string  exp)

Evaluates a complex numerical expression and returns the resulting value using TFormula.

Definition at line 166 of file TRestStringHelper.cxx.

◆ FindNthStringPosition()

Int_t REST_StringHelper::FindNthStringPosition ( const std::string &  in,
size_t  pos,
const std::string &  strToFind,
size_t  nth 
)

Returns the position of the nth occurence of the string strToFind inside the string in.

Definition at line 356 of file TRestStringHelper.cxx.

◆ FirstToUpper()

string REST_StringHelper::FirstToUpper ( std::string  s)

Convert the first character of a string to upper case.

Definition at line 758 of file TRestStringHelper.cxx.

◆ GetChar()

Int_t REST_StringHelper::GetChar ( std::string  hint = "Press a KEY to continue ...")

Helps to pause the program, printing a message before pausing.

ROOT GUI won't be jammed during this pause.

Definition at line 198 of file TRestStringHelper.cxx.

◆ IntegerToBinary()

std::vector< int > REST_StringHelper::IntegerToBinary ( int  number,
size_t  dimension = 0 
)

It returns an integer vector with the binary digits decomposed.

Example: IntegerToBinary(7) will return { 1, 1, 1 }.

Optionally we can fix the minimum number of digits to be returned, so that it will be filled with zeros to the left.

Example: IntegerToBinary(9,8) will return { 0, 0, 0, 0, 1, 0, 0, 1 }.

Definition at line 646 of file TRestStringHelper.cxx.

◆ IntegerToString()

string REST_StringHelper::IntegerToString ( Int_t  n,
std::string  format = "%d" 
)

Gets a string from an integer.

Definition at line 634 of file TRestStringHelper.cxx.

◆ isAExpression()

Int_t REST_StringHelper::isAExpression ( const std::string &  in)

Returns 1 only if valid mathematical expression keywords (or numbers) are found in the string in. If not it returns 0.

By logic, mathematical expressions must have: +-*‍/e^% in the middle, or % in the end, or math functions in the beginning. despite those symbols, the string should be purely numeric. example: 1+1 --> expression sin(1.5) --> expression 123456789 --> not expression, It is a pure number that can be directly parsed. ./123 --> not expression, it is a path 333/555 --> is expression. But it may also be a path. We should avoid using paths like that

Definition at line 27 of file TRestStringHelper.cxx.

◆ isANumber()

Int_t REST_StringHelper::isANumber ( std::string  in)

Returns 1 only if a valid number is found in the string in. If not it returns 0.

Definition at line 221 of file TRestStringHelper.cxx.

◆ LeftTrim()

string REST_StringHelper::LeftTrim ( std::string  s,
const char *  t = " \t\n\r\f\v" 
)

Removes all white spaces found at the beginning of the string (https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring)

Definition at line 785 of file TRestStringHelper.cxx.

◆ MatchString()

Bool_t REST_StringHelper::MatchString ( std::string  str,
std::string  matcher 
)

This method matches a string with certain matcher. Returns true if matched. Supports wildcard characters.

Wildcard character includes "*" and "?". "*" means to replace any number of any characters "?" means to replace a single arbitary character.

e.g. (string, matcher) "abcddd", "abc?d" --> not matched "abcddd", "abc??d" --> matched "abcddd", "abc*d" --> matched

Note that this method is in equal-match logic. It is not matching substrings. So: "abcddd", "abcddd" --> matched "abcddd", "a?c" --> not matched

Source code from https://blog.csdn.net/dalao_whs/article/details/110477705

Definition at line 381 of file TRestStringHelper.cxx.

◆ ParameterNameToDataMemberName()

string REST_StringHelper::ParameterNameToDataMemberName ( std::string  name)

Convert parameter name to datamember name, following REST parameter naming convention.

‍The name of class data member, if starts from “f” and have the second character in

capital form, will be linked to a parameter. The linked parameter will strip the first “f” and have the first letter in lowercase. For example, data member “fTargetName” is linked to parameter “targetName”.

Definition at line 839 of file TRestStringHelper.cxx.

◆ RemoveDelimiters()

string REST_StringHelper::RemoveDelimiters ( std::string  in)

Returns the input string removing any delimiters ({[]})

Definition at line 302 of file TRestStringHelper.cxx.

◆ RemoveWhiteSpaces()

string REST_StringHelper::RemoveWhiteSpaces ( std::string  in)

Returns the input string removing all white spaces.

Definition at line 315 of file TRestStringHelper.cxx.

◆ Replace()

string REST_StringHelper::Replace ( std::string  in,
std::string  thisString,
std::string  byThisString,
size_t  fromPosition = 0,
Int_t  N = 0 
)

Replace any occurences of thisSring by byThisString inside string in.

Definition at line 485 of file TRestStringHelper.cxx.

◆ ReplaceMathematicalExpressions()

string REST_StringHelper::ReplaceMathematicalExpressions ( std::string  buffer,
Int_t  precision = 0,
std::string  errorMessage = "" 
)

Evaluates and replaces valid mathematical expressions found in the input string buffer.

The buffer string may define sub-expressions that will be evaluated by using single quotes.

I.e. The sentence "The following operation 3 x 4 is '3*4'" will be translated to "The following operatin 3 x 4 is 12".

Definition at line 102 of file TRestStringHelper.cxx.

◆ RightTrim()

string REST_StringHelper::RightTrim ( std::string  s,
const char *  t = " \t\n\r\f\v" 
)

Removes all white spaces found at the end of the string (https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring)

Definition at line 776 of file TRestStringHelper.cxx.

◆ Split()

vector< string > REST_StringHelper::Split ( std::string  in,
std::string  separator,
bool  allowBlankString = false,
bool  removeWhiteSpaces = false,
int  startPos = -1 
)

Split the input string according to the given separator. Returning a vector of fragments.

e.g. Input: "" and "", Output: {} Input: ":" and ":", Output: {} Input: "abc" and "", Output: { "a", "b", "c" } Input: "abc:def" and ":", Output: { "abc", "def" } Input: "abc:def" and ":def", Output: { "abc" }

Definition at line 236 of file TRestStringHelper.cxx.

◆ StringTo2DVector()

TVector2 REST_StringHelper::StringTo2DVector ( std::string  in)

Gets a 2D-vector from a string.

Definition at line 723 of file TRestStringHelper.cxx.

◆ StringTo3DVector()

TVector3 REST_StringHelper::StringTo3DVector ( std::string  in)

Gets a 3D-vector from a string. Format should be : (X,Y,Z).

Definition at line 693 of file TRestStringHelper.cxx.

◆ StringToBool()

Bool_t REST_StringHelper::StringToBool ( std::string  booleanString)

Definition at line 672 of file TRestStringHelper.cxx.

◆ StringToDouble()

Double_t REST_StringHelper::StringToDouble ( std::string  in)

Gets a double from a string.

Definition at line 602 of file TRestStringHelper.cxx.

◆ StringToElements() [1/2]

vector< double > REST_StringHelper::StringToElements ( std::string  in,
std::string  headChar,
std::string  separator,
std::string  tailChar 
)

Convert the input string in into a vector of double elements.

Called as StringToElements( in, "[", ",", "]" ); will get the elements from a string with the following format "[a,b,c]" where a,b,c are double numbers.

Definition at line 281 of file TRestStringHelper.cxx.

◆ StringToElements() [2/2]

vector< double > REST_StringHelper::StringToElements ( std::string  in,
std::string  separator 
)

Convert the input string into a vector of double elements.

The method will remove any delimiters found in the string (), [] or {}.

e.g. Input: "1,2,3,4", Output: {1.,2.,3.,4.}

Definition at line 263 of file TRestStringHelper.cxx.

◆ StringToFloat()

Float_t REST_StringHelper::StringToFloat ( std::string  in)

Gets a float from a string.

Definition at line 613 of file TRestStringHelper.cxx.

◆ StringToInteger()

Int_t REST_StringHelper::StringToInteger ( std::string  in)

Gets an integer from a string.

Definition at line 624 of file TRestStringHelper.cxx.

◆ StringToLong()

Long64_t REST_StringHelper::StringToLong ( std::string  in)

Definition at line 678 of file TRestStringHelper.cxx.

◆ StringToTimeStamp()

time_t REST_StringHelper::StringToTimeStamp ( std::string  time)

A method to convert a date/time formatted string to a timestamp.

The input datatime format should match any of the following patterns: "YYYY-mm-DD HH:MM:SS", "YYYY/mm/DD HH:MM:SS", "YYYY-mm-DD", or "YYYY/mm/DD". If no time is given it will be assumed to be 00:00:00.

REST_StringHelper::ToTime("2018-1-1 8:00:00")
(return) 1514764800

here the type "time_t" is actually the type "long long", which indicates the elapsed time in second from 1970-1-1 8:00:00

Definition at line 560 of file TRestStringHelper.cxx.

◆ StringToVerboseLevel()

TRestStringOutput::REST_Verbose_Level REST_StringHelper::StringToVerboseLevel ( std::string  in)

Definition at line 588 of file TRestStringHelper.cxx.

◆ StringWithPrecision()

template<class T >
std::string REST_StringHelper::StringWithPrecision ( T &  value,
int  precision 
)

Definition at line 80 of file TRestStringHelper.h.

◆ ToDateTimeString()

string REST_StringHelper::ToDateTimeString ( time_t  time)

Format time_t into string.

The output datatime format is "Y-M-D H:M:S". e.g.

(return) 1970-1-1 8:00:00
std::string ToDateTimeString(time_t time)
Format time_t into string.

here the type "time_t" is actually the type "long long", which indicates the elapsed time in second from 1970-1-1 8:00:00

Definition at line 523 of file TRestStringHelper.cxx.

◆ ToHash() [1/2]

constexpr ULong64_t REST_StringHelper::ToHash ( const char *  str,
ULong64_t  last_value = 0xCBF29CE484222325ull 
)
constexpr

Definition at line 58 of file TRestStringHelper.h.

◆ ToHash() [2/2]

ULong64_t REST_StringHelper::ToHash ( std::string  str)

Definition at line 325 of file TRestStringHelper.cxx.

◆ ToLower()

string REST_StringHelper::ToLower ( std::string  in)

Convert string to its lower case. Alternative of TString::ToLower.

Definition at line 767 of file TRestStringHelper.cxx.

◆ ToString()

template<class T >
std::string REST_StringHelper::ToString ( source,
int  length = -1,
char  fill = ' ' 
)

Definition at line 66 of file TRestStringHelper.h.

◆ ToUpper()

string REST_StringHelper::ToUpper ( std::string  in)

Convert string to its upper case. Alternative of TString::ToUpper.

Definition at line 750 of file TRestStringHelper.cxx.

◆ Trim()

string REST_StringHelper::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.com/questions/216823/whats-the-best-way-to-trim-stdstring)

Definition at line 794 of file TRestStringHelper.cxx.

◆ TrimAndLower()

string REST_StringHelper::TrimAndLower ( std::string  s)

It trims and lowers the string.

Definition at line 808 of file TRestStringHelper.cxx.

◆ TrimAndUpper()

string REST_StringHelper::TrimAndUpper ( std::string  s)

It trims and uppers the string.

Definition at line 799 of file TRestStringHelper.cxx.

◆ Vector_cast()

template<class T1 , class T2 >
std::vector< T2 > REST_StringHelper::Vector_cast ( std::vector< T1 >  vecstring)
inline

Definition at line 89 of file TRestStringHelper.h.