Skip to content

Commit 12db6b3

Browse files
committed
[skip-ci][rfile] add more documentation to RFile class
1 parent cbbca24 commit 12db6b3

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

io/io/inc/ROOT/RFile.hxx

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,66 @@ ROOT::RLogChannel &RFileLog();
3434
\ingroup RFile
3535
\brief An interface to read from, or write to, a ROOT file, as well as performing other common operations.
3636
37-
TODO: more in-depth explanation
37+
## When and why should you use RFile
38+
39+
RFile is a modern and minimalistic interface to ROOT files, both local and remote, that can be used instead of TFile
40+
when the following conditions are met:
41+
- you want a simple interface that makes it easy to do things right and hard to do things wrong;
42+
- you only need basic Put/Get operations and don't need the more advanced TFile/TDirectory functionalities;
43+
- you want more robustness and better error reporting for those operations;
44+
- you want clearer ownership semantics expressed through the type system rather than having objects "automagically"
45+
handled for you via implicit ownership of raw pointers.
46+
47+
RFile doesn't try to cover the entirety of use cases covered by TFile/TDirectory/TDirectoryFile and is not
48+
a 1:1 replacement for them. It is meant to simplify the most common use cases and make them easier to handle by
49+
minimizing the amount of ROOT-specific quirks and conforming to more standard C++ practices.
50+
51+
## Ownership model
52+
53+
RFile handles ownership via smart pointers, typically std::unique_ptr.
54+
55+
When getting an object from the file (via RFile::Get) you get back a unique copy of the object. Calling `Get` on the
56+
same object twice produces two independent clones of the object. The ownership over that object is solely on the caller
57+
and not shared with the RFile. Therefore, the object will remain valid after closing or destroying the RFile that
58+
generated it. This also means that any modification done to the object are **not** reflected to the file automatically:
59+
to update the object in the file you need to write it again (via RFile::Overwrite).
60+
61+
RFile::Put and RFile::Overwrite are the way to write objects to the file. Both methods take a const reference to the
62+
object to write and don't change the ownership of the object in any way. Calling Put or Overwrite doesn't guarantee that
63+
the object is immediately written to the underlying storage: to ensure that, you need to call RFile::Flush (or close the
64+
file).
65+
66+
## Directories
67+
68+
Differently from TFile, the RFile class itself is not also a "directory". In fact, there is no RDirectory class at all.
69+
70+
Directories are still an existing concept in RFile (since they are a concept in the ROOT binary format),
71+
but they are usually interacted with indirectly, via the use of filesystem-like string-based paths. If you Put an object
72+
in an RFile under the path "path/to/object", "object" will be stored under directory "to" which is in turn stored under
73+
directory "path". This hierarchy is encoded in the ROOT file itself and it can provide some optimization and/or
74+
conveniencies when querying objects.
75+
76+
For the most part, it is convenient to think about RFile in terms of a key-value storage where string-based paths are
77+
used to refer to arbitrary objects. However, given the hierarchical nature of ROOT files, certain filesystem-like
78+
properties are applied to paths, for ease of use: the '/' character is treated specially as the directory separator;
79+
multiple '/' in a row are collapsed into one (since RFile doesn't allow directories with empty names).
80+
81+
At the moment, RFile doesn't allow getting directories via Get, nor writing ones via Put (this may change in the
82+
future).
83+
84+
## Sample usage
85+
Opening an RFile (for writing) and writing an object to it:
86+
~~~{.cpp}
87+
auto rfile = ROOT::RFile::Recreate("my_file.root");
88+
auto myObj = TH1D("h", "h", 10, 0, 1);
89+
rfile->Put(myObj.GetName(), myObj);
90+
~~~
91+
92+
Opening an RFile (for reading) and reading an object from it:
93+
~~~{.cpp}
94+
auto rfile = ROOT::RFile::Open("my_file.root");
95+
auto myObj = file->Get<TH1D>("h");
96+
~~~
3897
*/
3998
class RFile final {
4099
enum PutFlags {

0 commit comments

Comments
 (0)