C   12/09/79 206221544  MEMBER NAME  TOFMASO  (S)           FORTRAN
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
       SUBROUTINE TOFMAS
C
C THIS IS THE OLD VERSION WHICH HAS THE 2 TRACK ALGORITHM
C REPLACED ON 22.6.1982 BY TOFMAS4(NOW RENAMED INTO TOFMAS)
C
C  DETERMINES TOF FOR EACH TRACK AND ITS QUALITY
C  CREATES BANK TOFR
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
#include "tcalcm.for"
#include "tfprm.for"
      INTEGER *2  HEADR(108)
      COMMON /CHEADR/ IHEADR(54)
      EQUIVALENCE (IHEADR(1),HEADR(1))
      COMMON/PRPOS/PRPLS,CPOS
      DIMENSION IAGREE(3),ZT(2),TMNUS(2),TPLUS(2),DELT(2)
C
      DATA IENTRY/0/
      DATA CVEL/ 2.9979246E2/,DTAU1/1./
C
      NRUN = HEADR(18)
      IF(IENTRY.NE.0)  GOTO 66
      IENTRY = IENTRY + 1
   66 ZTOF = 0.
      ZTOF1 = 0.
      NNG= 0
      NOK= 0
      CALL SETSL(IW,0,704*4,0)
      CALL SETSL(TAUM,0,84*4,0.)
C
       DO 1000 I=1,42
       MTPY= ICRT1(2,I)
            IF(MTPY.EQ.0) GO TO 1000
            IF(MTPY.EQ.1) GO TO 300
            IF(MTPY.EQ.2) GO TO 400
C/////////
                 MTPY= MTPY+2
                 IF(MTPY.GE.6) MTPY=5
                 DO 200 K=3,MTPY
                      NTK= ICRT1(K,I)
                      IRELT(1,NTK)= NTK
                      IRELT(2,NTK)= -MTPY+2
                      IRELT(3,NTK)= I
                      NNG= NNG+1
 200             CONTINUE
            GO TO 1000
C
C=========  ONE TRACK IN THE COUNTER
C
  300 NTR1= ICRT1(MTPY+2,I)
      Z1= TRK(1,NTR1)
      PATH1= TRK(3,NTR1)
      PMEV1= TRK(4,NTR1)
      TAUP1= RAW(3,I)+Z1/VELSC
      TAUM1= RAW(2,I)-Z1/VELSC
      TAUM1 = TAUM1 - (TPARM(7)*PATH1+TPARM(8))
      TAUP1 = TAUP1 - (TPARM(7)*PATH1+TPARM(8))
C  BEAM CURRENT CORR
      IF(NRUN.LT.4993) GOTO  321
      TAUM1 = TAUM1 - CPOS *TPARM(21)
      TAUP1 = TAUP1 - CPOS *TPARM(22)
C  AD HOC CORR FOR BEGIN 1981
      TAUM1 = TAUM1 - TPARM(25)
      TAUP1 = TAUP1 - TPARM(25)
      IF(NRUN.GT.6000.AND.NRUN.LT.7592) TAUM1 = TAUM1 +3.07
      IF(NRUN.GT.6000.AND.NRUN.LT.7592) TAUP1 = TAUP1 +3.07
  321 TAUM(I) = TAUM1
      TAUP(I) = TAUP1
      NFLG= -1
            IF(HTDC(1,I).GT.10.AND.HTDC(1,I).LT.2048) GO TO 320
            TOF1= TAUP1
            GO TO 360
C
 320        IF(HTDC(2,I).LT.2048) GO TO 330
            TOF1= TAUM1
            GO TO 360
C
  330 IF(ABS(TAUM1-TAUP1).LE.3.*DTAU1) GO TO 350
                 IF(Z1.LT.0.) GO TO 340
                 TOF1= TAUP1
                 GO TO 360
 340  TOF1= TAUM1
      GO TO 360
C
 350  NFLG= 1
      ADCM = HADC(1,I) - PEDLM(I)
      ADCP = HADC(2,I) - PEDLP(I)
      IF(ADCM.LE.0.) ADCM = 1.
      IF(ADCP.LE.0.) ADCP = 1.
      IF(HADC(1,I).GT.1024) ADCM = 1024.
      IF(HADC(2,I).GT.1024) ADCP = 1024.
      VWM = TPARM(9)/ADCM+TPARM(10)
      VWP = TPARM(9)/ADCP+TPARM(10)
      VWM = 1./(VWM*VWM)
      VWP = 1./(VWP*VWP)
      TOF1 = (VWM*TAUM1+VWP*TAUP1)/(VWM+VWP)
      ZTOF1 = (RAW(2,I)-RAW(3,I))/2.*VELSC
 360  CALL CMASS( I,PMEV1,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -            PRO1,PKA1,PPI1,PEL1)
      CALL TFSTOR(NTR1,NFLG,I,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -            PRO1,PKA1,PPI1,PEL1,ZTOF1)
      NOK= NOK+1
CXXXXXXXXXX
      GO TO 1000
C
C=========   TWO HITS IN THE COUNTER
C
 400  NTR= ICRT1(3,I)
      Z= TRK(1,NTR)
      NTR1 = ICRT1(4,I)
      Z1 = TRK(1,NTR1)
            IF(Z.GT.Z1) GO TO 430
            NTRP= NTR
            NTR= NTR1
            NTR1= NTRP
            ZP1= Z
            Z= Z1
            Z1= ZP1
 430  PATH= TRK(3,NTR)
      P = TRK(4,NTR)
      P1 = TRK(4,NTR1)
      PATH1 = TRK(3,NTR1)
