BmnRoot
Loading...
Searching...
No Matches
BmnFunctionSet.cxx
Go to the documentation of this file.
1#include "BmnFunctionSet.h"
2
3#include "FairLogger.h"
4#include "FairRootManager.h"
5#include "TFile.h"
6#include "TSystem.h"
7
8#include <iomanip>
9#include <iostream>
10using namespace std;
11using namespace std::chrono;
12using namespace std::chrono_literals;
13
14// check whether file exists: 1 - exists, 0 - not exists, -1 - exists but cannot access with the mode (default:
15// kFileExists)
16int BmnFunctionSet::CheckFileExist(TString& fileName, int iVerbose, EAccessMode mode)
17{
18 gSystem->ExpandPathName(fileName);
19 if (gSystem->AccessPathName(fileName.Data(), kFileExists) == true) {
20 if (iVerbose)
21 cout << "ERROR: no specified file: " << fileName << endl;
22 return 0;
23 }
24 if ((mode != kFileExists) && (gSystem->AccessPathName(fileName.Data(), mode) == true)) {
25 if (iVerbose)
26 cout << "ERROR: no required permissions to access the file: " << fileName << endl;
27 return -1;
28 }
29
30 return 1;
31}
32
33// check whether directory exists: 1 - exists, 0 - not exists, -1 - exists but cannot access with the mode (default:
34// kFileExists)
35int BmnFunctionSet::CheckDirectoryExist(TString& fileName, int iVerbose, EAccessMode mode)
36{
37 gSystem->ExpandPathName(fileName);
38 TString dirName(gSystem->DirName(fileName.Data()));
39 if (gSystem->AccessPathName(dirName.Data(), kFileExists) == true) {
40 if (iVerbose)
41 cout << "ERROR: no specified directory: " << dirName << endl;
42 return 0;
43 }
44 if ((mode != kFileExists) && (gSystem->AccessPathName(dirName.Data(), mode) == true)) {
45 if (iVerbose)
46 cout << "ERROR: no required permissions to access the directory: " << dirName << endl;
47 return -1;
48 }
49
50 return 1;
51}
52
53// create directory tree for the file name if not exists: 1 - exists, 0 - created
54// error codes: -1 - exists but cannot access with the mode (default: kWritePermission), -2 - cannot be created, -3 -
55// empty file name
56int BmnFunctionSet::CreateDirectoryTree(TString& fileName, int iVerbose, EAccessMode mode)
57{
58 if (fileName == "") {
59 if (iVerbose)
60 cout << "ERROR: the specified file name is empty" << endl;
61 return -3;
62 }
63
64 int status = CheckDirectoryExist(fileName, 0, mode);
65 if (status < 0) {
66 if (iVerbose)
67 cout << "ERROR: the directory of the specified file (\"" << fileName
68 << "\") does not provide required permissions" << endl;
69 return status;
70 }
71 if (status == 0) {
72 TString dirName(gSystem->DirName(fileName.Data()));
73 int status_parent = CreateDirectoryTree(dirName, 0, mode);
74 if (status_parent < 0)
75 return status_parent;
76 if (gSystem->MakeDirectory(dirName) != 0) {
77 if (iVerbose)
78 cout << "ERROR: the directory of the specified file (\"" << fileName << "\") could not be created"
79 << endl;
80 return -2;
81 }
82 return 0;
83 }
84
85 return 1;
86}
87
89{
90 TFile* fRootFile = nullptr;
91 if (fileName == "") {
92 FairRootManager* ioman = FairRootManager::Instance();
93 if (ioman == nullptr) {
94 LOG(error) << "isSimulationFile(): FairRootManager is not instantiated";
95 return -1;
96 }
97 TTree* cur_tree = ioman->GetInTree();
98 if (cur_tree == nullptr) {
99 LOG(error) << "isSimulationFile(): There is no current tree opened";
100 return -2;
101 }
102
103 fRootFile = cur_tree->GetCurrentFile();
104 } else {
105 gSystem->ExpandPathName(fileName);
106 if (gSystem->AccessPathName(fileName.Data()) == true) {
107 LOG(error) << "isSimulationFile(): no specified file: " << fileName;
108 return -3;
109 }
110
111 fRootFile = TFile::Open(fileName.Data());
112 if (!fRootFile || fRootFile->IsZombie()) {
113 LOG(error) << "isSimulationFile(): opening the input file failed: " << fileName;
114 if (fRootFile) {
115 fRootFile->Close();
116 delete fRootFile;
117 }
118 return -4;
119 }
120 }
121
122 TObject* branch_list = fRootFile->Get("BranchList");
123 if (branch_list != nullptr) {
124 TObject* mctrack_find = branch_list->FindObject("MCTrack");
125 if (mctrack_find != nullptr) {
126 if (fileName != "") {
127 fRootFile->Close();
128 delete fRootFile;
129 }
130
131 return 1;
132 }
133 }
134
135 if (fileName != "") {
136 fRootFile->Close();
137 delete fRootFile;
138 }
139 return 0;
140}
141
142// check whether path is a directory
144{
145 FileStat_t file_stat;
146 gSystem->GetPathInfo(path.Data(), file_stat);
147 if (R_ISDIR(file_stat.fMode))
148 return true;
149 else
150 return false;
151}
152
154{
155 Int_t nans = duration_cast<nanoseconds>(p.time_since_epoch()).count() % 1000000000;
156 Long64_t sec = duration_cast<nanoseconds>(p.time_since_epoch()).count() / 1000000000;
157 return TTimeStamp{time_t(sec), nans};
158}
159
161{
162 Int_t nans = t.GetNanoSec();
163 Int_t sec = t.GetSec();
164 return SysPoint{sec * 1s + nans * 1ns};
165}
166
168{
169 LOG(info) << TimePoint2String(p);
170}
171
173{
174 time_t tt = SysClock::to_time_t(p);
175 auto nans = duration_cast<nanoseconds>(p.time_since_epoch()).count() % 1000000000;
176 std::stringstream sst;
177 sst << std::put_time(std::gmtime(&tt), "%a, %d %b %Y %T %z (%Z)"); // as TTimeStamp
178 // sst << std::put_time(std::localtime(&tt), "%a, %d %b %Y %T %z (%Z)"); // as TTimeStamp
179 sst << " +" << std::setw(9) << nans;
180 return sst.str();
181}
182
183SysPoint BmnFunctionSet::TDatime2GMT(TDatime& tdatime, Int_t timezoneOffsetHours)
184{
185 std::tm tm = {};
186 tm.tm_year = tdatime.GetYear() - 1900;
187 tm.tm_mon = tdatime.GetMonth() - 1;
188 tm.tm_mday = tdatime.GetDay();
189 tm.tm_hour = tdatime.GetHour();
190 tm.tm_min = tdatime.GetMinute();
191 tm.tm_sec = tdatime.GetSecond();
192 time_t t = std::time(nullptr);
193 std::tm* local = std::localtime(&t);
194 int offset = local->tm_gmtoff;
195 return (SysClock::from_time_t(std::mktime(&tm) + offset) - 1h * timezoneOffsetHours);
196}
static void PrintTimePoint(SysPoint p)
static int CreateDirectoryTree(TString &fileName, int iVerbose=0, EAccessMode mode=kWritePermission)
static SysPoint TDatime2GMT(TDatime &tdatime, Int_t timezoneOffsetHours=0)
static int CheckDirectoryExist(TString &fileName, int iVerbose=0, EAccessMode mode=kFileExists)
static std::string TimePoint2String(SysPoint p)
static int isSimulationFile(TString fileName="")
static SysPoint TimeStamp2TP(TTimeStamp p)
static bool isDirectory(TString path)
static TTimeStamp TimePoint2TS(SysPoint p)
static int CheckFileExist(TString &fileName, int iVerbose=0, EAccessMode mode=kFileExists)
std::chrono::time_point< SysClock > SysPoint
STL namespace.