forked from wverbeke/deepLearning
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtreeToArray.py
More file actions
45 lines (33 loc) · 1.09 KB
/
treeToArray.py
File metadata and controls
45 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import root_numpy
from ROOT import TFile
import numpy as np
def treeToArray(tree, branchList, cut = ''):
#convert tree to array of tuples
arrayTuples = root_numpy.tree2array(tree, branchList, cut)
#convert to ndarray
output_shape = ()
num_rows = len(arrayTuples)
#check if one branch name or list of branches is given
argument_is_list = not ( type(branchList) is str )
if argument_is_list :
num_columns = len(arrayTuples[0])
output_shape = (num_rows, num_columns)
else :
output_shape = (num_rows, )
retArray = np.zeros( (output_shape) )
for i, entry in enumerate(arrayTuples):
if argument_is_list:
entry = list(entry)
retArray[i] = np.asarray( entry )
else :
retArray[i] = entry
return retArray
def writeArrayToFile(array, fileName):
np.save( fileName , array)
def loadArray(fileName):
return np.load(fileName)
def listOfBranches(tree):
names = [ branch.GetName() for branch in tree.GetListOfBranches() ]
return names
if __name__ == '__main__' :
pass