Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_rfile.pyzdoc
Go to the documentation of this file.
1\pythondoc RFile
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
5for metadata.
6
7Here is a typical use of RFile:
8
9\code{.py}
10# Reading an object from a file. Note that Get will return None if no object is stored under the given path.
11with ROOT.RFile.Open("myfile.root") as file:
12 myHisto = file.Get("myHisto")
13 # file will be closed upon exiting the with statement, but all objects retrieved from it remain valid.
14
15print(f"{myHisto.GetName()} has {myHisto.GetNbins()} bins.")
16
17# Writing an object to a new file
18with ROOT.RFile.Recreate("myfile2.root") as file:
19 file.Put("myHisto", myHisto)
20 # file will be written upon exiting the with statement, or explicitly with:
21 # file.Flush()
22\endcode
23
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:
26
27\code{.py}
28file.Overwrite("myHisto", myOtherHisto)
29\endcode
30
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.
33\code{.py}
34# Overwrite object without preserving it as a backup cycle
35file.Overwrite("myHisto", myOtherHisto, "my new histo", createNewCycle = False)
36\endcode
37
38You can iterate the metadata of all objects inside an RFile using the ListKeys method:
39\code{.py}
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())
45 histos.append(histo)
46\endcode
47
48ListKeys is recursive by default. If you want non-recursive behavior, you can specify it with a keyword argument:
49\code{.py}
50for key in file.ListKeys("", recursive=False):
51 # this will only print the top-level objects.
52 print(key.GetBaseName())
53\endcode
54
55See the documentation of ListKeys for more options.
56
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:
61
62\code{.py}
63# lists all objects under the "myDir" directory.
64for key in file.ListKeys("myDir"):
65 # will print something like "myDir/myObjName"
66 print(key.GetPath())
67\endcode
68
69## RNTuple
70RNTuple may be read from and written to RFile via the appropriate RNTupleReader/RNTupleWriter methods. See the
71[RFile tutorials](https://root.cern/doc/latest-stable/group__tutorial__io.html) for examples.
72For most use cases, however, we recommend the use of RDataFrame to deal with RNTuple (and TTree) data.
73
74## TTree
75TTree may be read from RFile but not written through it. To write TTree, keep using TFile (or, better if possible,
76RDataFrame).
77
78\endpythondoc
#define f(i)
Definition RSha256.hxx:104
#define a(i)
Definition RSha256.hxx:99
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.
Definition RFile.hxx:254
static std::unique_ptr< RFile > Open(std::string_view path, const ROpenOptions &opts=ROpenOptions())
Opens the file for reading.
Definition RFile.cxx:204
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.
Definition RFile.cxx:226
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
A TTree represents a columnar dataset.
Definition TTree.h:95
for(Int_t i=0;i< n;i++)
Definition legend1.C:18