c----------------------------------------------------------------------*
c  subroutine angle(isys,i1,i2,i3,iv,cthtv*,cthtx*,ctheta*,phi*,iflag) *
c                                                                      *
c      this routine calculates theta and phi-angles of a two-body      *
c    or three body decay in a given helicity frame defined by the      *
c    p-vector of isys.                                                 *
c                                                                      *
c             e+e- --> (j/psi) --> v + x                               *
c                                      |                               *
c                                      ---> 1 + 2 (+ 3)                *
c                                                                      *
c    convention: +x' has to lie in production plane.                   *
c    frames:                                                           *
c       xyz frame(laboratory system, j/psi rest system, e+e- c.m.s)    *
c           resonance x moving with momenta p in xyz-frame.            *
c           1, 2, (3)  moving with momenta p1,p2,(p3) in xyz-frame.    *
c       x'y'z' frame(x rest system)                                    *
c    notes:                                                            *
c         isys   :  z-vector in xyz frame (defining convention).       *
c         i1     :  no. of particle 1, in 4-vector in xyz frame.       *
c         i2     :  no. of particle 2, in 4-vector in xyz frame.       *
c         i3     :  no. of particle 3, in 4-vector in xyz frame.       *
c         iv     :  no. of particle v, in 4-vector in xyz frame.       *
c         cthtv  :  cosine theta of v in xyz frame.                    *
c         cthtx  :  cosine theta of x in xyz frame.                    *
c         ctheta :  cosine theta of 1 in x'y'z' frame(if i3=0).        *
c                   or                                                 *
c                   cosine theta of normal to decay-plane in x'y'z'    *
c                   frame (if i3 not equals 0).                        *
c         phi    :  angle phi of 1 in x'y'z'-frame (if i3=0)           *
c                   or                                                 *
c                   angle phi of normal to decay-plane in x'y'z' frame *
c                   (if i3 not equals 0).                              *
c         iflag  :  coodinate system of x'y'z'-frame.                  *
c                   if it equals zero or it isn't given, default       *
c                   coordinate system is defined, ie. z'direction      *
c                   in direction of v (opposite to decaying particle). *
c                   if it equals 1, then z' direction in direction     *
c                   of x which decays into 1,2,(3).                    *
c    attention: needs locations 92-100 for temporary use.             *
c                                                                      *
c----------------------------------------------------------------------*
c                                                                      *
c   n.wermes last update 02/07/84                                      *
c   l.koepke last update 02/11/85 aditional flag for z-direction       *
c   r.xu     copied from mark iii. 10/23/90                            * 
c   s.j.chen aditional cosine theta(v) and cosine theta(x) 11/22/1990  *
c                                                                      *
c----------------------------------------------------------------------*
c
c
       subroutine angle(isys,i1,i2,i3,iv,cthtv,cthtx,ctheta,phi,iflag) 
       include 'ppp.inc'
       integer i1p/92/,i2p/93/
       integer zsys/94/,ix/95/,xpr/96/,ypr/97/,zpr/98/,normal/100/
       real*4 sp(4),sb(4),pf(4)

c
c  initial values
c
       cthtv =-100
       cthtx =-100
       ctheta=-100
       phi   =-100
       call pzer$(94,100)
c
c  values in xyz-system
c
       call norz$(isys,zsys)                   ! normalize z input axis
       call add$(i1,i2,ix)                     ! 4-vector of x
       if (i3.ne.0) call add$(ix,i3,ix)
       call ang$(iv,zsys,cthtv,tt)             ! theta of v
       call ang$(ix,zsys,cthtx,tt)             ! theta of x
c
c  vectors of x'y'z' system
c
       if (iflag.eq.0) then
          call smul$(ix,zpr,-1.)               ! z' = v direction         
          call norz$ (zpr,zpr)                 ! z' unit-vector
c          call cross$(zpr,zsys,ypr)            ! y' = z' cross z
          call cross$(zsys,zpr,ypr)            ! y' = z cross z'
          call norz$ (ypr,ypr)                 ! y' unit-vector
          call cross$(ypr,zpr,xpr)             ! x' = y' cross z'
          call norz$ (xpr,xpr)                 ! x' unit-vector
       endif
       if (iflag.eq.1) then
          call smul$(ix,zpr,+1.)               ! z' = x direction  
          call norz$ (zpr,zpr)                 ! z' unit-vector
c         call cross$(zpr,zsys,ypr)            ! y' = z' cross z
          call cross$(zsys,zpr,ypr)            ! y' = z cross z'
          call norz$ (ypr,ypr)                 ! y' unit-vector
          call cross$(ypr,zpr,xpr)             ! x' = y' cross z'
          call norz$ (xpr,xpr)                 ! x' unit-vector
	endif
c
c  do lorentz-boost & projections
c
       call lor$(i1,ix,i1p)                    ! transform to x-rest-frame
c 
       if (i3.ne.0) then
        call lor$(i2,ix,i2p)                 ! transform to x-rest-frame
c
          call cross$(i1p,i2p,normal)          ! normal to decay-plane 
          call norz$(normal,normal)            ! make it a unit vector
          iuse = normal
       else
          iuse =i1p
       endif
       call ang$(iuse,zpr,ctheta,tt)           ! theta of ii in x'y'z'
       call dot$(iuse,xpr,pxpr)                ! px given in x'y'z'-frame
       call dot$(iuse,ypr,pypr)                ! py given in x'y'z'-frame
       phi = xatan(pypr,pxpr)                  ! phi of ii in x'y'z'
       call pzer$(92,100)                     ! clear temporary storage
       return
       end

       function xatan(y,x)
c  get value of phi between 0 and twopi
c
       xatan = 0.
       if(x.eq.0.0.and.y.eq.0.0) return
       xatan = atan2(y,x)
       if(xatan.lt.0.0) then
       xatan =xatan+3.14159267*2
       endif
       return
       end
