# Replace entries in a single branch only of an existing tree with several branches based on an if condition

**URL:** <https://root-forum.cern.ch/t/replace-entries-in-a-single-branch-only-of-an-existing-tree-with-several-branches-based-on-an-if-condition/61691>\
**Category:** Newbie\
**Created:** [September 18, 2024, 5:07am UTC](https://root-forum.cern.ch/t/replace-entries-in-a-single-branch-only-of-an-existing-tree-with-several-branches-based-on-an-if-condition/61691 "2024-09-18T05:07:29Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [September 18, 2024, 3:06pm UTC](https://root-forum.cern.ch/t/replace-entries-in-a-single-branch-only-of-an-existing-tree-with-several-branches-based-on-an-if-condition/61691/4 "2024-09-18T15:06:14Z")

</div>

Correct a `TTree` is a write-once, read-many time structure.

As summarized here [Changing the values in the branch - #2 by Wile\_E\_Coyote](https://root-forum.cern.ch/t/changing-the-values-in-the-branch/52683/2)  
Your 4 options are:

1. 

```
You could apply your “fixes” when you actually analyze the data (e.g., define some alias “corrected_branch = original_branch - corerection_value” and then use the “corrected_branch” in the analysis).

```

2. 

```
You could “copy” your tree to another file, modifying its entries on the fly.

```

3. 

```
You could create a “friend tree” with new branches.

```

4. 

```
You could add new branches to the existing tree.

```

Where option 3. (using a friend tree) is the most commonly used when option 1. is not possible.

Your example implements option 2. Albeit you should change the name of the branch (and/or first remove the old branches from the `newTree`.

Your example can implement option 4 by creating an empty 'newTree`rather than cloning the existing tree (and then use the result by calling`TTree::AddFriend`).

Note that you can optionally writing the `newTree` in the same file (open the old file with the `UPDATE` option) and make the friendship ‘permanent’ (by updating ‘just’ the meta-data of the old TTree).

---

_[View the full topic](https://root-forum.cern.ch/t/replace-entries-in-a-single-branch-only-of-an-existing-tree-with-several-branches-based-on-an-if-condition/61691)._
