REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestTools.h
1/*************************************************************************
2 * This file is part of the REST software framework. *
3 * *
4 * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5 * For more information see http://gifna.unizar.es/trex *
6 * *
7 * REST is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * REST is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have a copy of the GNU General Public License along with *
18 * REST in $REST_PATH/LICENSE. *
19 * If not, see http://www.gnu.org/licenses/. *
20 * For the list of contributors see $REST_PATH/CREDITS. *
21 *************************************************************************/
22
23#ifndef RestCore_TRestTools
24#define RestCore_TRestTools
25
26#include <TObject.h>
27#include <TString.h>
28#include <TVector3.h>
29
30#include <map>
31#include <memory>
32#include <string>
33#include <vector>
34
35#define UNUSED(x) (void)x
36
37#ifdef WIN32
38#define EXTERN_DEF __declspec(dllimport)
39#define EXTERN_IMP __declspec(dllexport)
40#else
41#define EXTERN_DEF extern
42#define EXTERN_IMP
43#endif
44
45const std::string PARAMETER_NOT_FOUND_STR = "NO_SUCH_PARA";
46const double PARAMETER_NOT_FOUND_DBL = -99999999;
47
48EXTERN_DEF std::string REST_COMMIT;
49EXTERN_DEF std::string REST_PATH;
50EXTERN_DEF std::string REST_USER;
51EXTERN_DEF std::string REST_USER_PATH;
52EXTERN_DEF std::string REST_TMP_PATH;
53EXTERN_DEF std::map<std::string, std::string> REST_ARGS;
54
57 public:
58 static std::vector<std::string> GetOptions(std::string optionsStr);
59
60 static void LoadRESTLibrary(bool silent = false);
61
62 static int ReadASCIITable(std::string fName, std::vector<std::vector<Double_t>>& data,
63 Int_t skipLines = 0, std::string separator = "\t");
64 static int ReadASCIITable(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0,
65 std::string separator = "\t");
66 static int ReadASCIITable(std::string fName, std::vector<std::vector<std::string>>& data,
67 Int_t skipLines = 0, std::string separator = "\t");
68
69 static int ReadCSVFile(std::string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines = 0);
70 static int ReadCSVFile(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0);
71
72 template <typename T>
73 static void TransposeTable(std::vector<std::vector<T>>& data);
74
75 template <typename T>
76 static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);
77
78 static Bool_t IsBinaryFile(std::string fname);
79
80 static std::string GetFileNameExtension(const std::string& fullname);
81 static std::string GetFileNameRoot(const std::string& fullname);
82
83 static int GetBinaryFileColumns(std::string fname);
84
85 template <typename T>
86 static T GetMaxValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
87
88 template <typename T>
89 static T GetMinValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
90
91 template <typename T>
92 static T GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column);
93
94 template <typename T>
95 static T GetIntegralFromTable(const std::vector<std::vector<T>>& data);
96
97 template <typename T>
98 static std::vector<T> GetColumnFromTable(const std::vector<std::vector<T>>& data, unsigned int column);
99
100 template <typename T>
101 static int PrintTable(std::vector<std::vector<T>> data, Int_t start = 0, Int_t end = 0);
102
103 template <typename T>
104 static int ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data);
105
106 template <typename T>
107 static int ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data);
108
109 static Int_t isValidFile(const std::string& path);
110 static bool fileExists(const std::string& filename);
111 static bool isRootFile(const std::string& filename);
112 static bool isRunFile(const std::string& filename);
113 static bool isDataSet(const std::string& filename);
114 static bool isURL(const std::string& filename);
115 static bool isPathWritable(const std::string& path);
116 static bool isAbsolutePath(const std::string& path);
117 static std::string RemoveMultipleSlash(std::string);
118 static std::string ToAbsoluteName(const std::string& filename);
119 static std::vector<std::string> GetSubdirectories(const std::string& path, int recursion = -1);
120 static std::pair<std::string, std::string> SeparatePathAndName(const std::string& fullname);
121 static std::string GetPureFileName(const std::string& fullPathFileName);
122 static std::string SearchFileInPath(std::vector<std::string> path, std::string filename);
123 static bool CheckFileIsAccessible(const std::string&);
124 static std::vector<std::string> GetFilesMatchingPattern(std::string pattern, bool unlimited = false);
125 static int ConvertVersionCode(std::string in);
126 static std::istream& GetLine(std::istream& is, std::string& t);
127
128 static std::string Execute(std::string cmd);
129
130 static std::string DownloadRemoteFile(const std::string& remoteFile, bool pidPrefix = false);
131 static int DownloadRemoteFile(std::string remoteFile, std::string localFile);
132 static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl = "");
133
134 static std::string POSTRequest(const std::string& url, const std::map<std::string, std::string>& keys);
135 static void ChangeDirectory(const std::string& toDirectory);
136
137 static std::vector<int> CanvasDivisions(int n);
138};
139
140namespace REST_InitTools {
141
142template <class T>
144 static int level;
145};
146template <class T>
148
149template <class T>
150inline bool CanOverwrite(T* name, int level) {
151 if (level > GlobalVarInit<T>::level) {
152 return true;
153 }
154 return false;
155}
156
157template <class T>
158inline void SetInitLevel(T* name, int level) {
159 GlobalVarInit<T>::level = level;
160}
161
162// Initialize global variable with certain class, overwriting the dummy variable of its base class.
163// For example, we initialize gDataBase as TRestDataBase in Framework
164// library. When we load restP3DB library, this object will be overwritten
165// by a new TRestDataBaseP3DB class object, by calling this macro
166#define MakeGlobal(classname, objName, level) \
167 struct __##classname##_Init { \
168 __##classname##_Init() { \
169 REST_ARGS[#objName] = #classname; \
170 if ((objName) != nullptr) { \
171 if (REST_InitTools::CanOverwrite(objName, level)) { \
172 delete (objName); \
173 (objName) = new classname(); \
174 REST_InitTools::SetInitLevel(objName, level); \
175 } \
176 } else { \
177 (objName) = new classname(); \
178 REST_InitTools::SetInitLevel(objName, level); \
179 } \
180 } \
181 }; \
182 const __##classname##_Init classname##_Init;
183
184} // namespace REST_InitTools
185
186enum Quantities { ENERGY, LENGTH, TIME };
188 public:
189 ValueWithQuantity(double value, Quantities quantity) : fValue(value), fQuantity(quantity){};
190 double GetValue() const { return fValue; }
191 std::string ToString() const;
192
193 inline operator std::string() const { return ToString(); }
194
195 private:
196 const double fValue;
197 const Quantities fQuantity;
198};
199
200inline std::string ToEnergyString(double value) { return (std::string)ValueWithQuantity(value, ENERGY); }
201inline std::string ToTimeString(double value) { return (std::string)ValueWithQuantity(value, TIME); }
202inline std::string ToLengthString(double value) { return (std::string)ValueWithQuantity(value, LENGTH); }
203
204std::string ToTimeStringLong(double value);
205
206inline std::string VectorToString(const TVector3& v) {
207 return TString::Format("(%0.3f, %0.3f, %0.3f)", v.X(), v.Y(), v.Z()).Data();
208}
209
210inline std::string VectorToString(const TVector2& v) {
211 return TString::Format("(%0.3f, %0.3f)", v.X(), v.Y()).Data();
212}
213
214#endif
A generic class with useful static methods.
Definition: TRestTools.h:56
static std::string Execute(std::string cmd)
Executes a shell command and returns its output in a string.
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.
Definition: TRestTools.cxx:813
static Int_t isValidFile(const std::string &path)
Returns true if the file with path filename exists.
Definition: TRestTools.cxx:719
static std::string GetFileNameExtension(const std::string &fullname)
Gets the file extension as the substring found after the latest ".".
Definition: TRestTools.cxx:823
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,...
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.
Definition: TRestTools.cxx:904
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.
Definition: TRestTools.cxx:577
static bool CheckFileIsAccessible(const std::string &)
Checks if the config file can be opened (and thus exists). It returns true in case of success,...
Definition: TRestTools.cxx:956
static std::string GetPureFileName(const std::string &fullPathFileName)
Removes all directories in the full path filename description given in the argument.
Definition: TRestTools.cxx:863
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
static int GetBinaryFileColumns(std::string fname)
It extracts the number of columns from the filename extension given by argument. The file should cont...
Definition: TRestTools.cxx:315
static std::vector< std::string > GetOptions(std::string optionsStr)
Returns all the options in an option string.
Definition: TRestTools.cxx:86
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,...
Definition: TRestTools.cxx:163
static std::string GetFileNameRoot(const std::string &fullname)
Gets the filename root as the substring found before the latest ".".
Definition: TRestTools.cxx:834
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....
Definition: TRestTools.cxx:370
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...
Definition: TRestTools.cxx:442
static void LoadRESTLibrary(bool silent=false)
Calls gSystem to load REST library.
Definition: TRestTools.cxx:95
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.
Definition: TRestTools.cxx:695
static bool isRunFile(const std::string &filename)
It checks if the file has been processed using a REST event processing chain.
Definition: TRestTools.cxx:738
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,...
Definition: TRestTools.cxx:185
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
Definition: TRestTools.cxx:728
static std::vector< int > CanvasDivisions(int n)
It returns a vector with 2 components {a,b}, the components satisfy that a x b = n,...
static std::string DownloadRemoteFile(const std::string &remoteFile, bool pidPrefix=false)
download the remote file automatically, returns the downloaded file name.
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....
Definition: TRestTools.cxx:405
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....
Definition: TRestTools.cxx:976
static int ConvertVersionCode(std::string in)
Convert version to a unique string.
static bool isPathWritable(const std::string &path)
Returns true if the path given by argument is writable.
Definition: TRestTools.cxx:778
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...
Definition: TRestTools.cxx:472
static bool isAbsolutePath(const std::string &path)
Check if the path is absolute path or not.
Definition: TRestTools.cxx:795
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....
Definition: TRestTools.cxx:214
static Bool_t IsBinaryFile(std::string fname)
It identifies if the filename extension follows the formatting ".Nxyzf", where the number of columns ...
Definition: TRestTools.cxx:303
static std::string RemoveMultipleSlash(std::string)
Returns the input string but without multiple slashes ("/")
Definition: TRestTools.cxx:844
static bool isDataSet(const std::string &filename)
It checks if the file contains a dataset object.
Definition: TRestTools.cxx:754
static bool isURL(const std::string &filename)
Returns true if filename is an http address.
Definition: TRestTools.cxx:770
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....
static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl="")
static bool isRootFile(const std::string &filename)
Returns true if the filename has *.root* extension.
Definition: TRestTools.cxx:733
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.
Definition: TRestTools.cxx:929
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 col...
Definition: TRestTools.cxx:345
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 ...
Definition: TRestTools.cxx:494
static std::string ToAbsoluteName(const std::string &filename)
It takes a path and returns its absolute path.
Definition: TRestTools.cxx:868