C   05/03/80 003210625  MEMBER NAME  MOCARD   (S)           FORTRAN
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
        SUBROUTINE MOCARD(INLA,INTF,IFLG,*)
C  SPECIAL VERSION OF TOFARD TO READ MONTE CARLO DATA
C          CONVERTS THE TOF DATA FROM CAMAC FORMAT
C       TO ANALYSIS FORMAT :
C                   IRAW(1,NCN)= NCN  ; COUNTER#
C      RAW(2,NCN)= TDC(-)-CM(NCN)-WM(NCN)*(1./SQRT(A0M)-1./SQRT(ADC(-)))
C      RAW(3,NCN)= TDC(+)-CP(NCN)-WP(NCN)*(1./SQRT(A0M)-1./SQRT(ADC(+)))
C       OR
C      RAW(2,NCN)= TDC(-) , IF IFLG= 1.
C      RAW(3,NCN)= TDC(+) , IF IFLG= 1.
C                   RAW(4,NCN) = ADC(-)
C                   RAW(5,NCN) = ADC(+)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CC
#include "ddata.for"
      INTEGER *2  HADC,HTDC,HB,HBLK,HSTAR,HPEK,HNG
       REAL *8 DYC1,DYC2
       COMMON/TFPRM/ NRNC1,NRNC2,DYC1,DYC2,IFLC(42),CM(42),CP(42),
     -DTAU(42),WM(42),WP(42),SV(42),DM(42),DP(42),PEDLM(42),PEDLP(42)
       COMMON/CWORK/NR,RAW(5,42),NC,ICRT1(5,42),NTRK,ICRT2(50),TRK(5,50)
     - ,ITRC(50),NTC,ITRK(5,42),INFM(4),IR(14,50)
      COMMON/TFPED/ HADC(2,42),HTDC(2,42),HB(42)
      DIMENSION MASK(16)
      DATA MASK/1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,
     1          16384,32768/
      DIMENSION CORADC(20)
      DIMENSION IX(7),IRAW(5,42),RX(210)
      EQUIVALENCE (IRAW(1,1),RAW(1,1)),(RX(1),RAW(1,1))
      DATA HBLK,HSTAR,HPEK,HNG/ '  ','**','XX','NG'/
      DATA A0M/ 200./,A0P/200./
      DATA CORADC/1.9,2.45,2.8,3.0,3.15,3.3,3.35,3.45,3.5,3.55,3.55,
     1            3.6,8*3.6/
      DATA IENTRY/0/,LERR/0/,IBUG/70/,JBUG/10/
      IF(IENTRY.NE.0)  GOTO 320
      IENTRY = IENTRY + 1
  101 FORMAT(/4(1X,10F12.3/),1X,2F12.3)
C
 320  DO 10 I=1,210
10    RX(I)= 0.
C
C========= ADDRESSING
C
      IBUG = IBUG + 1
      JBUG = JBUG + 1
       IER= 0
       INLA2= INLA*2
       INTF2= INTF*2
       IL0= INLA2+6
       IA0= INTF2+3
       IT0= INTF2+94
C//////////
C
        NUM= 0
        DO 3000 NCN= 1,42
C
C========= ADDRESS OF ADC AND TDC FOR COUNTER# NCN
C
        NCN2= NCN*2
        IAA= (NCN-1)/6+NCN2+IA0-2
        IAT= (NCN-1)/4+NCN2+IT0-2
        IAA1= IAA+1
        IAT1= IAT+1
        HADC(1,NCN)= HDATA(IAA)
        HADC(2,NCN)= HDATA(IAA1)
      IF(NRUN.LE.2306.OR.NCN.LE.38)  GOTO  75
      ADCM = HADC(1,NCN) - PEDLM(NCN)
      ADCP = HADC(2,NCN) - PEDLP(NCN)
      HADC(1,NCN) = ADCM*2+PEDLM(NCN)
      HADC(2,NCN) = ADCP*2+PEDLP(NCN)
   75 HTDC(1,NCN)= HDATA(IAT)
      HTDC(2,NCN)= HDATA(IAT1)
      HB(NCN)= HBLK
C
C========= CHECK OF LATCHED BIT
C
        NW= (NCN-1)/7+IL0
        MDATA= HDATA(NW)
        IF(MDATA) 100,3000, 100
 100    NORD= MOD(NCN,7)
      IF(NORD.EQ.0) NORD = 7
        IF(LAND(MDATA,MASK(NORD)).GT.0)  GOTO  1500
      GOTO  3000
C
C========= BIT HAD BEEN SET
C
 1500 CONTINUE
C-------- PULSE HEIGHT CORRECTION
      ADCM = HADC(1,NCN)
      ADCP = HADC(2,NCN)
      IF(ADCM.LE.0.) ADCM = 1.
      IF(ADCP.LE.0.) ADCP = 1.
        TCM= WM(NCN)*(1./SQRT(A0M)-1./SQRT(ADCM))
        TCP= WP(NCN)*(1./SQRT(A0P)-1./SQRT(ADCP))
        ADCM= HDATA(IAA)
        TDCM= HDATA(IAT)
      IF(TDCM.LT.10..OR.TDCM.GT.2048.)  GOTO  2000
        TDCM0= TDCM
        IF(IFLG.EQ.1) GO TO 1600
 1510   TDCM0= TDCM0*DM(NCN)
        TDCM0= TDCM0-CM(NCN)-TCM
 1600   IRAW(1,NCN)= NCN
        RAW(2,NCN)= TDCM0
        RAW(4,NCN)= ADCM
 2000   IAA1= IAA+1
        IAT1= IAT+1
        ADCP= HDATA(IAA1)
        TDCP= HDATA(IAT1)
      IF(TDCP.LT.10.OR.TDCP.GT.2048.)  GOTO  2100
        TDCP0= TDCP
        IF(IFLG.EQ.1) GO TO 2050
2010    TDCP0= TDCP0*DP(NCN)
        TDCP0= TDCP0-CP(NCN)-TCP
2050    IRAW(1,NCN)= NCN
        RAW(3,NCN)= TDCP0
        RAW(5,NCN)= ADCP
2100    CONTINUE
C/////////
 3000    CONTINUE
C/////////
       RETURN
 4000  CONTINUE
C/////////
  994 LERR = LERR + 1
      IF(LERR.GT.20) RETURN
 9440 FORMAT(' ERROR IN COMPUTING INDEX FOR ADC CORRECTION '/
     1       1X,2I5,4F10.4)
      RETURN
       END
