C   16/05/87 705201759  MEMBER NAME  TJOCK1   (S)           FORTRAN
      SUBROUTINE TJOCK(COSTHE,XLEN90,XLEN)
C
C SUBROUTINE TO CALCULATE ACTUAL THICKNESS OF ABSORBER IN FRONT OF
C  LEADGLASS BARREL.
C
C    INPUT IS COSTHE=COS(THETA), THETA WITH RESPECT TO BEAM
C
C    OUTPUT IS XLEN, THE ACTUAL RADIATION LENGTH
C    OUTPUT IS XLEN90, CORRESPONDING RAD.LENGTH AT RIGHT ANGLE
      INTEGER*2 HDATE
      COMMON /TODAY/ HDATE(6)
C
C
      COMMON /CJTRIG/ PI,TWOPI,PIHALF
C
C RAD.LENGTH FOR RESIN ASSUMED, MEAN OF PLEXIGLAS AND SCINTILLATOR
C
      DATA XRESIN /400./
      DATA XWATER /3610./
      DATA XALUMI /89./
C
C RAD.LENGTH FOR REST OF ABSORB MATERIAL FROM JCN
C THIS VALUE IS ADJUSTED FOR TWO ADDITIONAL LAYERS OF RESIN ABOVE AND
C BELOW THE COIL, EACH 3MM THICK (2 * 0.0075 X0)
C
C    19779-1983 VALUE
      DATA XRESI1 /.3235/
C    19784-1986 VALUE
      DATA XRESI2 /.3860/
C ZDRES IS THICKNESS OF RESIN SEPARATING WINDINGS IN Z-DIRECTION
      DATA ZDRES /1.2/
C----------------
C
      XRESID = XRESI1
      IF(HDATE(6).GT.1983) XRESID = XRESI2
C
      XLEN = 0.
C
      XLH2O = 0.
      XLAL = 0.
      TOTLEN = 77.
C
      COST = COSTHE
      IF(COST.LT.0) COST = - COST
C
C  GET Z-VALUE AT IMPACT ON INSIDE OF COIL, RADIUS IS SET TO 950 MM
C
C
C     WRITE(6,323) COST
323   FORMAT(' TJOCK WITH COST ',E14.6)
C
      IF(COST.LT..00158) GO TO 1050
      THE = ARCOS(COST)
C
C     WRITE(6,331) THE
331   FORMAT(' TJOCK WITH FIRST THE ',E14.6)
C
      THE = PIHALF - THE
      TANTHE = TAN(THE)
      COST1 = COS(THE)
C
      Z = 950. * TANTHE
C
C     WRITE(6,327) Z,TANTHE,THE
327   FORMAT(' TJOCK WITH Z AND TANTHE THE',3E14.6)
C
      IF(Z.GT.1700.) GO TO 9999
C
C GET LOCAL LINE EQUATION. 3 MM RESIN SEPARATES THE COIL WINDINGS. THESE
C ARE DISPLACED WITH RESPECT TO EACH OTHER BY HALF A UNIT
C
C   THE UNIT IN X(Z) IS THEN ZDRES*.5 RESIN, 31 MM AL, .5*ZDRES MM RESIN
C
C   THE LINE EQUATION IS THEN Y = Y0 + A*X
C
      ZUNIT = 31. + ZDRES
      ZFRAC = Z / ZUNIT
      X0 = Z - IFIX(ZFRAC) * ZUNIT
      A = 1./TANTHE
      Y0 = - A*X0
C
C     WRITE(6,344) A,X0,Y0
344   FORMAT(' TJOCK WITH A X0 Y0',3E14.6)
C
C ***********************************
C    GET TOTAL LENGTH
C
      XLOW = X0
      YLOW = 0.
      YUPP = 77.
      XUPP = (YUPP - Y0) / A
      TOTLEN = SQRT((YUPP-YLOW)**2 + (XUPP-XLOW)**2)
C
C     WRITE(6,324) XLOW,XUPP,TOTLEN
324   FORMAT(' TJOCK WITH XLOW XUPP TOTLEN',3E14.6)
C
C
C     LOOP OVER COILWINDINGS
C
      XORI = ZDRES*.5
      YORI = 0.
      XC = XORI + 15.5
      YC = 18.5
C
100   IF(XORI.GT.XUPP) GO TO 1000
      CALL LINSQU(Y0,A,XORI,YORI,FLEN)
      XLAL = XLAL + FLEN
      CALL LINCIR(Y0,A,XC,YC,FLEN)
      XLH2O = XLH2O + FLEN
C
      YORI = YORI + 40.
      YC = YC + 40.
      XORI = XORI - ZUNIT*.5
      XC = XC - ZUNIT*.5
      CALL LINSQU(Y0,A,XORI,YORI,FLEN)
      XLAL = XLAL + FLEN
      CALL LINCIR(Y0,A,XC,YC,FLEN)
      XLH2O = XLH2O + FLEN
