Analysis-Utilities 26.9.9
C++/ROOT utilities for nuclear measurement data analysis
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
6namespace {
7TString g_root_files_base = "root_files";
8Bool_t g_thread_safe = kFALSE;
9std::recursive_mutex g_file_open_mutex;
10
11TString 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
20void 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
30void 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
37TString IO::GetRootFilesBaseDir() { return g_root_files_base; }
38
39void IO::SetThreadSafe(Bool_t enabled) {
40 g_thread_safe = enabled;
41 if (enabled) {
42 ROOT::EnableThreadSafety();
43 }
44}
45
46Bool_t IO::IsThreadSafe() { return g_thread_safe; }
47
48IO::ScopedRootLock::ScopedRootLock() : engaged_(g_thread_safe) {
49 if (engaged_)
50 g_file_open_mutex.lock();
51}
52
54 if (engaged_)
55 g_file_open_mutex.unlock();
56}
57
58TFile *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
68TFile *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}
ScopedRootLock()
Acquire the shared file-open lock if thread-safe mode is on.
Definition IOUtils.cpp:48
~ScopedRootLock()
Release the lock if this guard acquired one.
Definition IOUtils.cpp:53
TString GetRootFilesBaseDir()
Current base directory for relative subpaths, without trailing slash.
Definition IOUtils.cpp:37
void SetRootFilesBaseDir(const TString &dir)
Set the base directory that relative subpaths resolve against.
Definition IOUtils.cpp:30
Bool_t IsThreadSafe()
Whether thread-safe file opening is currently engaged.
Definition IOUtils.cpp:46
void SetThreadSafe(Bool_t enabled=kTRUE)
Enable ROOT thread safety and serialise file opening.
Definition IOUtils.cpp:39
TFile * OpenForWriting(const TString &subpath, const TString mode="RECREATE")
Open a ROOT file for writing, creating parent directories first.
Definition IOUtils.cpp:68
TFile * OpenForReading(const TString &subpath)
Open a ROOT file for reading, resolved against the base directory.
Definition IOUtils.cpp:58