      SUBROUTINE RD_TRK(ITD)
      IMPLICIT NONE
c--------------------------------------------------------------------------
c   2/10/95  JRS -- Re-write of RD_TRK
c      --  Only one track is found, beginning at T.2 closest in time
c              to the detector strobe.
c      --  Only positive tracks (bending towards lower sector numbers)
c              are searched for.
c      --  TD mode (ITD=1) looks for TD hits in a narrow time window
c              around the T.2 time and fills the array IRHIT.
c              A TD hit in either end in coincidence with an ADC hit in
c              either end is sufficient.
c      --  Gaps are allowed for sector crossings occurring before layer 12.
c      --  If no T.2 can be found, the online T.2 sector (TSEC_SCF) is
c              used if one and only one was found by the SCF.
c      --  If the track does not extend at least to layer 6, try to find
c              another track in time with the detector strobe.
c      --  Note:  The old RD_TRK has been renamed rd_trk_old.cdf.  Link
c              in this version if you want to look for more than one track.
c   5/12/95  JRS -- Remove second call to TDUNP (for speed).
c   11/9/95   MC -- Add UMC support. Since there's no detector strobe 
c                   in UMC data, we need to widen the window for looking
c                   for T.2's
c   3/13/96  JRS -- Use ITD = 2 to run RD_TRK_OLD.  This is useful for
c                   analyzing data with no TD's.  (ITD = 0 mode uses only
c                   ADC information when finding the track, but still
c                   uses the TD's for determining the T.2 sector.)
c   1/1/97   JRS -- If no DS can be found in the TDs, look for one in TDCs.
c--------------------------------------------------------------------------
#include <info.cmn>
#include <adcamp.cmn>
#include <rdtrack.cmn>
#include <tr2bits.cmn>
#include <td.par>
#include <fbtdc.cmn>

      INTEGER*4 I,IMOD,ISEC,ILAY,SEC,LAY,ITD,IE,IH,SNUM,SMIN,
     +           T2SEC,STSEC,STLAY
      REAL      THRESH(21),DSOFF,TMIN,TDIF,TIMEMIN,TAV,TSUM,TBEGIN,TEND,
     +           DSTIME,TWIN,DSWIN
      LOGICAL   NONE,IRHIT(21,24),IRHITADC(21,24),SKIPSEC,
     +           LAYHIT(2),MODHIT,ENDHIT,BADTSEC(24)

      DATA THRESH /0.1,20*0.5/ ! energy threshold for each layer
      DATA TWIN /10./ ! Half time window to look for hits in TDs

      IF (ITD.GT.1) THEN
        CALL RD_TRK_OLD(ITD-2)
        RETURN
      ENDIF

      NTRK     = 0
      NHTRK(1) = 0
      ETRK(1)  = 0.
      SKIPSEC  = .FALSE.
      DO ISEC = 1,24
        BADTSEC(ISEC) = .FALSE.
        DO ILAY = 1, 21
          IRUSE(ILAY,ISEC)    = 0
          IRHIT(ILAY,ISEC)    = .FALSE.
          IRHITADC(ILAY,ISEC) = .FALSE.
        ENDDO
      ENDDO
      DO I=1,2
        IMOST(I)=0
        TTDOTA(I)=0
      ENDDO
      NEV_RT = NEVT
      NRN_RT = NRUN

      CALL ADCUNP('RD','CMB')
      IF(NRDHIT(4).EQ.0)RETURN
      DO I = 1,NRDHIT(4)
        IMOD = RDMOD(I,4)
        ISEC = 1 + (IMOD-1)/21
        ILAY = IMOD - (ISEC-1)*21
        IF( RDCMB(1,ILAY,ISEC).GT.0.0 ) THEN 
          IRHITADC(ILAY,ISEC)=.TRUE.
        ENDIF
      ENDDO
C
C Find T.2 sector using time of detector strobe
C
      IF (UMC_FLAG) THEN 
        DSTIME = 0.
        DSWIN = 200.
      ELSE
        DSWIN = 20.
