23#ifndef RestCore_TRestTools
24#define RestCore_TRestTools
36#define UNUSED(x) (void)x
39#define EXTERN_DEF __declspec(dllimport)
40#define EXTERN_IMP __declspec(dllexport)
42#define EXTERN_DEF extern
50const std::string PARAMETER_NOT_FOUND_STR =
"NO_SUCH_PARA";
51const double PARAMETER_NOT_FOUND_DBL = -99999999;
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;
63enum class TRestRootFileMode { Read, Recreate, Update };
75 std::unique_ptr<TFile> fFile;
88 explicit operator bool()
const noexcept {
return fFile !=
nullptr; }
90 TFile*
Get() const noexcept {
return fFile.get(); }
91 TFile* operator->() const noexcept {
return fFile.get(); }
92 TFile& operator*() const noexcept {
return *fFile; }
93 const std::string& Error() const noexcept {
return fError; }
97 bool Close() noexcept;
108 static std::vector<std::string>
GetOptions(std::string optionsStr);
112 static int ReadASCIITable(std::string fName, std::vector<std::vector<Double_t>>& data,
113 Int_t skipLines = 0, std::string separator =
"\t");
114 static int ReadASCIITable(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0,
115 std::string separator =
"\t");
116 static int ReadASCIITable(std::string fName, std::vector<std::vector<std::string>>& data,
117 Int_t skipLines = 0, std::string separator =
"\t");
119 static int ReadCSVFile(std::string fName, std::vector<std::vector<Double_t>>& data, Int_t skipLines = 0);
120 static int ReadCSVFile(std::string fName, std::vector<std::vector<Float_t>>& data, Int_t skipLines = 0);
122 template <
typename T>
125 template <
typename T>
126 static int ReadBinaryTable(std::string fName, std::vector<std::vector<T>>& data, Int_t columns = -1);
133 bool removeDuplicates =
true);
134 static std::set<std::string>
GetMatchingStrings(
const std::vector<std::string>& stack,
135 const std::vector<std::string>& wantedStrings);
139 template <
typename T>
142 template <
typename T>
145 template <
typename T>
148 template <
typename T>
151 template <
typename T>
152 static std::vector<T>
GetColumnFromTable(
const std::vector<std::vector<T>>& data,
unsigned int column);
154 template <
typename T>
155 static int PrintTable(std::vector<std::vector<T>> data, Int_t start = 0, Int_t end = 0);
157 template <
typename T>
158 static int ExportASCIITable(std::string fname, std::vector<std::vector<T>>& data);
160 template <
typename T>
161 static int ExportBinaryTable(std::string fname, std::vector<std::vector<T>>& data);
164 static bool fileExists(
const std::string& filename);
165 static bool isRootFile(
const std::string& filename);
166 static bool isRunFile(
const std::string& filename);
167 static bool isDataSet(
const std::string& filename);
168 static bool isURL(
const std::string& filename);
181 const std::vector<std::string>& inputFiles,
182 const std::string& existingTarget =
"",
183 bool removeInputsOnSuccess =
true,
184 std::string* error =
nullptr);
186#ifdef REST_TESTING_ENABLED
189 static void ForceNextRootUpdatePreflightFailureForTesting();
193 static void ForceNextTransactionalMergeValidationFailureForTesting();
200 static std::vector<std::string>
GetSubdirectories(
const std::string& path,
int recursion = -1);
201 static std::pair<std::string, std::string>
SeparatePathAndName(
const std::string& fullname);
202 static std::string
GetPureFileName(
const std::string& fullPathFileName);
203 static std::string
SearchFileInPath(std::vector<std::string> path, std::string filename);
207 static std::istream&
GetLine(std::istream& is, std::string& t);
209 static std::string
Execute(std::string cmd);
211 static std::string
DownloadRemoteFile(
const std::string& remoteFile,
bool pidPrefix =
false);
213 static int UploadToServer(std::string localFile, std::string remoteFile, std::string methodUrl =
"");
215 static std::string
POSTRequest(
const std::string& url,
const std::map<std::string, std::string>& keys);
216 static void ChangeDirectory(
const std::string& toDirectory);
221namespace REST_InitTools {
231inline bool CanOverwrite(T* name,
int level) {
239inline void SetInitLevel(T* name,
int level) {
240 GlobalVarInit<T>::level = level;
247#define MakeGlobal(classname, objName, level) \
248 struct __##classname##_Init { \
249 __##classname##_Init() { \
250 REST_ARGS[#objName] = #classname; \
251 if ((objName) != nullptr) { \
252 if (REST_InitTools::CanOverwrite(objName, level)) { \
254 (objName) = new classname(); \
255 REST_InitTools::SetInitLevel(objName, level); \
258 (objName) = new classname(); \
259 REST_InitTools::SetInitLevel(objName, level); \
263 const __##classname##_Init classname##_Init;
267enum Quantities { ENERGY, LENGTH, TIME };
270 ValueWithQuantity(
double value, Quantities quantity) : fValue(value), fQuantity(quantity){};
271 double GetValue()
const {
return fValue; }
272 std::string ToString()
const;
274 inline operator std::string()
const {
return ToString(); }
278 const Quantities fQuantity;
281inline std::string ToEnergyString(
double value) {
return (std::string)
ValueWithQuantity(value, ENERGY); }
282inline std::string ToTimeString(
double value) {
return (std::string)
ValueWithQuantity(value, TIME); }
283inline std::string ToLengthString(
double value) {
return (std::string)
ValueWithQuantity(value, LENGTH); }
285std::string ToTimeStringLong(
double value);
287inline std::string VectorToString(
const TVector3& v) {
288 return TString::Format(
"(%0.3f, %0.3f, %0.3f)", v.X(), v.Y(), v.Z()).Data();
291inline std::string VectorToString(
const TVector2& v) {
292 return TString::Format(
"(%0.3f, %0.3f)", v.X(), v.Y()).Data();
TFile * Get() const noexcept
Borrow the file without transferring ownership; do not delete or outlive the handle.
static TRestRootFileHandle Open(const std::string &filename, TRestRootFileMode mode)
static bool PrepareBorrowedUpdate(TFile &file, std::string *error=nullptr)