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
67 static int ReadCSVFile(std::string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines = 0);
68 static int ReadCSVFile(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0);
69
70 template <typename T>
71 static void TransposeTable(std::vector<std::vector<T>>& data);
72
73 template <typename T>
74 static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);
75
76 static Bool_t IsBinaryFile(std::string fname);
77
78 static std::string GetFileNameExtension(const std::string& fullname);
79 static std::string GetFileNameRoot(const std::string& fullname);
80
81 static int GetBinaryFileColumns(std::string fname);
82
83 template <typename T>
84 static T GetMaxValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
85
86 template <typename T>
87 static T GetMinValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
88
89 template <typename T>
90 static T GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column);
91
92 template <typename T>
93 static T GetIntegralFromTable(const std::vector<std::vector<T>>& data);
94
95 template <typename T>
96 static std::vector<T> GetColumnFromTable(const std::vector<std::vector<T>>& data, unsigned int column);
97
98 template <typename T>
99 static int PrintTable(std::vector<std::vector<T>> data, Int_t start = 0, Int_t end = 0);
100
101 template <typename T>
102 static int ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data);
103
104 template <typename T>
105 static int ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data);
106
107 static Int_t isValidFile(const std::string& path);
108 static bool fileExists(const std::string& filename);
109 static bool isRootFile(const std::string& filename);
110 static bool isRunFile(const std::string& filename);
111 static bool isDataSet(const std::string& filename);
112 static bool isURL(const std::string& filename);
113 static bool isPathWritable(const std::string& path);
114 static bool isAbsolutePath(const std::string& path);
115 static std::string RemoveMultipleSlash(std::string);
116 static std::string ToAbsoluteName(const std::string& filename);
117 static std::vector<std::string> GetSubdirectories(const std::string& path, int recursion = -1);
118 static std::pair<std::string, std::string> SeparatePathAndName(const std::string& fullname);
119 static std::string GetPureFileName(const std::string& fullPathFileName);
120 static std::string SearchFileInPath(std::vector<std::string> path, std::string filename);
121 static bool CheckFileIsAccessible(const std::string&);
122 static std::vector<std::string> GetFilesMatchingPattern(std::string pattern);
123 static int ConvertVersionCode(std::string in);
124 static std::istream& GetLine(std::istream& is, std::string& t);
125
126 static std::string Execute(std::string cmd);
127
128 static std::string DownloadRemoteFile(const std::string& remoteFile);
129 static int DownloadRemoteFile(std::string remoteFile, std::string localFile);
130 static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl = "");
131
132 static std::string POSTRequest(const std::string& url, const std::map<std::string, std::string>& keys);
133 static void ChangeDirectory(const std::string& toDirectory);
134};
135
136namespace REST_InitTools {
137
138template <class T>
140 static int level;
141};
142template <class T>
144
145template <class T>
146inline bool CanOverwrite(T* name, int level) {
147 if (level > GlobalVarInit<T>::level) {
148 return true;
149 }
150 return false;
151}
152
153template <class T>
154inline void SetInitLevel(T* name, int level) {
155 GlobalVarInit<T>::level = level;
156}
157
158// Initialize global variable with certain class, overwriting the dummy variable of its base class.
159// For example, we initialize gDataBase as TRestDataBase in Framework
160// library. When we load restP3DB library, this object will be overwritten
161// by a new TRestDataBaseP3DB class object, by calling this macro
162#define MakeGlobal(classname, objName, level) \
163 struct __##classname##_Init { \
164 __##classname##_Init() { \
165 REST_ARGS[#objName] = #classname; \
166 if ((objName) != nullptr) { \
167 if (REST_InitTools::CanOverwrite(objName, level)) { \
168 delete (objName); \
169 (objName) = new classname(); \
170 REST_InitTools::SetInitLevel(objName, level); \
171 } \
172 } else { \
173 (objName) = new classname(); \
174 REST_InitTools::SetInitLevel(objName, level); \
175 } \
176 } \
177 }; \
178 const __##classname##_Init classname##_Init;
179
180} // namespace REST_InitTools
181
182enum Quantities { ENERGY, LENGTH, TIME };
184 public:
185 ValueWithQuantity(double value, Quantities quantity) : fValue(value), fQuantity(quantity){};
186 double GetValue() const { return fValue; }
187 std::string ToString() const;
188
189 inline operator std::string() const { return ToString(); }
190
191 private:
192 const double fValue;
193 const Quantities fQuantity;
194};
195
196inline std::string ToEnergyString(double value) { return (std::string)ValueWithQuantity(value, ENERGY); }
197inline std::string ToTimeString(double value) { return (std::string)ValueWithQuantity(value, TIME); }
198inline std::string ToLengthString(double value) { return (std::string)ValueWithQuantity(value, LENGTH); }
199
200std::string ToTimeStringLong(double value);
201
202inline std::string VectorToString(const TVector3& v) {
203 return TString::Format("(%0.3f, %0.3f, %0.3f)", v.X(), v.Y(), v.Z()).Data();
204}
205
206inline std::string VectorToString(const TVector2& v) {
207 return TString::Format("(%0.3f, %0.3f)", v.X(), v.Y()).Data();
208}
209
210#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.
Definition: TRestTools.cxx:989
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:759
static Int_t isValidFile(const std::string &path)
Returns true if the file with path filename exists.
Definition: TRestTools.cxx:665
static std::string GetFileNameExtension(const std::string &fullname)
Gets the file extension as the substring found after the latest ".".
Definition: TRestTools.cxx:769
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:850
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:523
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:902
static std::string GetPureFileName(const std::string &fullPathFileName)
Removes all directories in the full path filename description given in the argument.
Definition: TRestTools.cxx:809
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:251
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:313
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:780
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:368
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:440
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:641
static bool isRunFile(const std::string &filename)
It checks if the file has been processed using a REST event processing chain.
Definition: TRestTools.cxx:684
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:183
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
Definition: TRestTools.cxx:674
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:403
static int ConvertVersionCode(std::string in)
Convert version to a unique string.
Definition: TRestTools.cxx:966
static bool isPathWritable(const std::string &path)
Returns true if the path given by argument is writable.
Definition: TRestTools.cxx:724
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:470
static bool isAbsolutePath(const std::string &path)
Check if the path is absolute path or not.
Definition: TRestTools.cxx:741
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:212
static std::vector< std::string > GetFilesMatchingPattern(std::string pattern)
Returns a list of files whose name match the pattern string. Key word is "*". e.g....
Definition: TRestTools.cxx:918
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:301
static std::string DownloadRemoteFile(const std::string &remoteFile)
download the remote file automatically, returns the downloaded file name.
static std::string RemoveMultipleSlash(std::string)
Returns the input string but without multiple slashes ("/")
Definition: TRestTools.cxx:790
static bool isDataSet(const std::string &filename)
It checks if the file contains a dataset object.
Definition: TRestTools.cxx:700
static bool isURL(const std::string &filename)
Returns true if filename is an http address.
Definition: TRestTools.cxx:716
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:679
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:875
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:343
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:492
static std::string ToAbsoluteName(const std::string &filename)
It takes a path and returns its absolute path.
Definition: TRestTools.cxx:814