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/// \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 2025-11-06
12/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
13
14void write_hist_to_rfile(const char *fileName)
15{
16 // Create a histogram to write to the file
17 TH1D hist("hist", "hist", 10, 0, 100);
18 hist.FillRandom("gaus", 1000);
19
20 // Create a new ROOT file for writing
21 auto file = ROOT::Experimental::RFile::Recreate(fileName);
22
23 // Put objects into the file (in this case we write the same object multiple times
24 // under different paths). Note that the ownership of `hist` is untouched by `file->Put`.
25 file->Put(hist.GetName(), hist);
26 file->Put(std::string("a/") + hist.GetName(), hist);
27 file->Put(std::string("a/b/") + hist.GetName(), hist);
28
29 // When `file` goes out of scope it will write itself to disk.
30 // To manually write the file to disk without closing it, one can use `file->Flush()`.
31}
32
33void read_hist_from_rfile(const char *fileName)
34{
35 // Open an existing ROOT file for reading (will throw an exception if `fileName` cannot be read).
36 auto file = ROOT::Experimental::RFile::Open(fileName);
37 // Iterate all keys of all objects in the file (this excludes directories by default - see the documentation of
38 // ListKeys() for all the options).
39 for (auto key : file->ListKeys()) {
40 // Retrieve the objects from the file. `file->Get` will return a `std::unique_ptr` to the object, or `nullptr`
41 // if the object isn't there.
42 // Once an object is retrieved, it is fully owned by the application, so it survives even if `file` is closed.
43 auto hist = file->Get<TH1D>(key.GetPath());
44 if (!hist)
45 continue;
46 std::cout << key.GetClassName() << " at " << key.GetPath() << ';' << key.GetCycle() << ":\n";
47 std::cout << " entries: " << hist->GetEntries() << "\n";
48 }
49}
50
51void rfile001_basics()
52{
53 const char *const fileName = "rfile_basics.root";
54
55 write_hist_to_rfile(fileName);
56 read_hist_from_rfile(fileName);
57
58 gSystem->Unlink(fileName);
59}
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:582
static std::unique_ptr< RFile > Open(std::string_view path)
Opens the file for reading.
Definition RFile.cxx:205
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:225
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:4457
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1392