C   12/03/84 412041848  MEMBER NAME  TAGCLS   (S)           FORTRAN
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C
C
       SUBROUTINE TAGCLS(ISTART,IEND,THRESH,IWRITE)
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C THIS ROUTINE FINDS THE CLUSTERS IN THE TAGGING SYSTEM
C BY LOOKING FOR BLOCKS WITH LOTS OF ENERGY AND ASSUMING ALL
C ITS NEIGHBOURS ARE POTENTIAL MEMBERS OF THE CLUSTER AND THERFORE
C CAN'T BE PARENTS OF ANOTHER CLUSTER.
C
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
      COMMON/COMTAG/LISTOK,NLIST(64,9,2),
     1XMAP(64),YMAP(64)
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C COMTAG - THESE NUMBERS ARE NEEDED THROUGHOUT THE JOB BY ANALYSIS
C           ROUTINES
C ================================================================
C
C
C LISTOK->  FLAGS THAT NEIGHBOUR LIST HAS BEEN GENERATED
C
C NLIST ->  LIST OF CLOSEST NEIGHBOURS TO A BLOCK   (I,1) = NUMBER
C           OF CLOSEST NEIGHBOURS (I,2-9)ADC ADDRESSES OF NEIGHBOURING
C           BLOCKS  (CLUSFN).THE THIRD ARGUMENT REFERS TO THE MARK
C           OF TAGGING SYSTEM - EITHER MARK 2 OR MARK 3.
C
C XMAP(64) -> XMAP AND YMAP CONTAIN THE X,Y ADDRESSES OF THE BLOCKS IN
C YMAP(64) -> THE 1982 TAGGING SYSTEM
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C
C
C------------ C O M M O N    C W O R K   F O R   T A G A N -------------
C
C
       COMMON/CWORK/MARK,IFLMRK,IMC,NCLST,NNEI,
     *              ISTMZ,ISTPZ,IENDMZ,IENDPZ,
     *              SIGX,SIGY,SIGEN,
     *              CAND(3),CLUS(9,2),CMAP(10,9),
     *              SADC(32,2),CATAG(192)
C
C
C CWORK - WORKSPACE USED ONLY ONCE PER EVENT FOR INTERNAL PROCESSING
C ==================================================================
C
C MARK   ->  WHICH 'MARK' OF TAGGER - 1 = 1981,2
C                                   - 2 = 1983 ONWARDS
C
C IFLMRK ->  SET TO '1' BY TAGMRK
C
C IMC    ->  SET TO '1' BY TAGMRK IF MC DATA
C
C CATAG  ->  CONTAINS THE ADC CONTENTS UNPACKED FROM ATAG
C
C SADC   ->  COMMON FOR ADC'S AFTER SORTING  (SORT 1)
C
C CMAP(I,1...9) ->  ADDRESS OF ADC'S IN CLUSTER I,SORT23 PUTS THESE IN
C                   ORDER OF ENERGY.
C
C CAND(3) ->  X, Y, AND ENERGY OF A FOUND CLUSTER IN AFTER CLSPS
C
C SIGX,SIGY,SIGEN ->  ERROR ON X, Y, AND ENERGY AFTER CLSPS
C
C CLUS(9,2) ->  ADC ADDRESS AND CONTENTS OF CLUSTERS - SORTED BY ENERGY
C
C NCLST   ->  NUMBER OF CLUSTERS THIS END
C ISTMZ   ->  POINTER TO START OF -Z DATA IN CATAG ( ALWAYS  1       )
C ISTPZ   ->  POINTER TO START OF +Z DATA IN CATAG ( EITHER 33 OR 25 )
C IENDMZ  ->  POINTER TO   END OF -Z DATA IN CATAG ( EITHER 32 OR 24 )
C IENDPZ  ->  POINTER TO   END OF +Z DATA IN CATAG ( EITHER 64 OR 48 )
C
C A.J.FINCH 24/2/84
C MODIFIED 12/3/84 CATAG PUT TO END AND INCREASED TO 192
C  TO ALLOW IT TO BE USED FOR 1979,80 TAGGER IN GRAPHICS
C LAST MOD : J. NYE  30/05/84  RE-ORGANIZED INCLUDING IFLMRK
C
C
C-----------------------------------------------------------------------
C
C
       DIMENSION FOUND(90)
       DIMENSION FLIST(64)
