REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Static Public Member Functions
TRestTools Class Reference

Detailed Description

A generic class with useful static methods.

TRestTools is a class that defines static methods that might be handy when accessing files or system utilities, or other basic methods that do not fit in a specialized class, and are generic enought to be considered a REST tool.


RESTsoft - Software for Rare Event Searches with TPCs

History of developments:

2016-December: First concept. Javier Galan

Author
Javier Galan javie.nosp@m.r.ga.nosp@m.lan@u.nosp@m.niza.nosp@m.r.es
Kaixiang Ni

Definition at line 60 of file TRestTools.h.

#include <TRestTools.h>

Static Public Member Functions

static std::vector< int > CanvasDivisions (int n)
 It returns a vector with 2 components {a,b}, the components satisfy that a x b = n, being the ratio a/b as close to 1 as possible. More...
 
static void ChangeDirectory (const std::string &toDirectory)
 
static bool CheckFileIsAccessible (const std::string &)
 Checks if the config file can be opened (and thus exists). It returns true in case of success, false otherwise. More...
 
static int ConvertVersionCode (std::string in)
 Convert version to a unique string. More...
 
static std::string DownloadRemoteFile (const std::string &remoteFile, bool pidPrefix=false)
 download the remote file automatically, returns the downloaded file name. More...
 
static int DownloadRemoteFile (std::string remoteFile, std::string localFile)
 download the remote file to the given local address. More...
 
static std::string Execute (std::string cmd)
 Executes a shell command and returns its output in a string. More...
 
template<typename T >
static int ExportASCIITable (std::string fname, std::vector< std::vector< T > > &data)
 Writes the contents of the vector table given as argument to fname. Allowed types are Int_t, Float_t and Double_t. More...
 
template<typename T >
static int ExportBinaryTable (std::string fname, std::vector< std::vector< T > > &data)
 Writes the contents of the vector table given as argument to fname as a binary file. Allowed types are Int_t, Float_t and Double_t. More...
 
static bool fileExists (const std::string &filename)
 Returns true if the file (or directory) with path filename exists. More...
 
static int GetBinaryFileColumns (std::string fname)
 It extracts the number of columns from the filename extension given by argument. The file should contain a binary formatted table with a fixed number of rows and columns. More...
 
template<typename T >
static std::vector< T > GetColumnFromTable (const std::vector< std::vector< T > > &data, unsigned int column)
 It returns a vector with the values extracted from the particular column inside the data table given by argument. More...
 
static std::string GetFileNameExtension (const std::string &fullname)
 Gets the file extension as the substring found after the latest ".". More...
 
static std::string GetFileNameRoot (const std::string &fullname)
 Gets the filename root as the substring found before the latest ".". More...
 
static std::vector< std::string > GetFilesMatchingPattern (std::string pattern, bool unlimited=false)
 Returns a list of files whose name match the pattern string. Key word is "*". e.g. abc00*.root. More...
 
template<typename T >
static T GetIntegralFromTable (const std::vector< std::vector< T > > &data)
 It returns the lowest increase, different from zero, between the elements of a particular column from the table given by argument. More...
 
static std::istream & GetLine (std::istream &is, std::string &t)
 It reads the next line from the incoming istream and puts it in the string argument t. The remaining istream is returned. More...
 
template<typename T >
static T GetLowestIncreaseFromTable (std::vector< std::vector< T > > data, Int_t column)
 It returns the lowest increase, different from zero, between the elements of a particular column from the table given by argument. More...
 
template<typename T >
static T GetMaxValueFromTable (const std::vector< std::vector< T > > &data, Int_t column=-1)
 It returns the maximum value for a particular column from the table given by argument. If no column is specified in the arguments, then it gets the maximum from the full table. More...
 
template<typename T >
static T GetMinValueFromTable (const std::vector< std::vector< T > > &data, Int_t column=-1)
 It returns the minimum value for a particular column from the table given by argument. If no column is specified in the arguments, then it gets the minimum from the full table. More...
 
static std::vector< std::string > GetOptions (std::string optionsStr)
 Returns all the options in an option string. More...
 
