Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,63 @@
r"""
\pythondoc RFile

TODO: document RFile
RFile is a modern, minimal interface to ROOT files with a focus on simplicity.
It allows to perform the basic actions of getting objects from a file, putting objects into it and query the file
for metadata.

Here is a typical use of RFile:

\code{.py}
# Reading an object from a file. Note that Get will return None if no object is stored under the given path.
with ROOT.Experimental.RFile.Open("myfile.root") as file:
myHisto = file.Get("myHisto")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a side note, other Python interface e.g. the dictionary have a get method which raises error by default and optionally returns a default argument which could be None e.g. get("mykey", None).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently the behavior on the C++ side; if we strongly believe we should change it we should change it for both languages, but I'd avoid having it behave differently. We still have a bit of time to decide before moving RFile out of Experimental.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't object keeping things as they are, as long as they're properly documented. If you want to have this discussion with other ROOT team members let me know.

# file will be closed upon exiting the with statement, but all objects retrieved from it remain valid.
Comment thread
silverweed marked this conversation as resolved.

print(f"{myHisto.GetName()} has {myHisto.GetNbins()} bins.")

# Writing an object to a new file
with ROOT.Experimental.RFile.Recreate("myfile2.root") as file:
file.Put("myHisto", myHisto)
# file will be written upon exiting the with statement, or explicitly with:
# file.Flush()
\endcode

The Put method will raise an error if an object is already present at the given path; if you want to overwrite an
existing object use the Overwrite method instead:

\code{.py}
file.Overwrite("myHisto", myOtherHisto)
\endcode

You can iterate the metadata of all objects inside an RFile using the ListKeys method:
\code{.py}
for key in file.ListKeys():
print(key.GetBaseName())
# We can use the key to get information about the object without loading it from storage
if key.GetClassName() == "TH1D":
histo = file.Get(key.GetPath())
histos.append(histo)
\endcode

ListKeys is recursive by default. If you want non-recursive behavior, you can specify it with a keyword argument:
\code{.py}
for key in file.ListKeys("", recursive=False):
# this will only print the top-level objects.
print(key.GetBaseName())
\endcode

See the documentation of ListKeys for more options.

## Directories and paths
Objects in an RFile are organized in a hierarchical structure represented by their "path". A path is the string you
pass to Get or Put (like in the examples above) and it uses '/' as the directory separator. A directory is useful to
group multiple related objects and it can be used alongside ListKeys:

\code{.py}
# TODO code example
# lists all objects under the "myDir" directory.
for key in file.ListKeys("myDir"):
# will print something like "myDir/myObjName"
print(key.GetPath())
\endcode

\endpythondoc
Expand Down