C
C CLEAR CMAP,FOUND,FLIST
C
       DO  9 I = 1,10
          DO  9 J = 1,9
             CMAP(I,J) = 0.0
    9  CONTINUE
       NUM = IEND - ISTART + 1
       DO 1 N = 1,NUM
          FOUND(N)     = 0.0
          FLIST(N)     = 0.0
          FLIST(N + NUM) = 0.0
    1  CONTINUE
       NCLST = 0
       K     = 1
C
C
C MAIN LOOP >>
C
C
    4  DO 10 I = ISTMZ    ,IENDMZ
C
C CHECK IF WE HAVE ALREADY USED THIS ADC
C A 'PARENT' CANT BE THE NEIGHBOUR OF ANOTHER CLUSTER
C
          DO 5 J = 1,K
             IF ( SADC(I,1) .EQ. FOUND(J) ) GOTO 10
    5     CONTINUE
C
C CHECK IF ENERGY IS BELOW THRESHOLD TO CALL IT A CLUSTER
C
          IF ( SADC(I,2) .LT. THRESH ) GOTO 11
C
C CLUSTER FOUND - STORE THE INFORMATION
C
          NCLST = NCLST + 1
          IF ( NCLST .GT. 10 ) GOTO 999
C
C PARENT ADC IS FIRST MEMBER OF CLUSTER MAP
C
          CMAP(NCLST,1) = SADC(I,1)
C
C NO OF NEIGHBOURS = NNEI  FOUND FROM NEIGHBOUR LIST
C
          NFADC = SADC(I,1)
C
C
          NNEI = NLIST(NFADC,1,MARK)
C
C
          IELOOP = NNEI + 1
C
C NLIST 2 TO NLIST (2 + NNEI) CONTAIN THE ADDRESSES ( IN CATAG)
C OF NEIGHBOURS OF THIS 'PARENT'
C
          DO 20 L = 2, IELOOP
C
C ADC NO. OF NEIGHBOUR = NADC
C
             NADC = NLIST(NFADC,L,MARK)
C
C FILL CLUSTER MAP
C
             CMAP(NCLST,L) = NADC
C
C SAVE ADC ADDRESSES USED,SO THEY CANT BECOME PARENTSOF LATER CLUSTERS
C
             FOUND(K) = NADC
C
             K = K + 1
             IF ( K .GT. 90 ) GOTO 999
C
C COUNT NUMBER OF CLUSTERS FOR WHICH THIS IS A NEIGHBOUR
C END OF LOOP OVER NEIGHBOURS OF PARENT
C
             FLIST(NADC) = FLIST(NADC) + 1
C
   20     CONTINUE
C
   10  CONTINUE
C
   11  CONTINUE
C
       IF ( NCLST .LE. 1 ) GOTO 40
C
C
C FOR ADC'S IN MORE THAN ONE CLUSTER,DIVIDE ENERGY BY NUMBER OF CLUSTERS
C FOR WHICH IT IS A MEMBER
C
 1000  DO 30 I = ISTART,IEND
          IF ( FLIST(I) .LE. 1 ) GOTO 30
          CATAG(I) = CATAG(I) / FLIST(I)
   30  CONTINUE
C
   40  CONTINUE
       IF ( IWRITE .NE. 1 ) RETURN
       DO 50 I = 1,NCLST
          WRITE(6,602) CMAP(I,1) ,CMAP(I,2) ,CMAP(I,3) ,CMAP(I,4) ,
     *                 CMAP(I,5) ,CMAP(I,6) ,CMAP(I,7) ,CMAP(I,9 )
   50  CONTINUE
  602  FORMAT(9(2X,F10.2))
       RETURN
C
C
  999  WRITE(6,600)
  600  FORMAT(' WARNING MORE THAN 10 CLUSTERS FOUND BY TAGCLS ')
       GOTO 1000
       END
