7
7
_rootBranchType2PythonArray = { 'b' :'B' , 'B' :'b' , 'i' :'I' , 'I' :'i' , 'F' :'f' , 'D' :'d' , 'l' :'L' , 'L' :'l' , 'O' :'B' }
8
8
9
9
class OutputBranch :
10
- def __init__ (self , tree , name , rootBranchType , n = 1 , lenVar = None , title = None ):
10
+ def __init__ (self , tree , name , rootBranchType , n = 1 , lenVar = None , title = None , limitedPrecision = False ):
11
11
n = int (n )
12
12
self .buff = array (_rootBranchType2PythonArray [rootBranchType ], n * [0. if rootBranchType in 'FD' else 0 ])
13
13
self .lenVar = lenVar
14
14
self .n = n
15
+ self .precision = ROOT .ReduceMantissaToNbitsRounding (limitedPrecision ) if limitedPrecision and rootBranchType == 'F' else lambda x : x
15
16
#check if a branch was already there
16
17
existingBranch = tree .GetBranch (name )
17
18
if (existingBranch ):
@@ -31,9 +32,9 @@ def fill(self, val):
31
32
if len (self .buff ) < len (val ): # realloc
32
33
self .buff = array (self .buff .typecode , max (len (val ),2 * len (self .buff ))* [0. if self .buff .typecode in 'fd' else 0 ])
33
34
self .branch .SetAddress (self .buff )
34
- for i ,v in enumerate (val ): self .buff [i ] = v
35
+ for i ,v in enumerate (val ): self .buff [i ] = self . precision ( v )
35
36
elif self .n == 1 :
36
- self .buff [0 ] = val
37
+ self .buff [0 ] = self . precision ( val )
37
38
else :
38
39
if len (val ) != self .n : raise RuntimeError ("Mismatch in filling branch %s of fixed length %d with %d values (%s)" % (self .Branch .GetName (),self .n ,len (val ),val ))
39
40
for i ,v in enumerate (val ): self .buff [i ] = v
@@ -44,10 +45,10 @@ def __init__(self, tfile, ttree, intree):
44
45
self ._tree = ttree
45
46
self ._intree = intree
46
47
self ._branches = {}
47
- def branch (self , name , rootBranchType , n = 1 , lenVar = None , title = None ):
48
+ def branch (self , name , rootBranchType , n = 1 , lenVar = None , title = None , limitedPrecision = False ):
48
49
if (lenVar != None ) and (lenVar not in self ._branches ): #and (not self._tree.GetBranch(lenVar)):
49
50
self ._branches [lenVar ] = OutputBranch (self ._tree , lenVar , "i" )
50
- self ._branches [name ] = OutputBranch (self ._tree , name , rootBranchType , n = n , lenVar = lenVar , title = title )
51
+ self ._branches [name ] = OutputBranch (self ._tree , name , rootBranchType , n = n , lenVar = lenVar , title = title , limitedPrecision = limitedPrecision )
51
52
return self ._branches [name ]
52
53
def fillBranch (self , name , val ):
53
54
br = self ._branches [name ]
0 commit comments