3RFile is
a modern, minimal
interface to
ROOT files with
a focus
on simplicity.
4It allows to perform the basic actions of getting objects from
a file, putting objects into it and query the file
7Here is
a typical use of
RFile:
10# Reading an object from a file. Note that Get will return None if no object is stored under the given path.
12 myHisto = file.Get(
"myHisto")
13 # file will be closed upon exiting the with statement, but all objects retrieved from it remain valid.
15print(
f"{myHisto.GetName()} has {myHisto.GetNbins()} bins.")
17# Writing an object to a new file
19 file.Put(
"myHisto", myHisto)
20 # file will be written upon exiting the with statement, or explicitly with:
24The Put method will raise an error
if an
object is already present at the given path;
if you want to overwrite an
25existing
object use the Overwrite method instead:
28file.Overwrite(
"myHisto", myOtherHisto)
31When overwriting, you may specify
a title and whether to keep
a backup cycle of
a potentially-overwritten
object.
32Note that Overwrite will work just like Put
if there is no
object to overwrite.
34# Overwrite object without preserving it as a backup cycle
35file.Overwrite(
"myHisto", myOtherHisto,
"my new histo", createNewCycle = False)
38You can iterate the metadata of all objects inside an
RFile using the ListKeys method:
40for key in file.ListKeys():
41 print(key.GetBaseName())
42 # We can use the key to get information about the object without loading it from storage
43 if key.GetClassName() ==
"TH1D":
44 histo = file.Get(key.GetPath())
48ListKeys is recursive by
default. If you want non-recursive behavior, you can specify it with
a keyword argument:
50for key in file.ListKeys(
"", recursive=False):
51 # this will only print the top-level objects.
52 print(key.GetBaseName())
55See the documentation of ListKeys
for more options.
57## Directories and paths
58Objects in an
RFile are organized in
a hierarchical structure represented by their
"path". A path is the
string you
59pass to Get or Put (like in the examples above) and it uses
'/' as the directory separator. A directory is useful to
60group multiple related objects and it can be used alongside ListKeys:
63# lists all objects under the "myDir" directory.
64for key in file.ListKeys(
"myDir"):
65 # will print something like "myDir/myObjName"
70RNTuple may be read from and written to
RFile via the appropriate RNTupleReader/
RNTupleWriter methods. See the
71[
RFile tutorials](https:
72For most use cases, however, we recommend the use of RDataFrame to deal with RNTuple (and
TTree)
data.
75TTree may be read from
RFile but not written through it. To write
TTree, keep
using TFile (or, better
if possible,
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
An interface to read from, or write to, a ROOT file, as well as performing other common operations.
static std::unique_ptr< RFile > Open(std::string_view path, const ROpenOptions &opts=ROpenOptions())
Opens the file for reading.
static std::unique_ptr< RFile > Recreate(std::string_view path, const RRecreateOptions &opts=RRecreateOptions())
Opens the file for reading/writing, overwriting it if it already exists.
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
A TTree represents a columnar dataset.