Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rfile001_basics.py File Reference

Detailed Description

Demonstrate the basic usage of RFile.

import ROOT
import os
def write_hist_to_rfile(fileName):
# Create a histogram to write to the file
hist = ROOT.TH1D("hist", "hist", 10, 0, 100)
hist.FillRandom("gaus", 1000)
# Create a new ROOT file for writing
with ROOT.RFile.Recreate(fileName) as file:
# Put objects into the file (in this case we write the same object multiple times
# under different paths). Note that the ownership of `hist` is untouched by `file.Put`.
file.Put(hist.GetName(), hist)
file.Put(f"a/{hist.GetName()}", hist)
file.Put(f"a/b/{hist.GetName()}", hist)
# When the `with` statement is exited the file will write itself to disk and close itself.
# To manually write the file without closing it, one can use `file.Flush()`.
def read_hist_from_rfile(fileName):
# Open an existing ROOT file for reading (will raise an exception if `fileName` cannot be read).
with ROOT.RFile.Open(fileName) as file:
# Iterate all keys of all objects in the file (this excludes directories by default - see the documentation of
# ListKeys() for all the options).
for key in file.ListKeys():
# Retrieve the objects from the file. `file.Get` will return an object of the proper type or None if
# the object isn't there.
# Once an object is retrieved, it is fully owned by the application, so it survives even if `file` is closed.
hist = file.Get(key.GetPath())
if hist is not None:
continue
print(f"{key.GetClassName()} at {key.GetPath()};{key.GetCycle()}:")
print(f" entries: {hist.GetEntries()}")
fileName = "rfile_basics_py.root"
try:
write_hist_to_rfile(fileName)
read_hist_from_rfile(fileName)
os.remove(fileName)
except FileNotFoundError:
pass
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
Date
2025-11-06
Author
Giacomo Parolini giaco.nosp@m.mo.p.nosp@m.aroli.nosp@m.ni@c.nosp@m.ern.c.nosp@m.h

Definition in file rfile001_basics.py.