GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TGRSIUtilities.cxx
Go to the documentation of this file.
1#include "TGRSIUtilities.h"
2
3#include <cstdlib>
4#include <sys/stat.h>
5#include <iostream>
6#include <fstream>
7
8#include "TObjArray.h"
9#include "TObjString.h"
10#include "TPRegexp.h"
11#include "TString.h"
12
13bool FileExists(const char* filename)
14{
15 /// This checks if the path exist, and if it is a file and not a directory!
16 struct stat buffer{};
17 int state = stat(filename, &buffer);
18 // state != 0 means we couldn't get file attributes. This doesn't necessary mean the file
19 // does not exist, we might just be missing permission to access it. But for our purposes
20 // this is the same as the file not existing.
21 if(state != 0) { return false; }
22 // we got the file attributes, so it exsist, we just need to check if it is a directory.
23 return !S_ISDIR(buffer.st_mode);
24}
25
26bool DirectoryExists(const char* dirname)
27{
28 /// This checks if the directory exists
29 struct stat buffer{};
30 int state = stat(dirname, &buffer);
31 // state != 0 means we couldn't get file attributes. This doesn't necessary mean the file
32 // does not exist, we might just be missing permission to access it. But for our purposes
33 // this is the same as the file not existing.
34 if(state != 0) { return false; }
35 // we got the file attributes, so it exsist, we just need to check if it is a directory.
36 return S_ISDIR(buffer.st_mode);
37}
38
39bool AllFilesExist(const std::vector<std::string>& filenames)
40{
41 return std::all_of(filenames.begin(), filenames.end(), [](auto filename) { return FileExists(filename.c_str()); });
42}
43
44void trim(std::string& line, const std::string& trimChars)
45{
46 /// Removes the string "trimCars" from the string 'line'
47 if(line.length() == 0) {
48 return;
49 }
50 std::size_t found = line.find_first_not_of(trimChars);
51 if(found != std::string::npos) {
52 line = line.substr(found, line.length());
53 }
54 found = line.find_last_not_of(trimChars);
55 if(found != std::string::npos) {
56 line = line.substr(0, found + 1);
57 }
58}
59
60void trimWS(std::string& line)
61{
62 /// Removes whitespace from the string 'line'
63 line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](int ch) { return std::isspace(ch) == 0; }));
64 line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) { return std::isspace(ch) == 0; }).base(), line.end());
65}
66
67int GetRunNumber(const std::string& fileName)
68{
69 if(fileName.length() == 0) {
70 return 0;
71 }
72 std::size_t found = fileName.rfind(".root");
73 if(found == std::string::npos) {
74 return 0;
75 }
76 std::size_t found2 = fileName.rfind('-');
77
78 if(found2 == std::string::npos) {
79 found2 = fileName.rfind('_');
80 }
81 std::string temp;
82 if(found2 == std::string::npos || fileName.compare(found2 + 4, 5, ".root") != 0) {
83 temp = fileName.substr(found - 5, 5);
84 } else {
85 temp = fileName.substr(found - 9, 5);
86 }
87 return atoi(temp.c_str());
88}
89
90int GetSubRunNumber(const std::string& fileName)
91{
92 if(fileName.length() == 0) {
93 return -1;
94 }
95
96 std::size_t found = fileName.rfind('-');
97 if(found != std::string::npos) {
98 std::string temp = fileName.substr(found + 1, 3);
99 return atoi(temp.c_str());
100 }
101 found = fileName.rfind('_');
102 if(found != std::string::npos) {
103 std::string temp = fileName.substr(found + 1, 3);
104 return atoi(temp.c_str());
105 }
106 return -1;
107}
int GetRunNumber(const std::string &fileName)
void trimWS(std::string &line)
bool FileExists(const char *filename)
int GetSubRunNumber(const std::string &fileName)
bool DirectoryExists(const char *dirname)
bool AllFilesExist(const std::vector< std::string > &filenames)
void trim(std::string &line, const std::string &trimChars)