Skip to content

Commit dadb182

Browse files
committed
[rfile] Add possibility of setting an object title when writing
1 parent cbffb23 commit dadb182

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

io/io/inc/ROOT/RFile.hxx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ public:
336336
void *obj = GetUntyped(path, typeid(T));
337337
return std::unique_ptr<T>(static_cast<T *>(obj));
338338
}
339-
340-
/// Puts object `obj` into the file.
339+
340+
/// Puts object `obj` into the file, optionally giving it a title.
341341
/// The object will be effectively copied into the file, so any further modifications won't be seen by the object
342342
/// inside the file.
343343
/// Note that the object is not necessarily written to storage until the RFile is closed or `Flush()` is called.
@@ -348,9 +348,9 @@ public:
348348
/// \throws ROOT::RException if `path` already identifies a valid object or directory.
349349
/// \throws ROOT::RException if the file was opened in read-only mode.
350350
template <typename T>
351-
void Put(std::string_view path, const T &obj)
351+
void Put(std::string_view path, const T &obj, std::string_view title = "")
352352
{
353-
PutInternal(path, obj, /* flags = */ 0);
353+
PutInternal(path, obj, /* flags = */ 0, title);
354354
}
355355

356356
/// Puts an object into the file, overwriting any previously-existing object at that path.
@@ -365,10 +365,17 @@ public:
365365
/// \throws ROOT::RException if the file was opened in read-only mode.
366366
template <typename T>
367367
void Overwrite(std::string_view path, const T &obj, bool createNewCycle = true)
368+
{
369+
Overwrite(path, obj, "", createNewCycle);
370+
}
371+
372+
/// Like Overwrite(std::string_view, const T&, bool) but allows giving a title to the object.
373+
template <typename T>
374+
void Overwrite(std::string_view path, const T &obj, std::string_view title, bool createNewCycle = true)
368375
{
369376
std::uint32_t flags = kPutFlag_AllowOverwrite;
370377
flags |= createNewCycle * kPutFlag_OverwriteKeepCycle;
371-
PutInternal(path, obj, flags);
378+
PutInternal(path, obj, flags, title);
372379
}
373380

374381
/// Writes all objects and the file structure to disk.

io/io/test/rfile.cxx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,15 @@ TEST(RFile, PutOverwrite)
231231
{
232232
TH1D hist("hist", "", 100, -10, 10);
233233
hist.FillRandom("gaus", 1000);
234-
file->Put("hist", hist);
234+
file->Put("hist", hist, "My Histo");
235235
}
236236

237237
{
238238
auto hist = file->Get<TH1D>("hist");
239239
ASSERT_TRUE(hist);
240240
EXPECT_EQ(static_cast<int>(hist->GetEntries()), 1000);
241+
auto key = file->GetKeyInfo("hist").value();
242+
EXPECT_EQ(key.GetTitle(), "My Histo");
241243
}
242244

243245
// Try putting another object at the same path, should fail
@@ -267,6 +269,8 @@ TEST(RFile, PutOverwrite)
267269
// ...but any cycle before the latest should still be there!
268270
hist = file->Get<TH1D>("hist;1");
269271
EXPECT_NE(hist, nullptr);
272+
auto key = file->GetKeyInfo("hist;1").value();
273+
EXPECT_EQ(key.GetTitle(), "My Histo");
270274
}
271275
}
272276

io/io/test/rfile.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def test_getkeyinfo(self):
6363
with RFile.Recreate(fileName) as rfile:
6464
hist = ROOT.TH1D("hist", "", 100, -10, 10)
6565
hist.FillRandom("gaus", 10)
66-
rfile.Put("hist", hist)
66+
rfile.Put("hist", hist, "My Histo")
6767
rfile.Put("foo/hist", hist)
6868
rfile.Put("foo/bar/hist", hist)
6969
rfile.Put("foo/bar/hist2", hist)
@@ -73,6 +73,7 @@ def test_getkeyinfo(self):
7373
key = rfile.GetKeyInfo("hist")
7474
self.assertEqual(key.GetPath(), "hist")
7575
self.assertEqual(key.GetClassName(), "TH1D")
76+
self.assertEqual(key.GetTitle(), "My Histo")
7677

7778
key = rfile.GetKeyInfo("does_not_exist")
7879
self.assertEqual(key, None)

0 commit comments

Comments
 (0)