40 likes | 152 Views
LHCb Core Software Meeting. Containers in GaudiPython E. Rodrigues, NIKHEF. Access to Gaudi containers not natural/simple in Python. The Standard Way. import gaudimodule gaudi = gaudimodule.AppMgr() EVT = gaudi.evtsvc(). >>> hits = EVT[ ‘Event/MC/OT/Hits’ ] >>> type(hits)
E N D
LHCb Core Software Meeting Containers in GaudiPython E. Rodrigues, NIKHEF LHCb Core Software Meeting, 18 Jan. 2006
Access to Gaudi containers not natural/simple in Python The Standard Way import gaudimodule gaudi = gaudimodule.AppMgr() EVT = gaudi.evtsvc() >>> hits = EVT[ ‘Event/MC/OT/Hits’ ] >>> type(hits) <class 'gaudimodule.KeyedContainer<MCHit,Containers::KeyedObjectManager<Containers::hashmap> >'> >>> for i in range( hits.size() ): ... print hits.containedObject(i) # simple conversion container Python list def toList( container ) : if container == None : return [] return [ container.containedObject(i) for i in range( container.size() ) ] >>> lhits = toList( hits ) >>> type(lhits) <type ‘list’> >>> for hit in lhits: ... print hit This would be preferred … … and is widely used in practice … LHCb Core Software Meeting, 18 Jan. 2006
Some possibilities … • Let’s agree for a second that we want to deal with containers naturally in (Gaudi)Python, i.e. as with lists or tuples • 1st option: every user drags “toList” definitions everywhere – not great • 2nd option: define a “toList” function centrally, say in gaudimodule.py • – one still has to call “toList” everywhere • 3rd option: incorporate the conversion in GaudiPython itself – my preference import gaudimodule gaudi = gaudimodule.AppMgr() LEVT = gaudi.levtsvc() IMAGINE A NEW SERVICE >>> hits = LEVT[ ‘Event/MC/OT/Hits’ ] >>> type(hits) >>> <type ‘list’> This is possible … LHCb Core Software Meeting, 18 Jan. 2006
Simple extension to gaudimodule.py class iListDataSvc( iDataSvc ) : def __getitem__( self, path ) : container = iDataSvc.__getitem__( self, path ) if container == None : return [] return [ container.containedObjects(i) for i in range( container.size() ) ] # inside the AppMgr class! class AppMgr( iService ) : # … def levtsvc( self ) : svc = Helper.service( self._svcloc, 'EventDataSvc' ) return iListDataSvc(name, svc) # … levtSvc = levtsvc PROPOSAL BTW: also works for the detector data service with the replacement ‘EventDataSvc’ by ‘DetectorDataSvc’ LHCb Core Software Meeting, 18 Jan. 2006