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:
24Getting an
object can also be done via the dictionary syntax:
27myHist = file[
'myHisto']
30The difference between the two methods is that Get will
return `
None`
if the
object is missing,
while the square
31bracket
operator will raise
a `KeyError`.
33There are two
main ways to test
for the presence of objects in the file:
36histoIsThere =
'myHisto' in file
38histoIsThere = file.GetKeyInfo(
'myHisto') is None
41The Put method will raise an error
if an
object is already present at the given path;
if you want to overwrite an
42existing
object use the Overwrite method instead:
45file.Overwrite(
"myHisto", myOtherHisto)
48When overwriting, you may specify
a title and whether to keep
a backup cycle of
a potentially-overwritten
object.
49Note that Overwrite will work just like Put
if there is no
object to overwrite.
51# Overwrite object without preserving it as a backup cycle
52file.Overwrite(
"myHisto", myOtherHisto,
"my new histo", createNewCycle = False)
55You can iterate the metadata of all objects inside an RFile
using the ListKeys method:
57for key in file.ListKeys():
58 print(key.GetBaseName())
59 # We can use the key to get information about the object without loading it from storage
60 if key.GetClassName() ==
"TH1D":
61 histo = file.Get(key.GetPath())
65ListKeys is recursive by
default. If you want non-recursive behavior, you can specify it with
a keyword argument:
67for key in file.ListKeys(
"", recursive=False):
68 # this will only print the top-level objects.
69 print(key.GetBaseName())
72See the documentation of ListKeys
for more options.
74## Directories and paths
75Objects in an RFile are organized in
a hierarchical structure represented by their
"path". A path is the
string you
76pass to Get or Put (like in the examples above) and it uses
'/' as the directory separator. A directory is useful to
77group multiple related objects and it can be used alongside ListKeys:
80# lists all objects under the "myDir" directory.
81for key in file.ListKeys(
"myDir"):
82 # will print something like "myDir/myObjName"
87RNTuple may be read from and written to RFile via the appropriate RNTupleReader/
RNTupleWriter methods. See the
88[RFile tutorials](https:
89For most use cases, however, we recommend the use of RDataFrame to deal with RNTuple (and
TTree)
data.
92TTree 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.