question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Particle.pdgid should return int

See original GitHub issue

Context: I use Numba a lot, so it would be great if particle would work with Numba directly, but I guess that this is probably a more elaborate project. In the meantime as a workaround, I extract information from particle and store that in Python lists and dicts, which I then pass to the numba-compiled functions.

The attribute Particle.pdgid is of type particle.pdgid.pdgid.PDGID instead of int, which leads to unexpected issues. I argue it would be better to return a plain int. The type particle.pdgid.pdgid.PDGID emulates a plain int, so you won’t notice the difference much in pure Python, but it is a different type nonetheless and Numba does not understand this type. If I prepare a list or dict of PDG IDs to pass to Numba, I need to call int(Particle.pdgid) explicitly, which is doable but irritating.

I argue that there is no benefit of Particle.pdgid being particle.pdgid.pdgid.PDGID here, because the extra information that can be queried from particle.pdgid.pdgid.PDGID is can already be queried from Particle.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:32 (29 by maintainers)

github_iconTop GitHub Comments

1reaction
henryiiicommented, Aug 29, 2020

PS: int(p) produces an int, and PDGID(p) produces a PDGID. p.pdgid is just a shortcut for PDGID(p).

Second reminder: you can also make Particles that do not have a valid PDGID by specifying all the properties. int(p) may fail.

1reaction
henryiiicommented, Aug 29, 2020

Perhaps you heard somewhere that composition is better than inheritance

This was partially a historical result, rather than a conscious design. @eduardo-rodrigues mostly wrote PDGID, and I mostly wrote Particle. PDGID did not exist when Particle was designed, so it would be quite odd for Particle to inherit from it.

A 1.0 redesign could possibly include larger changes, and possibly include moving the particle search functionality out of Particle and into a Particles/ParticleTable class (have not thought about it at this point, maybe @eduardo-rodrigues has). Another option would be to reduce the correspondence between PDGID’s and Particles, and instead treat pdgid as one of many possible representations, removing __int__().

PS: I really don’t like APIs like “make a singleton-like class then use it” for a library supporting beginners, but we might be able to provide beginner friendly options and a “default” particle table.

The defense of the current design would be that the intent of int(Particle(...)) is clear - you are asking for a int representation of the current particle, and that by far most likely to be a PDG ID int (see option two, though, which might remove this). But a particle is not an int; it just has a explicit conversion to an int. abs(Particle(...)) fails (or should return a Particle, possibly?). Particle(...) + 1 has no meaning, since not all particles exist. PDGID() + 1 does, since it’s just an int representing the ID of a particle. (PDGID(...) + 1).has_charm is a perfectly valid question. PDGID is valid for all possible ints, while Particle is a lookup-based object; the really do not have anything in common. Particle is not supposed to duck type as an int, while PDGID does (since it is an int). While PDGID is an int, but also has further information beyond an int.

Again, the real problem is that Numba is picky, and doesn’t treat subclasses of ints as ints like normal Python does. But we could probably easily teach it otherwise (maybe even teaching it a few details about PDGIDs, in fact) if there’s a strong enough desire for it.

PS: @eduardo-rodrigues , if you are interested in enabling all PDGID’s methods on particle, but without turning Particles also into ints and causing Particle() + 1 to “work” but make an invalid particle, we can explore this; I have a simple idea for how this could be done.

Read more comments on GitHub >

github_iconTop Results From Across the Web

particle/functions.py at master · scikit-hep/particle - pdgid
Package to deal with particles, the PDG particle data table, PDGIDs, etc. ... particle/src/particle/pdgid/functions.py ... return abs(int(pdgid)).
Read more >
VertexCompositeAnalysis < Sandbox < TWiki
A class for daughter particle storage. pdgId(). Return PDG ID of the particle. charge(). Return charge of the specie of particle.
Read more >
Particle Properties
If there are no such carbon copies, the index of the particle itself will be returned. A carbon copy is when the "same"...
Read more >
StarParticleData Class Reference - StRoot
TParticlePDG *, SetTrackingCode (const int pdgid, const int g3id). Maps the particle with the given PDG id to G3 tracking ID and returns...
Read more >
Rivet::Particle Class Reference - Hepforge
Particle (PdgId pid, const FourMomentum &mom, const FourVector &pos=FourVector(), ... The returned vector will be empty if this particle is non-composite, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found