c.k3        CALL UPTDMOD('TR',2,1,0,-200.,400.,'CAL')
c.k3        IF (NPULSE_C.GE.1) THEN
c.k3          DSTIME=EDGETD_C(1)
c.k3        ELSE
c.dv      CALL TDCUNP('BM','CAL',-200.,400.)
c.dv      IF (BMTHIT(5,2,10).GE.1) THEN
c.dv        DSTIME=BMTTDC(5,2,10,1)
c.dv      ELSE
c.dv        DSTIME=0.
c.dv      ENDIF
c.k3        ENDIF
        CALL TDCUNP('TS','CAL',-200.,400.)
        IF (TSTHIT(2).GE.1) THEN
           DSTIME=TSTTDC(2,1)
        ELSE
           DSTIME=0.0
        ENDIF
      ENDIF

c.k3 100  CALL TDUNP('RD','CAL',DSTIME-DSWIN,DSTIME+DSWIN)
 100  CALL TDCUNP('RD','CAL',DSTIME-DSWIN,DSTIME+DSWIN)
      TIMEMIN=100.
      SMIN=0
      DO ISEC=1,24
        IF (BADTSEC(ISEC)) GOTO 110 
        SNUM=0
        TSUM=0.0
        DO ILAY=1,2
          LAYHIT(ILAY)=.FALSE.
          IF (IRHITADC(ILAY,ISEC)) THEN
            DO IE=1,2
              TMIN=100.
              ENDHIT=.FALSE.
c.k3              DO IH=1,rdmodhit(IE,ILAY,ISEC)
c.k3                TDIF=(rdtdtim(IE,ILAY,ISEC,IH)-DSTIME)
              DO IH=1,rdthit(IE,ILAY,ISEC)
                TDIF=(rdttdc(IE,ILAY,ISEC,IH)-DSTIME)
                IF (ABS(TDIF).LT.ABS(TMIN)) THEN
                  TMIN=TDIF
                  LAYHIT(ILAY)=.TRUE.
                  ENDHIT=.TRUE.
                ENDIF
              ENDDO
              IF (ENDHIT) THEN
                SNUM=SNUM+1
                TSUM=TSUM+TMIN
              ENDIF
            ENDDO
          ENDIF
        ENDDO
        IF (LAYHIT(1).AND.LAYHIT(2)) THEN
          TAV=TSUM/SNUM
          IF (ABS(TAV)-SNUM.LT.ABS(TIMEMIN)-SMIN) THEN
C         IF (ABS(TAV)     .LT.ABS(TIMEMIN)     ) THEN
            TIMEMIN=TAV
            SMIN=SNUM
            SEC=ISEC
          ENDIF
        ENDIF
 110  ENDDO
      IF (TIMEMIN.EQ.100.) THEN ! Couldn't find a T.2
        IF (SKIPSEC) RETURN     ! We have already found at least one T.2
        CALL TR2UNP(0)
        IF (ONET2_SCF) THEN
          CALL KERROR(-1,10,'RD_TRK','found no T.2, using TSEC_SCF')
          SEC=TSEC_SCF
          NTRK = 1
          TTDOTA(1)=DSTIME
          TTDOTA(2)=DSTIME
          IMOST(1)=1
          IMOST(2)=1
        ELSE
          CALL KERROR(-2,10,'RD_TRK','found no T.2, no tracks found')
          NTRK = 0
          RETURN
        ENDIF
      ELSE
        NTRK = 1
        TTDOTA(1)=TIMEMIN+DSTIME
        TTDOTA(2)=TIMEMIN+DSTIME
        IMOST(1)=1
        IMOST(2)=1
      ENDIF
C
C  First fill array IRHIT according to ADC and/or TD information.
C
      IF (ITD.EQ.0) THEN
        IF(NRDHIT(4).EQ.0)RETURN
        DO I = 1,NRDHIT(4)
          IMOD = RDMOD(I,4)
          ISEC = 1 + (IMOD-1)/21
          ILAY = IMOD - (ISEC-1)*21
          IF( RDCMB(1,ILAY,ISEC).GT.THRESH(ILAY) ) THEN 
            IRHIT(ILAY,ISEC)=.TRUE.
          ENDIF
        ENDDO
      ELSE
c       CALL TDUNP('RD','CAL',TTDOTA(1)-TWIN,TTDOTA(1)+TWIN)
        DO ILAY=1,19
          DO ISEC=1,24
            MODHIT=.FALSE.
            DO IE=1,2
