Moving a Branch containing a 64 elements buffer into a numpy list

Hello everyone, I’m trying to make a gif of a 64 channels SiPM matrix using PyROOT.
I’m almost a newbie.
I have a ROOT file containing a Tree called “thk” with different Branches as shown below:

******************************************************************************
*Tree    :thk       : HK packet tree                                         *
*Entries :       25 : Total =            9677 bytes  File  Size =       1589 *
*        :          : Tree compression factor =   7.74                       *
******************************************************************************
*Br    0 :cpu_packet_time : time/I                                           *
*Entries :       25 : Total  Size=        662 bytes  File Size  =        161 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.12     *
*............................................................................*
*Br    1 :photodiode_data : photodiode_data[4]/F                             *
*Entries :       25 : Total  Size=       1001 bytes  File Size  =        146 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   3.29     *
*............................................................................*
*Br    2 :sipm_data : sipm_data[64]/F                                        *
*Entries :       25 : Total  Size=       6973 bytes  File Size  =        540 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=  11.99     *
*............................................................................*
*Br    3 :sipm_single : sipm_single/F                                        *
*Entries :       25 : Total  Size=        675 bytes  File Size  =         98 *
*Baskets :        1 : Basket Size=      32000 bytes  Compression=   1.81     *
*............................................................................*

Actually I’m making a chain of different ROOT files sorted into a folder and here come all my problems.
I already read the PyROOT tutorials on how to fill a numpy array from a Tree or a Branch without any
success.
Let me explain better what i would like to do: I have 25 events, corresponding to different times, each containing a 64 float elements buffer called sipm_data[]. Let’s say the chain finally will contain 80 events, I would like to fill a numpy list/matrix made up of 80 rows and 2 columns in this way:

data = [cpu_packet_time ] [sipm_data[] (64 sipm channel)]

This is my code:

import sys
a=r'/user/root/bin'
sys.path.append(a)


from ROOT import gStyle
import ROOT as XROOT
import numpy as np
import os 
from ROOT import TChain, TSelector, TTree
from array import array
from ROOT import TCanvas, TPad, TFile, TPaveText
from ROOT import gBenchmark, gROOT
import argparse

path = sys.argv[1]

folder=path+'sipm_gif/'
if not os.path.exists(folder):
	os.makedirs(folder)

HKchain=TChain("thk");


for filename in sorted(os.listdir(path)):
	os.chdir(path)
	if filename.endswith('.root') and("CPU_RUN_MAIN"  in filename) :
		print filename
		HKchain.Add(filename)

nEntries = HKchain.GetEntries()   
print nEntries

HKchain.GetEntry(0)     
sipm_t0=HKchain.cpu_packet_time
print sipm_t0
HKchain.GetEntry(nEntries-1)  
sipm_tlast=HKchain.time
print sipm_tlast

sipm=[]
sipm_all=[]
nSIPM=64
#c=0
for a in range(0,nEntries-1):
	#print HKchain.sipm_data
	#c=c+1
	for b in range(0,nSIPM-1):
		sipm= np.array(np.frombuffer(HKchain.sipm_data, dtype=np.uint8))
	#print sipm
	sipm_all.append(sipm)
#print sipm_all
#print c

So far, i was only able to make just a list with shape (80,64) in which, the first column doesn’t
show times but just a counter, id est 0,1,2,3…n
Furthermore there are problems with data: all the 80 elements show the same 64 counts and they are completely different from any contained in the ROOT branch.
Moreover putting np.float64 instead np.uint8 when filling the numpy array lead to strange results.

I’m starting to read something about python pandas library, maybe it could fit but I don’t have a clear idea right now.
Any help would be very apprecciated!
Thanks in advance.

Hi,

The first issue I see is that you set the chain to the last entry here:

HKchain.GetEntry(nEntries-1)  

but then in the loop you do not move the cursor of the chain anymore, so when you do HKchain.sipm_data you will always get it for the last entry of the chain.

You can construct the loop like this to make sure you move the cursor:

for event in HKchain:

Inside the loop, you need to:

sipm_data_for_this_event = event.sipm_data
sipm_data_for_this_event.SetSize(64)  # this might be needed to ensure the buffer knows its size

After this, you need to copy sipm_data_for_this_event into a numpy array, since in the next iteration the buffer will be overwritten with the contents of the next event for that branch.

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