Hi Sebastien, A Dijous 06 Juliol 2006 07:07, Sebastien Binet va escriure: > Hi there, > > I noticed the PyTables conference at CERN and I was wondering:
Yeah, we attended the EuroPython conference last week. I'll post details on
how to access to an improved version of the talk (with some additional
explanations that ease the reading for non-interactive readers).
> from the examples of the PyTables package, one can easily figure out
> how to define a nested structure of structures of... etc...
>
> But is there a way to define a column of user tables.IsDescription ?
> Something like that:
>
> class Vertex(IsDescription):
> position = FloatCol( dflt=0., shape=3 )
>
> class Particle(IsDescription):
> fourMomentum = FloatCol( dflt=0., shape=4 )
> idCode = IntCol( dflt=0, shape=1)
> productionVtx= Vertex()
> decayVtx = Vertex()
First, I'd recommend to skip this declaration for Vertex. By doing so, you are
introducing a unnecessary nested field in your table declaration. Instead,
I'd go with something like:
class Particle(IsDescription):
fourMomentum = FloatCol( dflt=0., shape=4 )
idCode = IntCol( dflt=0, shape=1 )
productionVtx= FloatCol( dflt=0., shape=3 )
decayVtx = FloatCol( dflt=0., shape=3 )
This is more simple to read and you don't have to introduce nested records.
>
> class Particles(IsDescription):
> particles = MakeCol(Particle) #<== here is the tricky part
>
> class Event(IsDescription):
> electrons = Particles()
> photons = Particles()
> neutrinos = Particles()
>
> An Event is hence made from several collections of particles which are
> themselves made from a production and a decay vertices, a four-momentum
> and some barcode to distinguish between different types of particles.
>
> Of course I don't know a priori the number of electrons and photons I can
> find in each of my events...
>
> Any hint on how to model this event structure ?
Well, I'd follow the path of simplicity and feed all the information you need
right into the table. Then, you just have to select the info in the table
that you are interested in. In case that you end with redundant info in the
table, activating compression will reduce most of the duplicity on-disk
(without affecting performance a great deal: see my latest talk).
I'd recommend you studying the next example (it is also attached):
--------------------------------------------------------------------------------------------------------
# Warning!. This script need Python 2.4 because it uses generator expresions.
from math import sqrt
from tables import *
from numarray import dot
codes = {
"electron": 0,
"photon": 1,
"neutrino": 2
}
class Particle(IsDescription):
fourMomentum = FloatCol( dflt=0., shape=4 )
idCode = IntCol( dflt=0, shape=1 )
productionVtx= FloatCol( dflt=0., shape=3 )
decayVtx = FloatCol( dflt=0., shape=3 )
fileh = openFile("particles.h5", mode = "w")
table = fileh.createTable(fileh.root, 'particles', Particle,
"A table for keeping particles", Filters(1))
# Fill the table with synthetic data
particle = table.row
for i in xrange(100):
particle['fourMomentum'] = range(i,i+4)
particle['idCode'] = i % 3
particle.append()
table.flush()
# Get the row IDs for the electron particles
electronsRID = table.getWhereList(table.cols.idCode == codes["electron"])
print "Row ID for electrons-->", electronsRID
# Get the sum of energy for all neutrinos
idc = table.cols.idCode
nenergy = sum(sqrt(dot(n['fourMomentum'], n['fourMomentum']))
for n in table.where(idc == codes["neutrino"]))
print "Energy for all neutrinos-->", nenergy
fileh.close()
------------------------------------------------------------------------------------------------
If you have more doubts or questions, I'll be glad to address them.
Cheers,
--
>0,0< Francesc Altet http://www.carabos.com/
V V Cárabos Coop. V. Enjoy Data
"-"
prova.py
Description: application/python
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ Pytables-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/pytables-users
