C   02/11/78 C9021901   MEMBER NAME  LGAVRP   (JADELGS)     FORTRAN
C
      SUBROUTINE LGAVRP(COTTH,IPHO,NPMIN,PHAV,PHSIG)
C
C     S.YAMADA   09-10-78  14:45        VERSION 1
C     RECOPIED FROM LVAVRZ AND MODIFIED FOR PHI
C     LAST MODIFICATION  20-10-78  10:40
C
C---- CALUCULATE THE AVERAGE POSITION OF THE SHOWER MAX.
C
C---- INPUTS
C     COTTH  COT(THETA) OF THE CLUSTER MEASURED FROM THE BEAM.
C     IPHO   REFERENCE COUNTER NO.THE FIRST DATA ACTS AS THE REFERENCE.
C            IT CORRESPONDS TO #42 IN THE PHI-ARRAY.
C     NPMIN  THE MINIMUM OF THE INDECES OF THE PHI COUNTERS.
C---- OUTPUTS
C     PHAV   THE AVERAGE PHI OF THE SHOWER
C     PHSIG  VARIANCE OF PHI WIDTH.
C
C---- PULSE HEIGHT OF EACH BAR SHOULD BE STORED IN PHI
      COMMON /CLGZPT/ ETOT,Y(32),WEIGHT(32),YFIT(32),DERIV(32),PHI(84)
      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)
      DATA PHUNIT/0.07479983/, PHUNI2/0.559501E-2/
      DATA UNIT/0.2721/
C     UNIT=1 RAD.LENG MEASURED BY RADIUS(3)*PHUNIT.
C
      COMMON/ CLGVRN/ NVRSN(20)
      DATA NVCODE/178102010/
      NVRSN(3) = NVCODE
C
C---- CALCULATE THE AVERAGE PHI(WEIGHT=PULSE HEIGHT)
      PHISUM = 0.
      PPSUM = 0.
C---- NPMIN   INDEX FOR THE FIRST HIT IN PHI
        DO 1 I=NPMIN,84
        IF(PHISUM.GT.0. .AND. PHI(I).LE.0.) GO TO 4
        IF(PHI(I).GT.0.) LST = I
        PHISUM = PHISUM+PHI(I)
        PPSUM = PPSUM+PHI(I)*I
    1   CONTINUE
C
    4 IF(PHISUM.LE.0.) PHISUM = 1.0
      PHAV = PPSUM/PHISUM
      IF(LST-NPMIN) 10,10,5
C
C---- FROM P.H. RATIO IN PHI,POSITION OF THE HIT POINT IS RECALCULATED.
C
C---- NP --- BAR# WHICH CONTAINS THE AVERAGE PH,NZ1 AND NZ2 ARE NEIGHBR.
    5 NP = PHAV+0.5
      NP1 = NP-1
      NP2 = NP+1
C
C---- ENERGY FRCTION OF LEFT<MIDDLE AND RIGHT PARTARE CALCULATED.
      Y1 = 0.
      IF(NP1.LT.NPMIN) GO TO 11
        DO 20 N=NPMIN,NP1
   20   Y1 = Y1+PHI(N)
   11 Y2 = PHI(NP)
      Y3 = 0.
      IF(NP2.GT.LST) GO TO 13
        DO 12 N=NP2,LST
   12   Y3 = Y3+PHI(N)
C
C---- NORMALIZATION
   13 Y1 = Y1/PHISUM
      Y2 = Y2/PHISUM
      Y3 = Y3/PHISUM
C
C---- FOR ALMOST SYMMETRIC CASE NO CORRECTION.
      IF(Y1.LT.0.01 .AND. Y3.LT.0.01) GO TO 10
      IF(Y1-Y3) 14,10,15
C
C---- Y1<Y3
   14 YMIN= Y3
      PHBND = NP+0.5
      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
      PHBND = NP-0.5
      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 Y --------------------
C        I    YMIN = 0.5*A**2/(DX+A)**2                  I
C        I    MONTE CARLO GIVES  A=0.3598                I
C        I    TEST DATA GIVES COTTH DEPENDENT A          I
C        I    A = 0.80-0.09*(ABS(COTTH)-2.0)**2          I
C        -------------------------------------------------
   16 A = 0.80-0.09*(ABS(COTTH)-2.0)**2
      DX = A*(1.0/SQRT(2.0*YMIN)-1.0)*UNIT
      IF(DX.GT.0.5) DX = 0.5
      PHAV = PHBND+S*DX
C
C---- CHANGE UNIT
   10 PHAV = (PHAV-41.5+IPHO)*PHUNIT
CCCC  PHSIG = PHSIG*PHUNI2  **** NOT CALCULATED YET*****
C
  100 RETURN
      END