static std::string GetPureFileName (const std::string &fullPathFileName)
 Removes all directories in the full path filename description given in the argument. More...
 
static std::vector< std::string > GetSubdirectories (const std::string &path, int recursion=-1)
 It lists all the subdirectories inside path and adds them to the result vector. More...
 
static bool isAbsolutePath (const std::string &path)
 Check if the path is absolute path or not. More...
 
static Bool_t IsBinaryFile (std::string fname)
 It identifies if the filename extension follows the formatting ".Nxyzf", where the number of columns is xyz, and the last character is the type of data (f/d/i), float, double and integer respectively. More...
 
static bool isDataSet (const std::string &filename)
 It checks if the file contains a dataset object. More...
 
static bool isPathWritable (const std::string &path)
 Returns true if the path given by argument is writable. More...
 
static bool isRootFile (const std::string &filename)
 Returns true if the filename has *.root* extension. More...
 
static bool isRunFile (const std::string &filename)
 It checks if the file has been processed using a REST event processing chain. More...
 
static bool isURL (const std::string &filename)
 Returns true if filename is an http address. More...
 
static Int_t isValidFile (const std::string &path)
 Returns true if the file with path filename exists. More...
 
static void LoadRESTLibrary (bool silent=false)
 Calls gSystem to load REST library. More...
 
static std::string POSTRequest (const std::string &url, const std::map< std::string, std::string > &keys)
 It performs a POST web protocol request using a set of keys and values given by argument, and returns the result as a string. More...
 
template<typename T >
static int PrintTable (std::vector< std::vector< T > > data, Int_t start=0, Int_t end=0)
 Prints the contents of the vector table given as argument in screen. Allowed types are Int_t, Float_t and Double_t. More...
 
static int ReadASCIITable (std::string fName, std::vector< std::vector< Double_t > > &data, Int_t skipLines=0, std::string separator="\t")
 Reads an ASCII file containing a table with values. More...
 
static int ReadASCIITable (std::string fName, std::vector< std::vector< Float_t > > &data, Int_t skipLines=0, std::string separator="\t")
 Reads an ASCII file containing a table with values. More...
 
static int ReadASCIITable (std::string fName, std::vector< std::vector< std::string > > &data, Int_t skipLines=0, std::string separator="\t")
 Reads an ASCII file containing a table with values. More...
 
template<typename T >
static int ReadBinaryTable (std::string fName, std::vector< std::vector< T > > &data, Int_t columns=-1)
 Reads a binary file containing a fixed-columns table with values. More...
 
static int ReadCSVFile (std::string fName, std::vector< std::vector< Double_t > > &data, Int_t skipLines=0)
 Reads a CSV file containing a table with comma separated values. More...
 
static int ReadCSVFile (std::string fName, std::vector< std::vector< Float_t > > &data, Int_t skipLines=0)
 Reads a CSV file containing a table with comma separated values. More...
 
static std::string RemoveMultipleSlash (std::string)
 Returns the input string but without multiple slashes ("/") More...
 
static std::string SearchFileInPath (std::vector< std::string > path, std::string filename)
 Search file in the given vector of path strings, return a full name if found, return "" if not. More...
 
static std::pair< std::string, std::string > SeparatePathAndName (const std::string &fullname)
 Separate path and filename in a full path+filename string, returns a pair of string. More...
 
static std::string ToAbsoluteName (const std::string &filename)
 It takes a path and returns its absolute path. More...
 
template<typename T >
static void TransposeTable (std::vector< std::vector< T > > &data)
 It transposes the std::vector<std::vector> table given in the argument. It will transform rows in columns. More...
 
static int UploadToServer (std::string localFile, std::string remoteFile, std::string methodUrl="")
 

Member Function Documentation

◆ CanvasDivisions()

std::vector< int > TRestTools::CanvasDivisions ( int  n)
static

It returns a vector with 2 components {a,b}, the components satisfy that a x b = n, being the ratio a/b as close to 1 as possible.

This method can be used to help dividing a canvas that will contain a number n of plots.

If n is a prime number, then the pair generated will be n x 1.

Definition at line 1332 of file TRestTools.cxx.

◆ ChangeDirectory()

