C   12/03/84 411131802  MEMBER NAME  TAGSR1   (S)           FORTRAN
C
C
C
C
C
C
C
       SUBROUTINE TAGSR1(ISTART,IEND,IWRITE)
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C THIS ROUTINE SORTS THE CONTENTS OF CATAG INTO ORDER OF ENERGY AND
C PUTS THE ESULTING LIST IN SADC(32,2) WHERE SADC(I,1) = ADDRESS OF BLOC
C AND SADC(I,2) = CONTENTS
C
C ISTART - INPUT - FIRST ADDRESS IN CATAG FROM WHICH DATA SHOULD BE TAKE
C IEND  - INPUT - LAST ADDRESS IN CATAG FROM WHICH DATA SHOULD BE TAKEN
C
C IWRITE - INPUT - FLAG TO TURN ON DEBUGGING INFO IF SET TO 1
C
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
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 STEMP(2)
C
C                                   COPY THE CONTENTS OF PART OF
C                                   CATAG TO BE SORTED INTO SADC
C                                   PLUS ADC ADDRESSES
C
       L = 1
C
       DO 5 I = ISTART,IEND
          SADC(L,1) = I
          SADC(L,2) = CATAG(I)
          L = L + 1
    5  CONTINUE
C
C---DEBUG--------------------------------DEBUG  START-------------------
C
       IF ( IWRITE .EQ. 0) GOTO 101
       WRITE(6,601)
  601  FORMAT('  CONTENTS OF SADC BEFORE SORT ')
       DO 91 I = 1,IENDMZ,8
          WRITE(6,901) SADC(I,2),SADC(I+1,2),SADC(I+2,2),SADC(I+3,2),
     *                 SADC(I+4,2 ),SADC(I+5,2),SADC(I+6,2),SADC(I+7,2)
   91  CONTINUE
C
C---DEBUG--------------------------------DEBUG  END---------------------
C
  101  CONTINUE
C
C
C                                   NOW DO SORT ON SADC
C
       IELOO = IEND - ISTART
       DO 10 J = 1 ,IELOO
          DO 11 K = 1,IELOO
             IF ( SADC(K,2) .GT. SADC(K+1,2) )  GOTO 11
             STEMP(1)    = SADC(K,1)
             STEMP(2)    = SADC(K,2)
             SADC(K,1)   = SADC(K+1,1)
             SADC(K,2)   = SADC(K+1,2)
             SADC(K+1,1) = STEMP(1)
             SADC(K+1,2) = STEMP(2)
   11     CONTINUE
   10  CONTINUE
C
C
       IF ( IWRITE .EQ. 0) GOTO 100
C
C
C---DEBUG ONLY---------------------------DEBUG  ONLY--------------------
C
C
       WRITE(6,600)
  600  FORMAT('  CONTENTS OF SADC AFTER SORT ')
       DO 80 I = 1,IENDMZ,8
          WRITE(6,901) SADC(I,2),SADC(I+1,2),SADC(I+2,2),SADC(I+3,2),
     *                 SADC(I+4,2),SADC(I+5,2),SADC(I+6,2),SADC(I+7,2)
   80  CONTINUE
  901  FORMAT(8(3X,F10.2) )
C
C
  100  RETURN
       END
