REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
Loading...
Searching...
No Matches
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 <set>
33#include <string>
34#include <vector>
35
36#define UNUSED(x) (void)x
37
38#ifdef WIN32
39#define EXTERN_DEF __declspec(dllimport)
40#define EXTERN_IMP __declspec(dllexport)
41#else
42#define EXTERN_DEF extern
43#define EXTERN_IMP
44#endif
45
46#ifndef WIN32
47#include <unistd.h>
48#endif
49
50const std::string PARAMETER_NOT_FOUND_STR = "NO_SUCH_PARA";
51const double PARAMETER_NOT_FOUND_DBL = -99999999;
52
53EXTERN_DEF std::string REST_COMMIT;
54EXTERN_DEF std::string REST_PATH;
55EXTERN_DEF std::string REST_USER;
56EXTERN_DEF std::string REST_USER_PATH;
57EXTERN_DEF std::string REST_TMP_PATH;
58EXTERN_DEF std::map<std::string, std::string> REST_ARGS;
59
62 public:
63 static std::vector<std::string> GetOptions(std::string optionsStr);
64
65 static void LoadRESTLibrary(bool silent = false);
66
67 static int ReadASCIITable(std::string fName, std::vector<std::vector<Double_t>>& data,
68 Int_t skipLines = 0, std::string separator = "\t");
69 static int ReadASCIITable(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0,
70 std::string separator = "\t");
71 static int ReadASCIITable(std::string fName, std::vector<std::vector<std::string>>& data,
72 Int_t skipLines = 0, std::string separator = "\t");
73
74 static int ReadCSVFile(std::string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines = 0);
75 static int ReadCSVFile(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0);
76
77 template <typename T>
78 static void TransposeTable(std::vector<std::vector<T>>& data);
79
80 template <typename T>
81 static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);
82
83 static Bool_t IsBinaryFile(std::string fname);
84
85 static std::string GetFileNameExtension(const std::string& fullname);
86 static std::string GetFileNameRoot(const std::string& fullname);
87 static std::vector<std::string> GetObservablesInString(const std::string& observablesStr,
88 bool removeDuplicates = true);
89 static std::set<std::string> GetMatchingStrings(const std::vector<std::string>& stack,
90 const std::vector<std::string>& wantedStrings);
91
92 static int GetBinaryFileColumns(std::string fname);
93
94 template <typename T>
95 static T GetMaxValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
96
97 template <typename T>
98 static T GetMinValueFromTable(const std::vector<std::vector<T>>& data, Int_t column = -1);
99
100 template <typename T>
101 static T GetLowestIncreaseFromTable(std::vector<std::vector<T>> data, Int_t column);
102
103 template <typename T>
104 static T GetIntegralFromTable(const std::vector<std::vector<T>>& data);
105
106 template <typename T>
107 static std::vector<T> GetColumnFromTable(const std::vector<std::vector<T>>& data, unsigned int column);
108
109 template <typename T>
110 static int PrintTable(std::vector<std::vector<T>> data, Int_t start = 0, Int_t end = 0);
111
112 template <typename T>
113 static int ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data);
114
115 template <typename T>
116 static int ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data);
117
118 static Int_t isValidFile(const std::string& path);
119 static bool fileExists(const std::string& filename);
120 static bool isRootFile(const std::string& filename);
121 static bool isRunFile(const std::string& filename);
122 static bool isDataSet(const std::string& filename);
123 static bool isURL(const std::string& filename);
124 static bool isPathWritable(const std::string& path);
125 static bool isAbsolutePath(const std::string& path);
126 static std::string RemoveMultipleSlash(std::string);
127 static std::string ToAbsoluteName(const std::string& filename);
128 static std::vector<std::string> GetSubdirectories(const std::string& path, int recursion = -1);
129 static std::pair<std::string, std::string> SeparatePathAndName(const std::string& fullname);
130 static std::string GetPureFileName(const std::string& fullPathFileName);
131 static std::string SearchFileInPath(std::vector<std::string> path, std::string filename);
132 static bool CheckFileIsAccessible(const std::string&);
133 static std::vector<std::string> GetFilesMatchingPattern(std::string pattern, bool unlimited = false);
134 static int ConvertVersionCode(std::string in);
135 static std::istream& GetLine(std::istream& is, std::string& t);
136
137 static std::string Execute(std::string cmd);
138
139 static std::string DownloadRemoteFile(const std::string& remoteFile, bool pidPrefix = false);
140 static int DownloadRemoteFile(std::string remoteFile, std::string localFile);
141 static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl = "");
142
143 static std::string POSTRequest(const std::string& url, const std::map<std::string, std::string>& keys);
144 static void ChangeDirectory(const std::string& toDirectory);
145
146 static std::vector<int> CanvasDivisions(int n);
147};
148
149namespace REST_InitTools {
150
151template <class T>
153 static int level;
154};
155template <class T>
157
158template <class T>
159inline bool CanOverwrite(T* name, int level) {
160 if (level > GlobalVarInit<T>::level) {
161 return true;
162 }
163 return false;
164}
165
166template <class T>
167inline void SetInitLevel(T* name, int level) {
168 GlobalVarInit<T>::level = level;
169}
170
171// Initialize global variable with certain class, overwriting the dummy variable of its base class.
172// For example, we initialize gDataBase as TRestDataBase in Framework
173// library. When we load restP3DB library, this object will be overwritten
174// by a new TRestDataBaseP3DB class object, by calling this macro
175#define MakeGlobal(classname, objName, level) \
176 struct __##classname##_Init { \
177 __##classname##_Init() { \
178 REST_ARGS[#objName] = #classname; \
179 if ((objName) != nullptr) { \
180 if (REST_InitTools::CanOverwrite(objName, level)) { \
181 delete (objName); \
182 (objName) = new classname(); \
183 REST_InitTools::SetInitLevel(objName, level); \
184 } \
185 } else { \
186 (objName) = new classname(); \
187 REST_InitTools::SetInitLevel(objName, level); \
188 } \
189 } \
190 }; \
191 const __##classname##_Init classname##_Init;
192
193} // namespace REST_InitTools
194
195enum Quantities { ENERGY, LENGTH, TIME };
197 public:
198 ValueWithQuantity(double value, Quantities quantity) : fValue(value), fQuantity(quantity){};
199 double GetValue() const { return fValue; }
200 std::string ToString() const;
201
202 inline operator std::string() const { return ToString(); }
203
204 private:
205 const double fValue;
206 const Quantities fQuantity;
207};
208
209inline std::string ToEnergyString(double value) { return (std::string)ValueWithQuantity(value, ENERGY); }
210inline std::string ToTimeString(double value) { return (std::string)ValueWithQuantity(value, TIME); }
211inline std::string ToLengthString(double value) { return (std::string)ValueWithQuantity(value, LENGTH); }
212
213std::string ToTimeStringLong(double value);
214
215inline std::string VectorToString(const TVector3& v) {
216 return TString::Format("(%0.3f, %0.3f, %0.3f)", v.X(), v.Y(), v.Z()).Data();
217}
218
219inline std::string VectorToString(const TVector2& v) {
220 return TString::Format("(%0.3f, %0.3f)", v.X(), v.Y()).Data();
221}
222
223#endif
A generic class with useful static methods.
Definition TRestTools.h:61
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.
static Int_t isValidFile(const std::string &path)
Returns true if the file with path filename exists.
static std::string GetFileNameExtension(const std::string &fullname)
Gets the file extension as the substring found after the latest ".".
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.
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.
static bool CheckFileIsAccessible(const std::string &)
Checks if the config file can be opened (and thus exists). It returns true in case of success,...
static std::string GetPureFileName(const std::string &fullPathFileName)
Removes all directories in the full path filename description given in the argument.
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.
static int GetBinaryFileColumns(std::string fname)
It extracts the number of columns from the filename extension given by argument. The file should cont...
static std::vector< std::string > GetOptions(std::string optionsStr)
Returns all the options in an option string.
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,...
static std::string GetFileNameRoot(const std::string &fullname)
Gets the filename root as the substring found before the latest ".".
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....
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...
static void LoadRESTLibrary(bool silent=false)
Calls gSystem to load REST library.
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.
static bool isRunFile(const std::string &filename)
It checks if the file has been processed using a REST event processing chain.
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,...
static bool fileExists(const std::string &filename)
Returns true if the file (or directory) with path filename exists.
static std::set< std::string > GetMatchingStrings(const std::vector< std::string > &stack, const std::vector< std::string > &wantedStrings)
Returns a set of strings that match the wanted strings from the stack. The wanted strings can contain...
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....
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....
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.
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...
static bool isAbsolutePath(const std::string &path)
Check if the path is absolute path or not.
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....
static Bool_t IsBinaryFile(std::string fname)
It identifies if the filename extension follows the formatting ".Nxyzf", where the number of columns ...
static std::string RemoveMultipleSlash(std::string)
Returns the input string but without multiple slashes ("/")
static bool isDataSet(const std::string &filename)
It checks if the file contains a dataset object.
static bool isURL(const std::string &filename)
Returns true if filename is an http address.
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 std::vector< std::string > GetObservablesInString(const std::string &observablesStr, bool removeDuplicates=true)
Returns a vector with the observables names found in the input string. The observables names must con...
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.
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.
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...
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 ...
static std::string ToAbsoluteName(const std::string &filename)
It takes a path and returns its absolute path.