distinguish objects when closest hit program occured

No, that’s not going to find all intersections along a ray.

If you reach the closest hit program there is no further action on that specific ray.
It will return to the next statement after the calling rtTrace().

There was an explanation about the anyhit program just two days ago:
[url]https://devtalk.nvidia.com/default/topic/1050973/optix/when-the-rtpotentialintersection-t-return-true-is-t-the-any-hit-or-must-closest-hit/[/url]

Then there are some older threads about finding all intersections along a ray.
[url]https://devtalk.nvidia.com/default/topic/767646/optix/how-to-get-primitive-geometry-id/post/4285701/#4285701[/url]
[url]https://devtalk.nvidia.com/default/topic/923772/optix/query-on-optix-baking-texture-data-storage-basic-query-/post/4835978/#4835978[/url]
[url]https://devtalk.nvidia.com/default/topic/930666/optix/radiation-physics-problems/post/4857707[/url]

I would start implementing the second approach in the first thread. That’s super simple and should work with any acceleration structure. That’s also going to be very fast on an RTX board when using OptiX 6 with GeometryTriangles and a ray type without an anyhit program.