Usrmed changing direction cosine


I’m trying to setup a simple geometry where a photon beam, initially collimated gets diffracted after passind an aperture. For doing so I’m changing the direction cosine when passing between two regions using the subroutine USRMED. I generate a randomly an angle between 0 and PI every time a photon crosses a boundary and I assign the new direction cosines to the photon (top simulate the diffraction).
I see the photon changing direction but not as expected: the distribution is not homogenous. I am most probably making a very silly mistake or I misunderstood how to use the direction cosines, but it would be great to have some suggestion.
I attach the input file and the usrmed subroutine I am using.
thanks in advance for your help,

diffraction.f (4.1 KB)
NPty.inp (1.5 KB)

Dear @silviacipi,

It seems to me your routine is working as expected. You obtain an angular spread of your photons in the XZ plane while the direction remains the same in the YZ plane. Perhaps you should try with a finer binning like I did: it’s much easier to see the change in direction (see attachments).