Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rfile002_rntuple.C
Go to the documentation of this file.
1/// \file
2/// \ingroup ROOT7 tutorial_io
3/// Demonstrate how to read/write an RNTuple with RFile
4///
5/// \macro_code
6/// \macro_output
7///
8/// \date 2026-10-12
9/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
10
11void write_rntuple_to_rfile(const char *fileName)
12{
13 // Create a new ROOT file for writing
14 auto file = ROOT::RFile::Recreate(fileName);
15
16 // Define the RNTuple schema
18 auto pInt = model->MakeField<int>("intField");
19
20 // Create an RNTupleWriter to append to the RFile
21 auto writer = ROOT::RNTupleWriter::Append(std::move(model), "ntuple", *file);
22
23 // Fill the RNTuple with data
24 for (int i = 0; i < 10; ++i) {
25 *pInt = i;
26 writer->Fill();
27 }
28
29 // `writer` will write the data to file upon destruction. Then `file` will write itself to disk in turn.
30}
31
32void read_rntuple_from_rfile(const char *fileName)
33{
34 // Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read).
35 auto file = ROOT::RFile::Open(fileName);
36
37 // Get back the RNTuple anchor from the file
38 auto rntuple = file->Get<ROOT::RNTuple>("ntuple");
39 R__ASSERT(rntuple); // we just wrote the RNTuple to the file so we expect to find it
40
41 // Create an RNTupleReader from the anchor
42 auto reader = ROOT::RNTupleReader::Open(*rntuple);
43 auto pInt = reader->GetModel().GetDefaultEntry().GetPtr<int>("intField");
44
45 for (auto i : reader->GetEntryRange()) {
46 reader->LoadEntry(i);
47 std::cout << "value[" << i << "] = " << *pInt << "\n";
48 }
49}
50
51void rfile002_rntuple()
52{
53 const char *const fileName = "rfile_rntuple.root";
54
55 write_rntuple_to_rfile(fileName);
56 read_rntuple_from_rfile(fileName);
57
58 gSystem->Unlink(fileName);
59}
#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.
static std::unique_ptr< RNTupleWriter > Append(std::unique_ptr< ROOT::RNTupleModel > model, std::string_view ntupleName, TDirectory &fileOrDirectory, const ROOT::RNTupleWriteOptions &options=ROOT::RNTupleWriteOptions())
Creates an RNTupleWriter that writes into an existing TFile or TDirectory, without overwriting its co...
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