Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFile.hxx
Go to the documentation of this file.
1/// \file ROOT/RFile.hxx
2/// \ingroup Base ROOT7
3/// \author Giacomo Parolini <giacomo.parolini@cern.ch>
4/// \date 2025-03-19
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. Feedback
6/// is welcome!
7
8#ifndef ROOT7_RFile
9#define ROOT7_RFile
10
11#include <Compression.h>
12#include <ROOT/RError.hxx>
13
14#include <deque>
15#include <functional>
16#include <iostream>
17#include <memory>
18#include <string_view>
19#include <typeinfo>
20#include <variant>
21
22class TFile;
23class TIterator;
24class TKey;
25
26namespace ROOT {
27namespace Experimental {
28
29class RKeyInfo;
30class RFile;
31
32namespace Internal {
33
35
36/// Returns an **owning** pointer to the object referenced by `key`. The caller must delete this pointer.
37/// This method is meant to only be used by the pythonization.
38[[nodiscard]] void *RFile_GetObjectFromKey(RFile &file, const RKeyInfo &key);
39
41
42} // namespace Internal
43
44namespace Detail {
45
46/// Given a "path-like" string (like foo/bar/baz), returns a pair `{ dirName, baseName }`.
47/// `baseName` will be empty if the string ends with '/'.
48/// `dirName` will be empty if the string contains no '/'.
49/// `dirName`, if not empty, always ends with a '/'.
50/// NOTE: this function does no semantic checking or path expansion, nor does it interact with the
51/// filesystem in any way (so it won't follow symlink or anything like that).
52/// Moreover it doesn't trim the path in any way, so any leading or trailing whitespaces will be preserved.
53/// This function does not perform any copy: the returned string_views have the same lifetime as `path`.
54std::pair<std::string_view, std::string_view> DecomposePath(std::string_view path);
55
56}
57
58class RFileKeyIterable;
59
60/**
61\class ROOT::Experimental::RKeyInfo
62\ingroup RFile
63\brief Information about an RFile object's Key.
64
65Every object inside a ROOT file has an associated "Key" which contains metadata on the object, such as its name, type
66etc.
67Querying this information can be done via RFile::ListKeys(). Reading an object's Key
68doesn't deserialize the full object, so it's a relatively lightweight operation.
69*/
73
74public:
75 enum class ECategory : std::uint16_t {
77 kObject,
79 };
80
81private:
82 std::string fPath;
83 std::string fTitle;
84 std::string fClassName;
85 std::uint16_t fCycle = 0;
87 std::uint64_t fLenObj = 0;
88 std::uint64_t fNBytesObj = 0;
89 std::uint64_t fNBytesKey = 0;
90 std::uint64_t fSeekKey = 0;
91 std::uint64_t fSeekParentDir = 0;
92
93 explicit RKeyInfo(const TKey &key);
94
95public:
96 RKeyInfo() = default;
97
98 /// Returns the absolute path of this key, i.e. the directory part plus the object name.
99 const std::string &GetPath() const { return fPath; }
100 /// Returns the base name of this key, i.e. the name of the object without the directory part.
101 std::string GetBaseName() const { return std::string(Detail::DecomposePath(fPath).second); }
102 const std::string &GetTitle() const { return fTitle; }
103 const std::string &GetClassName() const { return fClassName; }
104 std::uint16_t GetCycle() const { return fCycle; }
105 ECategory GetCategory() const { return fCategory; }
106 /// Returns the in-memory size of the uncompressed object.
107
108 std::uint64_t GetLenObj() const { return fLenObj; }
109 /// Returns the on-disk size of the (potentially compressed) object, excluding its key.
110 std::uint64_t GetNBytesObj() const { return fNBytesObj; }
111
112 /// Returns the on-disk size of this object's key.
113 std::uint64_t GetNBytesKey() const { return fNBytesKey; }
114 /// Returns the on-disk offset of this object's key.
115 std::uint64_t GetSeekKey() const { return fSeekKey; }
116
117 /// Returns the on-disk offset of this object's parent directory key.
118 std::uint64_t GetSeekParentDir() const { return fSeekParentDir; }
119};
120
121/// The iterable returned by RFile::ListKeys()
123 using Pattern_t = std::string;
124
125 TFile *fFile = nullptr;
127 std::uint32_t fFlags = 0;
128
129public:
130 class RIterator {
131 friend class RFileKeyIterable;
132
134 // This is ugly, but TList returns an (owning) pointer to a polymorphic TIterator...and we need this class
135 // to be copy-constructible.
136 std::shared_ptr<TIterator> fIter;
137 std::string fDirPath;
138
139 // Outlined to avoid including TIterator.h
140 RIterStackElem(TIterator *it, const std::string &path = "");
141 // Outlined to avoid including TIterator.h
143
144 // fDirPath doesn't need to be compared because it's implied by fIter.
145 bool operator==(const RIterStackElem &other) const { return fIter == other.fIter; }
146 };
147
148 // Using a deque to have pointer stability
149 std::deque<RIterStackElem> fIterStack;
151 const TKey *fCurKey = nullptr;
152 std::uint16_t fRootDirNesting = 0;
153 std::uint32_t fFlags = 0;
154
155 void Advance();
156
157 // NOTE: `iter` here is an owning pointer (or null)
158 RIterator(TIterator *iter, Pattern_t pattern, std::uint32_t flags);
159
160 public:
162 using iterator_category = std::input_iterator_tag;
163 using difference_type = std::ptrdiff_t;
165 using pointer = const value_type *;
166 using reference = const value_type &;
167
169 {
170 Advance();
171 return *this;
172 }
173 value_type operator*();
174 bool operator!=(const iterator &rh) const { return !(*this == rh); }
175 bool operator==(const iterator &rh) const { return fIterStack == rh.fIterStack; }
176 };
177
178 RFileKeyIterable(TFile *file, std::string_view rootDir, std::uint32_t flags)
179 : fFile(file), fPattern(std::string(rootDir)), fFlags(flags)
180 {
181 }
182
183 RIterator begin() const;
184 RIterator end() const;
185};
186
187/**
188\class ROOT::Experimental::RFile
189\ingroup RFile
190\brief An interface to read from, or write to, a ROOT file, as well as performing other common operations.
191
192Please refer to the documentation of TFile for the details related to how data and executable code can be stored
193in ROOT files.
194
195## When and why should you use RFile
196
197RFile is a modern and minimalistic interface to ROOT files, both local and remote, that can be used instead of TFile
198when you only need basic Put/Get operations and don't need the more advanced TFile/TDirectory functionalities.
199It provides:
200- a simple interface that makes it easy to do things right and hard to do things wrong;
201- more robustness and better error reporting for those operations;
202- clearer ownership semantics expressed through the type system.
203
204RFile doesn't cover the entirety of use cases covered by TFile/TDirectory/TDirectoryFile and is not
205a 1:1 replacement for them. It is meant to simplify the most common use cases by following newer standard C++
206practices.
207
208## Ownership model
209
210RFile handles ownership via smart pointers, typically std::unique_ptr.
211
212When getting an object from the file (via RFile::Get) you get back a unique copy of the object. Calling `Get` on the
213same object twice produces two independent clones of the object. The ownership over that object is solely on the caller
214and not shared with the RFile. Therefore, the object will remain valid after closing or destroying the RFile that
215generated it. This also means that any modification done to the object are **not** reflected to the file automatically:
216to update the object in the file you need to write it again (via RFile::Overwrite).
217
218RFile::Put and RFile::Overwrite are the way to write objects to the file. Both methods take a const reference to the
219object to write and don't change the ownership of the object in any way. Calling Put or Overwrite doesn't guarantee that
220the object is immediately written to the underlying storage: to ensure that, you need to call RFile::Flush (or close the
221file).
222
223## Directories
224
225Even though there is no equivalent of TDirectory in the RFile API, directories are still an existing concept in RFile
226(since they are a concept in the ROOT binary format). However they are for now only interacted with indirectly, via the
227use of filesystem-like string-based paths. If you Put an object in an RFile under the path "path/to/object", "object"
228will be stored under directory "to" which is in turn stored under directory "path". This hierarchy is encoded in the
229ROOT file itself and it can provide some optimization and/or conveniences when querying objects.
230
231For the most part, it is convenient to think about RFile in terms of a key-value storage where string-based paths are
232used to refer to arbitrary objects. However, given the hierarchical nature of ROOT files, certain filesystem-like
233properties are applied to paths, for ease of use: the '/' character is treated specially as the directory separator;
234multiple '/' in a row are collapsed into one (since RFile doesn't allow directories with empty names).
235
236At the moment, RFile doesn't allow getting directories via Get, nor writing ones via Put (this may change in the
237future).
238
239## Sample usage
240Opening an RFile (for writing) and writing an object to it:
241~~~{.cpp}
242auto rfile = ROOT::RFile::Recreate("my_file.root");
243auto myObj = TH1D("h", "h", 10, 0, 1);
244rfile->Put(myObj.GetName(), myObj);
245~~~
246
247Opening an RFile (for reading) and reading an object from it:
248~~~{.cpp}
249auto rfile = ROOT::RFile::Open("my_file.root");
250auto myObj = file->Get<TH1D>("h");
251~~~
252*/
254 friend void *Internal::RFile_GetObjectFromKey(RFile &file, const RKeyInfo &key);
256
257 /// Flags used in PutInternal()
258 enum PutFlags {
259 /// When encountering an object at the specified path, overwrite it with the new one instead of erroring out.
261 /// When overwriting an object, preserve the existing one and create a new cycle, rather than removing it.
263 };
264
265 std::unique_ptr<TFile> fFile;
266
267 // Outlined to avoid including TFile.h
268 explicit RFile(std::unique_ptr<TFile> file);
269
270 /// Gets object `path` from the file and returns an **owning** pointer to it.
271 /// The caller should immediately wrap it into a unique_ptr of the type described by `type`.
272 [[nodiscard]] void *GetUntyped(std::string_view path,
273 std::variant<const char *, std::reference_wrapper<const std::type_info>> type) const;
274
275 /// Writes `obj` to file, without taking its ownership.
276 void PutUntyped(std::string_view path, const std::type_info &type, const void *obj, std::uint32_t flags);
277
278 /// \see Put
279 template <typename T>
280 void PutInternal(std::string_view path, const T &obj, std::uint32_t flags)
281 {
282 PutUntyped(path, typeid(T), &obj, flags);
283 }
284
285 /// Given `path`, returns the TKey corresponding to the object at that path (assuming the path is fully split, i.e.
286 /// "a/b/c" always means "object 'c' inside directory 'b' inside directory 'a'").
287 /// IMPORTANT: `path` must have been validated/normalized via ValidateAndNormalizePath() (see RFile.cxx).
288 TKey *GetTKey(std::string_view path) const;
289
290public:
292 kListObjects = 1 << 0,
293 kListDirs = 1 << 1,
295 };
296
298 /// See core/zip/inc/Compression.h for the meaning of the `compression` argument.
299 /// Default compression is 505 (ZSTD level 10).
301
303 };
304
305 // This is arbitrary, but it's useful to avoid pathological cases
306 static constexpr int kMaxPathNesting = 1000;
307
308 ///// Factory methods /////
309
310 /// Opens the file for reading. `path` may be a regular file path or a remote URL.
311 /// \throw ROOT::RException if the file at `path` could not be opened.
312 static std::unique_ptr<RFile> Open(std::string_view path);
313
314 /// Opens the file for reading/writing, overwriting it if it already exists.
315 /// \throw ROOT::RException if a file could not be created at `path` (e.g. if the specified
316 /// directory tree does not exist).
317 static std::unique_ptr<RFile> Recreate(std::string_view path, const RRecreateOptions &opts = RRecreateOptions());
318
319 /// Opens the file for updating, creating a new one if it doesn't exist.
320 /// \throw ROOT::RException if the file at `path` could neither be read nor created
321 /// (e.g. if the specified directory tree does not exist).
322 static std::unique_ptr<RFile> Update(std::string_view path);
323
324 ///// Instance methods /////
325
326 // Outlined to avoid including TFile.h
328
329 /// Retrieves an object from the file.
330 /// `path` should be a string such that `IsValidPath(path) == true`, otherwise an exception will be thrown.
331 /// See \ref ValidateAndNormalizePath() for info about valid path names.
332 /// If the object is not there returns a null pointer.
333 template <typename T>
334 std::unique_ptr<T> Get(std::string_view path) const
335 {
336 void *obj = GetUntyped(path, typeid(T));
337 return std::unique_ptr<T>(static_cast<T *>(obj));
338 }
339
340 /// Puts an object into the file.
341 /// The application retains ownership of the object.
342 /// `path` should be a string such that `IsValidPath(path) == true`, otherwise an exception will be thrown.
343 /// See \ref ValidateAndNormalizePath() for info about valid path names.
344 ///
345 /// Throws a RException if `path` already identifies a valid object or directory.
346 /// Throws a RException if the file was opened in read-only mode.
347 template <typename T>
348 void Put(std::string_view path, const T &obj)
349 {
350 PutInternal(path, obj, /* flags = */ 0);
351 }
352
353 /// Puts an object into the file, overwriting any previously-existing object at that path.
354 /// The application retains ownership of the object.
355 ///
356 /// If an object already exists at that path, it is kept as a backup cycle unless `backupPrevious` is false.
357 /// Note that even if `backupPrevious` is false, any existing cycle except the latest will be preserved.
358 ///
359 /// Throws a RException if `path` is already the path of a directory.
360 /// Throws a RException if the file was opened in read-only mode.
361 template <typename T>
362 void Overwrite(std::string_view path, const T &obj, bool backupPrevious = true)
363 {
364 std::uint32_t flags = kPutAllowOverwrite;
366 PutInternal(path, obj, flags);
367 }
368
369 /// Writes all objects and the file structure to disk.
370 /// Returns the number of bytes written.
371 size_t Flush();
372
373 /// Flushes the RFile if needed and closes it, disallowing any further reading or writing.
374 void Close();
375
376 /// Returns an iterable over all keys of objects and/or directories written into this RFile starting at path
377 /// `basePath` (defaulting to include the content of all subdirectories).
378 /// By default, keys referring to directories are not returned: only those referring to leaf objects are.
379 /// If `basePath` is the path of a leaf object, only `basePath` itself will be returned.
380 /// `flags` is a bitmask specifying the listing mode.
381 /// If `(flags & kListObjects) != 0`, the listing will include keys of non-directory objects (default);
382 /// If `(flags & kListDirs) != 0`, the listing will include keys of directory objects;
383 /// If `(flags & kListRecursive) != 0`, the listing will recurse on all subdirectories of `basePath` (default),
384 /// otherwise it will only list immediate children of `basePath`.
385 ///
386 /// Example usage:
387 /// ~~~{.cpp}
388 /// for (RKeyInfo key : file->ListKeys()) {
389 /// /* iterate over all objects in the RFile */
390 /// cout << key.GetPath() << ";" << key.GetCycle() << " of type " << key.GetClassName() << "\n";
391 /// }
392 /// for (RKeyInfo key : file->ListKeys("", kListDirs|kListObjects|kListRecursive)) {
393 /// /* iterate over all objects and directories in the RFile */
394 /// }
395 /// for (RKeyInfo key : file->ListKeys("a/b", kListObjects)) {
396 /// /* iterate over all objects that are immediate children of directory "a/b" */
397 /// }
398 /// for (RKeyInfo key : file->ListKeys("foo", kListDirs|kListRecursive)) {
399 /// /* iterate over all directories under directory "foo", recursively */
400 /// }
401 /// ~~~
402 RFileKeyIterable ListKeys(std::string_view basePath = "", std::uint32_t flags = kListObjects | kListRecursive) const
403 {
404 return RFileKeyIterable(fFile.get(), basePath, flags);
405 }
406
407 /// Retrieves information about the key of object at `path`, if one exists.
408 std::optional<RKeyInfo> GetKeyInfo(std::string_view path) const;
409
410 /// Prints the internal structure of this RFile to the given stream.
411 void Print(std::ostream &out = std::cout) const;
412};
413
414} // namespace Experimental
415} // namespace ROOT
416
417#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
bool operator==(const iterator &rh) const
Definition RFile.hxx:175
bool operator!=(const iterator &rh) const
Definition RFile.hxx:174
std::deque< RIterStackElem > fIterStack
Definition RFile.hxx:149
RIterator(TIterator *iter, Pattern_t pattern, std::uint32_t flags)
Definition RFile.cxx:408
The iterable returned by RFile::ListKeys()
Definition RFile.hxx:122
RFileKeyIterable(TFile *file, std::string_view rootDir, std::uint32_t flags)
Definition RFile.hxx:178
An interface to read from, or write to, a ROOT file, as well as performing other common operations.
Definition RFile.hxx:253
void Close()
Flushes the RFile if needed and closes it, disallowing any further reading or writing.
Definition RFile.cxx:539
static constexpr int kMaxPathNesting
Definition RFile.hxx:306
std::unique_ptr< TFile > fFile
Definition RFile.hxx:265
size_t Flush()
Writes all objects and the file structure to disk.
Definition RFile.cxx:534
static std::unique_ptr< RFile > Update(std::string_view path)
Opens the file for updating, creating a new one if it doesn't exist.
Definition RFile.cxx:215
void Print(std::ostream &out=std::cout) const
Prints the internal structure of this RFile to the given stream.
Definition RFile.cxx:520
void PutInternal(std::string_view path, const T &obj, std::uint32_t flags)
Definition RFile.hxx:280
TKey * GetTKey(std::string_view path) const
Given path, returns the TKey corresponding to the object at that path (assuming the path is fully spl...
Definition RFile.cxx:240
std::optional< RKeyInfo > GetKeyInfo(std::string_view path) const
Retrieves information about the key of object at path, if one exists.
Definition RFile.cxx:558
static std::unique_ptr< RFile > Open(std::string_view path)
Opens the file for reading.
Definition RFile.cxx:205
std::unique_ptr< T > Get(std::string_view path) const
Retrieves an object from the file.
Definition RFile.hxx:334
void * GetUntyped(std::string_view path, std::variant< const char *, std::reference_wrapper< const std::type_info > > type) const
Gets object path from the file and returns an owning pointer to it.
Definition RFile.cxx:285
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
void Overwrite(std::string_view path, const T &obj, bool backupPrevious=true)
Puts an object into the file, overwriting any previously-existing object at that path.
Definition RFile.hxx:362
void PutUntyped(std::string_view path, const std::type_info &type, const void *obj, std::uint32_t flags)
Writes obj to file, without taking its ownership.
Definition RFile.cxx:330
PutFlags
Flags used in PutInternal()
Definition RFile.hxx:258
@ kPutOverwriteKeepCycle
When overwriting an object, preserve the existing one and create a new cycle, rather than removing it...
Definition RFile.hxx:262
@ kPutAllowOverwrite
When encountering an object at the specified path, overwrite it with the new one instead of erroring ...
Definition RFile.hxx:260
RFileKeyIterable ListKeys(std::string_view basePath="", std::uint32_t flags=kListObjects|kListRecursive) const
Returns an iterable over all keys of objects and/or directories written into this RFile starting at p...
Definition RFile.hxx:402
void Put(std::string_view path, const T &obj)
Puts an object into the file.
Definition RFile.hxx:348
RFile(std::unique_ptr< TFile > file)
Definition RFile.cxx:236
Information about an RFile object's Key.
Definition RFile.hxx:70
const std::string & GetClassName() const
Definition RFile.hxx:103
std::uint64_t GetNBytesKey() const
Returns the on-disk size of this object's key.
Definition RFile.hxx:113
std::uint64_t fNBytesObj
Definition RFile.hxx:88
std::uint64_t fNBytesKey
Definition RFile.hxx:89
const std::string & GetTitle() const
Definition RFile.hxx:102
std::uint64_t fSeekParentDir
Definition RFile.hxx:91
ECategory GetCategory() const
Definition RFile.hxx:105
std::uint64_t GetSeekParentDir() const
Returns the on-disk offset of this object's parent directory key.
Definition RFile.hxx:118
std::string GetBaseName() const
Returns the base name of this key, i.e. the name of the object without the directory part.
Definition RFile.hxx:101
std::uint16_t GetCycle() const
Definition RFile.hxx:104
const std::string & GetPath() const
Returns the absolute path of this key, i.e. the directory part plus the object name.
Definition RFile.hxx:99
std::uint64_t GetSeekKey() const
Returns the on-disk offset of this object's key.
Definition RFile.hxx:115
std::uint64_t GetLenObj() const
Returns the in-memory size of the uncompressed object.
Definition RFile.hxx:108
std::uint64_t GetNBytesObj() const
Returns the on-disk size of the (potentially compressed) object, excluding its key.
Definition RFile.hxx:110
A log configuration for a channel, e.g.
Definition RLogger.hxx:98
A file, usually with extension .root, that stores data and code in the form of serialized objects in ...
Definition TFile.h:130
Iterator abstract base class.
Definition TIterator.h:30
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
std::pair< std::string_view, std::string_view > DecomposePath(std::string_view path)
Given a "path-like" string (like foo/bar/baz), returns a pair { dirName, baseName }.
Definition RFile.cxx:194
ROOT::RLogChannel & RFileLog()
Definition RFile.cxx:25
void * RFile_GetObjectFromKey(RFile &file, const RKeyInfo &key)
Returns an owning pointer to the object referenced by key.
Definition RFile.cxx:568
TFile * GetRFileTFile(RFile &rfile)
Definition RFile.cxx:574
bool operator==(const RIterStackElem &other) const
Definition RFile.hxx:145
RIterStackElem(TIterator *it, const std::string &path="")
Definition RFile.cxx:401
int fCompressionSettings
See core/zip/inc/Compression.h for the meaning of the compression argument.
Definition RFile.hxx:300
@ kUseGeneralPurpose
Use the new recommended general-purpose setting; it is a best trade-off between compression ratio/dec...
Definition Compression.h:58