Repository navigation
[rfile] Add RFile tutorials for RNTuple and TTree #23688
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f0e3465
[ntuple] add pythonization for RNTupleWriter_Append(RFile)
silverweed 476228d
[rfile] move rfile tutorials to experimental/rfile
silverweed 9143aa8
[rfile] Add tutorials with RNTuple and TTree
silverweed b6d1a76
[rfile] ruff/clang format rfile tutorials
silverweed 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
File renamed without changes.
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,62 @@ | ||
| /// \file | ||
| /// \ingroup ROOT7 tutorial_io | ||
| /// Demonstrate how to read/write an RNTuple with RFile | ||
| /// | ||
| /// \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! | ||
| /// | ||
| /// \date 2026-10-12 | ||
| /// \author Giacomo Parolini <giacomo.parolini@cern.ch> | ||
|
|
||
| void write_rntuple_to_rfile(const char *fileName) | ||
| { | ||
| // Create a new ROOT file for writing | ||
| auto file = ROOT::Experimental::RFile::Recreate(fileName); | ||
|
|
||
| // Define the RNTuple schema | ||
| auto model = ROOT::RNTupleModel::Create(); | ||
| auto pInt = model->MakeField<int>("intField"); | ||
|
|
||
| // Create an RNTupleWriter to append to the RFile | ||
| auto writer = ROOT::Experimental::RNTupleWriter_Append(std::move(model), "ntuple", *file); | ||
|
vepadulano marked this conversation as resolved.
|
||
|
|
||
| // Fill the RNTuple with data | ||
| for (int i = 0; i < 10; ++i) { | ||
| *pInt = i; | ||
| writer->Fill(); | ||
| } | ||
|
|
||
| // `writer` will write the data to file upon destruction. Then `file` will write itself to disk in turn. | ||
| } | ||
|
|
||
| void read_rntuple_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); | ||
|
|
||
| // Get back the RNTuple anchor from the file | ||
| auto rntuple = file->Get<ROOT::RNTuple>("ntuple"); | ||
| R__ASSERT(rntuple); // we just wrote the RNTuple to the file so we expect to find it | ||
|
|
||
| // Create an RNTupleReader from the anchor | ||
| auto reader = ROOT::RNTupleReader::Open(*rntuple); | ||
| auto pInt = reader->GetModel().GetDefaultEntry().GetPtr<int>("intField"); | ||
|
|
||
| for (auto i : reader->GetEntryRange()) { | ||
| reader->LoadEntry(i); | ||
| std::cout << "value[" << i << "] = " << *pInt << "\n"; | ||
| } | ||
| } | ||
|
|
||
| void rfile002_rntuple() | ||
| { | ||
| const char *const fileName = "rfile_rntuple.root"; | ||
|
|
||
| write_rntuple_to_rfile(fileName); | ||
| read_rntuple_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,58 @@ | ||
| # \file | ||
| # \ingroup ROOT7 tutorial_io | ||
| # Demonstrate how to read/write an RNTuple with RFile | ||
| # | ||
| # \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! | ||
| # | ||
| # \date 2026-10-12 | ||
| # \author Giacomo Parolini <giacomo.parolini@cern.ch> | ||
|
|
||
| import ROOT | ||
|
|
||
| import os | ||
|
|
||
|
|
||
| def write_rntuple_to_rfile(fileName): | ||
| # Create a new ROOT file for writing | ||
| with ROOT.Experimental.RFile.Recreate(fileName) as file: | ||
| # Define the RNTuple schema | ||
| model = ROOT.RNTupleModel.Create() | ||
| model.MakeField["int"]("intField") | ||
| # Create an RNTupleWriter to append to the RFile | ||
| with ROOT.RNTupleWriter.Append(model, "ntuple", file) as writer: | ||
| entry = writer.CreateEntry() | ||
|
|
||
| # Fill the RNTuple with data | ||
| for i in range(10): | ||
| entry["intField"] = i | ||
| writer.Fill(entry) | ||
|
|
||
| # `writer` will write the data to file upon exiting the with statement.... | ||
| # ...then `file` will write itself to disk in turn when exiting the outer with statement. | ||
|
|
||
|
|
||
| def read_rntuple_from_rfile(fileName): | ||
| # Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read). | ||
| with ROOT.Experimental.RFile.Open(fileName) as file: | ||
| # Get back the RNTuple anchor from the file | ||
| rntuple = file.Get("ntuple") | ||
|
|
||
| # Create an RNTupleReader from the anchor | ||
| with ROOT.RNTupleReader.Open(rntuple) as reader: | ||
| entry = reader.CreateEntry() | ||
| for i in reader.GetEntryRange(): | ||
| reader.LoadEntry(i, entry) | ||
| print(f"value[{i}] = {entry['intField']}") | ||
|
|
||
|
|
||
| fileName = "rfile_rntuple_py.root" | ||
| try: | ||
| write_rntuple_to_rfile(fileName) | ||
| read_rntuple_from_rfile(fileName) | ||
| os.remove(fileName) | ||
| except FileNotFoundError: | ||
| pass | ||
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,50 @@ | ||
| /// \file | ||
| /// \ingroup ROOT7 tutorial_io | ||
| /// Demonstrate how to read a TTree with RFile. | ||
| /// | ||
| /// Note that, due to how TTree handles lifetime and its relationship with TDirectory, writing TTree through RFile | ||
| /// is currently unsupported. | ||
| /// | ||
| /// \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! | ||
| /// | ||
| /// \date 2026-10-12 | ||
| /// \author Giacomo Parolini <giacomo.parolini@cern.ch> | ||
| #include <iostream> | ||
| #include <TSystem.h> | ||
| #include <TTree.h> | ||
| #include <ROOT/RDataFrame.hxx> | ||
| #include <ROOT/RFile.hxx> | ||
|
|
||
| void read_tree_from_rfile(const char *fileName) | ||
| { | ||
| // Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read). | ||
| // IMPORTANT! This file must remain open while reading data from the tree. | ||
| auto file = ROOT::Experimental::RFile::Open(fileName); | ||
|
|
||
| // Get back the TTree from the file | ||
| auto tree = file->Get<TTree>("tree"); | ||
|
silverweed marked this conversation as resolved.
|
||
| R__ASSERT(tree); // we just wrote the tree so we expect to find it in the file | ||
| int intVal; | ||
| tree->SetBranchAddress("intBranch", &intVal); | ||
|
|
||
| for (auto i = 0; i < tree->GetEntries(); ++i) { | ||
| tree->GetEntry(i); | ||
| std::cout << "value[" << i << "] = " << intVal << "\n"; | ||
| } | ||
| } | ||
|
|
||
| void rfile003_ttree() | ||
| { | ||
| const char *const fileName = "rfile_ttree.root"; | ||
|
|
||
| // Note that TTree cannot be written to RFile, so we use RDataFrame to write it instead. | ||
| ROOT::RDataFrame(10).Define("intBranch", "static_cast<int>(rdfentry_)").Snapshot("tree", fileName); | ||
|
|
||
| read_tree_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,45 @@ | ||
| # \file | ||
| # \ingroup ROOT7 tutorial_io | ||
| # Demonstrate how to read a TTree with RFile. | ||
| # | ||
| # Note that, due to how TTree handles lifetime and its relationship with TDirectory, writing TTree through RFile | ||
| # is currently unsupported. | ||
| # | ||
| # \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! | ||
| # | ||
| # \date 2026-10-12 | ||
| # \author Giacomo Parolini <giacomo.parolini@cern.ch> | ||
|
|
||
| import array | ||
| import os | ||
|
|
||
| import ROOT | ||
|
|
||
|
|
||
| def read_ttree_from_rfile(fileName): | ||
| # Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read). | ||
| # IMPORTANT! This file must remain open while reading data from the tree. | ||
| with ROOT.Experimental.RFile.Open(fileName) as file: | ||
| # Get back the TTree from the file | ||
| tree = file.Get("tree") | ||
| floatVal = array.array("f", [0]) | ||
| tree.SetBranchAddress("floatBranch", floatVal) | ||
|
|
||
| for i in range(tree.GetEntries()): | ||
| tree.GetEntry(i) | ||
| print(f"value[{i}] = {floatVal[0]}") | ||
|
|
||
|
|
||
| fileName = "rfile_ttree_py.root" | ||
| try: | ||
| # Note that TTree cannot be written to RFile, so we use RDataFrame to write it instead. | ||
| ROOT.RDataFrame(10).Define("floatBranch", "static_cast<float>(rdfentry_)").Snapshot("tree", fileName) | ||
|
|
||
| read_ttree_from_rfile(fileName) | ||
| os.remove(fileName) | ||
| except FileNotFoundError: | ||
| pass |
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
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.