# TH1I copy constructor does not copy current directory

**URL:** <https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614>\
**Category:** ROOT\
**Created:** [December 12, 2014, 9:03pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614 "2014-12-12T21:03:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pliuphys](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/3da27b/32.png) [@pliuphys](https://root-forum.cern.ch/u/pliuphys)\
**Post date:** [December 12, 2014, 9:03pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/1 "2014-12-12T21:03:40Z")

</div>

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](https://root-forum.cern.ch/uploads/default/original/2X/d/d4005c51cf9833dc8fcc85e22c29e054eb0ccbc6.C) (1.16 KB)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 16, 2014, 9:55am UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/2 "2014-12-16T09:55:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 16, 2014, 1:25pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/3 "2014-12-16T13:25:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 16, 2014, 3:20pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/4 "2014-12-16T15:20:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brun](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/b/b9e5f3/32.png) [@brun](https://root-forum.cern.ch/u/brun)\
**Post date:** [December 16, 2014, 3:47pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/5 "2014-12-16T15:47:51Z")

</div>

Lorenzo,

NOOOO

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

Rene

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 16, 2014, 4:07pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/6 "2014-12-16T16:07:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 16, 2014, 5:47pm UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/7 "2014-12-16T17:47:34Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [December 17, 2014, 2:44am UTC](https://root-forum.cern.ch/t/th1i-copy-constructor-does-not-copy-current-directory/18614/8 "2014-12-17T02:44:00Z")

</div>

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.
