GRSISort "v4.1.1.0"
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#include <algorithm>
8
9#include "TObjArray.h"
10#include "TObjString.h"
11#include "TPRegexp.h"
12#include "TString.h"
13
14bool FileExists(const char* filename)
15{
16 /// This checks if the path exist, and if it is a file and not a directory!
17 struct stat buffer{};
18 int state = stat(filename, &buffer);
19 // state != 0 means we couldn't get file attributes. This doesn't necessary mean the file
20 // does not exist, we might just be missing permission to access it. But for our purposes
21 // this is the same as the file not existing.
22 if(state != 0) { return false; }
23 // we got the file attributes, so it exsist, we just need to check if it is a directory.
24 return !S_ISDIR(buffer.st_mode);
25}
26
27bool DirectoryExists(const char* dirname)
28{
29 /// This checks if the directory exists
30 struct stat buffer{};
31 int state = stat(dirname, &buffer);
32 // state != 0 means we couldn't get file attributes. This doesn't necessary mean the file
33 // does not exist, we might just be missing permission to access it. But for our purposes
34 // this is the same as the file not existing.
35 if(state != 0) { return false; }
36 // we got the file attributes, so it exsist, we just need to check if it is a directory.
37 return S_ISDIR(buffer.st_mode);
38}
39
40bool AllFilesExist(const std::vector<std::string>& filenames)
41{
42 return std::all_of(filenames.begin(), filenames.end(), [](auto filename) { return FileExists(filename.c_str()); });
43}
44
45void trim(std::string& line, const std::string& trimChars)
46{
47 /// Removes the string "trimCars" from the string 'line'
48 if(line.length() == 0) {
49 return;
50 }
51 std::size_t found = line.find_first_not_of(trimChars);
52 if(found != std::string::npos) {
53 line = line.substr(found, line.length());
54 }
55 found = line.find_last_not_of(trimChars);
56 if(found != std::string::npos) {
57 line = line.substr(0, found + 1);
58 }
59}
60
61void trimWS(std::string& line)
62{
63 /// Removes whitespace from the string 'line'
64 line.erase(line.begin(), std::find_if(line.begin(), line.end(), [](int ch) { return std::isspace(ch) == 0; }));
65 line.erase(std::find_if(line.rbegin(), line.rend(), [](int ch) { return std::isspace(ch) == 0; }).base(), line.end());
66}
67
68int GetRunNumber(const std::string& fileName)
69{
70 if(fileName.length() == 0) {
71 return 0;
72 }
73 std::size_t found = fileName.rfind(".root");
74 if(found == std::string::npos) {
75 return 0;
76 }
77 std::size_t found2 = fileName.rfind('-');
78
79 if(found2 == std::string::npos) {
80 found2 = fileName.rfind('_');
81 }
82 std::string temp;
83 if(found2 == std::string::npos || fileName.compare(found2 + 4, 5, ".root") != 0) {
84 temp = fileName.substr(found - 5, 5);
85 } else {
86 temp = fileName.substr(found - 9, 5);
87 }
88 return atoi(temp.c_str());
89}
90
91int GetSubRunNumber(const std::string& fileName)
92{
93 if(fileName.length() == 0) {
94 return -1;
95 }
96
97 std::size_t found = fileName.rfind('-');
98 if(found != std::string::npos) {
99 std::string temp = fileName.substr(found + 1, 3);
100 return atoi(temp.c_str());
101 }
102 found = fileName.rfind('_');
103 if(found != std::string::npos) {
104 std::string temp = fileName.substr(found + 1, 3);
105 return atoi(temp.c_str());
106 }
107 return -1;
108}
109
110const char* GetColorFromNumber(int number)
111{
112 switch(number) {
113 case(0): return "B";
114 case(1): return "G";
115 case(2): return "R";
116 case(3): return "W";
117 };
118 return "X";
119}
120
121const char* GetLongColorFromNumber(int number)
122{
123 switch(number) {
124 case(0): return "Blue";
125 case(1): return "Green";
126 case(2): return "Red";
127 case(3): return "White";
128 };
129 return "Unknown";
130}
const char * GetColorFromNumber(int number)
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)
const char * GetLongColorFromNumber(int number)