Looping to create new Branches with different data

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.

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()

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).

1 Like

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).

I also made the changes as you suggested but there is no data in the leaf for some reason.
image

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

array - Efficient arrays of numeric values

TTree → Add a column (“branch”) holding fundamental types and arrays thereof

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:

 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.

I have already shown you the fix:

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

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

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:

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

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

tree.SetEntries()
tree.Write()

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

1 Like

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

Well, I’ve forgotten about TTree::SetEntries (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).