C//////////
      TOF = 100.
      TOF1 = 100.
      IFLG = -2
      IFLG1 =-2
      IF(HTDC(1,I).LT.10.OR.HTDC(1,I).GT.2047)  GOTO  550
      IF(HTDC(2,I).LT.10.OR.HTDC(2,I).GT.2047)  GOTO  550
      TMNUS(1) = RAW(2,I) - Z1/VELSC
      TPLUS(1) = RAW(3,I) + Z1/VELSC
      DELT(1) = TMNUS(1) - TPLUS(1)
      TMNUS(2) = RAW(2,I) - Z/VELSC
      TPLUS(2) = RAW(3,I) + Z/VELSC
      DELT(2) = TMNUS(2) - TPLUS(2)
      DELZ = (Z-Z1)/VELSC
      IF(ABS(DELZ).LE.2.)  GOTO  560
      DO   431   L=1,2
      IAGREE(L) = 0
      IF(ABS(DELT(L)).LE.2.) IAGREE(L) = 1
  431 CONTINUE
      IAGREE(3) = 0
      TMM = TMNUS(1)+DELZ-TPLUS(2)
      TPP = TPLUS(2)+DELZ-TMNUS(1)
      IF(TMM.GT.-1..AND.TPP.GT.-1.) IAGREE(3) = 1
C
      IF(DELZ.LE.2.)  GOTO  560
      IF(IAGREE(1)+IAGREE(2)+IAGREE(3).EQ.0)  GOTO  550
      IF(IAGREE(1)+IAGREE(2)+IAGREE(3).EQ.3)  GOTO  560
      IF(IAGREE(1).EQ.1.AND.IAGREE(2).EQ.0) GOTO  520
      IF(IAGREE(2).EQ.1.AND.IAGREE(1).EQ.0) GOTO  530
      IF(IAGREE(3).EQ.1.AND.IAGREE(1)+IAGREE(2).EQ.0) GOTO  540
      GOTO  550
  520 CONTINUE
      IFLG1 = 2
      TOF1= .5*(TMNUS(1)+TPLUS(1))
      CALL CMASS( I,P1,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -                 PRO1,PKA1,PPI1,PEL1)
      NOK = NOK + 1
      IF(IAGREE(3).GT.0)  GOTO  521
      NNG = NNG + 1
      GOTO  500
  521 IFLG1 = -2
      TOF = TPLUS(2)
      CALL CMASS( I,P,TOF,PATH,BET,DBET,EMAS2,DMAS,
     -                 PRO,PKA,PPI,PEL)
      NOK = NOK + 1
      GOTO  500
  530 CONTINUE
      IFLG = 2
      TOF = .5*(TMNUS(2)+TPLUS(2))
      CALL CMASS( I,P,TOF,PATH,BET,DBET,EMAS2,DMAS,
     -                 PRO,PKA,PPI,PEL)
      NOK = NOK + 1
      IF(IAGREE(3).GT.0)  GOTO  531
      NNG = NNG + 1
      GOTO 500
  531 IFLG = -2
      TOF1 = TMNUS(1)
      CALL CMASS( I,P1,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -                 PRO1,PKA1,PPI1,PEL1)
      NOK = NOK + 1
      GOTO  500
  540 CONTINUE
      IFLG = 2
      IFLG1 = 2
      TOF1= TMNUS(1)
      TOF  = TPLUS(2)
      CALL CMASS( I,P,TOF,PATH,BET,DBET,EMAS2,DMAS,
     -                 PRO,PKA,PPI,PEL)
      CALL CMASS( I,P1,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -                 PRO1,PKA1,PPI1,PEL1)
      NOK = NOK + 2
      GOTO  500
  560 CONTINUE
      TOF = .25*(TMNUS(1)+TPLUS(1)+TMNUS(2)+TPLUS(2))
      TOF1 = TOF
      CALL CMASS( I,P,TOF,PATH,BET,DBET,EMAS2,DMAS,
     -                 PRO,PKA,PPI,PEL)
      CALL CMASS( I,P1,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -                 PRO1,PKA1,PPI1,PEL1)
      NOK = NOK + 2
      GOTO  500
  550 NNG = NNG + 2
  500 CONTINUE
            CALL TFSTOR(NTR1,IFLG1,I,TOF1,PATH1,BET1,DBET1,EMAS12,DMAS1,
     -                  PRO1,PKA1,PPI1,PEL1,DEDX1)
            CALL TFSTOR(NTR,IFLG,I,TOF,PATH,BET,DBET,EMAS2,DMAS,
     -                  PRO,PKA,PPI,PEL,DEDX)

 1000  CONTINUE
C
C=========   HEADER PART
C
                 NXX= 0
                 DO 2000 I=1,NTRK
                 IF(IRELT(1,I).NE.0) GO TO 2000
                 CALL TFSTOR(I,10,0,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.)
                 NXX= NXX+1
 2000            CONTINUE
            INFM(1)= NTRK
            INFM(2)= NOK
            INFM(3)= NNG
            INFM(4)= NXX
            LTFR= 14*NTRK+4
C
C=========   STORE RESULT INTO CURRENT BUFFER
C
       CALL BCRE(INTR,'TOFR',0,LTFR,&3000,IER)
       IF(INTR.EQ.0.AND.IER.EQ.2) GO TO 3000
       CALL BSTR(INTR,IW,LTFR)
       CALL BSAT(1,'TOFR')
       RETURN
3000   CONTINUE
C/////////
             WRITE(6,9300)
9300         FORMAT(1X,'BOS ERROR IN TOFMAS')
       RETURN
       END
