C   02/11/78 C9101501   MEMBER NAME  LGAVRZ   (SOURCE)      FORTRAN
C
      SUBROUTINE LGAVRZ(JEG,Z0,ZAV,ZSIG,DEPAV)
C
C     S.YAMADA   03-09-78  21:15        VERSION 2
C     RECOPIED FROM LVAVRZ IN(LGTESTP.S) AND MODIFIED FOR REAL DATA
C     LAST MODIFICATION  28-02-79  16:50
C
C---- CALUCULATE THE AVERAGE POSITION OF THE SHOWER MAX.
C
C---- INPUTS
C     JEG = 1 FOR EL/POS,  =0 FOR GAMMA
C     Z0     THE VERTEX POSITION OF THE EVENT.IT IS USED ONLY IF SINTO=000001200
C---- OUTPUTS
C     ZAV    THE AVERAGE Z OF THE SHOWER
C     ZSIG   VARIANCE OF Z WIDTH.
C     DEPAV  THE AVRG DEPTH OF THE SHOWER IN R(MEASURED FROM SURFACE)
C
C---- PULSE HEIGHT OF EACH RING SHOULD BE STORED IN Y
      COMMON /CLGZPT/ ETOT,Y(32),WEIGHT(32),YFIT(32),DERIV(32)
      COMMON /CLGDMS/ X0,RADIUS(6),RADSX0(6),THX0(4),
     $                ZEND(2),ZENDX0(2),ZWID(2),ZGAP(2),PHWID(2),
     $                ZECAP(4),ZECAPX(4),THECPX(2)
C
      COMMON /CLGMSB/ MSGVAL,AMSG1,AMSG2
C
      COMMON/ CLGVRN/ NVRSN(20)
      DATA NVCODE/278112410/
      NVRSN(4) = NVCODE
C
C---- CALCULATE THE AVERAGE Z (WEIGHT=PULSE HEIGHT)
      Z = ZEND(1)+0.5*ZWID(1)
      ZSTEP = ZWID(1)+ZGAP(1)
      YSUM = 0.
      YZSUM = 0.
C---- IST    INDEX FOR THE FIRST HIT IN Y
C---- LST    INDEX FOR THE LAST HIT IN Y
        DO 1 I=1,32
        IF(YSUM.LE.0.0 .AND. Y(I).GT.0.) IST = I
        IF(YSUM.GT.0. .AND. Y(I).LE.0.) GO TO 4
        IF(Y(I).GT.0.) LST = I
        YSUM = YSUM+Y(I)
        YZSUM = YZSUM+Z*Y(I)
        Z = Z+ZSTEP
    1   CONTINUE
C
    4 IF(YSUM.LE.0.) YSUM = 1.0
      ZAV = YZSUM/YSUM
      IF(ABS(ZAV-Z0).GT.360.) GO TO 8
C---- ONLY FOR HIGH ENERGY SHOWER
      IF(ETOT.LT.2000. .OR. LST.EQ.IST) GO TO 8
C
C     FOR NEARLY PARAREL INCIDENT ZAV IS CORRECTEDFOR QUANTIZATION EFF.
C---- FROM THE P.H.RATIO IN Z,POSITION OF THE HIT POINT IS RECALCULATED.
C---- THIS PART IS COPIED FROM AVTOZH(AN ENTRY POINT OF ORIGINAL LGAVRZ)
C
C---- NZ --- RING# WHICH CONTAINS THE AVERAGE Z,NZ1 AND NZ2 ARE NEIGHBR.
      NZ1 = (ZAV-ZEND(1))/ZSTEP
      NZ = NZ1+1
      NZ2 = NZ+1
C
C---- ENERGY FRCTION OF LEFT<MIDDLE AND RIGHT PARTARE CALCULATED.
      Y1 = 0.
      IF(NZ1.LT.IST) GO TO 11
        DO 20 N=IST,NZ1
   20   Y1 = Y1+Y(N)
   11 Y2 = Y(NZ)
      Y3 = 0.
      IF(NZ2.GT.LST) GO TO 13
        DO 12 N=NZ2,LST
   12   Y3 = Y3+Y(N)
C
C---- NORMALIZATION
   13 Y1 = Y1/YSUM
      Y2 = Y2/YSUM
      Y3 = Y3/YSUM
C
C---- FOR ALMOST SYMMETRIC CASE NO CORRECTION.
      IF(Y1.LT.0.016 .AND. Y3.LT.0.016) GO TO 8
      IF(Y1-Y3) 14,8,15
C
C---- Y1<Y3
   14 YMIN= Y3
      XBND = ZEND(1)+NZ*ZSTEP
      IF(YMIN.LE.0.5) GO TO 140
C---- Y1+Y2<Y3
C---- AVZ DOES NOT CORRESPOND TO THE HIGHEST P.H. BUT GO ON!
      YMIN = Y1+Y2
      S = 1.0
      GO TO 16
  140 S = -1.
      GO TO 16
C
C---- Y1>Y3
   15 YMIN = Y1
      XBND = ZEND(1)+NZ1*ZSTEP
      IF(YMIN.LE.0.5) GO TO 150
C---- Y2+Y3<Y1
      YMIN = Y2+Y3
      S = -1.0
      GO TO 16
  150 S = 1.0
C
C---- EMPIRICAL FORMULA FOR Z --------------------
C     YMIN = 0.5*A**2/(DX+A)**2                  I
C     A=0.3598            <<<TEMP 0.50 FOR Z>>>  I
C-------------------------------------------------
      DATA A/0.5000/
   16 HB = 0.5*ZWID(1)
      DX = A*(1.0/SQRT(2.0*YMIN)-1.0)*X0
      IF(DX.GT.HB) DX = HB
      ZAV = XBND+S*DX
C
C       ***********************************
C----   *   AVERAGE DEPTH OF THE SHOWER   *
C       ***********************************
C---- SHOWER ANGLE IS NOT KNOWN.
C---- ITERATION IS USED.
    8 DEPO = 0.
      SM = SHMAX(ETOT,JEG)
      NTRY = 0
C---- SM=AVERAGE SHOWER POSITION ALONG THE SHOWER AXIS.
    2 NTRY = NTRY+1
      IF(NTRY.GT.20) GO TO 21
      DEP = SM/SQRT(1.0+ZAV**2/(RADIUS(3)+DEPO)**2)
      IF(ABS(DEP-DEPO)/DEP.LE.0.01) GO TO 3
      DEPO = DEP
      GO TO 2
   21 MSGVAL = NTRY
      AMSG1 = DEPO
      AMSG2 = DEP
      CALL MESSAG( 5, 1 )
C
    3 DEPAV = DEP
  100 RETURN
      END
