# Two Trees are getting created while asking for one

**URL:** <https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923>\
**Category:** ROOT\
**Tags:** tree, root\
**Created:** [December 2, 2021, 1:40pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923 "2021-12-02T13:40:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [December 2, 2021, 1:40pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/1 "2021-12-02T13:40:48Z")

</div>

* * *

_ROOT Version:_ 6.20/08  
_Platform:_ Ubuntu 18.04.06  
_Compiler:_ gcc7.5.0

* * *

Hi Experts,  
I was trying to extract some variables from one ROOT file to another. I am defining only one `TTree` to be created, but the new file is having **two** `TTree`. I cannot understand what is happening. More peculiarly, when I was creating the macro it was working fine. But just when I ran it via a shell script it started this behavior. After that even for manual single runs, it’s not working. This happened twice.  
Could you people give some idea what’s wrong in the macro?  
I guess I have to delete the memory pointer to the new `TTree`. But how to do it? I tried with `delete outtree; delete outfile;`, but it didn’t work.  
I have attached an example input file and the macro.  
Thanks in advance.

Regards,  
Saumyen

[Zextract.C](https://root-forum.cern.ch/uploads/short-url/yvktqR1eVQu0Eo5uKX7t0Glmx9B.C) (2.9 KB)  
[ToyFile.root](https://root-forum.cern.ch/uploads/short-url/9yAB8TaVi382wbZPWkFxgj8QDvx.root) (2.4 MB)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [December 2, 2021, 5:35pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/2 "2021-12-02T17:35:54Z")

</div>

Instead of

```auto
  outfile->Write();
delete outtree;
delete outfile;

```

try

```auto
outfile->cd(); // just in case
outtree->Write();
outfile->Close();
delete outfile;

```

---

<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 2, 2021, 7:09pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/3 "2021-12-02T19:09:49Z")

</div>

What do you get from: `outfile->ls();`

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [December 2, 2021, 8:31pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/4 "2021-12-02T20:31:53Z")

</div>

Thanks a lot to both of you @dastudillo and @Wile_E_Coyote .  
I tried the suggestion by @dastudillo. But that didn’t work. Then I tried @Wile_E_Coyote’s suggestion to see what’s happening. What I found is this:

```auto
TFile** /home/photoniman/Research/SP_BP2.root	
TFile* /home/photoniman/Research/SP_BP2.root	
  OBJ: TTree	event_tree	Test : 0 at: 0x55981d7365f0
  KEY: TTree	event_tree;2	Test
  KEY: TTree	event_tree;1	Test

```

Although in `TBrowser` it was showing this:  
 ![Screenshot from 2021-12-03 01-54-52](https://root-forum.cern.ch/uploads/default/original/3X/6/1/61253d3fd9d6b00e3686a517007b70cc1da2ff57.png)

Any idea what’s happening?

Regards,  
Saumyen

---

<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 2, 2021, 8:43pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/5 "2021-12-02T20:43:41Z")

</div>

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2):
>
> Search for “cycle” and/or “namecycle”, for example in: In a Nutshell: [Input and Output](https://root.cern.ch/d/input-and-output) . ROOT User’s Guide: [Input/Output](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#inputoutput) : [Retrieving Objects from Disk](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#retrieving-objects-from-disk) , [Subdirectories and Navigation](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#subdirectories-and-navigation) . Just for reference: [some additional notes about GetObject, Get, FindObjectAny, FindKeyAny, ReadObj, ReadObjAny](https://root-forum.cern.ch/t/broken-tdirectoryfile-findobjectany-and-tdirectoryfile-findkeyany/20943/16) .

---

<div class="post-metadata">

**Author:** ![Saumyenk](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/3bc359/32.png) [@Saumyenk](https://root-forum.cern.ch/u/Saumyenk)\
**Post date:** [December 2, 2021, 10:41pm UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/6 "2021-12-02T22:41:42Z")

</div>

Thanks a lot @Wile_E_Coyote . Now it’s working fine. I just replaced `outtree->Write();` with `outtree->Write("", TObject::kOverwrite);` in @dastudillo’s code-suggestion.  
Thanks to both of you.

Regards,  
Saumyen

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [December 3, 2021, 8:51am UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/7 "2021-12-03T08:51:08Z")

</div>

Note that `event_tree;2` and `event_tree;1` are _not_ two completely different trees. These are namecycles for the same tree (see e.g. the link by @Wile_E_Coyote ), a crash-recovery mechanism. Only the TTree metadata is duplicated between namecycles, data is only present once in the file.

In practice you can ignore the namecycles and only use the name `event_tree`. @pcanal might be able to point you to more resources.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [December 17, 2021, 8:51am UTC](https://root-forum.cern.ch/t/two-trees-are-getting-created-while-asking-for-one/47923/8 "2021-12-17T08:51:37Z")

</div>

Closed on December 17, 2021, 8:51am UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
