# Copy tree to new file and rename it

**URL:** <https://root-forum.cern.ch/t/copy-tree-to-new-file-and-rename-it/44022>\
**Category:** Newbie\
**Created:** [March 17, 2021, 9:18am UTC](https://root-forum.cern.ch/t/copy-tree-to-new-file-and-rename-it/44022 "2021-03-17T09:18:13Z")\
**Posts on this page:** 1\
**Showing post:** 26

<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:** [March 23, 2021, 4:36pm UTC](https://root-forum.cern.ch/t/copy-tree-to-new-file-and-rename-it/44022/26 "2021-03-23T16:36:56Z")

</div>

So, the behavior is _exactly_ as intended. The flow is:

```auto
  oldfile.GetObject("ntuple", oldtree);
  ...
  auto newtree = oldtree->CloneTree();

```

where the request is here “explicitly” to slow copy a `TTree` named “ntuple”, and which has, in the original case and the modified hsimple case, more entries that the AutoSave limit. This means that during the execution of `CloneTree` a cycle for the `TTree` named “ntuple” is (of course) created.  
Calling `TTree::SetObject` (better version of `SetNameTitle` but same result here), will change the name of the live object (and we see that with the name of the key/cycle written by `TFile::Write`) but will not (and should not) retroactively change the name of the existing cycles in the file. So

```auto
  KEY: TNtuple	ntuple;1	Demo ntuple
  KEY: TNtuple	PolSig;1	PolSig

```

depicts accurately what has happened:

1. a `TNtuple` named “ntuple” was auto-saved
2. a `TNtuple` named “PolSig” was saved.

To modify the behavior you can change the name of the `TNtuple` **\*before\*** cloning it:

```auto
     oldtree->SetObject("PolSig","PolSig");
     auto newtree = oldtree->CloneTree();

```

**\*or\*** avoid the call to `AutoSave` (and save a lot of time! 🙂 ) by using the fast cloning.

```auto
  auto newtree = oldtree->CloneTree(-1, "fast");
  newtree->SetObject("PolSig","PolSig");

```

but still even in that case I would rename the object before cloning.

Cheers,  
Philippe.

---

_[View the full topic](https://root-forum.cern.ch/t/copy-tree-to-new-file-and-rename-it/44022)._
