Hi,
I am trying to convert the content of a bunch of txt files to a root tree.
Each of the txt files corresponds to one event and just contains two columns, time and amplitude:
1.800015e-05, 4.000000e-03
1.801015e-05, 1.200000e-02
1.802015e-05, 8.000000e-03
1.803015e-05, 8.000000e-03
1.804015e-05, 8.000000e-03
1.805015e-05, 4.000000e-03
1.806015e-05, 4.000000e-03
1.807015e-05, 4.000000e-03
1.808015e-05, 8.000000e-03
1.809015e-05, 8.000000e-03
…
I want to fill these wave forms into a tree. This is my code:
rfile = root.TFile.Open("tree.root", "RECREATE")
tree = root.TTree("tree","tree")
wf_length = array('i',[0])
wf_time = array('d', 10000*[0.])
wf_ampl = array('d', 10000*[0.])
tree.Branch('wf_length', wf_length, 'wf_length/I')
tree.Branch('wf_time', wf_time, 'wf_time[wf_length]/D')
tree.Branch('wf_ampl', wf_ampl, 'wf_ampl[wf_length]/D')
for f in file_list:
f = args.inpath + f
v = np.loadtxt(f,unpack=True, comments="#",skiprows=0,delimiter=',')
wf_length[0] = len(v[0])
# does not work
#wf_time = v[0]
#wf_ampl = v[1]
# does not work
#wf_time = array('d',v[0])
#wf_ampl = array('d',v[1])
# this works
for i in range(wf_length[0]):
wf_time[i] = v[0][i]
wf_ampl[i] = v[1][i]
tree.Fill()
rfile.Write("",root.TFile.kOverwrite)
rfile.Close()
As you can see, I manage to accomplish my goal, but I would have assumed that tone of the two options with are commented “does not work” would give the same results.
I would appreciate some suggestions.
I am using:
ROOT Version: 6.24/04
Built for linuxx8664gcc on Aug 25 2021, 12:59:28
From tags/v6-24-04@v6-24-04
Python 2.7.16