Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ROOT::Experimental::RFile Class Referencefinal

An interface to read from, or write to, a ROOT file, as well as performing other common operations.

When and why should you use RFile

RFile is a modern and minimalistic interface to ROOT files, both local and remote, that can be used instead of TFile when the following conditions are met:

  • you want a simple interface that makes it easy to do things right and hard to do things wrong;
  • you only need basic Put/Get operations and don't need the more advanced TFile/TDirectory functionalities;
  • you want more robustness and better error reporting for those operations;
  • you want clearer ownership semantics expressed through the type system rather than having objects "automagically" handled for you via implicit ownership of raw pointers.

RFile doesn't try to cover the entirety of use cases covered by TFile/TDirectory/TDirectoryFile and is not a 1:1 replacement for them. It is meant to simplify the most common use cases and make them easier to handle by minimizing the amount of ROOT-specific quirks and conforming to more standard C++ practices.

Ownership model

RFile handles ownership via smart pointers, typically std::unique_ptr.

When getting an object from the file (via RFile::Get) you get back a unique copy of the object. Calling Get on the same object twice produces two independent clones of the object. The ownership over that object is solely on the caller and not shared with the RFile. Therefore, the object will remain valid after closing or destroying the RFile that generated it. This also means that any modification done to the object are not reflected to the file automatically: to update the object in the file you need to write it again (via RFile::Overwrite).

RFile::Put and RFile::Overwrite are the way to write objects to the file. Both methods take a const reference to the object to write and don't change the ownership of the object in any way. Calling Put or Overwrite doesn't guarantee that the object is immediately written to the underlying storage: to ensure that, you need to call RFile::Flush (or close the file).

Directories

Differently from TFile, the RFile class itself is not also a "directory". In fact, there is no RDirectory class at all.

Directories are still an existing concept in RFile (since they are a concept in the ROOT binary format), but they are usually interacted with indirectly, via the use of filesystem-like string-based paths. If you Put an object in an RFile under the path "path/to/object", "object" will be stored under directory "to" which is in turn stored under directory "path". This hierarchy is encoded in the ROOT file itself and it can provide some optimization and/or conveniencies when querying objects.

