Skip to content

Commit 6b8fdfc

Browse files
committed
[rfile] add basic rfile tutorials
1 parent af0e375 commit 6b8fdfc

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/// \file tutorials/io/experimental/rfile001_basics.py
2+
/// \ingroup Base ROOT7 tutorial_io
3+
/// \author Giacomo Parolini <[email protected]>
4+
/// \date 2025-11-06
5+
/// \macro_code
6+
/// \macro_output
7+
/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
8+
/// is welcome!
9+
10+
void write_hist_to_rfile(const char *fileName)
11+
{
12+
// Create a histogram to write to the file
13+
TH1D hist("hist", "hist", 10, 0, 100);
14+
hist.FillRandom("gaus", 1000);
15+
16+
// Create a new ROOT file for writing
17+
auto file = ROOT::Experimental::RFile::Recreate(fileName);
18+
19+
// Put objects into the file (in this case we write the same object multiple times
20+
// under different paths). Note that the ownership of `hist` is untouched by `file->Put`.
21+
file->Put(hist.GetName(), hist);
22+
file->Put(std::string("a/") + hist.GetName(), hist);
23+
file->Put(std::string("a/b/") + hist.GetName(), hist);
24+
25+
// When `file` goes out of scope it will write itself to disk.
26+
// To manually write the file to disk without closing it, one can use `file->Flush()`.
27+
}
28+
29+
void read_hist_from_rfile(const char *fileName)
30+
{
31+
// Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read).
32+
auto file = ROOT::Experimental::RFile::Open(fileName);
33+
// Iterate all keys of all objects in the file (this excludes directories by default - see the documentation of
34+
// ListKeys() for all the options).
35+
for (auto key : file->ListKeys()) {
36+
// Retrieve the objects from the file. `file->Get` will return a `std::unique_ptr` to the object, or `nullptr`
37+
// if the object isn't there.
38+
// Once an object is retrieved, it is fully owned by the application, so it survives even if `file` is closed.
39+
auto hist = file->Get<TH1D>(key.GetPath());
40+
if (!hist)
41+
continue;
42+
std::cout << key.GetClassName() << " at " << key.GetPath() << ';' << key.GetCycle() << ":\n";
43+
std::cout << " entries: " << hist->GetEntries() << "\n";
44+
}
45+
}
46+
47+
void rfile001_basics()
48+
{
49+
const char *const fileName = "rfile_basics.root";
50+
51+
write_hist_to_rfile(fileName);
52+
read_hist_from_rfile(fileName);
53+
54+
std::filesystem::remove(fileName);
55+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# \file tutorials/io/experimental/rfile001_basics.py
2+
# \ingroup Base ROOT7 tutorial_io
3+
# \author Giacomo Parolini <[email protected]>
4+
# \date 2025-11-06
5+
# \macro_code
6+
# \macro_output
7+
# \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
8+
# is welcome!
9+
10+
import ROOT
11+
12+
import os
13+
14+
def write_hist_to_rfile(fileName):
15+
# Create a histogram to write to the file
16+
hist = ROOT.TH1D("hist", "hist", 10, 0, 100)
17+
hist.FillRandom("gaus", 1000)
18+
19+
# Create a new ROOT file for writing
20+
with ROOT.Experimental.RFile.Recreate(fileName) as file:
21+
# Put objects into the file (in this case we write the same object multiple times
22+
# under different paths). Note that the ownership of `hist` is untouched by `file.Put`.
23+
file.Put(hist.GetName(), hist)
24+
file.Put(f"a/{hist.GetName()}", hist)
25+
file.Put(f"a/b/{hist.GetName()}", hist)
26+
27+
# When the `with` statement is exited the file will write itself to disk and close itself.
28+
# To manually write the file without closing it, one can use `file.Flush()`.
29+
30+
31+
def read_hist_from_rfile(fileName):
32+
# Open an existing ROOT file for reading (will raise an exception if `fileName` cannot be read).
33+
with ROOT.Experimental.RFile.Open(fileName) as file:
34+
# Iterate all keys of all objects in the file (this excludes directories by default - see the documentation of
35+
# ListKeys() for all the options).
36+
for key in file.ListKeys():
37+
# Retrieve the objects from the file. `file.Get` will return an object of the proper type or None if
38+
# the object isn't there.
39+
# Once an object is retrieved, it is fully owned by the application, so it survives even if `file` is closed.
40+
hist = file.Get(key.GetPath())
41+
if not hist:
42+
continue
43+
print(f"{key.GetClassName()} at {key.GetPath()};{key.GetCycle()}:")
44+
print(f" entries: {hist.GetEntries()}")
45+
46+
47+
fileName = "rfile_basics_py.root"
48+
try:
49+
write_hist_to_rfile(fileName)
50+
read_hist_from_rfile(fileName)
51+
os.remove(fileName)
52+
except FileNotFoundError:
53+
pass

0 commit comments

Comments
 (0)