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 
45 #ifndef WIN32
46 #include <unistd.h>
47 #endif
48 
49 const std::string PARAMETER_NOT_FOUND_STR = "NO_SUCH_PARA";
50 const double PARAMETER_NOT_FOUND_DBL = -99999999;
51 
52 EXTERN_DEF std::string REST_COMMIT;
53 EXTERN_DEF std::string REST_PATH;
54 EXTERN_DEF std::string REST_USER;
55 EXTERN_DEF std::string REST_USER_PATH;
56 EXTERN_DEF std::string REST_TMP_PATH;
57 EXTERN_DEF std::map<std::string, std::string> REST_ARGS;
58 
60 class TRestTools {
61  public:
62  static std::vector<std::string> GetOptions(std::string optionsStr);
63 
64  static void LoadRESTLibrary(bool silent = false);
65 
66  static int ReadASCIITable(std::string fName, std::vector<std::vector<Double_t>>& data,
67  Int_t skipLines = 0, std::string separator = "\t");
68  static int ReadASCIITable(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0,
69  std::string separator = "\t");
70  static int ReadASCIITable(std::string fName, std::vector<std::vector<std::string>>& data,
71  Int_t skipLines = 0, std::string separator = "\t");
72 
73  static int ReadCSVFile(std::string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines = 0);
74  static int ReadCSVFile(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0);
75 
76  template <typename T>
77  static void TransposeTable(std::vector<std::vector<T>>& data);
78 
79  template <typename T>
80  static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);
81 
82  static Bool_t IsBinaryFile(std::string fname);
83 
84  static std::string GetFileNameExtension(const std::string& fullname);
85  static std::string GetFileNameRoot(const std::string& fullname);
86 
87  static int GetBinaryFileColumns(std::string fname);
88 
89  template <typename T>
90  static T GetMaxValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
91 
92  template <typename T>
93  static T GetMinValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
94 
95  template <typename T>
96  static T GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column);
97 
98  template <typename T>
99  static T GetIntegralFromTable(const std::vector<std::vector<T>>& data);
100 
101  template <typename T>
102  static std::vector<T> GetColumnFromTable(const std::vector<std::vector<T>>& data, unsigned int column);
103 
104  template <typename T>
105  static int PrintTable(std::vector<std::vector<T>> data, Int_t start = 0, Int_t end = 0);
106 
107  template <typename T>
108  static int ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data);
109 
110  template <typename T>
111  static int ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data);
112 
113  static Int_t isValidFile(const std::string& path);
114  static bool fileExists(const std::string& filename);
115  static bool isRootFile(const std::string& filename);
116  static bool isRunFile(const std::string& filename);
117  static bool isDataSet(const std::string& filename);
118  static bool isURL(const std::string& filename);
119  static bool isPathWritable(const std::string& path);
120  static bool isAbsolutePath(const std::string& path);
121  static std::string RemoveMultipleSlash(std::string);
122  static std::string ToAbsoluteName(const std::string& filename);
123  static std::vector<std::string> GetSubdirectories(const std::string& path, int recursion = -1);
124  static std::pair<std::string, std::string> SeparatePathAndName(const std::string& fullname);
125  static std::string GetPureFileName(const std::string& fullPathFileName);
126  static std::string SearchFileInPath(std::vector<std::string> path, std::string filename);
127  static bool CheckFileIsAccessible(const std::string&);
128  static std::vector<std::string> GetFilesMatchingPattern(std::string pattern, bool unlimited = false);
129  static int ConvertVersionCode(std::string in);
130  static std::istream& GetLine(std::istream& is, std::string& t);
131 
132  static std::string Execute(std::string cmd);
133 
134  static std::string DownloadRemoteFile(const std::string& remoteFile, bool pidPrefix = false);
135  static int DownloadRemoteFile(std::string remoteFile, std::string localFile);
136  static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl = "");
137 
138  static std::string POSTRequest(const std::string& url, const std::map<std::string, std::string>& keys);
139  static void ChangeDirectory(const std::string& toDirectory);
140 
141  static std::vector<int> CanvasDivisions(int n);
142 };
143 
144 namespace REST_InitTools {
145 
146 template <class T>
148  static int level;
149 };
150 template <class T>
152 
153 template <class T>
154 inline bool CanOverwrite(T* name, int level) {
155  if (level > GlobalVarInit<T>::level) {
156  return true;
157  }
158  return false;
159 }
160 
161 template <class T>
162 inline void SetInitLevel(T* name, int level) {
163  GlobalVarInit<T>::level = level;
164 }
165 
166 // Initialize global variable with certain class, overwriting the dummy variable of its base class.
167 // For example, we initialize gDataBase as TRestDataBase in Framework
168 // library. When we load restP3DB library, this object will be overwritten
169 // by a new TRestDataBaseP3DB class object, by calling this macro
170 #define MakeGlobal(classname, objName, level) \
171  struct __##classname##_Init { \
172  __##classname##_Init() { \
173  REST_ARGS[#objName] = #classname; \
174  if ((objName) != nullptr) { \
175  if (REST_InitTools::CanOverwrite(objName, level)) { \
176  delete (objName); \
177  (objName) = new classname(); \
178  REST_InitTools::SetInitLevel(objName, level); \
179  } \
180  } else { \
181  (objName) = new classname(); \
182  REST_InitTools::SetInitLevel(objName, level); \
183  } \
184  } \
185  }; \
186  const __##classname##_Init classname##_Init;
187 
188 } // namespace REST_InitTools
189 
190 enum Quantities { ENERGY, LENGTH, TIME };
192  public:
193  ValueWithQuantity(double value, Quantities quantity) : fValue(value), fQuantity(quantity){};
194  double GetValue() const { return fValue; }
195  std::string ToString() const;
196 
197  inline operator std::string() const { return ToString(); }
198 
199  private:
200  const double fValue;
201  const Quantities fQuantity;
202 };
203 
204 inline std::string ToEnergyString(double value) { return (std::string)ValueWithQuantity(value, ENERGY); }
205 inline std::string ToTimeString(double value) { return (std::string)ValueWithQuantity(value, TIME); }
206 inline std::string ToLengthString(double value) { return (std::string)ValueWithQuantity(value, LENGTH); }
207 
208 std::string ToTimeStringLong(double value);
209 
210 inline std::string VectorToString(const TVector3& v) {
211  return TString::Format("(%0.3f, %0.3f, %0.3f)", v.X(), v.Y(), v.Z()).Data();
212 }
213 
214 inline std::string VectorToString(const TVector2& v) {
215  return TString::Format("(%0.3f, %0.3f)", v.X(), v.Y()).Data();
216 }
217 
218 #endif
A generic class with useful static methods.
Definition: TRestTools.h:60
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::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 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 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 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 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 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 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 std::string GetFileNameRoot(const std::string &fullname)
Gets the filename root as the substring found before the latest ".".
Definition: TRestTools.cxx:834
static void LoadRESTLibrary(bool silent=false)
Calls gSystem to load REST library.
Definition: TRestTools.cxx:95
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 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 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 bool isAbsolutePath(const std::string &path)
Check if the path is absolute path or not.
Definition: TRestTools.cxx:795
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 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 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 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 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 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 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 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 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 std::string ToAbsoluteName(const std::string &filename)
It takes a path and returns its absolute path.
Definition: TRestTools.cxx:868