For the most part, it is convenient to think about RFile in terms of a key-value storage where string-based paths are used to refer to arbitrary objects. However, given the hierarchical nature of ROOT files, certain filesystem-like properties are applied to paths, for ease of use: the '/' character is treated specially as the directory separator; multiple '/' in a row are collapsed into one (since RFile doesn't allow directories with empty names).

At the moment, RFile doesn't allow getting directories via Get, nor writing ones via Put (this may change in the future).

Sample usage

Opening an RFile (for writing) and writing an object to it:

auto rfile = ROOT::RFile::Recreate("my_file.root");
auto myObj = TH1D("h", "h", 10, 0, 1);
rfile->Put(myObj.GetName(), myObj);
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:927

Opening an RFile (for reading) and reading an object from it:

auto rfile = ROOT::RFile::Open("my_file.root");
auto myObj = file->Get<TH1D>("h");

Definition at line 98 of file RFile.hxx.

Public Member Functions

 ~RFile ()
 
void Close ()
 Flushes the RFile if needed and closes it, disallowing any further reading or writing.
 
size_t Flush ()
 Writes all objects and the file structure to disk.
 
template<typename T >
std::unique_ptr< T > Get (std::string_view path) const
 Retrieves an object from the file.
 
template<typename T >
void Overwrite (std::string_view path, const T &obj, bool backupPrevious=true)
 Puts an object into the file, overwriting any previously-existing object at that path.
 
template<typename T >
void Put (std::string_view path, const T &obj)
 Puts an object into the file.
 

Static Public Member Functions

static std::unique_ptr< RFileOpen (std::string_view path)
 Opens the file for reading.
 
static std::unique_ptr< RFileRecreate (std::string_view path)
 Opens the file for reading/writing, overwriting it if it already exists.
 
static std::unique_ptr< RFileUpdate (std::string_view path)
 Opens the file for updating, creating a new one if it doesn't exist.
 

Static Public Attributes

static constexpr int kMaxPathNesting = 1000
 

Private Types

enum  PutFlags { kPutAllowOverwrite = 0x1 , kPutOverwriteKeepCycle = 0x2 }
 

Private Member Functions

 RFile (std::unique_ptr< TFile > file)
 
TKeyGetTKey (std::string_view path) const
 Given path, returns the TKey corresponding to the object at that path (assuming the path is fully split, i.e.
 
void * GetUntyped (std::string_view path, const std::type_info &type) const
 Gets object path from the file and returns an owning pointer to it.
 
template<typename T >
void PutInternal (std::string_view path, const T &obj, std::uint32_t flags)
 
void PutUntyped (std::string_view path, const std::type_info &type, const void *obj, std::uint32_t flags)
 Writes obj to file, without taking its ownership.
 

Private Attributes

std::unique_ptr< TFilefFile
 

#include <ROOT/RFile.hxx>

Member Enumeration Documentation

◆ PutFlags

Enumerator
kPutAllowOverwrite 
kPutOverwriteKeepCycle 

Definition at line 99 of file RFile.hxx.

Constructor & Destructor Documentation

◆ RFile()

RFile::RFile ( std::unique_ptr< TFile > file)
explicitprivate

Definition at line 183 of file RFile.cxx.

◆ ~RFile()

RFile::~RFile ( )
default

Member Function Documentation

◆ Close()

void RFile::Close ( )

Flushes the RFile if needed and closes it, disallowing any further reading or writing.

Definition at line 377 of file RFile.cxx.

◆ Flush()

size_t RFile::Flush ( )

Writes all objects and the file structure to disk.

Returns the number of bytes written.

Definition at line 372 of file RFile.cxx.

◆ Get()

template<typename T >
std::unique_ptr< T > ROOT::Experimental::RFile::Get ( std::string_view path) const
inline

Retrieves an object from the file.

path should be a string such that IsValidPath(path) == true, otherwise an exception will be thrown. See ValidateAndNormalizePath() for info about valid path names. If the object is not there returns a null pointer.

Definition at line 158 of file RFile.hxx.

◆ GetTKey()

TKey * RFile::GetTKey ( std::string_view path) const
private

Given path, returns the TKey corresponding to the object at that path (assuming the path is fully split, i.e.

"a/b/c" always means "object 'c' inside directory 'b' inside directory 'a'"). IMPORTANT: path must have been validated/normalized via ValidateAndNormalizePath() (see RFile.cxx).

Definition at line 226 of file RFile.cxx.

◆ GetUntyped()

void * RFile::GetUntyped ( std::string_view path,
const std::type_info & type ) const
private

Gets object path from the file and returns an owning pointer to it.

The caller should immediately wrap it into a unique_ptr of the type described by type.

Definition at line 271 of file RFile.cxx.

◆ Open()

std::unique_ptr< RFile > RFile::Open ( std::string_view path)
static

Opens the file for reading.

path may be a regular file path or a remote URL.

Exceptions
ROOT::RExceptionif the file at path could not be opened.

Definition at line 187 of file RFile.cxx.

◆ Overwrite()

template<typename T >
void ROOT::Experimental::RFile::Overwrite ( std::string_view path,
const T & obj,
bool backupPrevious = true )
inline

Puts an object into the file, overwriting any previously-existing object at that path.

The application retains ownership of the object.

If an object already exists at that path, it is kept as a backup cycle unless backupPrevious is false. Note that even if backupPrevious is false, any existing cycle except the latest will be preserved.

Throws a RException if path is already the path of a directory. Throws a RException if the file was opened in read-only mode.

Definition at line 186 of file RFile.hxx.

◆ Put()

template<typename T >
void ROOT::Experimental::RFile::Put ( std::string_view path,
const T & obj )
inline

Puts an object into the file.

The application retains ownership of the object. path should be a string such that IsValidPath(path) == true, otherwise an exception will be thrown. See ValidateAndNormalizePath() for info about valid path names.

Throws a RException if path already identifies a valid object or directory. Throws a RException if the file was opened in read-only mode.

Definition at line 172 of file RFile.hxx.

◆ PutInternal()

template<typename T >
void ROOT::Experimental::RFile::PutInternal ( std::string_view path,
const T & obj,
std::uint32_t flags )
inlineprivate
See also
Put

Definition at line 118 of file RFile.hxx.

◆ PutUntyped()

void RFile::PutUntyped ( std::string_view path,
const std::type_info & type,
const void * obj,
std::uint32_t flags )
private

Writes obj to file, without taking its ownership.

Definition at line 301 of file RFile.cxx.

◆ Recreate()

std::unique_ptr< RFile > RFile::Recreate ( std::string_view path)
static

Opens the file for reading/writing, overwriting it if it already exists.

Exceptions
ROOT::RExceptionif a file could not be created at path (e.g. if the specified directory tree does not exist).

Definition at line 213 of file RFile.cxx.

◆ Update()

std::unique_ptr< RFile > RFile::Update ( std::string_view path)
static

Opens the file for updating, creating a new one if it doesn't exist.

Exceptions
ROOT::RExceptionif the file at path could neither be read nor created (e.g. if the specified directory tree does not exist).

Definition at line 200 of file RFile.cxx.

Member Data Documentation

◆ fFile

std::unique_ptr<TFile> ROOT::Experimental::RFile::fFile
private

Definition at line 104 of file RFile.hxx.

◆ kMaxPathNesting

constexpr int ROOT::Experimental::RFile::kMaxPathNesting = 1000
staticconstexpr

Definition at line 130 of file RFile.hxx.

Libraries for ROOT::Experimental::RFile:

The documentation for this class was generated from the following files: