C   11/03/79 C9073001   MEMBER NAME  LGCLUS   (SOURCE)      FORTRAN
      SUBROUTINE LGCLUS(JP,INIT,LNGJP,IER)
C
C     S.YAMADA      06-10-78  08:50    VERSION 2 OF LGCLUS
C     LAST MODIFICATION   16-07-79  17:30
C
C     LOCATE HIT CLUSTERS IN THE LEAD-GLASS COUNTERS
C     ADC-S BELONGING TO A CLUSTER ARE GROUPED TOGETHER AND THE MAP OF
C     THE STARTING MEMBER FOR EACH CLUSTER IS MADE.
C     (SOME MODIFICATIONS ARE MADE FOR SPEED UP)
C
      IMPLICIT INTEGER *2 (H)
C
C---- THERESHOLDS FOR THE CLUSTER FINDING ARE DEFINED IN THE BLOCK DATA.
      COMMON /CLGPRM/ ITH,MAXCLS,IRLTHD,IRLTH2,IRLTH3
C
#include "clgwork1.for"
C
      DIMENSION LGADC(2000)
      EQUIVALENCE (LGADC(1),HLGADC(1,1))
C
C---- NCLST = NO.OF THE LOCATED CLUSTERS.
C---- HMAPCL(1,J)=THE INDEX OF THE FIRST MEMBER OF THE J-TH CLUSTER
C               IN THE HLGADC.
C---- HMAPCL(2,J)=THE INDEX OF THE LAST MEMBER OF THE J-TH CLUSTER.
C
      COMMON /CLGMSB/ MESVAL
C
      DIMENSION LIST(5)
C     (LIST) STORES NEIGHBOUR POSITIONS.
C
C---- INPUT
C     JP      PART OF THE LG-COUNTER TO BE ANALYSED, 1 FOR BARREL
C                                                 2 FOR Z<0 END CAP
C                                                 3 FOR Z>0 END CAP
C     INIT    INITIAL DATA IN THE HLGADC FOR THE JP-TH PART
C     LNGJP     NO. OF ADC DATA FOR THE JP-TH PART
C
      COMMON/ CLGVRN/ NVRSN(20)
      DATA NVCODE/379071113/
      NVRSN(8) = NVCODE
C
      IER = 0
      IF(LNGJP) 80,80,2000
C
C---- ADC DATA SORTING ACCORDING TO THE PULSE HEIGHT
 2000 CALL LGSRTH(INIT-1,LNGJP)
      KEND = INIT+LNGJP-1
C
C---- INITIALIZATION
      NCLST = NCLST+1
      IF(NCLST.GT.MAXCLS) GO TO 90
      HMAPCL(1,NCLST) = INIT
      IPRNT = INIT
C
C---- SET THRESHOLDS
   30 IF(HLGADC(2,IPRNT).LT.ITH) GO TO 70
      IABTHD = HLGADC(2,IPRNT)/IRLTHD
      IABTH2 = HLGADC(2,IPRNT)/IRLTH2
C
C---- NEXT IS THE INDEX OF THE 1-ST NONREPLACED MEMBER.
      NEXT = IPRNT+1
   20 IF(NEXT.GT.KEND) GO TO 4
C---- OBTAIN THE NEIGHBOUR LIST FOR THE IPRNT-TH ADC.
      HJPPRN = HLGADC(1,IPRNT)
      CALL LGNBLS(HJPPRN,NNBR,LIST)
C
      LGAPRN = HLGADC(2,IPRNT)
      IABTH3 = LGAPRN*IRLTH3
C---- LOOK FOR DAUGHTERS
      NCAND = NEXT
   10   JPCND = HLGADC(1,NCAND)
C----   GLOBAL POSITION CHECK
        JDF = JPCND-HJPPRN
        IF(JDF.LT.0) JDF = -JDF
        IF(JDF.GT.80.AND.JDF.LT.2656) GO TO 1
C
C----   CAND IS CLOSE TO PRNT.SURVEY THE NEIGHBOUR TABLE.
          DO 2 NN=1,NNBR
          IF(JPCND.EQ.LIST(NN)) GO TO 40
    2     CONTINUE
        GO TO 1
C
C----   A NEIGHBOUR IS FOUND. CHECK PULSE HEIGHT.
   40   IF(LGAPRN.GE.IABTHD) GO TO 3
C----   TAIL OF THE CLUSTER
        IF(HLGADC(2,NCAND).GT.IABTH2) GO TO 1
        IF(HLGADC(2,NCAND).GT.IABTH3) GO TO 1
C
C----   THE FOUND NEIGHBOUR IS MOVED TO FOLLOW THE PARENT.
    3   IF(NEXT-NCAND) 6,5,5
    6   LWORK = LGADC(NCAND)
C       SHIFT THE DATA BETWEEN NEXT AND NCAND.
        NSHIFT = NCAND-NEXT
          DO 60 NN=1,NSHIFT
          LL = NCAND-NN
          LGADC(LL+1) = LGADC(LL)
   60     CONTINUE
        LGADC(NEXT) = LWORK
C       PROCEED TO THE NEXT PLACE.
    5   NEXT = NEXT+1
    1   NCAND = NCAND+1
      IF(NCAND.LE.KEND) GO TO 10
C
C---- A SURVEY IS OVER FOR A PARENT.
C---- IF A NEW NEIGHBOUR WAS FOUND,TREAT IT AS A PARENT.
      IPRNT = IPRNT+1
      IF(IPRNT.LT.NEXT) GO TO 20
C
C---- ALL NEIGHBOURS ARE ALREADY FOUND.
C     STORE THE LAST * STARTING INDEX OF THE BLOCK INTO HMAPCL
    4 HMAPCL(2,NCLST) = NEXT-1
      NCLST = NCLST+1
      HMAPCL(1,NCLST) = NEXT
      IF(NCLST.GT.MAXCLS) GO TO 90
C---- CHECK IF ANY MORE DATA IS LEFT
      IF(NEXT.LE.KEND) GO TO 30
C
C---- CLUSTER SEARCH IS FINISHED.
C
C---- SET THE LAST CLUSTER NO. FOR EACH PART INTO NCLBEC
   70 NCLST = NCLST-1
   80 NCLBEC(JP) = NCLST
      GO TO 100
C
C---- TOO MANY CLUSTER
   90 NCLST = NCLST-1
      NCLBEC(JP) = NCLST
      CALL LGMESG( 4, 1)
      IER = 10
  100 RETURN
      END