void TRestTools::ChangeDirectory ( const std::string &  toDirectory)
static

Definition at line 1322 of file TRestTools.cxx.

◆ CheckFileIsAccessible()

bool TRestTools::CheckFileIsAccessible ( const std::string &  filename)
static

Checks if the config file can be opened (and thus exists). It returns true in case of success, false otherwise.

Definition at line 956 of file TRestTools.cxx.

◆ ConvertVersionCode()

int TRestTools::ConvertVersionCode ( std::string  in)
static

Convert version to a unique string.

Definition at line 1038 of file TRestTools.cxx.

◆ DownloadRemoteFile() [1/2]

std::string TRestTools::DownloadRemoteFile ( const std::string &  remoteFile,
bool  pidPrefix = false 
)
static

download the remote file automatically, returns the downloaded file name.

The file name is given in url format, and is parsed by TUrl. Various methods will be used, including scp, wget. Downloads to REST_USER_PATH + "/download/" + filename by default.

Definition at line 1124 of file TRestTools.cxx.

◆ DownloadRemoteFile() [2/2]

int TRestTools::DownloadRemoteFile ( std::string  remoteFile,
std::string  localFile 
)
static

download the remote file to the given local address.

The file name is given in url format, and is parsed by TUrl. Various methods will be used, including scp, wget. returns 0 if succeed.

Definition at line 1165 of file TRestTools.cxx.

◆ Execute()

string TRestTools::Execute ( std::string  cmd)
static

Executes a shell command and returns its output in a string.

Definition at line 1061 of file TRestTools.cxx.

◆ ExportASCIITable()

template<typename T >
template int TRestTools::ExportASCIITable< Double_t > ( std::string  fname,
std::vector< std::vector< T > > &  data 
)
static

Writes the contents of the vector table given as argument to fname. Allowed types are Int_t, Float_t and Double_t.

Definition at line 185 of file TRestTools.cxx.

◆ ExportBinaryTable()

template<typename T >
template int TRestTools::ExportBinaryTable< Double_t > ( std::string  fname,
std::vector< std::vector< T > > &  data 
)
static

Writes the contents of the vector table given as argument to fname as a binary file. Allowed types are Int_t, Float_t and Double_t.

Definition at line 214 of file TRestTools.cxx.

◆ fileExists()

bool TRestTools::fileExists ( const std::string &  filename)
static

Returns true if the file (or directory) with path filename exists.

This method will return true even if it is a pure path. We should call isValidFile to check if we will be able to open it without problems.

Definition at line 728 of file TRestTools.cxx.

◆ GetBinaryFileColumns()

int TRestTools::GetBinaryFileColumns ( std::string  fname)
static

It extracts the number of columns from the filename extension given by argument. The file should contain a binary formatted table with a fixed number of rows and columns.

The filename extension will be : ".Nxyzf", where the number of columns is xyz, and the last character is the type of data (f/d/i), float, double and integer respectively.

Definition at line 315 of file TRestTools.cxx.

◆ GetColumnFromTable()

template<typename T >
template std::vector< Double_t > TRestTools::GetColumnFromTable< Double_t > ( const std::vector< std::vector< T > > &  data,
unsigned int  column 
)
static

It returns a vector with the values extracted from the particular column inside the data table given by argument.

This method is available for tables of type Float_t, Double_t and Int_t.

Definition at line 494 of file TRestTools.cxx.

◆ GetFileNameExtension()

string TRestTools::GetFileNameExtension ( const std::string &  fullname)
static

Gets the file extension as the substring found after the latest ".".

Input: "/home/jgalan/abc.txt" Output: "txt"

Definition at line 823 of file TRestTools.cxx.

◆ GetFileNameRoot()

string TRestTools::GetFileNameRoot ( const std::string &  fullname)
static

Gets the filename root as the substring found before the latest ".".

Input: "/home/jgalan/abc.txt" Output: "abc"

Definition at line 834 of file TRestTools.cxx.

◆ GetFilesMatchingPattern()

vector< string > TRestTools::GetFilesMatchingPattern ( std::string  pattern,
bool  unlimited = false 
)
static

Returns a list of files whose name match the pattern string. Key word is "*". e.g. abc00*.root.

