Sample angular distributions from a histogram with respect to a custom coordinate system

Dear @udo.friman-gayer,

Many thanks for your hints and the clear explanation! Now, I really see that the initial approach does not work as I expected.

Actually, after the last run I got a very strange result which absolutely doesn’t match the beam direction I thought I was setting:

The beam direction is mostly aligned with the “traditional” z-axis.
So, I’m going to try the first approach from the previously mentioned post and specify in my case:

      subroutine SOURCE ( nomore )

      use source_library
      use source_variables

      implicit none
      include 'tetgcm.inc'

      double precision mu
      double precision r
      double precision sinph
      double precision cosph
*     .............................................
      mu = sample_histogram_file("theta.txt", 1.D0)
*     .............................................

where the cos\theta = cos\gamma = \mu is the z-axis directional cosine, and than since
cos\alpha = \cos\phi \cdot \sqrt{1 - \cos^{2}\theta} = r \cdot \cos\phi and
cos\beta = \sin\phi \cdot \sqrt{1 - \cos^{2}\theta} = r \cdot \sin\phi

*     ..............................................
      call SFECFE(sinph, cosph)
      r = sqrt(max(0.D0, 1.D0 - mu * mu))
      direction_cosx = r * cosph
      direction_cosy = r * sinph
      direction_cosz = mu

I will see the results only tomorrow after running the simulation over the night, but I’m still slightly confused how to implement and apply a rotation matrix to the (cos\alpha, cos\beta, cos\gamma) directional vector in the source_newgen.f user routine. I mean should I do this manually or there’s something pre-defined? Could anybody suggest anything about that please?

Cheers,
Ihor