C   10/01/82 311161219  MEMBER NAME  TOFSM3   (S)           FORTRAN
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
       SUBROUTINE TOFSM3(NRUN)
C
C      COMPUTES TOF WITHOUT CHECKING AGREEMENT OF THE 2 PHOTOTUBES
C  OUTPUT TTOF(I) : TIME OF FLIGHT IN COUNTER I(ONLY COMPUTED IF LATCH
C                   HAS FIRED) OTHERWISE SET TO -100
C  HTOF(I) = 0 IF LATCH OF COUNTER I HAS NOT FIRED
C          = 1 IF IT HAS
C  NTOF = NUMBER OF TOF LATCHES THAT HAVE FIRED
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      IMPLICIT INTEGER*2 (H)
      COMMON/TFPRM/ CORNOR(84),COROVF(84),TPARM(41),VELSC,TCALM(42),
     1 TCALP(42),PEDLM(42),PEDLP(42),DUM(90)
      COMMON/TFADC/ CADCA(84),CADCB(84)
      COMMON/TFPED/ HADC(2,42),HTDC(2,42),HSTAT(42),HON(42)
      COMMON/FWPED/ HADCF(2,16),HTDCF(2,16),HFSTAT(16),HFON(16)
      COMMON/TFPED1/HTDC1(2,42),HON1(42),HTSPAR(16)
C
      COMMON/SMPTOF/MTOF,TTOF(42),NTOF,HTOF(42),ZTOF(42)
      COMMON/BCS/IDATA(1)
      DIMENSION HDATA(2)
      EQUIVALENCE  (HDATA(1),IDATA(1))
      DIMENSION MASK(7),LIST(19),COR(42),COR1(42)
      DATA MASK/1,2,4,8,16,32,64/
      DATA IRUN/0/,IBUG/10/
      DATA MBAD/2/
      DATA LIST/8,11,17*0/
      DATA COR1/-19.,-32.,17*-19.,2*-17.,3*-19.,2*-17.,16*-19./
      DATA COR/42*0/
C
      NTOF = 0
      CALL SETSL(TTOF,0,42*4,0.)
      CALL SETSL(HTOF,0,42*2,0.)
      IF(NRUN.EQ.IRUN) GOTO  10
      IENTRY = IENTRY + 1
C     CALL UCOPY(COR1,COR,42)
C     IF(IENTRY.LE.1) PRINT 115,NRUN,COR
C     IF(NRUN.LE.10362) GOTO  10
C     DO   15   N=1,42
C  15 COR(N) = COR1(N) + 11.5
C     IF(I10362.GT.0)  GOTO  10
C     PRINT 115,NRUN,COR
C 115 FORMAT(' CONSTANTS TOFSM3 AT RUN',I10/(1X,20F6.1))
C     I10362 = 1
   10 IRUN = NRUN
      IPLTC  = IDATA(IBLN('LATC'))
      IF(IPLTC.LE.0)  RETURN
      IPTOF  = IDATA(IBLN('ATOF'))
      IF(IPTOF.LE.0)  RETURN
      JPTOF = IPTOF*2
      JPLTC = IPLTC*2
C
C  FILL ARRAY OF TOF-LATCHES
C
      DO   5   N=4,9
      ID = HDATA(JPLTC+N+2)
      IF(ID.EQ.0)  GOTO  5
      DO   6   M=1,7
      IF(LAND(ID,MASK(M)).LE.0)  GOTO  6
      KTOF = (N-4)*7 + M
      NTOF = NTOF + 1
      IF(NTOF.GT.42)  GOTO  6
      HTOF(KTOF) = 1
    6 CONTINUE
    5 CONTINUE
C
      IF(NTOF.LE.0) RETURN
C     CALL ATOFUN(IPTOF)
      CALL ATFUGR(IPTOF,*999)
C
      IBUG = IBUG + 1
C
      DO   1   I=1,42
      IF(HTOF(I).LE.0)  GOTO  1
      IF1 = 2*I-1
      ITDCL = HTDC(1,I)
      ITDCR = HTDC(2,I)
      IF(IBUG.GT.5)  GOTO  33
      PRINT 733,NRUN,I,ITDCL,ITDCR,HADC(1,I),HADC(2,I)
  733 FORMAT(' CHECK',10I8)
   33 IF(NRUN.LT.8712.OR.NRUN.GT.10000)  GOTO 12
      DO  19   ML=1,MBAD
      IF(I.NE.LIST(ML))  GOTO 19
      TTOF(I) = -20.
      GOTO  1
   19 CONTINUE
C
   12 IF(ITDCL.LT. 5.OR.ITDCL.GE.2048)  GOTO  1
      IF(ITDCR.LT. 5.OR.ITDCR.GE.2048)  GOTO  1
      CORADM = 0.
      CORADP = 0.
      ITIF = IABS(ITDCL-ITDCR)
      IF(NRUN.GT.6600.AND.ITIF.LE.1)  GOTO  20
      IADC =  IF1 + (IF1-1)/12
      IADCM = HADC(1,I)
      IADCP = HADC(2,I)
      ADCM = FLOAT(IADCM)
      ADCP = FLOAT(IADCP)
      IF(ADCM.LT.1024.) CMS = CORNOR(IF1)
      IF(ADCP.LT.1024.) CPS = CORNOR(IF1+1)
      IF(ADCM.GE.1024.) CMS = COROVF(IF1)
      IF(ADCP.GE.1024.) CPS = COROVF(IF1+1)
      ADCM = ADCM - PEDLM(I)
      ADCP = ADCP - PEDLP(I)
      IF(ADCM.LE.0.) ADCM = 1.
      IF(ADCP.LE.0.) ADCP = 1.
      ADCMIV = 1000./ADCM
      ADCPIV = 1000./ADCP
      SQADM = SQRT(ADCMIV)
      SQADP = SQRT(ADCPIV)
      BADCM= CADCB(2*I-1)
      BADCP= CADCB(2*I)
      IF(ADCM.GT.1.) CORADM =  BADCM*SQADM
      IF(ADCP.GT.1.) CORADP =  BADCP*SQADP
      IF(CORADM.GT.5.) CORADM = 5.
      IF(CORADP.GT.5.) CORADP = 5.
  20  TDCL = ITDCL*TCALM(I)-CMS-CORADM
      TDCR = ITDCR*TCALP(I)-CPS-CORADP
      TDCL = TDCL -TPARM(25)
      TDCR = TDCR -TPARM(25)
      IF(IBUG.LE.5) PRINT 320,I,ITDCL,TCALM(I),CMS,CORADM,TPARM(25),TDCL
      IF(IBUG.LE.5) PRINT 320,I,ITDCR,TCALP(I),CPS,CORADP,TPARM(25),TDCR
  320 FORMAT(' TOFSM3',2I5,5F12.4)
      TTOF(I) = .5*(TDCL+TDCR)
      IF(NRUN.GE.10000) TTOF(I) = TTOF(I) + 3.07
C     IF(NRUN.GT.12518) TTOF(I) = TTOF(I) + 11.
      ZTOF(I) = (TDCL-TDCR)/2.*VELSC
      IF(IBUG.LE.5) PRINT 734,I,TTOF(I)
  734 FORMAT(' CHECK',I5,F10.2)
    1 CONTINUE
C/////////
  999 RETURN
      END