Argument unlimited will fix an issue with the number of files being to high. However, it causes issues when searching/listing the macros. The default value for unlimited is false.

Definition at line 976 of file TRestTools.cxx.

◆ GetIntegralFromTable()

template<typename T >
template Double_t TRestTools::GetIntegralFromTable< Double_t > ( const std::vector< std::vector< T > > &  data)
static

It returns the lowest increase, different from zero, between the elements of a particular column from the table given by argument.

This method is available for tables of type Float_t, Double_t and Int_t.

Warning
This method will not check every possible column element difference. It will only look for consecutive elements steps.

Definition at line 472 of file TRestTools.cxx.

◆ GetLine()

std::istream & TRestTools::GetLine ( std::istream &  is,
std::string &  t 
)
static

It reads the next line from the incoming istream and puts it in the string argument t. The remaining istream is returned.

Definition at line 1087 of file TRestTools.cxx.

◆ GetLowestIncreaseFromTable()

template<typename T >
template Double_t TRestTools::GetLowestIncreaseFromTable< Double_t > ( std::vector< std::vector< T > >  data,
Int_t  column 
)
static

It returns the lowest increase, different from zero, between the elements of a particular column from the table given by argument.

This method is available for tables of type Float_t, Double_t and Int_t.

Warning
This method will not check every possible column element difference. It will only look for consecutive elements steps.

Definition at line 442 of file TRestTools.cxx.

◆ GetMaxValueFromTable()

template<typename T >
template Double_t TRestTools::GetMaxValueFromTable< Double_t > ( const std::vector< std::vector< T > > &  data,
Int_t  column = -1 
)
static

It returns the maximum value for a particular column from the table given by argument. If no column is specified in the arguments, then it gets the maximum from the full table.

This method is available for tables of type Float_t, Double_t and Int_t.

Definition at line 370 of file TRestTools.cxx.

◆ GetMinValueFromTable()

template<typename T >
template Double_t TRestTools::GetMinValueFromTable< Double_t > ( const std::vector< std::vector< T > > &  data,
Int_t  column = -1 
)
static

It returns the minimum value for a particular column from the table given by argument. If no column is specified in the arguments, then it gets the minimum from the full table.

This method is available for tables of type Float_t, Double_t and Int_t.

Definition at line 405 of file TRestTools.cxx.

◆ GetOptions()

std::vector< string > TRestTools::GetOptions ( std::string  optionsStr)
static

Returns all the options in an option string.

This method gives string to the method GetFirstOption(). And then adds the result to the list.

Definition at line 86 of file TRestTools.cxx.

◆ GetPureFileName()

string TRestTools::GetPureFileName ( const std::string &  fullPathFileName)
static

Removes all directories in the full path filename description given in the argument.

e.g. Input: "/home/nkx/abc.txt", Returns: "abc.txt" Input: "/home/nkx/", Output: ""

Definition at line 863 of file TRestTools.cxx.

◆ GetSubdirectories()

vector< string > TRestTools::GetSubdirectories ( const std::string &  path,
int  recursion = -1 
)
static

It lists all the subdirectories inside path and adds them to the result vector.

If recursion is 0, then list only the subdirectory of this directory If recursion is < 0, then list subdirectories recursively

Otherwise recurse only certain times.

Definition at line 904 of file TRestTools.cxx.

◆ isAbsolutePath()

bool TRestTools::isAbsolutePath ( const std::string &  path)
static

Check if the path is absolute path or not.

Definition at line 795 of file TRestTools.cxx.

◆ IsBinaryFile()

Bool_t TRestTools::IsBinaryFile ( std::string  fname)
static

It identifies if the filename extension follows the formatting ".Nxyzf", where the number of columns is xyz, and the last character is the type of data (f/d/i), float, double and integer respectively.

Definition at line 303 of file TRestTools.cxx.

◆ isDataSet()

bool TRestTools::isDataSet ( const std::string &  filename)
static

It checks if the file contains a dataset object.

Definition at line 754 of file TRestTools.cxx.

◆ isPathWritable()

bool TRestTools::isPathWritable ( const std::string &  path)
static

Returns true if the path given by argument is writable.

