Skip to content
This repository has been archived by the owner on Jan 7, 2023. It is now read-only.

Append to existing tree in array2root #166

Merged
merged 1 commit into from
Jan 18, 2015
Merged

Conversation

ibab
Copy link
Contributor

@ibab ibab commented Jan 18, 2015

Hi,

while working on a pandas wrapper for root_numpy (https://github.com/ibab/root_pandas) I noticed that array2root doesn't properly append to existing TTrees: the file just gets larger and larger, but only the last saved version of the tree is accessible.

import numpy as np
from root_numpy import root2array, array2root

arr = np.array(np.array([1,2,3]), dtype=[('test', '<i8')])
array2root(arr, 'test.root', 'default')
array2root(arr, 'test.root', 'default')

x = root2array('test.root', 'default')
print(x)
# prints [1,2,3] instead of [1,2,3,1,2,3]

As far as I can tell, this can be fixed by looking for an existing tree and passing it to array2tree.

What do you think?

@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling ec92275 on ibab:master into ecdda0c on rootpy:master.

@kratsg
Copy link
Collaborator

kratsg commented Jan 18, 2015

I believe array2root is a single output file, when you run it twice, it
creates two outputs, the second overwriting the first. I've never used it
for streaming purposes.
On Sun, Jan 18, 2015 at 17:08 Coveralls notifications@github.com wrote:

[image: Coverage Status] https://coveralls.io/builds/1764223

Coverage remained the same when pulling ec92275
ec92275
on ibab:master
into ecdda0c
ecdda0c
on rootpy:master
.


Reply to this email directly or view it on GitHub
#166 (comment).

@ibab
Copy link
Contributor Author

ibab commented Jan 18, 2015

It depends on the mode you pass to array2root.
The default is 'update', which allows you to append to the file.
The file is overwritten when passing mode='recreate'.

@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

A wrapper for pandas! Nice!

Yes, array2root just writes out a file and will overwrite an existing file (the mode keyword argument can be used to update an existing file, but a tree with the same name would just be "overwritten").

Use array2tree instead of array2root. array2tree will extend an existing tree:

315 def array2tree(arr, name='tree', tree=None):
316     """Convert a numpy structured array into a ROOT TTree.
317 
318     .. warning::
319        This function is experimental. Please report problems.
320        Not all data types are supported (``np.object`` and ``np.float16``).
321 
322     Parameters
323     ----------
324     arr : array
325         A numpy structured array
326     name : str (optional, default='tree')
327         Name of the created ROOT TTree if ``tree`` is None.
328     tree : existing ROOT TTree (optional, default=None)
329         Any branch with the same name as a field in the
330         numpy array will be extended as long as the types are compatible,
331         otherwise a TypeError is raised. New branches will be created
332         and filled for all new fields.

@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

Although, this would be a nice feature to add. I think we should enable growing an existing TTree with array2root also. But for now, array2tree should do the job.

@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

Checking the diff and will merge.

@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

@ibab what version of Cython did you run? 0.21.2?

@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

(Sorry, originally thought this was an issue report, but then realised it is a pull request!)

@ibab
Copy link
Contributor Author

ibab commented Jan 18, 2015

I think I ran 0.21, but I could rerun with 0.21.2.

@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

Great, yes better to use the latest. Then I will merge.

@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling 3ac7936 on ibab:master into ecdda0c on rootpy:master.

@ibab
Copy link
Contributor Author

ibab commented Jan 18, 2015

Ok, I ran 0.21.2 and amended the commit.

ndawe added a commit that referenced this pull request Jan 18, 2015
Append to existing tree in array2root
@ndawe ndawe merged commit 2232c43 into scikit-hep:master Jan 18, 2015
@ndawe
Copy link
Member

ndawe commented Jan 18, 2015

thanks!

@ibab
Copy link
Contributor Author

ibab commented Jan 18, 2015

Thanks! That will make stream processing of ROOT files very convenient.
And I can use array2tree in my wrapper until the next version of root_numpy is released.

@ndawe ndawe mentioned this pull request Jan 19, 2015
@ndawe
Copy link
Member

ndawe commented Jan 19, 2015

3.4.0 is released: https://pypi.python.org/pypi/root_numpy

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants