Linear Collider Forum



Home » Analysis and Reconstruction » Analysis Tools » Hashing LCIO objects in pyLCIO (Does anyone know a way of using these objects as dict / hashmap keys?)
Hashing LCIO objects in pyLCIO [message #2336] Thu, 04 September 2014 08:59 Go to next message
oliverReardonSmith
Messages: 9
Registered: August 2014
Hi all,

From the tests I've done any attempt to use these objects as keys in a dict fails. Adding them as keys seems to work fine however attempting to access the dictionary element fails.

Here is a minimal example to illustrate this behaviour.
Presumably this is because the hash is based on references to the object rather than the object itself and the references change.

Currently I'm working around this with a "HashableMCParticle" python class that does implement __hash__ and __eq__ but the number of "HashableX" classes that I'm having to implement is rising rapidly.

Does anyone have a better solution?

Many thanks,

Oli

[Updated on: Thu, 04 September 2014 09:48]

Re: Hashing LCIO objects in pyLCIO [message #2337 is a reply to message #2336] Thu, 04 September 2014 16:51 Go to previous messageGo to next message
jfstrube
Messages: 64
Registered: September 2004
Location: Tohoku University
The easiest I can think of is to use the index in the collection, which you get from enumerate.

Then you would have to use collection.getElementAt(index) each time you access the particle.
That's not ideal, but you could sugar it by creating your own dict class with the collection in the constructor and an overloaded __getAttr__ or __getAttribute__ method that returns the particle rather than the index.

Without more information on why the number of HashableX classes explodes, that's the quickest way I can think of to get what you want.
It slows down the code, but maybe not noticeably so.

Re: Hashing LCIO objects in pyLCIO [message #2338 is a reply to message #2337] Fri, 05 September 2014 01:57 Go to previous messageGo to next message
oliverReardonSmith
Messages: 9
Registered: August 2014
Unfortunately I need to be able to access the object when its found in different collections. For example I'd like to create a dictionary of MCParticle -> [TrackerHit] by iterating over the relation collection but be able to access it by iterating over the MCParticle collection.

After thinking about it some more I don't think having to wrap the lcio classes in python ones is too bad (I only have five so far).

Thanks for your help though!

Edit: Actually using the wonders of reflection I only need one (highly dodgy) class to do everything I need.

[Updated on: Fri, 05 September 2014 03:29]

Re: Hashing LCIO objects in pyLCIO [message #2339 is a reply to message #2338] Fri, 05 September 2014 04:31 Go to previous messageGo to next message
jfstrube
Messages: 64
Registered: September 2004
Location: Tohoku University
If you are going down this path, the fortran bindings maybe useful.
They are basically C code with Fortran headers if I'm not mistaken. You might be able to get rather far with ctypes, or cython.

Re: Hashing LCIO objects in pyLCIO [message #2340 is a reply to message #2339] Fri, 05 September 2014 07:39 Go to previous message
oliverReardonSmith
Messages: 9
Registered: August 2014
Pure python seems to be working ok for now. I'm trying to keep everything as simple as I can at the moment. Currently the class just provides transparent access to the getters and setters of an lcio object and a hash based on a subset of those (those that return primitive, TVector2, TVector3 or TLorentzVector types).

If I need anything more complicated then I'll look into the things you suggest.
Previous Topic:Best particle track display tool for a .slcio file
Next Topic:problems installing v17-06 of ilcsoft
Goto Forum:
  

[ PDF ]

Current Time: Sun Dec 15 15:16:23 Pacific Standard Time 2019
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.1.
Copyright ©2001-2010 FUDforum Bulletin Board Software