Definition at line 778 of file TRestTools.cxx.

◆ isRootFile()

bool TRestTools::isRootFile ( const std::string &  filename)
static

Returns true if the filename has *.root* extension.

Definition at line 733 of file TRestTools.cxx.

◆ isRunFile()

bool TRestTools::isRunFile ( const std::string &  filename)
static

It checks if the file has been processed using a REST event processing chain.

Definition at line 738 of file TRestTools.cxx.

◆ isURL()

bool TRestTools::isURL ( const std::string &  filename)
static

Returns true if filename is an http address.

Definition at line 770 of file TRestTools.cxx.

◆ isValidFile()

Int_t TRestTools::isValidFile ( const std::string &  path)
static

Returns true if the file with path filename exists.

Definition at line 719 of file TRestTools.cxx.

◆ LoadRESTLibrary()

void TRestTools::LoadRESTLibrary ( bool  silent = false)
static

Calls gSystem to load REST library.

REST library lies in $REST_PATH/lib and $HOME/.rest/userlib/ After this we can use reflection methods TClass::GetClass() and TRestMetadata::GetDataMemberRef()

Definition at line 95 of file TRestTools.cxx.

◆ POSTRequest()

std::string TRestTools::POSTRequest ( const std::string &  url,
const std::map< std::string, std::string > &  keys 
)
static

It performs a POST web protocol request using a set of keys and values given by argument, and returns the result as a string.

Definition at line 1223 of file TRestTools.cxx.

◆ PrintTable()

template<typename T >
template int TRestTools::PrintTable< Double_t > ( std::vector< std::vector< T > >  data,
Int_t  start = 0,
Int_t  end = 0 
)
static

Prints the contents of the vector table given as argument in screen. Allowed types are Int_t, Float_t and Double_t.

The printed out data can be restricted to the row lines between start and end parameters given by argument.

Definition at line 163 of file TRestTools.cxx.

◆ ReadASCIITable() [1/3]

int TRestTools::ReadASCIITable ( std::string  fName,
std::vector< std::vector< Double_t > > &  data,
Int_t  skipLines = 0,
std::string  separator = "\t" 
)
static

Reads an ASCII file containing a table with values.

This method will open the file fName. This file should contain a tabulated ASCII table containing numeric values. The values on the table will be loaded in the matrix provided through the argument data. The content of data will be cleared in this method.

If any header in the file is present, it should be skipped using the argument skipLines or preceding any line inside the header using #.

Only works with Double_t vector since we use StringToDouble method.

Definition at line 577 of file TRestTools.cxx.

◆ ReadASCIITable() [2/3]

int TRestTools::ReadASCIITable ( std::string  fName,
std::vector< std::vector< Float_t > > &  data,
Int_t  skipLines = 0,
std::string  separator = "\t" 
)
static

Reads an ASCII file containing a table with values.

This method will open the file fName. This file should contain a tabulated ASCII table containing numeric values. The values on the table will be loaded in the matrix provided through the argument data. The content of data will be cleared in this method.

If any header in the file is present, it should be skipped using the argument skipLines or preceding any line inside the header using #.

This version works with Float_t vector since we use StringToFloat method.

Definition at line 636 of file TRestTools.cxx.

◆ ReadASCIITable() [3/3]

int TRestTools::ReadASCIITable ( std::string  fName,
std::vector< std::vector< std::string > > &  data,
Int_t  skipLines = 0,
std::string  separator = "\t" 
)
static

Reads an ASCII file containing a table with values.

This method will open the file fName. This file should contain a tabulated ASCII table containing any format values. The values on the table will be loaded in the matrix provided through the argument data. The content of data will be cleared in this method.

If any header in the file is present, it should be skipped using the argument skipLines or preceding any line inside the header using #.

This table will just split the ASCII elements inside a std::string matrix

Definition at line 528 of file TRestTools.cxx.

◆ ReadBinaryTable()

template<typename T >
template int TRestTools::ReadBinaryTable< Double_t > ( std::string  fName,
std::vector< std::vector< T > > &  data,
Int_t  columns = -1 
)
static

Reads a binary file containing a fixed-columns table with values.

