Filling variable-sized branches in PyROOT

I am filling a tree in pyROOT (root version 6.08). It does the job, doesn’t give any errors, but when I look at the values in TBrowser, all the variable-sized branches have values that are completely off from what I expect them to be (10^33 instead of 10^3).
Here is a snippet of the code I use:

class DigitToTree(Module):
  def initialize(self):
    print("Initializing DigitToTree module.")

    self.f = TFile(OUTPUT, 'RECREATE')
    self.t = TTree('tree', 'Tree with plain data from ECLCalDigits')
    self.evtn = array('f', [0])
    self.cellID   = array('f', [0])  = array('f', [0])
    self.energy_sum = array('f', [0])
    self.time = array('f', [0])
    self.nentries = array('i', [0]) = array('i', [0])

    self.t.Branch('evtn', self.evtn, 'evtn/F')
    self.t.Branch('nentries', self.nentries, 'nentries/I')
    self.t.Branch('energy_sum', self.energy_sum, 'energy_sum/F')
    self.t.Branch('cellID', self.cellID, 'cellID[nentries]/F')
    self.t.Branch('energy',, 'energy[nentries]/F')
    self.t.Branch('time', self.time, 'time[nentries]/F')
    self.t.Branch('isCalibrated',, 'isCalibrated/I')

  def terminate(self):
    print("Finalizing DigitToTree module.")

  Convert ECLDigit data to simple ROOT Tree
  def event(self):
    Do conversion
    digits = PyStoreArray('ECLCalDigits')

    self.evtn[0] += 1
    self.energy_sum[0] = 0
    self.nentries[0] = digits.getEntries()
    print('###############')[0] = 0

    for i in range(digits.getEntries()):
      self.energy_sum[0] += digits[i].getEnergy()

    included = getEventInclusions(digits, self.evtn[0])

    for i in range(digits.getEntries()):
      if (not included[i]): continue
      self.cellID.insert(i, digits[i].getCellId()), digits[i].getEnergy())
      self.time.insert(i, digits[i].getTime())
      if (digits[i].isCalibrated()):[0] = 1
      else:[0] = 0
    del self.cellID[:]
    del self.time[:]

Branches ‘cellID’, ‘energy’ and ‘time’ are the problematic ones.
Any ideas of what went wrong?

The insert() calls require the memory held by the array objects to grow. This works by allocating a new buffer of the right size, copying over the old values, then deleting the old buffer. After insert(), the TTree thus holds a pointer to deleted memory. You’ll need to reset the branch address every time the array resizes, or make it big enough straight off the bat.

Thanks for your reply. Can you maybe show me an example for how to do that?

Creating a larger array is a simple matter of using array(‘f’, [0]*size) with ‘size’ set to the desired size. For the ROOT side, presumably using t.SetBranchAddress() should work (haven’t tried; I don’t have ROOT installed anymore).

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