@@ -114,11 +114,10 @@ def root2array(filenames,
114114 treename : str, optional (default=None)
115115 Name of the tree to convert (optional if each file contains exactly one
116116 tree).
117- branches : list of str, optional (default=None)
118- List of branch names to include as columns of the array. If None or
119- empty then include all branches than can be converted in the first
120- tree. If branches contains duplicate branches, only the first one is
121- used.
117+ branches : list of strings or single string, optional (default=None)
118+ List of branch names to include as columns of the array or a single
119+ branch name to convert into a one-dimensional array. If None then
120+ include all branches that can be converted.
122121 selection : str, optional (default=None)
123122 Only include entries fulfilling this condition.
124123 start, stop, step: int, optional (default=None)
@@ -189,16 +188,27 @@ def root2array(filenames,
189188 elif not trees :
190189 raise IOError (
191190 "no trees present in {0}" .format (filenames [0 ]))
192- else :
193- treename = trees [0 ]
191+ treename = trees [0 ]
192+
193+ if isinstance (branches , string_types ):
194+ # single branch selected
195+ branches = [branches ]
196+ flatten = True
197+ else :
198+ flatten = False
194199
195- return _librootnumpy .root2array_fromFname (
200+ arr = _librootnumpy .root2array_fromFname (
196201 filenames , treename , branches ,
197202 selection ,
198203 start , stop , step ,
199204 include_weight ,
200205 weight_name )
201206
207+ if flatten :
208+ # select single column
209+ return arr [branches [0 ]]
210+ return arr
211+
202212
203213def root2rec (filenames ,
204214 treename = None ,
@@ -245,11 +255,10 @@ def tree2array(tree,
245255 ----------
246256 tree : ROOT TTree instance
247257 The ROOT TTree to convert into an array.
248- branches : list of str, optional (default=None)
249- List of branch names to include as columns of the array. If None or
250- empty then include all branches than can be converted in the first
251- tree. If branches contains duplicate branches, only the first one is
252- used.
258+ branches : list of strings or single string, optional (default=None)
259+ List of branch names to include as columns of the array or a single
260+ branch name to convert into a one-dimensional array. If None then
261+ include all branches that can be converted.
253262 selection : str, optional (default=None)
254263 Only include entries fulfilling this condition.
255264 start, stop, step: int, optional (default=None)
@@ -271,13 +280,24 @@ def tree2array(tree,
271280 import ROOT
272281 if not isinstance (tree , ROOT .TTree ):
273282 raise TypeError ("tree must be a ROOT.TTree" )
274- # will need AsCapsule for Python 3
275283 cobj = ROOT .AsCObject (tree )
284+
285+ if isinstance (branches , string_types ):
286+ # single branch selected
287+ branches = [branches ]
288+ flatten = True
289+ else :
290+ flatten = False
291+
276292 arr = _librootnumpy .root2array_fromCObj (
277293 cobj , branches , selection ,
278294 start , stop , step ,
279295 include_weight ,
280296 weight_name )
297+
298+ if flatten :
299+ # select single column
300+ return arr [branches [0 ]]
281301 return arr
282302
283303
0 commit comments