DeepLearnPhysics / lartpc_mlreco3d

9 stars 32 forks source link

Fixed broken particle children count issue, and related issues. #168

Closed dkoh0207 closed 9 months ago

dkoh0207 commented 9 months ago

This PR fixes a bad initialization bug for TruthParticle.children_count.

In python classes, when a class __init__ function contains a parameter with a default mutable array-like argument, all instances of the same class will share the same reference to a single instance of the object declared for the default argument (see https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument).

As such, all particles were sharing the same children_count default array, which was initialized only once. This means when children_count was modified for one TruthParticle instance, it simultaneously modified all TruthParticles.children_counts.

Since similar initializations happen in other places, all such mutable array initializations will make a separate copy (except for tensor-like arguments such as .points and .depositions, in which it will point to the same empty array (which is ok to be shared).