TH1I copy constructor does not copy current directory

Hi,

I was trying to put some histograms into STL vectors, assign them to a TFile. But I found that when I push_back more, the previous histograms sometimes disappears in the TFile. See Attachment.

I believe this is because that the directory information is not saved when copying. Eg:

void histSimple()
{
TH1::AddDirectory(kFALSE);
TFile f;
TH1I h1(“h1”,“h1”,100,0.0,1.0);
h1.SetDirectory(&f);
cout<<“h1 directory:”<<h1.GetDirectory()<<endl;
TH1I h2(h1);
cout<<“h2 directory:”<<h2.GetDirectory()<<endl;
}

Output:
h1 directory:0xf591b0
h2 directory:0

Is this behavior intended? I know histograms are put into pwd by default, and so with TH1::AddDirectory(kFALSE) it makes some sense to be 0. But I feel that it’s nicer to make an exception for the copy constructor.

PS:this was tested on ROOT 5.34/23.
histClone.C (1.16 KB)

Hi,

I think this is a bug.
If histogram have TH1::AddDirectoryStatus(false) and a user given directory, the copied histogram should be copied in that directory.
If instead TH1::AddDirectoryStatus() is true and the current stored directory of the histogram (the one returned in TH1::GetDirectory() is not the current directory or it is null) then probably the histogram should be copied in the current directory (gDirectory).

I will fix this as described above

Best Regards

Lorenzo

I’m sorry but, in my opinion, if the user explicitly asks TH1::AddDirectory(kFALSE) then NO newly created histogram should be added to any directory. It should NOT matter where the “contents” of a newly created histogram comes from and the copy constructor is just another CONSTRUCTOR, i.e. it creates a completely NEW histogram.

Hi,

This is the case, when TH1::AddDirectoryStatus() is FALSE the histogram is not automatically attached to any directory. The user can anyway call later TH1::SetDirectory( someDirectory) to attached it to some existing directory.
The question is for copied or cloned histogram when TH1::GetDirectory() is not null.

  • If TH1::AddDirectoryStatus() is FALSE the copied histogram should have GetDirectory() = 0 (current case) or the one of the original histogram ( hold->GetDirectory()) ?
  • if TH1::ADDDirectoryStatus() is TRUE the copied histogram should be in gDirectory (current case) or in the one of the original histogram hold->GetDirectory() ?

In my opinion TH1::AddDirectory() refers only to the current directory (gDirectory) so in the first case we should fix to have the copied histogram the directory of the original histogram and in the second case we maintain the current status.

Best Regards

Lorenzo

Lorenzo,

NOOOO

If TH1::AddDirectory(kFALSE) the copied histogram should not be added to any directory.

Rene

Rene,

I am not sure of this, because a copy should be equal to the original.
If you think that all histogram should not be added to any directory when TH1::AddDirectory() is false, then we should disable TH1::SetDirectory() when TH1::AddDirectory is false.

Lorenzo

Just leave TH1::SetDirectory as it is. One must always be able to manually add a histogram to a directory, even it it was created as directory-less (the same goes in the other direction -> one must always be able to make a histogram directory-less just by using TH1::SetDirectory(0), regardless of the current TH1::AddDirectoryStatus).

Hi Lorenzo,

[quote]If you think that all histogram should not be added to any directory when TH1::AddDirectory() is false, then we should disable TH1::SetDirectory() when TH1::AddDirectory is false. [/quote]I think you missed the implicit word ‘automatically’ in Rene’s statement.

Cheers,
Philippe.