C
      XORI = XORI + ZUNIT*1.5
      XC = XC + ZUNIT*1.5
      YORI = YORI - 40.
      YC = YC - 40.
      GO TO 100
C###################################
1000  CONTINUE
      XRES = TOTLEN - XLAL - 3.
      XLAL = XLAL - XLH2O
C
      XLEN = XLAL/XALUMI + XLH2O/XWATER + XRES/XRESIN
C
C     WRITE(6,325) XRES,XLAL,XLH2O,XLEN
325   FORMAT(' TJOCK WITH XRES XLAL XLH2O XLEN ',4E14.6)
C
C
C  XLEN IS LENGTH AT CERTAIN DIRECTION. GET CORRESPONDING LENGTH AT
C  RIGHT ANGLE
C
      XLEN1 = XLEN * COST1
      GO TO 1100
1050  XLEN1 = 74./XRESIN
      XLEN = 74./XRESIN
C
C  ADD THE RESIDUAL RADIATION LENGTH FROM CONSTANT THICKNESS MATERIALS
C
1100  XLEN2 = XLEN1 + XRESID
      XLEN3 = XLEN + XRESID
C
C     WRITE(6,326) XLEN,XLEN1,XLEN2,XLEN3
326   FORMAT(' TJOCK WITH XLEN XLEN1-3 AT END ',4E14.6)
C
      XLEN90 = XLEN2
      XLEN = XLEN3
C
9999  CONTINUE
      RETURN
      END
      SUBROUTINE LINSQU(Y0,A,XORI,YORI,FLEN)
C
C  CALCULATE LENGTH OF LINE Y = Y0 + A*X INSIDE THE RECTANGLE WITH
C  BOTTOM LEFT CORNER AT XORI, YORI  AND SIDE LENGTHS 31 (X) AND 37 (Y)
C
      FLEN = 0.
C
C     WRITE(6,1541) Y0,A,XORI,YORI
1541  FORMAT(' LINSQU WITH Y0 A X-YORI ',4E14.6)
C
C-----------------
C      CUT SIDE 1 ?
C
      Y1 = YORI
      X1 = (Y1 - Y0)/A
      IF(X1.GE.XORI.AND.X1.LE.XORI+31.) GO TO 100
C
C-----------------
C      CUT SIDE 2 ?
C
      X1 = XORI
      Y1 = Y0 + A * X1
      IF(Y1.GE.YORI.AND.Y1.LE.YORI+37.) GO TO 100
C
C
C     WRITE(6,1542) Y0,A,XORI,YORI
1542  FORMAT(' LINSQU EXIT WITHOUT HIT ',4E14.6)
C
      GO TO 999
C
100   CONTINUE
C-----------------
C      CUT SIDE 3 ?
C
      Y2 = YORI + 37.
      X2 = (Y2 - Y0)/A
      IF(X2.GE.XORI.AND.X2.LE.XORI+31.) GO TO 200
C
C-----------------
C      CUT SIDE 4 ?
C
      X2 = XORI + 31.
      Y2 = Y0 + A * X2
      IF(Y2.GE.YORI.AND.Y2.LE.YORI+37.) GO TO 200
C
      WRITE(6,6777) X0,A,XORI,YORI
6777  FORMAT(' LINSQU ERROR: X0 A XORI,YORI ',4E14.6)
      GO TO 999
C
200   CONTINUE
      FLEN = SQRT((Y2-Y1)**2 + (X2-X1)**2)
C
C     WRITE(6,1543) FLEN,Y2,Y1,X2,X1
1543  FORMAT(' LINSQU EXIT WITH FLEN Y2-1 X2-1',5E14.6)
C
999   CONTINUE
      RETURN
      END
      SUBROUTINE LINCIR(Y0,A,XC,YC,FLEN)
C
C  CALCULATE LENGTH OF LINE Y = Y0 + A*X INSIDE THE CIRCLE WITH
C  CENTER AT XC,YC  AND RADIUS 10 MM
C
      FLEN = 0.
C
      FA = 1. + A*A
C
      FQ = XC**2 + (Y0-YC)**2 - 100.
      FQ = FQ / FA
C
      FP = (A*Y0 - XC - A*YC)/FA
C
      D = FP**2 - FQ
      IF(D.LE.0.) GO TO 999
C
C     D > 0, THERE ARE TWO REAL CROSS POINTS
C
      DSQ = SQRT(D)
      X1 = -FP + DSQ
      X2 = -FP - DSQ
      Y1 = Y0 + A*X1
      Y2 = Y0 + A*X2
C
      FLEN = SQRT((Y2-Y1)**2 + (X2-X1)**2)
C
999   CONTINUE
      RETURN
      END
