Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
_rfile.pyzdoc
Go to the documentation of this file.
1\pythondoc ROOT::RFile
2
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
24Getting an object can also be done via the dictionary syntax:
25
26\code{.py}
27myHist = file['myHisto']
28\endcode
29
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`.
32
33There are two main ways to test for the presence of objects in the file:
34
35\code{.py}
36histoIsThere = 'myHisto' in file
37# or
38histoIsThere = file.GetKeyInfo('myHisto') is None
39\endcode
40
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:
43
44\code{.py}
45file.Overwrite("myHisto", myOtherHisto)
46\endcode
47
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.
50\code{.py}
51# Overwrite object without preserving it as a backup cycle
52file.Overwrite("myHisto", myOtherHisto, "my new histo", createNewCycle = False)
53\endcode
54
55You can iterate the metadata of all objects inside an RFile using the ListKeys method:
56\code{.py}
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())
62 histos.append(histo)
63\endcode
64
65ListKeys is recursive by default. If you want non-recursive behavior, you can specify it with a keyword argument:
66\code{.py}
67for key in file.ListKeys("", recursive=False):
68 # this will only print the top-level objects.
69 print(key.GetBaseName())
70\endcode
71
72See the documentation of ListKeys for more options.
73
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:
78
79\code{.py}
80# lists all objects under the "myDir" directory.
81for key in file.ListKeys("myDir"):
82 # will print something like "myDir/myObjName"
83 print(key.GetPath())
84\endcode
85
86## RNTuple
87RNTuple may be read from and written to RFile via the appropriate RNTupleReader/RNTupleWriter methods. See the
88[RFile tutorials](https://root.cern/doc/latest-stable/group__tutorial__io.html) for examples.
89For most use cases, however, we recommend the use of RDataFrame to deal with RNTuple (and TTree) data.
90
91## TTree
92TTree may be read from RFile but not written through it. To write TTree, keep using TFile (or, better if possible,
93RDataFrame).
94
95\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:205
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:227
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
int main()
Definition main.cpp:18