Analysis-Utilities 26.9.9
C++/ROOT utilities for nuclear measurement data analysis
Loading...
Searching...
No Matches
IO Namespace Reference

Path-aware ROOT file helpers with optional thread-safe opening. More...

Classes

class  ScopedRootLock
 RAII guard engaging the same lock used by the open helpers. More...

Functions

void SetRootFilesBaseDir (const TString &dir)
 Set the base directory that relative subpaths resolve against.
TString GetRootFilesBaseDir ()
 Current base directory for relative subpaths, without trailing slash.
void SetThreadSafe (Bool_t enabled=kTRUE)
 Enable ROOT thread safety and serialise file opening.
Bool_t IsThreadSafe ()
 Whether thread-safe file opening is currently engaged.
TFile * OpenForReading (const TString &subpath)
 Open a ROOT file for reading, resolved against the base directory.
TFile * OpenForWriting (const TString &subpath, const TString mode="RECREATE")
 Open a ROOT file for writing, creating parent directories first.

Detailed Description

Path-aware ROOT file helpers with optional thread-safe opening.

Every path passed to this namespace is interpreted relative to a single configurable base directory, so a project can pin its ROOT I/O to an absolute root once and have every downstream call land in the same tree regardless of the current working directory. Absolute paths bypass the base entirely.

All filesystem work goes through gSystem rather than std::filesystem.

Function Documentation

◆ GetRootFilesBaseDir()

TString IO::GetRootFilesBaseDir ( )

Current base directory for relative subpaths, without trailing slash.

Returns
The directory last set by SetRootFilesBaseDir(), or "root_files".

Definition at line 37 of file IOUtils.cpp.

Referenced by InitUtils::ConvertCoMPASSBinToROOT(), InitUtils::ConvertSOLBinToROOT(), InitUtils::ConvertWavedumpBinToROOT(), WaveformProcessingUtils::PrintAllStatistics(), WaveformProcessingUtils::ProcessFile(), and InitUtils::SetROOTPreferences().

◆ IsThreadSafe()

Bool_t IO::IsThreadSafe ( )

Whether thread-safe file opening is currently engaged.

Returns
kTRUE if SetThreadSafe(kTRUE) was called.

Definition at line 46 of file IOUtils.cpp.

◆ OpenForReading()

TFile * IO::OpenForReading ( const TString & subpath)

Open a ROOT file for reading, resolved against the base directory.

Parameters
subpathPath to the file. Joined onto the base directory unless it is absolute, in which case it is used as-is.
Returns
A newly allocated TFile opened in "READ" mode. The caller owns this pointer and is responsible for Close() and delete. Never null, but may be a zombie — check IsZombie() before use, as ROOT reports a missing or corrupt file that way rather than by returning null.
Note
In thread-safe mode the open is serialised and gDirectory is preserved across the call via TDirectory::TContext.

Definition at line 58 of file IOUtils.cpp.

◆ OpenForWriting()

TFile * IO::OpenForWriting ( const TString & subpath,
const TString mode = "RECREATE" )

Open a ROOT file for writing, creating parent directories first.

Parent directories of the resolved path are created recursively before the file is opened, so a caller need not pre-create the output tree.

Parameters
subpathPath to the file. Joined onto the base directory unless it is absolute, in which case it is used as-is.
modeROOT open mode, passed through to TFile. "RECREATE" by default; "UPDATE" and "NEW" behave as ROOT documents them.
Returns
A newly allocated TFile. The caller owns this pointer and is responsible for Close() and delete. Check IsZombie() before use.
Note
The returned file is configured for ZSTD compression at level 5. This is set before any branches are created so that the setting propagates to the baskets of every TTree written into it.
Warning
Unlike OpenForReading(), this does not preserve gDirectory. Opening a file for writing makes it the current ROOT directory, so histograms created afterwards will be owned by it unless TH1::AddDirectory(kFALSE) is in effect (which InitUtils::SetROOTPreferences() sets when enable_mt is on).

Definition at line 68 of file IOUtils.cpp.

Referenced by InitUtils::ConvertCoMPASSBinToROOT(), InitUtils::ConvertSOLBinToROOT(), InitUtils::ConvertWavedumpBinToROOT(), and WaveformProcessingUtils::ProcessFile().

◆ SetRootFilesBaseDir()

void IO::SetRootFilesBaseDir ( const TString & dir)

Set the base directory that relative subpaths resolve against.

Parameters
dirBase directory. May be relative or absolute; pass an absolute path so output is anchored to a project root regardless of the current working directory. Trailing slashes are stripped.
Note
The default is the CWD-relative "root_files". InitUtils::SetROOTPreferences() calls this for you.

Definition at line 30 of file IOUtils.cpp.

Referenced by InitUtils::SetROOTPreferences().

◆ SetThreadSafe()

void IO::SetThreadSafe ( Bool_t enabled = kTRUE)

Enable ROOT thread safety and serialise file opening.

When enabled this calls ROOT::EnableThreadSafety() and routes every OpenForReading() / OpenForWriting() call through a shared recursive mutex.

Parameters
enabledkTRUE to enable, kFALSE to disable. Note that disabling only stops this namespace from taking the lock; ROOT's own thread-safety mode cannot be switched back off once enabled.
Note
Enabled by InitUtils::SetROOTPreferences() (via its enable_mt argument) and by the parallel file processing in WaveformProcessingUtils.

Definition at line 39 of file IOUtils.cpp.

Referenced by WaveformProcessingUtils::ProcessFilesParallel(), and InitUtils::SetROOTPreferences().