Analysis-Utilities
26.9.9
C++/ROOT utilities for nuclear measurement data analysis
Toggle main menu visibility
Loading...
Searching...
No Matches
IOUtils.cpp
Go to the documentation of this file.
1
#include "
IOUtils.hpp
"
2
#include <TDirectory.h>
3
#include <TROOT.h>
4
#include <mutex>
5
6
namespace
{
7
TString g_root_files_base =
"root_files"
;
8
Bool_t g_thread_safe = kFALSE;
9
std::recursive_mutex g_file_open_mutex;
10
11
TString JoinPath(
const
TString &subpath) {
12
if
(gSystem->IsAbsoluteFileName(subpath))
13
return
subpath;
14
char
*tmp = gSystem->ConcatFileName(g_root_files_base.Data(), subpath.Data());
15
TString full(tmp);
16
delete
[] tmp;
17
return
full;
18
}
19
20
void
ApplyCompressionDefaults(TFile *file) {
21
// ZSTD compression for every TTree/TFile produced by this package; set on the
22
// file before branches are created so the setting propagates to the baskets.
23
if
(file && !file->IsZombie()) {
24
file->SetCompressionAlgorithm(ROOT::RCompressionSetting::EAlgorithm::kZSTD);
25
file->SetCompressionLevel(5);
26
}
27
}
28
}
// namespace
29
30
void
IO::SetRootFilesBaseDir
(
const
TString &dir) {
31
TString d = dir;
32
while
(d.Length() > 0 && d[d.Length() - 1] ==
'/'
)
33
d.Chop();
34
g_root_files_base = d;
35
}
36
37
TString
IO::GetRootFilesBaseDir
() {
return
g_root_files_base; }
38
39
void
IO::SetThreadSafe
(Bool_t enabled) {
40
g_thread_safe = enabled;
41
if
(enabled) {
42
ROOT::EnableThreadSafety();
43
}
44
}
45
46
Bool_t
IO::IsThreadSafe
() {
return
g_thread_safe; }
47
48
IO::ScopedRootLock::ScopedRootLock
() : engaged_(g_thread_safe) {
49
if
(engaged_)
50
g_file_open_mutex.lock();
51
}
52
53
IO::ScopedRootLock::~ScopedRootLock
() {
54
if
(engaged_)
55
g_file_open_mutex.unlock();
56
}
57
58
TFile *
IO::OpenForReading
(
const
TString &subpath) {
59
TString full = JoinPath(subpath);
60
if
(g_thread_safe) {
61
std::lock_guard<std::recursive_mutex> lock(g_file_open_mutex);
62
TDirectory::TContext ctxGuard;
63
return
new
TFile(full,
"READ"
);
64
}
65
return
new
TFile(full,
"READ"
);
66
}
67
68
TFile *
IO::OpenForWriting
(
const
TString &subpath,
const
TString mode) {
69
TString full = JoinPath(subpath);
70
TString parent = gSystem->DirName(full);
71
gSystem->mkdir(parent, kTRUE);
72
if
(g_thread_safe) {
73
std::lock_guard<std::recursive_mutex> lock(g_file_open_mutex);
74
TFile *file =
new
TFile(full, mode);
75
ApplyCompressionDefaults(file);
76
return
file;
77
}
78
TFile *file =
new
TFile(full, mode);
79
ApplyCompressionDefaults(file);
80
return
file;
81
}
IOUtils.hpp
IO::ScopedRootLock::ScopedRootLock
ScopedRootLock()
Acquire the shared file-open lock if thread-safe mode is on.
Definition
IOUtils.cpp:48
IO::ScopedRootLock::~ScopedRootLock
~ScopedRootLock()
Release the lock if this guard acquired one.
Definition
IOUtils.cpp:53
IO::GetRootFilesBaseDir
TString GetRootFilesBaseDir()
Current base directory for relative subpaths, without trailing slash.
Definition
IOUtils.cpp:37
IO::SetRootFilesBaseDir
void SetRootFilesBaseDir(const TString &dir)
Set the base directory that relative subpaths resolve against.
Definition
IOUtils.cpp:30
IO::IsThreadSafe
Bool_t IsThreadSafe()
Whether thread-safe file opening is currently engaged.
Definition
IOUtils.cpp:46
IO::SetThreadSafe
void SetThreadSafe(Bool_t enabled=kTRUE)
Enable ROOT thread safety and serialise file opening.
Definition
IOUtils.cpp:39
IO::OpenForWriting
TFile * OpenForWriting(const TString &subpath, const TString mode="RECREATE")
Open a ROOT file for writing, creating parent directories first.
Definition
IOUtils.cpp:68
IO::OpenForReading
TFile * OpenForReading(const TString &subpath)
Open a ROOT file for reading, resolved against the base directory.
Definition
IOUtils.cpp:58
src
IOUtils.cpp
Generated by
1.17.0