-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[rfile] add basic rfile tutorials #20321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+109
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /// \file tutorials/io/experimental/rfile001_basics.py | ||
| /// \ingroup Base ROOT7 tutorial_io | ||
| /// \author Giacomo Parolini <[email protected]> | ||
| /// \date 2025-11-06 | ||
| /// \macro_code | ||
| /// \macro_output | ||
| /// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback | ||
| /// is welcome! | ||
|
|
||
| void write_hist_to_rfile(const char *fileName) | ||
| { | ||
| // Create a histogram to write to the file | ||
| TH1D hist("hist", "hist", 10, 0, 100); | ||
| hist.FillRandom("gaus", 1000); | ||
|
|
||
| // Create a new ROOT file for writing | ||
| auto file = ROOT::Experimental::RFile::Recreate(fileName); | ||
|
|
||
| // 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(std::string("a/") + hist.GetName(), hist); | ||
| file->Put(std::string("a/b/") + hist.GetName(), hist); | ||
|
|
||
| // When `file` goes out of scope it will write itself to disk. | ||
| // To manually write the file to disk without closing it, one can use `file->Flush()`. | ||
| } | ||
|
|
||
| void read_hist_from_rfile(const char *fileName) | ||
| { | ||
| // Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read). | ||
| auto file = ROOT::Experimental::RFile::Open(fileName); | ||
| // Iterate all keys of all objects in the file (this excludes directories by default - see the documentation of | ||
| // ListKeys() for all the options). | ||
| for (auto key : file->ListKeys()) { | ||
silverweed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Retrieve the objects from the file. `file->Get` will return a `std::unique_ptr` to the object, or `nullptr` | ||
| // 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. | ||
| auto hist = file->Get<TH1D>(key.GetPath()); | ||
silverweed marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!hist) | ||
| continue; | ||
| std::cout << key.GetClassName() << " at " << key.GetPath() << ';' << key.GetCycle() << ":\n"; | ||
| std::cout << " entries: " << hist->GetEntries() << "\n"; | ||
| } | ||
| } | ||
|
|
||
| void rfile001_basics() | ||
| { | ||
| const char *const fileName = "rfile_basics.root"; | ||
|
|
||
| write_hist_to_rfile(fileName); | ||
| read_hist_from_rfile(fileName); | ||
|
|
||
| gSystem->Unlink(fileName); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # \file tutorials/io/experimental/rfile001_basics.py | ||
| # \ingroup Base ROOT7 tutorial_io | ||
| # \author Giacomo Parolini <[email protected]> | ||
| # \date 2025-11-06 | ||
| # \macro_code | ||
| # \macro_output | ||
| # \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback | ||
| # is welcome! | ||
|
|
||
| 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.Experimental.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.Experimental.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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.