Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly use Copy(TObject &) methods in hist classes #10942

Merged
merged 7 commits into from
Aug 17, 2022

Conversation

linev
Copy link
Member

@linev linev commented Jul 11, 2022

This is virtual method and can be overriden in derived classes.
Therefore when such method used in copy constructor or assignment
operator, one have to explicitly specify class which used.

Fixes #10919

@linev linev requested a review from lmoneta as a code owner July 11, 2022 08:45
@linev linev self-assigned this Jul 11, 2022
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu18.04/nortcxxmod, ROOT-ubuntu2004/python3, mac1015/cxx17, mac11/cxx14, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu18.04/nortcxxmod.
Running on sft-ubuntu-1804-3.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu2004/python3.
Running on root-ubuntu-2004-1.cern.ch:/home/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

Copy link
Member

@lmoneta lmoneta left a comment

Choose a reason for hiding this comment

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

I am not sure if the use of non-virtual Copy in the copy constructor of the histogram class is correct.
This PR will make a copy a TProfile to a TH1D possible which can have some side effects.
Example:

TProfile p("p","p",10,0,10);
p.Fill(0.5,10); p.Fill(0.5,20);
TH1D & h1 = p;
TH1D h2 = h1;
h2.Draw();
h1.SetLineColor(kRed);
h1.Draw("SAME");

This code works silently with this PR producing an histogram from a Profile but with wrong information (e.g. the bin error).
I don't see then the benefits of not using the virtual Copy in the copy constructor and assignment operator in the case of the histograms.

@linev
Copy link
Member Author

linev commented Aug 3, 2022

If one want to keep usage of virtual Copy methods, in each such method one should check if source object type match this object - using dynamic cast. Otherwise it may produce segmentation violation.

In my mind, current implementation of copy constructors with using of virtual Copy methods is wrong and error prone.

@lmoneta
Copy link
Member

lmoneta commented Aug 3, 2022

Are you referring about a check that the final type of the source is the same final type pf the target, i.e. in this code below:

TH2D::TH2D(const TH2D &h2d) : TH2(), TArrayD()
 {
    ((TH2D&)h2d).Copy(*this);
 }

if this and 'h2dare actually the same final types (e.g. both TProfile histograms). This check is performed in theTProfile::Copy` function, see :
https://github.com/root-project/root/blob/master/hist/hist/src/TProfile.cxx#L422

@linev
Copy link
Member Author

linev commented Aug 3, 2022

This check is performed in theTProfile::Copy` function,

Such check must be introduced in all Copy methods in all classes.
To avoid such checks, I propose directly call method of appropriate class and do not use virtual interface.

@lmoneta
Copy link
Member

lmoneta commented Aug 3, 2022

I agree if one is able to call copy constructors only for the final classes and not for base ones. In the ROOT histogram design this is not the case, since we have classes as TProfile deriving from TH1D and not TH1.

@linev
Copy link
Member Author

linev commented Aug 3, 2022

When calling virtual Copy method in copy constructor - one never knows that exact happens.
User can define derived class from TH1D and provide own Copy implementation.
In such case TH1D th1 = user_th1; will leads to wrong results. TProfile is just example of that with existing ROOT classes.

These classes are base for TProfile[,2D,3D] and copy constructor
should not be used together with such classes. This is check
if TProfile::Copy() method.
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/soversion, ROOT-performance-centos8-multicore/cxx17, ROOT-ubuntu18.04/nortcxxmod, ROOT-ubuntu2004/python3, mac1015/cxx17, mac11/cxx14, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu18.04/nortcxxmod.
Running on sft-ubuntu-1804-2.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu2004/python3.
Running on root-ubuntu-2004-1.cern.ch:/home/sftnight/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

@phsft-bot
Copy link
Collaborator

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

Failing tests:

Copy link
Member

@lmoneta lmoneta left a comment

Choose a reason for hiding this comment

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

Thank you Sergei for the improvement and the change in TH1,2,3,D classes.
It looks fine to me now.
I think there is a reference file that needs to be update in the roottest repo for avoiding the failure seen in the CI, after the change to use nullptr.

@linev
Copy link
Member Author

linev commented Aug 17, 2022

I already prepare PR for root-test:

root-project/roottest#884

I will merge it shortly after merging this PR

@linev linev merged commit 0aba267 into root-project:master Aug 17, 2022
@linev linev deleted the copy_hist branch August 17, 2022 17:23
@phsft-bot
Copy link
Collaborator

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

Errors:

  • [2022-08-17T18:47:03.012Z] CMake Error at /Volumes/HD2/build/workspace/root-pullrequests-build/rootspi/jenkins/root-build.cmake:1088 (message):

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.

Histograms copy constructor has wrong implementation
3 participants