Re: [Gaudi-talk] PyRoot, Reflex, STL iterators and __deref__()

From: Pere Mato <pere.mato_at_cern.ch>
Date: Tue, 9 Dec 2008 21:45:03 +0100


Hi Brett,

   For what concerns STL classes the Python bindings provider by PyROOT should automatically provide Python iteration functionality (__iter__) if the STL iterators are found in the Reflex dictionaries. For what concerns the HepMC there was an issue. I am attaching the answer from Vanya to somebody that asked the same question:

   Cheers,

                Pere

On Sep 25, 2008, at 12:22 PM, Ivan Belyaev wrote:

> Dear Neal,
>
> You can get much more easier way for manipulation with HepMC-
> structures:
>
>
> from LoKiGen.HepMC import *
> from LoKiCore.function import *
>
>
>
> hepmcparticle = ...
> hepmcvertex = ...
>
> # loop over all 'in'-particles for the given vertex
>
> for p in particles_in ( hepmcvertex ) : print pname( p )
>
> # loop over all 'out'-particles for the given vertex:
>
> for p in particles_out ( hepmcvertex ) : print pname ( p )
>
>
> # loop over all 'parents' for the given hepmc vertex:
>
> for p in parents ( hepmcvertex ) : print pname ( p )
>
> # loop over all children for hep mc vertex or hepmc particle:
>
> for p in children ( hepmcparticle ) : print pname ( p )
>
> for p in children ( hepmcvertex ) : print pname ( p )
>
>
> # loop over all 'family' particles for the given vertex:
>
> for p in family ( hepmcvertex ) : print pname ( p )
>
>
> # loop over all 'ancesotrs' for the given particle or vertex:
>
> for p in ancestors ( hepmcparticle ) : ...
>
> for p in ancestors ( hepmcvertex ) : ...
>
> the similar method exists for 'descendants' and 'relatives'.
>
>
> Also oen can get the children by number:
>
>
> # get the first child (index starts from 1)
>
> child1 = child ( hepmcparticle , 1 )
>
>
> etc...
>
>
> you can try to see help(LoKiGen.HepMC) and help(LoKiCore.functions)
>
>
> cheers, Vanya
>
>
>
> -----Original Message-----
> From: Neal Gueissaz [mailto:neal.gueissaz_at_epfl.ch]
> Sent: Thursday, September 25, 2008 12:08 PM
> To: lhcb-davinci
> Subject: python : how to loop in GenVertex outgoing particles ?
>
> Dear Gaudipython experts,
>
> I am stuyding properties of GenParticles from the HepMC container in a
> python script.
> From the excellent tutorial of Thomas I managed to loop on events,
> then
> on particles in this event.
> But trying to loop on the outgoing particles of a GenVertex doesn't
> work
> :
> return type not handled (using void):
> __gnu_cxx::__normal_iterator<HepMC::GenParticle*
> const*,vector<HepMC::GenParticle*> >
> return type not handled (using void):
> __gnu_cxx::__normal_iterator<HepMC::GenParticle*
> const*,vector<HepMC::GenParticle*> >
>
> genevtcont = evt['Gen/HepMCEvents']
> for genevt in genevtcont:
> pgenevt = genevt.pGenEvt()
> for genp in irange(pgenevt.particles_begin(),
> pgenevt.particles_end()):
> if( genp.end_vertex() is None ): continue
> vtx = genp.end_vertex()
> print 'Pos vtx', vtx.position().z()
> for dau in irange( vtx.particles_out_const_begin(),
> <---- does not work here
>
> vtx.particles_out_const_end()
> ):
> print 'daughter', dau.momentum().eta()
>
> I defined class irange :
> class irange(object) :
> def __init__(self, b, e ):
> self.begin, self.end = b, e
> def __iter__(self):
> it = self.begin
> while it != self.end :
> yield it.__deref__()
> it.__postinc__(1)
>
> It perfectly works with HepMC v1.* (DaVinci v19r14)... but not with
> HepMCv2.* (DaVinci v20r0).
> iterators on outgoing particles are still the same...
> Full python test scrip is attached.
> Does anyone has an idea where the problem is ?
>
> Cheers,
>
> Neal
>
>
> --
> _______________________________________________
> Neal Gueissaz
> Laboratoire de Physique des Hautes Energies
> EPFL-SB-IPEP-LPHE1
> Bureau BSP 617
> CH-1015 Lausanne
> Phone: +41 21 693 04 97
> E-mail neal.gueissaz_at_epfl.ch
> _______________________________________________
>



Pere Mato CERN, PH Department, CH 1211 Geneva 23, Switzerland
           e-mail: pere.mato_at_cern.ch      tel: +41 22 76 78696
           fax:  +41 22 76 68792            gsm: +41 76 48 70855




On Dec 9, 2008, at 1:45 AM, Brett Viren wrote:

> I am trying to understand how to access STL iterators from Python.
> The classes in question are generated via GaudiObjDesc and use Reflex
> for their dictionaries.
>
> In particular I would like to know how to dereference an iterator to
> get at its underlying object. I believe the Python wrappers for the
> STL iterators, as generated by Reflex, are missing the __deref__()
> method.
>
> The reason is that I look at Relax's dictionaries for HepMC and I see
> that the particle iterators (for example) do have the __deref__()
> method. With this method I can access the underlying object. If it
> matters, these iterators inherit from STL ones and are not direct STL
> classes.
>
> Does anyone have any hints as to how I can dereference regular STL
> iterators from Python?
>
> We are currently using ROOT 5.18/00.
>
> Thanks,
> -Brett.
> _______________________________________________
> Gaudi-talk mailing list
> Gaudi-talk_at_lists.bnl.gov
> https://lists.bnl.gov/mailman/listinfo/gaudi-talk



Pere Mato CERN, PH Department, CH 1211 Geneva 23, Switzerland
           e-mail: pere.mato_at_cern.ch      tel: +41 22 76 78696
           fax:  +41 22 76 68792            gsm: +41 76 48 70855
Received on Tue Dec 09 2008 - 21:45:17 CET

This archive was generated by hypermail 2.2.0 : Wed Dec 10 2008 - 11:50:01 CET