# Looping to create new Branches with different data

**URL:** <https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044>\
**Category:** Newbie\
**Created:** [August 2, 2022, 2:16pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044 "2022-08-02T14:16:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 2, 2022, 2:16pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/1 "2022-08-02T14:16:50Z")

</div>

I am trying to create new branches (different names) with different data through looping. However, the data keeps adding up in all the branches. I believe it is due to reference argument of TBranch. Here is my code. Please help me out. Thank you.

```auto
4 import ROOT
  5 import json
  6 import array as arr
  7 
  8 jsfile = open('data.json')
  9 data = json.load(jsfile)
 10 
 11 rtfile = ROOT.TFile("js2rt.root", "RECREATE")
 12 tree = ROOT.TTree("0", "Events")
 13 
 14 for i in range(128):
 15 adc = arr.array('f', [0])
 16 tree.Branch("chn"+str(i), adc, "leafname/F")
 17 for entry in data['signalData'][0]['chn'+str(i)]:
 18 adc[0] = entry
 19 tree.Fill()
 20 
 21 tree.Write()
 22 rtfile.Close()
 23 jsfile.close()

```

---

<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:** [August 2, 2022, 6:06pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/2 "2022-08-02T18:06:03Z")

</div>

You need `branch=tree.Branch(...)` and then `branch.Fill()` instead of `tree.Fill()`.

BTW. Ensure all branches have precisely the same number of entries (otherwise, expect problems with your tree later).

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 3, 2022, 11:09am UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/3 "2022-08-03T11:09:35Z")

</div>

I made a mistake in the wording. I do not need branches, just my leaves are not as I am hoping them to be. I am trying to create 128 leaves (from 0 to 127) with 645 entires each. However, I do have 128 leaves but all of them 82650 entries (645 x 128).

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/b/d/bdd1ca87cb07079d3583313accf7cc2de0409001.png)

I also made the changes as you suggested but there is no data in the leaf for some reason.  
 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/d/8d843443f03f464053de72d3f9768572853bd56f.png)

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/9/3/93d5b5b72a4b1a3a55187d338ea7a59961843b9c.png)

---

<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:** [August 3, 2022, 11:52am UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/4 "2022-08-03T11:52:04Z")

</div>

`array('i', [0])` is a 2-bytes signed integer so you need `"leafname/S"`

[array - Efficient arrays of numeric values](https://docs.python.org/3/library/array.html)

[TTree → Add a column (“branch”) holding fundamental types and arrays thereof](https://root.cern/doc/master/classTTree.html#addcolumnoffundamentaltypes)

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 3, 2022, 2:42pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/5 "2022-08-03T14:42:18Z")

</div>

Thank you for your reply. I am still having same problems as each leaf (all 128 leaves) have all the entries (128 x 645 = 82650) instead of 645 entries each.

My code:

```auto
 1 import ROOT
  2 import json
  3 import array as arr
  4     
  5 jsfile = open('data.json')
  6 data = json.load(jsfile)
  7  
  8 rtfile = ROOT.TFile("new.root", "RECREATE")
  9 tree = ROOT.TTree("0", "Events")
 10  
 11 for i in range(128):
 12 adc = arr.array('f', [0])
 13 tree.Branch("chn"+str(i), adc, "leafname/S")
 14 for entry in data['signalData'][0]['chn'+str(i)]:
 15 adc[0] = entry
 16 tree.Fill()
 17     
 18 tree.Write()
 19 rtfile.Close()
 20 jsfile.close()

```

Previously I thought it was a reference problem but it was not. I have been working on this for 1 and half day now, still keeps on getting it wrong.

---

<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:** [August 3, 2022, 2:48pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/6 "2022-08-03T14:48:20Z")

</div>

I have already shown you the fix:

> [@Looping to create new Branches with different data](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/2):
>
> You need branch=tree.Branch(...) and then branch.Fill() instead of tree.Fill(). BTW. Ensure all branches have precisely the same number of entries (otherwise, expect problems with your tree later).

Also, do read the contents of the two links in this post:

> [@Looping to create new Branches with different data](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/4):
>
> array('i', [0]) is a 2-bytes signed integer so you need "leafname/S" [array - Efficient arrays of numeric values](https://docs.python.org/3/library/array.html)[TTree → Add a column (“branch”) holding fundamental types and arrays thereof](https://root.cern/doc/master/classTTree.html#addcolumnoffundamentaltypes)

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 3, 2022, 9:17pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/7 "2022-08-03T21:17:52Z")

</div>

I did ‘branch = tree.Branch() and then branch.Fill()’ as you suggested but my leaves are empty. Here’s the exact code I ran.

```auto
1 import ROOT
  2 import json
  3 import array as arr
  4 
  5 jsfile = open('data.json')
  6 data = json.load(jsfile)
  7 
  8 rtfile = ROOT.TFile("js2rt.root", "RECREATE")
  9 tree = ROOT.TTree("0", "Events")
 10 
 11 for i in range(128):
 12 adc = arr.array('f', [0])
 13 branch=tree.Branch("chn"+str(i), adc, "leafname/S")
 14 for entry in data['signalData'][0]['chn'+str(i)]:
 15 adc[0] = entry
 16 branch.Fill()
 17 
 18 tree.Write()
 19 rtfile.Close()
 20 jsfile.close()

```

Output:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/8/c/8cad256f83e12e1228363e8ade8d40e248fa3055.png)

When I do tree.Fill(), I do have the data in the leaves in the above described way.

---

<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:** [August 3, 2022, 10:27pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/8 "2022-08-03T22:27:16Z")

</div>

When filling a `TTree` branch by branch (your case) you must call `SetEntries` at the end:

```auto
tree.SetEntries()
tree.Write()

```

otherwise, the TTree thinks it is empty (has zero entries).

---

<div class="post-metadata">

**Author:** ![TheScientist](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/82dd89/32.png) [@TheScientist](https://root-forum.cern.ch/u/TheScientist)\
**Post date:** [August 3, 2022, 11:16pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/9 "2022-08-03T23:16:13Z")

</div>

YAY!! tree.SetEntries() did the trick. Thank you very much.

---

<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:** [August 4, 2022, 6:42pm UTC](https://root-forum.cern.ch/t/looping-to-create-new-branches-with-different-data/51044/10 "2022-08-04T18:42:09Z")

</div>

Well, I’ve forgotten about [TTree::SetEntries](https://root.cern/doc/master/classTTree.html#a21f854fbafc4c565695ce8e928289c7e) (which is only needed when one fills all branches independently).  
Fortunately, it also checks if the numbers of entries in all branches are identical (if not, a warning is issued, which one should not ignore).
