Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rfile001_basics.C
Go to the documentation of this file.
1/// \file
2/// \ingroup ROOT7 tutorial_io
3/// Demonstrate the basic usage of RFile.
4///
5/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
6/// \date 2025-11-06
7/// \macro_code
8/// \macro_output
9/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
10/// is welcome!
11
12void write_hist_to_rfile(const char *fileName)
13{
14 // Create a histogram to write to the file
15 TH1D hist("hist", "hist", 10, 0, 100);
16 hist.FillRandom("gaus", 1000);
17
18 // Create a new ROOT file for writing
19 auto file = ROOT::Experimental::RFile::Recreate(fileName);
20
21 // Put objects into the file (in this case we write the same object multiple times
22 // under different paths). Note that the ownership of `hist` is untouched by `file->Put`.
23 file->Put(hist.GetName(), hist);
24 file->Put(std::string("a/") + hist.GetName(), hist);
25 file->Put(std::string("a/b/") + hist.GetName(), hist);
26
27 // When `file` goes out of scope it will write itself to disk.
28 // To manually write the file to disk without closing it, one can use `file->Flush()`.
29}
30
31void read_hist_from_rfile(const char *fileName)
32{
33 // Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read).
34 auto file = ROOT::Experimental::RFile::Open(fileName);
35 // Iterate all keys of all objects in the file (this excludes directories by default - see the documentation of
36 // ListKeys() for all the options).
37 for (auto key : file->ListKeys()) {
38 // Retrieve the objects from the file. `file->Get` will return a `std::unique_ptr` to the object, or `nullptr`
39 // if the object isn't there.
40 // Once an object is retrieved, it is fully owned by the application, so it survives even if `file` is closed.
41 auto hist = file->Get<TH1D>(key.GetPath());
42 if (!hist)
43 continue;
44 std::cout << key.GetClassName() << " at " << key.GetPath() << ';' << key.GetCycle() << ":\n";
45 std::cout << " entries: " << hist->GetEntries() << "\n";
46 }
47}
48
49void rfile001_basics()
50{
51 const char *const fileName = "rfile_basics.root";
52
53 write_hist_to_rfile(fileName);
54 read_hist_from_rfile(fileName);
55
56 gSystem->Unlink(fileName);
57}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
static std::unique_ptr< RFile > Recreate(std::string_view path)
Opens the file for reading/writing, overwriting it if it already exists.
Definition RFile.cxx:224
static std::unique_ptr< RFile > Open(std::string_view path)
Opens the file for reading.
Definition RFile.cxx:204
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:926
virtual Double_t GetEntries() const
Return the current number of entries.
Definition TH1.cxx:4411
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1392