Skip to content

[core] WriteObject overload for TObject-derived classes#8934

Merged
vepadulano merged 1 commit intoroot-project:masterfrom
vepadulano:writeobject-overload
Sep 2, 2021
Merged

[core] WriteObject overload for TObject-derived classes#8934
vepadulano merged 1 commit intoroot-project:masterfrom
vepadulano:writeobject-overload

Conversation

@vepadulano
Copy link
Copy Markdown
Member

@vepadulano vepadulano commented Aug 30, 2021

The TDirectory::WriteObject method allows writing objects to files. If the written object actually has a title, this should be discarded because the function doesn't manage it as a TObject-derived instance on purpose. For example, the program below:

int main(){
    TFile f{"myfile.root","recreate"};
    TH1F h{"myhistoname","myhistotitle",100,0,100};
    f.WriteObject(&h, h.GetName());
    f.Close();
}

When executed creates a file where the object "h" gets the default title "object title":

$ rootls -l myfile.root
TH1F  Aug 21 10:41 2021 myhistoname;1 "object title"

This is because The TKey constructor that accepts a void pointer calls the parent TNamed constructor with a default title, because in general there is no guarantee the object has the interface GetTitle(),SetTitle() and there is no extra "title" parameter to the constructor.

This commit provides a solution by creating a new overload for TDirectory::WriteObject, using SFINAE to make it available for types that are derived from TObject. The method redirects to WriteTObject instead of WriteObjectAny. This way, the correct TKey constructor is called that uses the actual object title. As a result, the example above will now output a file like this:

$ rootls -l myfile.root
TH1F  Aug 21 11:00 2021 myhistoname;1 "myhistotitle"

The already present method is modified with SFINAE as well, to only be available if the type T of the template is not derived from TObject.

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-ubuntu16/nortcxxmod.
Running on sft-ubuntu-1604-1.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-08-30T15:59:34.507Z] ninja: error: 'tmva/pymva/ROOTTMVASofie', needed by 'tmva/pymva/G__PyMVA.cxx', missing and no known rule to make it

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft11.dyndns.cern.ch:/home/sftnight/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-08-30T16:00:30.999Z] ninja: error: '/home/sftnight/build/workspace/root-pullrequests-build/root/tmva/pymva/ROOTTMVASofie', needed by 'tmva/pymva/G__PyMVA.cxx', missing and no known rule to make it

@phsft-bot
Copy link
Copy Markdown

Build failed on mac11.0/cxx17.
Running on macphsft20.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-08-30T16:00:56.303Z] ninja: error: 'tmva/pymva/ROOTTMVASofie', needed by 'tmva/pymva/G__PyMVA.cxx', missing and no known rule to make it

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-performance-centos8-multicore/default.
Running on olbdw-01.cern.ch:/data/sftnight/workspace/root-pullrequests-build
See console output.

@phsft-bot
Copy link
Copy Markdown

Build failed on mac1014/python3.
Running on macitois21.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-08-30T16:09:05.723Z] ninja: error: 'tmva/pymva/ROOTTMVASofie', needed by 'tmva/pymva/G__PyMVA.cxx', missing and no known rule to make it

Copy link
Copy Markdown
Member

@pcanal pcanal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending test improvement

@vepadulano vepadulano force-pushed the writeobject-overload branch from 704d6c5 to c3d73fa Compare August 30, 2021 20:13
@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-performance-centos8-multicore/default.
Running on olbdw-01.cern.ch:/data/sftnight/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-ubuntu16/nortcxxmod.
Running on sft-ubuntu-1604-1.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Build failed on mac11.0/cxx17.
Running on macphsft23.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Build failed on mac1014/python3.
Running on macitois21.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Build failed on windows10/cxx14.
Running on null:C:\build\workspace\root-pullrequests-build
See console output.

Failing tests:

@vepadulano
Copy link
Copy Markdown
Member Author

Following Philippe's suggestion for the test (thanks!) it's clear that the current logic is not enough.

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on mac11.0/cxx17.
Running on macphsft20.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Copy Markdown

Build failed on mac11.0/cxx17.
Running on macphsft20.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

The
[TDirectory::WriteObject](https://github.com/root-project/root/blob/35b5aaef38b6635e131e7d93a0c96f69bb293b9d/core/base/inc/TDirectory.h#L265-L268)
method allows writing objects to files. If the written object actually has a
title, this would be discarded because the function doesn't manage it as a
TObject-derived instance on purpose. For example, the program below:

```cpp
int main(){
    TFile f{"myfile.root","recreate"};
    TH1F h{"myhistoname","myhistotitle",100,0,100};
    f.WriteObject(&h, h.GetName());
    f.Close();
}
```

When executed creates a file where the object "h" gets the default title "object title":

```bash
$ rootls -l myfile.root
TH1F  Aug 21 10:41 2021 myhistoname;1 "object title"
```

This is because The [TKey constructor that accepts a void
pointer](https://github.com/root-project/root/blob/35b5aaef38b6635e131e7d93a0c96f69bb293b9d/io/io/src/TKey.cxx#L299-L300)
calls the parent TNamed constructor with a default title, because in general
there is no guarantee the object has the interface `GetTitle(),SetTitle()` and
there is no extra "title" parameter to the constructor.

This commit provides a solution by creating a new overload for
`TDirectory::WriteObject`, using SFINAE to make it available for types that are
derived from TObject. The method redirects to `WriteTObject` instead of
`WriteObjectAny`. This way, the correct TKey constructor is called that uses the
actual object title. As a result, the example above will now output a file like
this:

```
$ rootls -l myfile.root
TH1F  Aug 21 11:00 2021 myhistoname;1 "myhistotitle"
```

The already present method is modified with SFINAE as well, to only be available
if the type T of the template is not derived from TObject.
@phsft-bot
Copy link
Copy Markdown

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@vepadulano
Copy link
Copy Markdown
Member Author

Updated docs, run clang-tidy on the modified file and squashed to one commit. If tests pass I will merge

@phsft-bot
Copy link
Copy Markdown

Build failed on mac11.0/cxx17.
Running on macphsft20.dyndns.cern.ch:/Users/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@vepadulano vepadulano merged commit eab34e6 into root-project:master Sep 2, 2021
@vepadulano vepadulano deleted the writeobject-overload branch October 30, 2021 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants