!----------------------------------------------------------------------
! get tracks from RTRA
! M. Staric, jan-2000, mar-2002
!
!----------------------------------------------------------------------


      SUBROUTINE GET_TRACKS(IER)

      IMPLICIT NONE
      INTEGER IER

#include "riter/RIC_TRA.fi"
#include "arte/Arte.fi"
#include "auto/RTRA.fi"
#include "auto/RPNT.fi"

      INTEGER IROW,IX,IR,IR0,IR1,ITRA,I
      REAL Z0,Z1,Z,TX,TY,TT
      INTEGER IER1
      SAVE IER1
      REAL Z_MIN0, Z_MAX0, Z_MIN1, Z_MAX1
      SAVE Z_MIN0, Z_MAX0, Z_MIN1, Z_MAX1
      DATA Z_MIN0 /680./, Z_MAX0 /860./
      DATA Z_MIN1 /1190./, Z_MAX1 /1330./

#include "arte/Arte.ff"
#include "auto/RTRA.ff"
#include "auto/RPNT.ff"

c--   get RPNT before RICH (if no point before, use point after RICH):

      NTRA=0
      NTRA_ALL=NRTRA()
      DO IROW=1,NRTRA()
         IF(IRTRA_FIT(IROW).NE.0) GOTO 1
         IF(IRTRA_HSI(IROW).EQ.0) GOTO 1
         IF(IRTRA_HOU(IROW)+IRTRA_HIN(IROW).EQ.0) GOTO 1
         IR0=0
         IR1=0
         Z0=Z_MIN0
         Z1=Z_MAX1
         IX=PRTRA_RPNT(IROW)
         DO WHILE(IX.GT.0)
            IR=MRTRA_RPNT(IX)
            IX=XRTRA_RPNT(IX)
            Z=FRPNT_ZF(IR)
            IF(Z.GT.Z0.AND.Z.LT.Z_MAX0) THEN
               Z0=Z
               IR0=IR
            ELSE IF(Z.LT.Z1.AND.Z.GT.Z_MIN1) THEN
               Z1=Z
               IR1=IR
            ENDIF
         ENDDO
         IF(IR0.EQ.0) IR0=IR1
         IF(IR0.EQ.0) THEN
            CALL MEPRIC(IER1,10,
     &           'RITER warning: - RTRA has no RPNT in PC or TC')
            GOTO 1
         ENDIF
         NTRA=NTRA+1
         IF(NTRA.GT.MAXTRA) GOTO 1
         T_R(1,NTRA)=FRPNT_XF(IR0)*10. ! convert to millimeters
         T_R(2,NTRA)=FRPNT_YF(IR0)*10.
         T_R(3,NTRA)=FRPNT_ZF(IR0)*10.
         TX=FRPNT_TXF(IR0)
         TY=FRPNT_TYF(IR0)
         TT=SQRT(TX**2+TY**2+1.)
         T_DIR(1,NTRA)=TX/TT
         T_DIR(2,NTRA)=TY/TT
         T_DIR(3,NTRA)=1./TT
         T_MOM(NTRA)=1./MAX(ABS(FRTRA_PF(IROW)),0.001)
         T_SIG(1,NTRA)=SQRT(ABS(FRPNT_CVF(6,IR0)))
         T_SIG(2,NTRA)=SQRT(ABS(FRPNT_CVF(10,IR0)))
         T_SIG(3,NTRA)=T_SIG(1,NTRA)
         T_SIG(4,NTRA)=T_SIG(2,NTRA)
         T_SMOM(NTRA)=SQRT(ABS(FRTRA_CVF(15,IROW)))*T_MOM(NTRA)**2
         MC_TIP(NTRA)=0
         RTRA_ROW(NTRA)=IROW
 1       CONTINUE
      ENDDO
      IER=MAX(0,NTRA-MAXTRA)
      NTRA=MIN(NTRA,MAXTRA)

