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/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
9/// is welcome!
10///
11/// \date 2026-10-12
12/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
13
14void write_rntuple_to_rfile(const char *fileName)
15{
16 // Create a new ROOT file for writing
17 auto file = ROOT::Experimental::RFile::Recreate(fileName);
18
19 // Define the RNTuple schema
21 auto pInt = model->MakeField<int>("intField");
22
23 // Create an RNTupleWriter to append to the RFile
24 auto writer = ROOT::Experimental::RNTupleWriter_Append(std::move(model), "ntuple", *file);
25
26 // Fill the RNTuple with data
27 for (int i = 0; i < 10; ++i) {
28 *pInt = i;
29 writer->Fill();
30 }
31
32 // `writer` will write the data to file upon destruction. Then `file` will write itself to disk in turn.
33}
34
35void read_rntuple_from_rfile(const char *fileName)
36{
37 // Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read).
38 auto file = ROOT::Experimental::RFile::Open(fileName);
39
40 // Get back the RNTuple anchor from the file
41 auto rntuple = file->Get<ROOT::RNTuple>("ntuple");
42 R__ASSERT(rntuple); // we just wrote the RNTuple to the file so we expect to find it
43
44 // Create an RNTupleReader from the anchor
45 auto reader = ROOT::RNTupleReader::Open(*rntuple);
46 auto pInt = reader->GetModel().GetDefaultEntry().GetPtr<int>("intField");
47
48 for (auto i : reader->GetEntryRange()) {
49 reader->LoadEntry(i);
50 std::cout << "value[" << i << "] = " << *pInt << "\n";
51 }
52}
53
54void rfile002_rntuple()
55{
56 const char *const fileName = "rfile_rntuple.root";
57
58 write_rntuple_to_rfile(fileName);
59 read_rntuple_from_rfile(fileName);
60
61 gSystem->Unlink(fileName);
62}
#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.