C   12/03/84 412041852  MEMBER NAME  CLSPS1   (S)           FORTRAN
C
        SUBROUTINE TAGPS1(SUM,IWRITE)
C
C WORK OUT CENTRE OF CLUSTER FOR 1981/2 TAGGER
C
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C
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
      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 D E  --------------------------------------
C
C
       TEST1 = SUM * 0.999
       TEST2 = SUM * 0.025
       XCORR = 0.0
       YCORR = 0.0
       SIGX  = 20.0
       SIGY  = 20.0
C
C
       CAND(1) = CLUS(1,2)
       IF ( CAND(1) .LT. 0.0 ) GOTO 9010
       ADDRES  = CLUS(1,1)
       CAND(2) = XMAP (ADDRES)
       CAND(3) = YMAP (ADDRES)
       IF ( CAND(1) .GT. TEST1 ) GOTO 10
C
C
C FIT TO RATIO OF ENERGIES FOR HIT BLOCK
C AND NEXT LARGEST HIT BLOCK
C
       CALL TAGFIT(2,XFIT,YFIT,IWRITE,*10)
C
       XCORR = XFIT
       YCORR = YFIT
C
C SOPHISTICATED CALCULATION OF ERROR ON X
C
       IF ( XCORR .GT. 5 ) SIGX = 5
       IF ( YCORR .GT. 5 ) SIGY = 5
C
C ALSO FIT TO 3RD BLOCK ,IF IT HAS SIGNIFICANT FRACTION OF ENERGY
C
       IF ( CLUS(3,2) .LT. TEST2 ) GOTO 10
       CALL TAGFIT(3,XFIT,YFIT,IWRITE,*10)
C
C CHOOSE ONE OF THE TWO FIT RESULTS - WHICHEVER IS THE LARGEST
C
       IF ( ABS(XFIT) .GT. ABS(XCORR) ) XCORR = XFIT
       IF ( ABS(YFIT) .GT. ABS(YCORR) ) YCORR = YFIT
C SOPHISTICATED CALCULATION OF ERROR ON X
       IF ( XCORR .GT. 5 ) SIGX = 5
       IF ( YCORR .GT. 5 ) SIGY = 5
C
C NOW ADD XCORR (CORRECTION ON X) TO X
C AND SIMILARLY Y
C
   10  CONTINUE
       CAND(2) = CAND(2) + XCORR
       CAND(3) = CAND(3) + YCORR
C
C CAND(1) = ENERGY OF CLUSTER - ONLY FIRST 4 MEMBERS
C
       CAND(1) = CLUS(1,2) + CLUS(2,2) + CLUS(3,2) + CLUS(4,2 )
       IF ( CAND(1) .LT. 0.0 ) GOTO 9010
       SIGEN = 0.20 * SQRT( CAND(1) )
C
C DEBUG - WRITE OUT RESULT
C
       IF ( IWRITE .EQ. 1 ) WRITE(6, 601) ((CLUS(I,J),I=1,9),J=1,2)
  601  FORMAT(' ++++ TAGPS1 : DEBUG  -  CLUS ARRAY ==>',/,
     *           2(9(1X,F8.2),/) )
       IF ( IWRITE .EQ. 1 ) WRITE(6,602) CAND(1 ) ,CAND(2 ) ,CAND(3)
  602  FORMAT(' CLUSTER INFO ENERGY X Y ',3(2X,F12.2) )
C
C---------------------------------------- R E T U R N ------------------
C
       RETURN
C
C
C---ERRORS :
C
C---                                      ENERGY < 0
C
 9010  WRITE(6,9011) ((CLUS(I,J),I=1,9),J=1,2)
       WRITE(6,602) CAND(1 ) ,CAND(2 ) ,CAND(3)
 9011  FORMAT(' ++++ TAGPS1 : NEGATIVE ENERGY  -  CLUS ARRAY ==>',/,
     *           2(9(1X,F8.2),/) )
       RETURN
       END
