# Copying tree using Clonetree Method

**URL:** <https://root-forum.cern.ch/t/copying-tree-using-clonetree-method/28433>\
**Category:** ROOT\
**Created:** [March 25, 2018, 2:17am UTC](https://root-forum.cern.ch/t/copying-tree-using-clonetree-method/28433 "2018-03-25T02:17:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noor1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/noor1/32/38240_2.png) [@Noor1](https://root-forum.cern.ch/u/Noor1)\
**Post date:** [March 25, 2018, 2:17am UTC](https://root-forum.cern.ch/t/copying-tree-using-clonetree-method/28433/1 "2018-03-25T02:17:55Z")

</div>

Dear  
I Have a tree with large number of events (49932086), and I want to copy it into new tree in different file.  
I followed the example of Clone tree and wrot this script

//Open old File and Trees  
TFile \*input = new TFile(input\_rootfile,“READ”);  
TTree input\_tree = (TTree)input-\>Get(“T”); //h509:name of the tree in the rootfile  
Long64\_t nentries = input\_tree-\>GetEntries();  
cout\<\<“nentries=”\<\<nentries\<\<endl;

TFile \*output = new TFile(output\_rootfile,“RECREATE”);  
TTree \*tree=input\_tree-\>CloneTree(0);

for (int i=0;i\<nentries; i++) {  
input\_tree-\>GetEntry(i);  
tree-\>Fill();  
}  
tree-\>Write();

and I obtained one root file with two trees with names T,1 (3372395 entries) and T,2 (49932086 entries).  
this was unexpected.

I start to change the filling of for loop (nentries), and choose 100, 1000, 10000. …etc ( for (int i=0;i\<100; i++)), and it works fine one root file with the chosen number of entries up to 3372396 entries, root stopped filling in the first tree and create new tree and start filling in T,2.

any explanation and how can I avoid this please

---

<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:** [March 25, 2018, 3:02am UTC](https://root-forum.cern.ch/t/copying-tree-using-clonetree-method/28433/2 "2018-03-25T03:02:49Z")

</div>

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345):
>
> Hello! I’m filling a tree and after I, sometimes, get this funny behavior AUX;1 AUX;2 with the same histograms inside but with different events in each ;1 and ;2… Can somebody please tell me why this is happening? Many thanks, Stefanos

---

<div class="post-metadata">

**Author:** ![Noor1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/noor1/32/38240_2.png) [@Noor1](https://root-forum.cern.ch/u/Noor1)\
**Post date:** [March 25, 2018, 3:30am UTC](https://root-forum.cern.ch/t/copying-tree-using-clonetree-method/28433/3 "2018-03-25T03:30:20Z")

</div>

Many Thanks for reply,  
I understood that there is indirect call to AutoSave, and in order to avoid this I need to write the last cycle of the tree  
I replace  
tree-\>Write(); by tree-\>Write(0,TObject::kOverwrite);

it works 🙂, thank you

---

<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:** [April 8, 2018, 3:30am UTC](https://root-forum.cern.ch/t/copying-tree-using-clonetree-method/28433/4 "2018-04-08T03:30:29Z")

</div>

Closed on April 8, 2018, 3:30am UTC

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