Filling a tree with an histo or an array

54 views
Skip to first unread message

Flo:

unread,
Oct 8, 2013, 8:11:09 AM10/8/13
to rootpy...@googlegroups.com
Hello,
        first thanks for the work done! that's appreciated.

I have some questions, though. I try to build a Tree to store datas.
To do so I would like to make a branch with float, string and arrays (or histos). But from these examples, I didn't find anyway to proceed with arrays...
I tried to use FloatArrayCol() but didn't manage to find the correct syntax...

Do I use a correct way to proceed? Is there anybody who can help?

Many thanks

Flo




Noel Dawe

unread,
Oct 8, 2013, 9:06:47 AM10/8/13
to
Hi Flo,

This was an oversight. Sorry for the trouble!

Defining branches of fixed-length arrays is now possible after some very minor changes that I just merged in. So just update your copy of rootpy and the code shown here will work:


Specifically, define the array branch in the model like this:

# define the model
class Event(TreeModel):

    x = FloatCol()
    y = FloatCol()
    z = FloatCol()
    # support for fixed-length arrays
    f = FloatArrayCol(5)
    i = IntCol()

Note that you must pass the length of the array. Support for variable-length arrays is not yet implemented. For that, I would recommend using a vector as done here:


Cheers!
Noel

Noel Dawe

unread,
Oct 9, 2013, 9:00:00 AM10/9/13
to Flo:, rootpy...@googlegroups.com
Hi Florian,

To create a branch of strings use CharArrayCol(n) where n is the length including the null termination. CharArrayCol(1) will raise a ValueError since there is no room for the null termination. Also, CharCol will actually make the same thing as CharArrayCol with length 2 where the second byte is the null byte.

I just fixed a serious bug in how rootpy treated char branches. It wasn't guaranteeing a null-termination and the error you found is from a buggy attempt to convert your string to an int (it should have used the built-in ord() function).

While fixing this bug I redesigned the char types to inherit from Python's built-in bytearray instead of array.array. This allows for a more transparent treatment of a CharArray like a python string.

I've updated the example to include a CharCol and CharArrayCol. See here:


Now, in the event loop you can do:

tree.string = 'abcd'
tree.string = 'ab'  # can set with a shorter string than the declared length, and remaining bytes will be zeroed out.
tree.s = 'a'

and this would result in a ValueError:

tree.string = 'abcde'

ValueError: string of length 5 is too long to fit in array of length 5 with null-termination

since it forces you to keep the last byte for the null termination.

I hope this helps, and thanks for reporting this!

Cheers,
Noel





On Wed, Oct 9, 2013 at 12:56 AM, Flo: <floria...@gmail.com> wrote:
Great thank you, it works fine!

if i may ask another question. When I use a variable with "CharCol", and fill it with a string, it returns an error:

for example:

from rootpy.tree import Tree, TreeModel, FloatCol, IntCol, FloatArrayCol, CharCol
from rootpy.io import root_open
from random import gauss
import numpy as np



f
= root_open("test.root", "recreate")


# define the model
class Event(TreeModel):


    x
= CharCol()

    y
= FloatCol()
    z
= FloatCol()
   
# support for fixed-length arrays
    f
= FloatArrayCol(5)
    i
= IntCol()


tree
= Tree("test", model=Event)

# fill the tree
for i in xrange(100):
    tree
.x = 'blibli'
    tree
.y = gauss(.3, 2.)
    tree
.z = gauss(13., 42.)
    tree
.f = np.linspace(0,10,5)
    tree
.i = i
    tree
.fill()
tree
.write()

f
.close()

the error:

  File "test2.py", line 24, in <module>
    tree
.x = 'blibli'
 
File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.1-py2.7.egg/rootpy/tree/tree.py", line 445, in __setattr__
   
return self._buffer.__setattr__(attr, value)
 
File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.1-py2.7.egg/rootpy/tree/treebuffer.py", line 189, in __setattr__
    variable
.set(value)
 
File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.1-py2.7.egg/rootpy/tree/treetypes.py", line 104, in set
   
self[0] = self.convert(value)
 
File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.1-py2.7.egg/rootpy/tree/treetypes.py", line 316, in convert
   
return int(value)
ValueError: invalid literal for int() with base 10: 'blibli'



Or maybe I do something wrong?

Florian

Flo:

unread,
Oct 10, 2013, 4:12:51 AM10/10/13
to rootpy...@googlegroups.com, Flo:
Hi Noel,
            Many thanks! it works really nice, and yes it helps a lot!
Cheers,
Flo
Reply all
Reply to author
Forward
0 new messages