Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rfile003_ttree.C File Reference

Detailed Description

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.

#include <iostream>
#include <TSystem.h>
#include <TTree.h>
#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");
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);
}
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:139
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
static std::unique_ptr< RFile > Open(std::string_view path, const ROpenOptions &opts=ROpenOptions())
Opens the file for reading.
Definition RFile.cxx:204
RResultPtr< RInterface< RLoopManager > > Snapshot(std::string_view treename, std::string_view filename, const ColumnNames_t &columnList, const RSnapshotOptions &options=RSnapshotOptions())
RInterface< Proxied > Define(std::string_view name, F expression, const ColumnNames_t &columns={})
Define a new column.
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1403
A TTree represents a columnar dataset.
Definition TTree.h:95
value[0] = 0
value[1] = 1
value[2] = 2
value[3] = 3
value[4] = 4
value[5] = 5
value[6] = 6
value[7] = 7
value[8] = 8
value[9] = 9
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 giaco.nosp@m.mo.p.nosp@m.aroli.nosp@m.ni@c.nosp@m.ern.c.nosp@m.h

Definition in file rfile003_ttree.C.