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

Detailed Description

Demonstrate how to read/write an RNTuple with RFile.

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 pInt = model->MakeField<int>("intField");
// Create an RNTupleWriter to append to the RFile
auto writer = ROOT::Experimental::RNTupleWriter_Append(std::move(model), "ntuple", *file);
// 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);
}
#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
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:226
static std::unique_ptr< RNTupleModel > Create()
static std::unique_ptr< RNTupleReader > Open(std::string_view ntupleName, std::string_view storage, const ROOT::RNTupleReadOptions &options=ROOT::RNTupleReadOptions())
Open an RNTuple for reading.
Representation of an RNTuple data set in a ROOT file.
Definition RNTuple.hxx:67
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1403
std::unique_ptr< RNTupleWriter > RNTupleWriter_Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntuplePath, ROOT::Experimental::RFile &file, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter that writes into the given file, appending to it.
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 rfile002_rntuple.C.