C   20/03/80 201261221  MEMBER NAME  CUTP81   (S)           FORTRAN
      SUBROUTINE CUTP81
C---  CUT OFF ADC-PEDESTALS (MICROPROCESSOR SUBTRACTED) BELOW 50 CH.
C---  (OR EVEN HIGHER, MAXIMUM:99, IF PEDESTAL HAS SHIFTED)
C---  & CREATE NEW VALUES FOR LNG, HPOINT, AND HGGADC
C---  (DEALS WITH 64 ADC-CHANNELS)
C
C     H.WRIEDT     12.06.79       14:00
C     LAST MODIFICATION     26.01.82      12:15
C
      IMPLICIT INTEGER*2 (H)
C
      COMMON /CWORK/ IWORK(42),LNG,HPOINT(4),HGGADC(2,64)
C
      DIMENSION NMBIN(9),NPBIN(9)
C
        DO 1 I = 2,9
        NMBIN(I) = 0
    1   NPBIN(I) = 0
C
      NDAT = LNG - 2
C---  COUNT NUMBER OF ENTRIES BETWEEN 20 & 100 ADC-CHANNELS ON EACH
C---  SIDE PER EVENT (10 CHANNEL-WISE)
        DO 3 I = 1,NDAT
        IBIN = HGGADC(2,I)/10
        IF (HGGADC(1,I).GT.31) GOTO 2
C       -Z - SIDE
        IF (IBIN.GE.2 .AND. IBIN.LE.9) NMBIN(IBIN) = NMBIN(IBIN) + 1
        GOTO 3
C       +Z - SIDE
    2   IF (IBIN.GE.2 .AND. IBIN.LE.9) NPBIN(IBIN) = NPBIN(IBIN) + 1
    3   CONTINUE
C---  CALCULATE NUMBER OF ADC-CHANNELS WITH CONTENT BETWEEN 20 & 99
      NMSUM = 0
      NPSUM = 0
        DO 5 I = 2,9
        NMSUM = NMSUM + NMBIN(I)
    5   NPSUM = NPSUM + NPBIN(I)
C---  FLAG ACCORDING TO NUMBER OF ADC-CHANNELS BETWEEN 20 & 99
C     IFLAG = 0  : 0 CHANNELS
C           = 1  : 1 - 4 CHANNELS
C           = 2  : > 4 CHANNELS
      IMFLAG = 0
      IF (NMSUM.GE.0) IMFLAG = 1
      IF (NMSUM.GE.5) IMFLAG = 2
      IPFLAG = 0
      IF (NPSUM.GE.0) IPFLAG = 1
      IF (NPSUM.GE.5) IPFLAG = 2
      IMCUT = 49
      IPCUT = 49
      IF (IMFLAG.LT.2 .AND. IPFLAG.LT.2) GOTO 90
C---  > 4 CHANNELS ON ONE SIDE BETWEEN 20 & 99 COUNTS
      MAXMCN = NMBIN(2)
      MAXMBN = 2
      MAXPCN = NPBIN(2)
      MAXPBN = 2
        DO  20 I = 3,9
        IF (IMFLAG.LT.2) GOTO 15
        IF (MAXMCN.GT.NMBIN(I)) GOTO 15
        MAXMCN = NMBIN(I)
        MAXMBN = I
   15   IF (IPFLAG.LT.2) GOTO 20
        IF (MAXPCN.GT.NPBIN(I)) GOTO 20
        MAXPCN = NPBIN(I)
        MAXPBN = I
   20   CONTINUE
C---  IF BIN WITH MAXIMUM NO OF COUNTS < 5 COUNTS: CUT AT 50
      IF (IMFLAG.EQ.2 .AND. MAXMCN.LT.5) IMFLAG = 1
      IF (IPFLAG.EQ.2 .AND. MAXPCN.LT.5) IPFLAG = 1
      IF (IMFLAG.LT.2 .AND. IPFLAG.LT.2) GOTO 90
C---  DETERMINE CUT-VALUE
      IF (IMFLAG.LT.2) GOTO 25
C     -Z - SIDE
      IBIN = MAXMBN
  21  IF (IBIN.LT.8) GOTO 22
      IMCUT = 99
      GOTO 25
  22  IF (NMBIN(IBIN+1).LT.5) GOTO 23
      IBIN = IBIN + 1
      GOTO 21
C     CUT ABOVE FIRST BIN WITH < 5 COUNTSABOVE MAXIMUM
C     (BUT NOT BELOW 49)
   23 IMCUT = IBIN*10 + 19
      IF (IMCUT.LT.49) IMCUT = 49
   25 IF (IPFLAG.LT.2) GOTO 90
C     +Z - SIDE
      IBIN = MAXPBN
  26  IF (IBIN.LT.8) GOTO 27
      IPCUT = 99
      GOTO 90
  27  IF (NPBIN(IBIN+1).LT.5) GOTO 28
      IBIN = IBIN + 1
      GOTO 26
C     CUT ABOVE FIRST BIN WITH < 5 COUNTSABOVE MAXIMUM
C     (BUT NOT BELOW 49)
   28 IPCUT = IBIN*10 + 19
      IF (IPCUT.LT.49) IPCUT = 49
C---  CUT AT 50 IF < 5 CHANNELS BETWEEN 20 & 99 COUNTS
   90 CONTINUE
        DO 100 I = 1,NDAT
        HADR = HGGADC(1,I)
        IF (HADR.LE.31 .AND. HGGADC(2,I).GT.IMCUT) GOTO 100
        IF (HADR.GE.32 .AND. HGGADC(2,I).GT.IPCUT) GOTO 101
C---    CUTOFF OF ADCS WITH LOW CONTENT
C       (THRESHOLD: 49 CHANNELS)
        HGGADC(2,I) = 0
        GOTO 100
C---    SCRATCH UNPHYSICAL ADC-ADDRESSES (I.E. ADDRESSES > 63
C---    AND CONTENTS
  101   IF (HADR.GT.63)
     &   HGGADC(2,I) = 0
  100   CONTINUE

  190 CALL GGSRTH(0,NDAT)
      LNG = 2
        DO 200 I = 1,NDAT
        IF (HGGADC(2,I).LE.0) GOTO 201
  200   LNG = I+2
  201 NDAT = LNG-2
      CALL GGSORT
C
C---  SET UP NEW POINTERS
      HPOINT(1) = 1
      HPOINT(2) = 1
      HPOINT(3) = 1
      IF (NDAT.LE.0) GOTO 211
        DO 210 I = 1,NDAT
        HADDR = HGGADC(1,I)
        IF (HADDR.LE.31) HPOINT(2) = 2*I+1
        IF (HADDR.LE.63) HPOINT(3) = 2*I+1
  210   CONTINUE
  211 HPOINT(4) = 2*NDAT+1
      RETURN
      END