c.k3              DO IH=1,RDMODHIT(IE,ILAY,ISEC)
c.k3                IF (ABS(RDTDTIM(IE,ILAY,ISEC,IH)-TTDOTA(1)).LE.TWIN) 
              DO IH=1,RDTHIT(IE,ILAY,ISEC)
                IF (ABS(RDTTDC(IE,ILAY,ISEC,IH)-TTDOTA(1)).LE.TWIN) 
     +                                                   MODHIT=.TRUE.
              ENDDO
            ENDDO
            IRHIT(ILAY,ISEC)=MODHIT.AND.IRHITADC(ILAY,ISEC)
          ENDDO
        ENDDO
      ENDIF
C
C  Now find the track.
C
      DO ISEC = 1,24    ! Re-init IRUSE
        DO ILAY = 1,21
          IRUSE(ILAY,ISEC) = 0
        ENDDO
      ENDDO
      NHTRK(1)     = 1
      ITRK(1,1)    = 21*(SEC-1)+1
      ETRK(1)      = RDCMB(1,1,SEC)
      IRUSE(1,SEC) = 1
      LAY          = 2
      NONE         = .FALSE.

      DO WHILE(.NOT.NONE)
        IF (IRHIT(LAY,SEC)) THEN
          NHTRK(1)=NHTRK(1)+1
          ITRK(NHTRK(1),1)=21*(SEC-1)+LAY
          ETRK(1)=ETRK(1)+RDCMB(1,LAY,SEC)
          IRUSE(LAY,SEC)=1
          LAY=LAY+1
          IF (LAY.EQ.20) NONE=.TRUE.
        ELSE 
          LAY=LAY-1
          SEC=SEC-1
          IF (SEC.LT.1) SEC=SEC+24
          IF (IRHIT(LAY,SEC)) THEN
            NHTRK(1)=NHTRK(1)+1
            ITRK(NHTRK(1),1)=21*(SEC-1)+LAY
            ETRK(1)=ETRK(1)+RDCMB(1,LAY,SEC)
            IRUSE(LAY,SEC)=1
            LAY=LAY+1
            IF (LAY.EQ.20) NONE=.TRUE.
          ELSE
            LAY=LAY+1
            IF (IRHIT(LAY,SEC)) THEN
              NHTRK(1)=NHTRK(1)+1
              ITRK(NHTRK(1),1)=21*(SEC-1)+LAY
              ETRK(1)=ETRK(1)+RDCMB(1,LAY,SEC)
              IRUSE(LAY,SEC)=1
              LAY=LAY+1
              IF (LAY.EQ.20) NONE=.TRUE.
            ELSE
              LAY=LAY+1
              IF (LAY.LE.12.AND.IRHIT(LAY,SEC)) THEN
                NHTRK(1)=NHTRK(1)+1
                ITRK(NHTRK(1),1)=21*(SEC-1)+LAY
                ETRK(1)=ETRK(1)+RDCMB(1,LAY,SEC)
                IRUSE(LAY,SEC)=1
                LAY=LAY+1
              ELSE
                NONE=.TRUE.   ! End of track.
              ENDIF
            ENDIF
          ENDIF
        ENDIF
      ENDDO
      IF (LAY.EQ.20) THEN
        LAY=LAY-1
        SEC=SEC-1
        IF (SEC.LT.1) SEC=SEC+24
        IF (IRHIT(LAY,SEC)) THEN
          NHTRK(1)=NHTRK(1)+1
          ITRK(NHTRK(1),1)=21*(SEC-1)+LAY
          ETRK(1)=ETRK(1)+RDCMB(1,LAY,SEC)
          IRUSE(LAY,SEC)=1
        ENDIF
      ENDIF
C
C  If stopping layer is less than 6 look for a different track.
C
      T2SEC = 1 + (ITRK(1       ,1)-1)/21
      STSEC = 1 + (ITRK(NHTRK(1),1)-1)/21
      STLAY =      ITRK(NHTRK(1),1) - (STSEC-1)*21
C     IF (STLAY.LT.6) THEN
      IF (STLAY.LT.0) THEN     ! Temp. For testing purpose only. DV.
        BADTSEC(T2SEC)=.TRUE.
        SKIPSEC=.TRUE.
        GOTO 100
      ENDIF

      RETURN
      END
