!----------------------------------------------------------------------
! Clone removal: set clone flag KLON in /RIC_PAR/, if track is a clone
! track quality parameter: Q=hsi+hin+hou-chi/ndf
! M. Staric, mar-2002, may-2002
!
!----------------------------------------------------------------------


      SUBROUTINE RITER_CLONE_RM

      IMPLICIT NONE
#include "riter/RIC_TRA.fi"
#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER IROW, ITRA, NHIT, NDOF, I, II, K, KK
      REAL QUAL(MAXTRA)
      INTEGER INDX(MAXTRA), CMAIN(MAXTRA), CSVD(MAXTRA)
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      
      IF(NTRA.LE.0) RETURN
      DO ITRA=1,NTRA
         IROW=RTRA_ROW(ITRA)
         NHIT=IRTRA_HSI(IROW)+IRTRA_HIN(IROW)+IRTRA_HOU(IROW)
         NDOF=IRTRA_NDOF(IROW)
         IF(NDOF.LE.0) NDOF=MAX(NHIT-5,1)
         QUAL(ITRA)=NHIT-FRTRA_CHI(IROW)/FLOAT(NDOF)
         CMAIN(ITRA)=IRTRA_CMAIN(IROW)
         CSVD(ITRA)=IRTRA_CSVD(IROW)
      ENDDO
      CALL SORTZV(QUAL,INDX,NTRA,1,1,0)
      DO I=1,NTRA
         II=INDX(I)
         KLON(II)=.FALSE.
         IF(CMAIN(II).EQ.0.AND.CSVD(II).EQ.0) GOTO 1
         DO K=1,I-1
            KK=INDX(K)
            IF((CMAIN(II).EQ.CMAIN(KK).AND.CMAIN(II).GT.0)
     &           .OR.(CSVD(II).EQ.CSVD(KK).AND.CSVD(II).GT.0)) THEN
               KLON(II)=.TRUE.
               GOTO 1
            ENDIF
         ENDDO
 1       CONTINUE
      ENDDO

      RETURN
      END


