C   16/08/80 008210544  MEMBER NAME  LGSUBC                 FORTRAN
      SUBROUTINE LGSUBC(IC,HSUB)
C         CALUCULATES THE SUBTRACTION CONST WHEN TOO MANY HITS
C         OCCUR IN CRATE IC. IC=4 FOR ALL CRATES.
C         HSUB=AV+3*SIGMA OF 80% LOWEST PH.
C                       16/8/80       Y.WATANABE
      IMPLICIT INTEGER *2 (H)
      COMMON /CWORK/ LNG,HNORD,HNORML,HPOINT(4),HLGADC(2,2880),
     1               HTEMP(2880)
      DIMENSION HC(2,4)
      DATA HC/0,959,960,1919,1920,2879,0,2879/
C     ORDER THE PULSE HIGHT FROM THE LOWEST
      HIT=LNG-3
      HSUB=0
      IF(HIT.LT.1) RETURN
      ND=0
      DO 10 I=1,HIT
      IF(HLGADC(1,I).LT.HC(1,IC)) GO TO 10
      IF(HLGADC(1,I).GT.HC(2,IC)) GO TO 15
      ND=ND+1
      HTEMP(ND)=HLGADC(2,I)
10    CONTINUE
15    IF(ND.LT.1) RETURN
C     WRITE(6,900) IC,HIT,ND
C     WRITE(6,910) (HTEMP(J),J=1,ND)
C
      M = ND
   20 M = M/2
      IF(M.EQ.0) GO TO 50
      K = ND-M
        DO 40 J=1,K
        I = J
   30   IF(I.LE.0) GO TO 40
        IF( HTEMP(I+M).GE.HTEMP(I) ) GO TO 40
C----   REPLACING
        HW = HTEMP(I)
        HTEMP(I) = HTEMP(I+M)
        HTEMP(I+M) = HW
        I = I-M
        GO TO 30
   40   CONTINUE
      GO TO 20
C
50    CONTINUE
C     WRITE(6,900) IC,HIT,ND
C900  FORMAT('0 LGSUBC; IC,HIT,ND',10I6)
C     WRITE(6,910) (HTEMP(J),J=1,ND)
C910  FORMAT(20I6)
      ND=ND-30
      IF(IC.EQ.4) ND=ND-90
      IF(ND.LT.1) RETURN
      XM=0.
      S=0.
      DO 60 I=1,ND
      PH=HTEMP(I)
      XM=XM+PH
      S=S+PH*PH
60    CONTINUE
C
      XM=XM/ND
      S=S/ND-XM*XM
      IF(S.LT.0.) S=0.
      S=SQRT(S)
      HSUB=XM+3.*S
C     WRITE(6,920) IC,ND,HSUB,XM,S
C920  FORMAT(' LGSUBC; IC,ND,SUB,XM,S',3I6,6F10.3)
      RETURN
      END