c--   reset, set clone flag

      DO ITRA=1,NTRA
         DO I=0,NHYP-1
            THC(I,ITRA)=0.
            STFS(I,ITRA)=0.
            PLKH(I,ITRA)=0
         ENDDO
         STFB(ITRA)=0.
         AX3(ITRA)=0.
         SFOT(ITRA)=0.
         PID(ITRA)=0
         RECO_FLAG(ITRA)=0
      ENDDO
      CALL RITER_CLONE_RM

c--   global to RICH local

      CALL GLOBAL_TO_RICH

c--   corrections for angular resolution

      CALL CORRECT_TSIG

      RETURN
      END



      SUBROUTINE GLOBAL_TO_RICH

! transformation from global to local (RICH)

      IMPLICIT NONE

#include "riter/RIC_PAR.fi"
#include "riter/RIC_TRA.fi"
#include "arte/Arte.fi"
#include "auto/ACGA.fi"
      integer CMP
      PARAMETER (CMP='00000005'x)
      INTEGER IROW, ITRA
      REAL DX, DY, DZ, DTX, DTY, FZ, FI, Z0
      REAL X,Y,Z,TX,TY,TXL,TYL,TT
      INTEGER IER1
      SAVE IER1
#include "arte/Arte.ff"
#include "auto/ACGA.ff"

      IF(.NOT.GLO_AL) RETURN
      IF(NACGA().EQ.0) RETURN

      DX=0.
      DY=0.
      DZ=0.
      DTX=-0.000258
      DTY=-0.000374
      FZ=  0.996422
      FI= -0.002047
      Z0=4500.
      IER1=0
      DO IROW=1,NACGA()
         IF(IACGA_CMP(IROW).EQ.CMP) THEN
            DX=FACGA_DX(IROW)*10.
            DY=FACGA_DY(IROW)*10.
            DZ=FACGA_DZ(IROW)*10.
            DTX=FACGA_DTX(IROW)
            DTY=FACGA_DTY(IROW)
            FZ=FACGA_FZ(IROW)
            FI=FACGA_ROT(4,IROW)
            Z0=FACGA_Z(IROW)*10.
            IER1=1
         ENDIF
      ENDDO
      IF(IER1.EQ.0) THEN
         CALL MEPRIC(IER1,10,
     &        'RITER warning: - ACGA for cmp=5 not found, default used')
      ENDIF
      IF(FZ.LE.0) FZ=1.

      DO ITRA=1,NTRA
         X=T_R(1,ITRA)
         Y=T_R(2,ITRA)
         Z=T_R(3,ITRA)-Z0
         TX=T_DIR(1,ITRA)/T_DIR(3,ITRA)
         TY=T_DIR(2,ITRA)/T_DIR(3,ITRA)
         T_R(1,ITRA)=X-DX+Y*FI-Z*DTX
         T_R(2,ITRA)=Y-DY-X*FI-Z*DTY
         T_R(3,ITRA)=Z/FZ+Z0-DZ
         TXL=FZ*TX+FI*TY-DTX
         TYL=FZ*TY-FI*TX-DTY
         TT=SQRT(TXL**2+TYL**2+1.)
         T_DIR(1,ITRA)=TXL/TT
         T_DIR(2,ITRA)=TYL/TT
         T_DIR(3,ITRA)=1./TT
      ENDDO

      RETURN
      END


      SUBROUTINE CORRECT_TSIG

! Correcton of track angular resolution, given by covariant matrix

      IMPLICIT NONE

#include "riter/RIC_PAR.fi"
#include "riter/RIC_TRA.fi"
      INTEGER ITRA

      IF(.NOT.COR_TSIG) RETURN
      DO ITRA=1,NTRA
         T_SIG(1,ITRA)=FAC_TSIG(1)*T_SIG(3,ITRA)+OFF_TSIG(1)
         T_SIG(2,ITRA)=FAC_TSIG(2)*T_SIG(4,ITRA)+OFF_TSIG(2)
      ENDDO

      RETURN
      END


