Skip to content

Writing TObject-derived objects to file does not store the object's title #9989

@vepadulano

Description

@vepadulano

Describe the bug

Following #8934, when writing TObject-derived objects to a TFile, their title is correctly stored as the corresponding TKey's title, so that when listing the contents of the file it is properly displayed:

(base) vpadulan@fedora: root
   ------------------------------------------------------------------
  | Welcome to ROOT 6.27/01                        https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Feb 16 2022, 23:26:00                 |
  | From heads/master@v6-25-02-553-g21fb792ac7                       |
  | With c++ (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)                |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |
   ------------------------------------------------------------------

root [0] TFile f{"myfile.root", "recreate"};
root [1] TH1F h{"myname","mytitle", 42, 13, 77};
root [2] f.WriteObject(&h, "myhisto");
root [3] f.Close()
root [4] .q
(base) vpadulan@fedora: rootls -lt myfile.root
TH1F  Feb 24 15:59 2022 myhisto;1 "mytitle"

In PyROOT this still does not happen

(base) vpadulan@fedora: python
Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:46) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
>>> a = ROOT.TFile("myfile.root", "recreate")
>>> h = ROOT.TH1F("myname","mytitle", 42, 13, 77)
>>> a.WriteObject(h, "myhisto")
279
>>> a.Close()
>>> quit()
(base) vpadulan@fedora: rootls -lt myfile.root
TH1F  Feb 24 15:58 2022 myhisto;1 "object title" 

The offending code is in the pythonization of TDirectory::WriteObject defined within cppyy:

if (option != nullptr) {
result = dir->WriteObjectAny(wrt->GetObject(), GetTClass(wrt), CPyCppyy_PyText_AsString(name),
CPyCppyy_PyText_AsString(option), bufsize);
} else {
result = dir->WriteObjectAny(wrt->GetObject(), GetTClass(wrt), CPyCppyy_PyText_AsString(name));

which only uses WriteObjectAny since the actual type of the object is lost at that point (it's a void*).

Expected behavior

The pythonized version of WriteObject should work as its C++ counterpart

To Reproduce

Code above should be fine whenever ROOT is built with a version that includes commits of #8934

Notes

The linked PR proposes a fix

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions