*****************************************************************************
      INTEGER FUNCTION MYDECTRK()
c
      IMPLICIT NONE
C
      INCLUDE 'COMMON.TXT'
C
      DOUBLE PRECISION al_p(5),
     &     xout(22),yout(22),zin(22)
C
      REAL ZALIG, DISTX
      REAL PIANO(22), VARFIT(2)
      REAL TBAR(2,22), CBAR(2,22)
C
      INTEGER m, i, T, IFAIL
      INTEGER NPFIT(2)
C
      REAL hmemor(9000000)
      INTEGER Iquest(100)
C
      COMMON / pawcd / hmemor
      save / pawcd /
C
      Common / QUESTd / Iquest
      save / questd /
C
C Begin !
C
      MYDECTRK = 0
C
C Z positions where to calculate the track
C
      PIANO(1) = 0.
      DO I = 2, 22
         IF ( MOD(I,2).EQ.0 ) THEN 
            PIANO(I) = PIANO(I-1) - 8.09
         ELSE
            PIANO(I) = PIANO(I-1) - 10.09
         ENDIF 
      ENDDO
      ZALIG = -263.
C
C     BEGIN WITH THE FISRT TRACK 
C
      T = 1
C
 10   CONTINUE
C     
C     COPY THE ALFA VECTOR FROM AL_PP TO AL_P FOR THE TRACK "T"
C     
      do m = 1, 5
         al_p(m) = al_pp(t,m)
      enddo
      if (al_p(5).eq.0.) THEN
      PRINT *,' MYDETECTOR2 - WARNING F77: track with R = 0, discarded'
         GOTO 969
      ENDIF
C
C     CALL THE TRACKING ROUTINE
C
      DO M = 1,2
         DO I = 1,22     
            XOUT(I) = 0.
            YOUT(I) = 0.
            IF (MOD(M,2).EQ.0) THEN
               DISTX = PIANO(I) + ZALIG 
            ELSE
               DISTX = PIANO(I) - 5.81 + ZALIG 
            ENDIF               
            ZIN(I) = distx / 10.
         enddo
         IFAIL = 0
         call TRACK(22,ZIN,XOUT,YOUT,AL_P,IFAIL)
         if(IFAIL.ne.0)then
            GOOD = 0
            print *,' MYDETECTOR2 - WARNING F77: tracking failed '
            goto 969
         endif
         DO I = 1, 22
C     
C     CHECK IF XOUT OR YOUT ARE NaN
C     
            IF (XOUT(I).NE.XOUT(I).OR.YOUT(I).NE.YOUT(I)) THEN
               print *,
     &         ' MYDETECTOR2 - WARNING F77: tracking error (NaN values)'                  
               GOTO 969
            ENDIF
C     
         ENDDO
      ENDDO
 969  continue
      
C     
C     HERE WE MAKE SOME KIND OF ALTERNATIVE FIT WITH OUR INSTRUMENT TO COMPARE IT WITH THE TRACK COMING FROM TRACKER
C     
      
C     
C     IF WE HAVE A GOOD FIT FROM OUR INSTRUMENT (NPFIT(2).GT.15.AND.VARFIT(2).LT.1000) AND WE HAVE AN IMAGE TRACK WE CAN TRY TO SEE IF WE ARE CLOSER TO THE IMAGE OR TO THE MAIN TRACK
C     
      IF (GOOD.EQ.1.AND.NPFIT(2).GT.15.AND.VARFIT(2).LT.1000
     &     .AND.TRKCHI2.EQ.1) THEN
         IF (ABS(TBAR(2,1)-CBAR(2,1))<40.) THEN ! HERE WE CHECK IF THE TWO TRACKS ARE THE SAME PHYSICS TRACK
C     
C     GOOD, THE TWO TRACKS COINCIDE
C     
            IF (T.EQ.2) TRKCHI2 = 2
            GOTO 6996
         ELSE
C     
C     IT IS NOT A GOOD FIT BUT WE HAVE AN IMAGE AND THIS IS THE FIRST TRACK
C     
            IF (T.EQ.1) THEN
               T = 2
               GOTO 10
            ENDIF
C     
C     IT IS NOT A GOOD FIT BUT WE HAVE AN IMAGE AND THIS IS THE SECOND TRACK, USE THE FIRST ONE
C     
            IF (T.EQ.2) THEN
               TRKCHI2 = -1
               T = 1
               GOTO 10
            ENDIF
         ENDIF
C     
      ENDIF
C     
      IF (T.EQ.1.AND.TRKCHI2.EQ.1) THEN
         GOOD = 1
         T = 2
         GOTO 10
      ENDIF
C     
         
 6996 CONTINUE
C
C     HERE WE DETERMINE TRACK-RELATED VARIABLES
C
      MYTRKVAR = 18.

 50   CONTINUE
C
      RETURN
      END


