C   11/02/81 701191523  MEMBER NAME  ELGOBS0  (SOURCE)      FORTRAN
      FUNCTION ELGOBS(IBE,DC,EINC)
C
C  THIS PROGRAM CALCULATE THE OBSERVED ENERGY.
C  FROM THE TRUE ENERGY (FOR MONTE CARLO)     11/2/81  Y.WATANABE
C   LAST MOD    04-11-81   15:50   (DEBUGGED)
C
C  IBE; 0=BARREL, +-1=+,-END CAP
C  DC; THE DIRECTION COSINE
C
      DIMENSION DC(3)
      COMMON /CLGDMS/  X0,RADIUS(6),RADSX0(6),
     1  THX0(4),ZEND(2),ZENDX0(2),
     2  ZWID(2),ZGAP(2),PHWID(2),
     3  ZECAP(4),ZECAPX(4),THECPX(2),
     4  ECXLST(24),ECYLST(24)
C
C  THECPX(1)   THICKNESS OF END CAP COUNTER IN MM
C        (2)                                   X0
C  RADIUS(1,2) DISTANCE TO THE COIL SURFACE (INNER AND OUTER)
C        (3,4) DISTANCE TO THE LEAD GLASS SURFACE (TYPE A)
C  ZEND(1)     END POINT OF BARREL PART COUNTERS  (-)
C      (2)                                        (+)
C  ZGAP(1)     COUNTER GAP OF BARREL PART IN MM
C  THX0(1)     MAGNET COIL THICKNESS IN X0
C              X0 = 8.9 CM FOR ALUMINUM
C
      DATA PAI2/6.283185/
C
C  INITIAL SET UP
C
      ELGOBS=EINC
      IF(EINC.LT.0.001) RETURN
      DEP=SHMAX(EINC*1000,1)
      COSX=DC(1)
      COSY=DC(2)
      COSZ=DC(3)
C     WRITE(6,900) IBE,EINC,ELGOBS,COSX,COSY,COSZ,DEP
C900  FORMAT(I5,10F10.3)
C
      IF(IBE.EQ.0) GO TO 1
CCCCCCCCCCCCCCCCCCCCC
C
C  END CAP COUNTERS
C
CCCCCCCCCCCCCCCCCCCCC
C
C  EXTRAPOLATION TO THE FACE OF THE LIGHT GUIDE
C
      III=1
      IF(COSZ.GT.0.) III=2
      XCO=ZEND(III)*COSX/COSZ
      YCO=ZEND(III)*COSY/COSZ
      R = THECPX(1)/COSZ-DEP
      R=ABS(R)
      IF( R.LT.0. ) R=0.
      XX=XCO+R*COSX
      YY=YCO+R*COSY
C
      THETA=ARCOS(COSZ)
      THETA=ABS(THETA)*57.2958
      IF(IBE.EQ.-1) THETA=180.0-THETA
C     WRITE(6,900) III,R,XX,YY,THETA
C
C   ITERATION FOR INCIDENT ENERGY
C
C
C
C  DISTANCE FROM THE CENTER OF LIGHT GUIDE
C
10    X=XX/X0
      Y=YY/X0
      CALL LGNMEC(X,Y,ID)
C
C   CHECK IF HIT IS INSIDE THE COUNTER
C
C
      IF(ID.GT.0) GO TO 60
      X=XCO/X0
      Y=YCO/X0
      CALL LGNMEC(X,Y,ID)
C
      IF(ID.GT.0) GO TO 60
      ELGOBS=0.
      RETURN
C
   60 MB=(ID-1)/24 +1
      ID=MOD(ID-1,24)+1
      U=ECXLST(ID)
      V=ECYLST(ID)
      GO TO (4,5,6,7),MB
    4 X=U
      Y=V
      GO TO 8
    5 X=-V
      Y=U
      GO TO 8
    6 X=-U
      Y=-V
      GO TO 8
    7 X=V
      Y=-U
    8 R=0.1*SQRT((X-XX)*(X-XX)+(Y-YY)*(Y-YY))
      DDR=SQRT(XX*XX+YY*YY)-SQRT(X*X+Y*Y)
      IF(ABS(R).GT.1.E-10) DDR=0.1*DDR/R
C
C     POS DEP
C
      CALL POSEND(EINC,R,DDR,FAC,&80)
C
      ELGOBS=EINC*FAC
      RETURN
C
CCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C  BARREL PART COUNTERS
C
CCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C  EXTRAPOLATION TO THE FACE OF LIGHT GUIDE
C
    1 R=RADIUS(3)+DEP
      RPHI=SQRT(COSX*COSX+COSY*COSY)
      COSPHI=COSX/RPHI
      SINPHI=COSY/RPHI
C       X Y POSITION AT THE SHOWER MEDIAN.
      X=R*COSPHI
      Y=R*SINPHI
C
C     DISTANCE FROM THE SHOWER MAX POINT TO THE EXTRAPOLATED POINT
C           AT THE END FACE OF LEAD GLASS
      A=COSX*COSX+COSY*COSY
      B=X*COSX+Y*COSY
      C=X*X+Y*Y-RADIUS(4)*RADIUS(4)
      RDIF=(SQRT(B*B-A*C)-B)/A
C      X Y Z POSITION AT THE END FACE OF LG
      X=X+RDIF*COSX
      Y=Y+RDIF*COSY
C
      IF(ABS(X).LT.1.E-15) X=1.E-15
      THETA=ATAN2(Y,X)
      IF(THETA.LT.0.) THETA=THETA+PAI2
      STEP=PAI2/84.
      ITH=THETA/STEP
C
C       INCIDENT ANGLE
      ANGNOR=(ITH+0.5)*STEP
      COSNOR=COS(ANGNOR)
      SINNOR=SIN(ANGNOR)
      THETA=COSNOR*COSX+SINNOR*COSY
C     ABSORBER THICKNESS TRAVERSED BY THE PARTICLE.
      D=7.88/ABS(THETA)
      THETA=ARCOS(THETA)*57.2958
C     WRITE(6,910) ITH,ANGNOR,COSNOR,SINNOR,THETA,D
C910  FORMAT(I4,10F12.4)
C
C        ENERGY LOSS IN THE COIL
C
21    CALL ENGLOS(EINC,D,DELTAE,&80)
      ELGOBS=EINC-DELTAE
      IF(ELGOBS.GT.0.) GO TO 22
      GO TO 80
C
C       ANGLE DEPENDENCE
C
22    CALL ANGBAR(ELGOBS,THETA,AFAC,&80)
C
C     CALL POSBAR(ELGOBS,R,THETA,PFAC,&80)
C
CCC   FAC=PFAC*AFAC
      ELGOBS=ELGOBS*AFAC
C     WRITE(6,920) EINC,ELGOBS,AFAC,DELTAE
C920  FORMAT(' EINC,ELGOBS,AFAC,DELTAE',10F12.4)
      RETURN
C
C     ILLEGAL RETURN
C
   80 CONTINUE
      ELGOBS=0.
      RETURN
      END