This method will open the file fName. This file should contain a table with numeric values of the type specified inside the syntax < >.

For example, a float number table with 6-columns would be read as follows:

std::vector<std::vector <Float_t> > fvec;
ReadBinaryTable( "myfile.bin", fvec, 6);
static int ReadBinaryTable(std::string fName, std::vector< std::vector< T > > &data, Int_t columns=-1)
Reads a binary file containing a fixed-columns table with values.
Definition: TRestTools.cxx:253

The values on the table will be loaded in the matrix provided through the argument data. The content of data will be cleared in this method.

Definition at line 253 of file TRestTools.cxx.

◆ ReadCSVFile() [1/2]

int TRestTools::ReadCSVFile ( std::string  fName,
std::vector< std::vector< Double_t > > &  data,
Int_t  skipLines = 0 
)
static

Reads a CSV file containing a table with comma separated values.

This method will open the file fName. This file should contain a comma separated table containing numeric values. The values on the table will be loaded in the matrix provided through the argument data. The content of data will be cleared in this method.

If any header in the file is present, it should be skipped using the argument skipLines or preceding any line inside the header using #.

Only works with Double_t vector since we use StringToDouble method.

Definition at line 695 of file TRestTools.cxx.

◆ ReadCSVFile() [2/2]

int TRestTools::ReadCSVFile ( std::string  fName,
std::vector< std::vector< Float_t > > &  data,
Int_t  skipLines = 0 
)
static

Reads a CSV file containing a table with comma separated values.

This method will open the file fName. This file should contain a comma separated table containing numeric values. The values on the table will be loaded in the matrix provided through the argument data. The content of data will be cleared in this method.

If any header in the file is present, it should be skipped using the argument skipLines or preceding any line inside the header using #.

Only works with Float_t vector since we use StringToFloat method.

Definition at line 712 of file TRestTools.cxx.

◆ RemoveMultipleSlash()

string TRestTools::RemoveMultipleSlash ( std::string  str)
static

Returns the input string but without multiple slashes ("/")

Parameters
strinput path string (e.g. "///home///test/")
Returns
path string without multiple slashes (e.g. "/home/test/")

Definition at line 844 of file TRestTools.cxx.

◆ SearchFileInPath()

string TRestTools::SearchFileInPath ( std::vector< std::string >  path,
std::string  filename 
)
static

Search file in the given vector of path strings, return a full name if found, return "" if not.

Definition at line 929 of file TRestTools.cxx.

◆ SeparatePathAndName()

std::pair< string, string > TRestTools::SeparatePathAndName ( const std::string &  fullname)
static

Separate path and filename in a full path+filename string, returns a pair of string.

if input file name contains no directory, the returned directory is set to "." if input file name contains no file, the returned filename is set to "" e.g. Input: "/home/nkx/abc.txt" and ":def", Output: { "/home/nkx/", "abc.txt" } Input: "abc.txt" and ":", Output: { ".", "abc.txt" } Input: "/home/nkx/" and ":", Output: { "/home/nkx/", "" }

Definition at line 813 of file TRestTools.cxx.

◆ ToAbsoluteName()

string TRestTools::ToAbsoluteName ( const std::string &  filename)
static

It takes a path and returns its absolute path.

Definition at line 868 of file TRestTools.cxx.

◆ TransposeTable()

template<typename T >
template void TRestTools::TransposeTable< Int_t > ( std::vector< std::vector< T > > &  data)
static

It transposes the std::vector<std::vector> table given in the argument. It will transform rows in columns.

Definition at line 345 of file TRestTools.cxx.

◆ UploadToServer()

int TRestTools::UploadToServer ( std::string  localFile,
std::string  remoteFile,
std::string  methodUrl = "" 
)
static

Upload the local file to remote file, method url will overwrite the login information inside remotefile.

Example: UploadToServer("/home/nkx/abc.txt", "https://sultan.unizar.es/gasFiles/gases.rml", "ssh://nkx:M123456@:8322") Then, the local file abc.txt will be uploaded to the server, renamed to gases.rml and overwrite it

Definition at line 1285 of file TRestTools.cxx.


The documentation for this class was generated from the following files: