C   27/04/79 C9050201   MEMBER NAME  RDREAD   (S)           FORTRAN
      SUBROUTINE RDREAD( NUNIT, HOPT, * )
C-----------------------------------------------------------

C      VERSION OF 27/04/79       LAST MOD 30/04/79
C      THIS ROUTINE READS MT CARLO DATA FROM UNIT NUNIT.
C      RETURN 1 IF END OF FILE ENCOUNTERED.
C      THE DATA HAVE TO BE IN BOS FORMAT. THE FIRST
C      RECORD WHICH IS READ HAS TO BE THE RECORD WITH THE MTC. CONST.
C      THE CONST ARE PUT INTO THE MT CARLO DATA COMMONS
C      EVENTS ARE READ INTO THE COMMON/ BCS /
C      DRIFT TIMES AND Z COORDINATES ARE PUT INTO PROPER BINNINGS
C      THERE ARE TWO OPTIONS IOPT:
C       1. HOPT='DE'      DEFAULT VALUES FOR DRIFT TIME BINS AND
C                         Z AMPLITUDES ARE INSERTED.
C                         THE DEFAULT VALUES ARE STORED IN FIDEF AND
C                         ZDEF
C       2. HOPT='SE'      THE USER HAS TO PROVIDE PHI AND Z BINNING
C                         THROUGH THE COMMON/CBIN/
C-----------------------------------------------------------
C
      IMPLICIT INTEGER * 2 (H)
C
#include "cmubcs.for"
C
      COMMON/CGEO1/IGEO(66)
C
      DIMENSION SAVTIM(6),IDOUBL(3)
      COMMON/CJDRCH/ DRCH(34)
C
      COMMON/C4VEC/I4VEC(424)
C
C      USER SUPPLIED COMMON/CBIN/
C
      COMMON/CBIN/TIME(6),ZOF,ZRS,ZL,ZSC,EPSI(3),DOUB(3),IRN(3)
C
C      TIME     BIN WIDTH FOR DRIFT TIME IN MM
C               WIRES ARE PUT TOGETHER IN GROUPS OF 8
C               DEFAULT.. 6 * .35 MM
C      ZOF      OFFSET IN Z  DIRECTION IN MM FOR AMPLITUDE CALCULATION
C               DEFAULT -10. MM
C      ZRS      STANDARD DEV FOR Z RESOLUTION IN MM
C               DEFAULT  20 MM
C      ZL       EFFECTIVE WIRE LENGTH IN MM
C               DEFAULT  2687 MM
C      ZSC      SCALING FACTOR FOR Z AMPLITUDES
C               DEFAULT 1.
C      EPSI     EFFICIENCY IN EACH RING
C               DEFAULT 3 * 1.
C      DOUB     DOUBLE TRACK RESOLUTION IN MM IN EACH RING
C               DEFAULT 3 * 5.
C      IRN      NO OF RANDOM HITS INSERTED IN EACH RING
C               DEFAULT 3 * 0
C
C      SET DEFAULT VALUES
C
      DIMENSION TIMDEL(6)
      DIMENSION EPSOPT(3)
      DIMENSION DBOPT(3)
      DIMENSION IRNOPT(3)
      DATA TIMDEL  /6*0.35/
      REAL ZOFFS   /-10./
      REAL ZRESOL  /20./
      REAL ZAL     /2687./
      REAL ZSCAL   /1./
      DATA EPSOPT  /3*1./
      DATA DBOPT   /3*5./
      DATA IRNOPT  /3*0/
C
      DATA HOPT1/'DE'/
C
      LOGICAL CALIBS,CALIB1,CALIB2
      DATA CALIBS/.FALSE./,CALIB1/.FALSE./,CALIB2/.FALSE./
C CALIB1 IS .TRUE. IF 1ST CALIB. DATA RECORD ('MTCO') HAS BEEN READ,
C CALIB2 IS .TRUE. IF 2ND CALIB. DATA RECORD ('MUCO') HAS BEEN READ.
C CALIBS IS .TRUE. IF BOTH CALIB DATA RECORDS HAVE BEEN READ.
C
 1001 WRITE(6,9101)
 9101 FORMAT(' *********   READ ERROR IN RDREAD')
 1000 CONTINUE
C MAIN READ STATEMENT.
      CALL BSLT
      CALL BDLG
      CALL BREAD( NUNIT, &1001, &98 )
      IF(CALIBS)GO TO 20
      IPJ = IDATA( IBLN( 'HEAD' ) )
      IF( IPJ .NE. 0 ) GO TO 91
      IPJ = IDATA( IBLN( 'MUCO' ) )
      IF( IPJ .NE. 0 ) GO TO 21
      IPJ = IDATA( IBLN( 'MTCO' ) )
      IF( IPJ .EQ. 0 ) GO TO 93
C
 23   CONTINUE
C       POINTER TABLE FOR CONST
C       POINTER  1   POSITION 55 JETCH CONST /CJDRCH/
C       POINTER  2   POSITION 56 GEOM CONST /CGEO1/
      CALL BPRS( 'MTCO', 1 )
      CALL BPRS( 'MJET', 2 )
      CALL BPRS( 'MGEO', 3 )
      IP1 = IDATA( IBLN( 'MJET' ) )
      IP2 = IDATA( IBLN( 'MGEO' ) )
      LJET = IDATA(IP1)
      LGEO = IDATA(IP2)
      DO 1 I=1,LJET
    1 DRCH(I) = ADATA(IP1+I)
      DO 2 I=1,LGEO
    2 IGEO(I) = IDATA(IP2+I)
C
C      SAVE TIMING BIN WIDTH OF MTC GENERATION
C
      DO 8 I=1,6
    8 SAVTIM(I) = DRCH(I+19)
C
      IF( HOPT.EQ.HOPT1 ) GO TO 10
C
C      INSERT USER SUPPLIED VALUES FOR RESOLUTION INTO COMMONS
C      THE DEFAULT OPTIONS ARE OVERWRITTEN
C
      DO 6 I=1,6
    6 TIMDEL(I)= TIME(I)
      ZOFFS    = ZOF
      ZRESOL   = ZRS
      ZAL      = ZL
      ZSCAL    = ZSC
      DO 7 I=1,3
      EPSOPT(I) = EPSI(I)
      DBOPT(I)  = DOUB(I)
    7 IRNOPT(I) = IRN(I)
C
C      INSERT DEFAULT VALUES FOR RESOLUTION INTO COMMONS
C
   10 CONTINUE
      DO 11 I=1,6
      SAVTIM(I) = SAVTIM(I)/TIMDEL(I)
   11 DRCH(19+I) = TIMDEL(I)
      DRCH(27)  = ZOFFS
      DRCH(28)  = ZRESOL
      DRCH(30)  = ZAL
      DRCH(31)  = ZSCAL
C
C      PRINT ACTUAL CONST
C
      WRITE(6,100) (DRCH(I),I=20,32),DBOPT,IRNOPT,EPSOPT
  100 FORMAT(20X,'ACTUAL CONST FOR CENTRAL DETECTOR',/,
     * 5X,'TIMING BINS IN MM ',6(1X,F7.3),/,
     * 5X,'ZMAX               ',F9.3,/,
     * 5X,'ZOFFS              ',F9.3,/,
     * 5X,'ZRESOL             ',F9.3,/,
     * 5X,'ZNORM              ',F9.3,/,
     * 5X,'ZAL                ',F9.3,/,
     * 5X,'ZSCAL              ',F9.3,/,
     * 5X,'TILT ANGLE         ',F9.3,/,
     * 5X,'DOUB HIT RES IN MM',3(1X,F7.3),/,
     * 5X,'NR OF RDM HITS    ',3(1X,I3),/,
     * 5X,'EFFICIENCY        ',3(1X,F7.3))
C
      CALIB1 = .TRUE.
      CALIBS = CALIB1.AND.CALIB2
      GO TO 1000
C
 21   CONTINUE
C READ MU CHAMBER CALIBRATION DATA
C THERE ARE 3 ALTERNATIVE ROUTINES TO MOVE MUON CALIBRATION DATA OUT OF
C    THE INPUT DATA ARRAY INTO COMMON AREAS.
C 1)MUCON SIMPLY TRANSFERS ALL 4000 I*4 WORDS INTO THE FULL MUON
C    CALIBRATION DATA COMMONS, /CMFFIX/, ETC.
C 2)MUCONT TAKES CALIBRATION DATA STRAIGHT OUT OF THE INPUT DATA ARRAY
C    AND PRODUCES A CONDENSED VERSION OF CALIBRATION DATA IN /CMUTNY/.
C    IT AVOIDS USING THE FULL MUON CALIBRATION DATA COMMONS, /CMFFIX/,
C    ETC., THUS SAVING ABOUT 3500 I*4 WORDS (NET).
C 3)MUCONB DOES THE SAME, BUT USES BOS.
      CALL BPRS( 'MUCO', 1 )
      CALL MUCON( 0 )
C
      CALIB2 = .TRUE.
      CALIBS = CALIB1.AND.CALIB2
      GO TO 1000
C
 20   CONTINUE
C CALIBRATION DATA COMPLETE.  EVENTS NORMALLY FOLLOW.
C IF, HOWEVER, A CALIB. DATA RECORD IS READ, REFILL THE APPROPRIATE
C   COMMONS.  (ALTERNATIVELY SKIP THEM BY WRITING GO TO 1000 ON
C   NEXT 2 IF'S.)
      IPJ = IDATA( IBLN( 'MTCO' ) )
      IF( IPJ .NE. 0 ) GO TO  23
      IPJ = IDATA( IBLN( 'MUCO' ) )
      IF( IPJ .NE. 0 ) GO TO  21
      IPJ = IDATA( IBLN( 'VECT' ) )
      IF( IPJ .NE. 0 ) GO TO  300
      IPJ = IDATA( IBLN( 'HEAD' ) )
      IF( IPJ .EQ. 0 ) GO TO  93
      GO TO 400
C
C      FILL /C4VEC/ FROM ID
C      I4VEC(1) = EVENT NUMBER
C      I4VEC(5) TO I4VEC(8) = 4 VECTOR COMPONENTS (REAL'S)
C      I4VEC(9) = MASS (REAL)
C      I4VEC(10)= CHARGE (INTEGER)
C      I4VEC(11)= PARTICLE TYPE (INTEGER)
C
  300 CONTINUE
      LL0 = IDATA( IPJ )
      LL = IDATA( LL0 )
      DO 301 L=1,LL
  301 I4VEC(L)=IDATA( L + LL0 )
      GO TO 1000
C
C PUT DATA RECORD INTO / BCS /
C
C      POINTER TABLE
C      BANK  1  HEAD
C      BANK  2  TRIG     POINTER ON LOC 55 IN HEAD BANK
C      BANK  3  SCAL     POINTER ON LOC 56 IN HEAD BANK
C      BANK  4  LATC     POINTER ON LOC 57 IN HEAD BANK
C      BANK  5  ATST     POINTER ON LOC 58 IN HEAD BANK
C      BANK  6  ATOF     POINTER ON LOC 59 IN HEAD BANK
C      BANK  7  ALGL     POINTER ON LOC 60 IN HEAD BANK
C      BANK  8  JETC     POINTER ON LOC 61 IN HEAD BANK
C      BANK  9  CONC     POINTER ON LOC 62 IN HEAD BANK
C      BANK 10  MUEV     POINTER ON LOC 63 IN HEAD BANK
C
C
C      SET UP JET CHAMBER DATA
C
  400 CONTINUE
      IPJ = IDATA( IBLN( 'JETC' ) )
      IF( IPJ .EQ. 0 ) RETURN
C
      CALL RDRESO( SAVTIM )
C
CCC CONSIDER DOUBLE HIT RESOLUTION, INEFFICIENCIES AND RANDOM HITS
      DO 52 I3=1,3
   52 IDOUBL(I3) = DBOPT(I3) / DRCH(2*I3+18)
      CALL RDMODN( IRNOPT, EPSOPT, IDOUBL )
C
CCC SET CORRECT STANDARD DEVIATIONS IN PATR BANK
      CALL RDPATR
      RETURN
C
C      ERROR EXITS
C
   91 WRITE(6,191)
  191 FORMAT(5X,6('*'),3X,'READMC FOUND AN EVENT RECORD BEFORE ALL CALIB
     *RATION DATA RECORDS WERE READ')
      STOP 777
C
 98   CONTINUE
      IF(CALIBS)GO TO 99
      WRITE(6,198)
  198 FORMAT(5X,6('*'),3X,'EOF ENCOUNTERED BEFORE ALL CALIBRATION DATA H
     *AS BEEN READ')
      STOP 777
C
 93   WRITE(6,193)
 193  FORMAT(5X,'******  UNRECOGNISED RECORD READ IN READMC')
      STOP 777
C
   99 RETURN 1
      END
