!-----------------------------------------------------------------------
! rich_select: particle selection functions 
! == using RICH likelihoods from RTRA
!
! before using these functions you should call for each event:
!       int copyRTRItoRTRA(int algorithm)
!           algorithm = 0 Rise 
!           algorithm = 1 Riter/Rire
! 
! selection functions :
!   LOGICAL FUNCTION RICH_ELECTRON(LEVEL,IROW)
!   LOGICAL FUNCTION RICH_MUON(LEVEL,IROW)
!   LOGICAL FUNCTION RICH_PION(LEVEL,IROW)
!   LOGICAL FUNCTION RICH_KAON(LEVEL,IROW)
!   LOGICAL FUNCTION RICH_PROTON(LEVEL,IROW)
!   LOGICAL FUNCTION RICH_MULTIE(LEVEL,IROW)
! parameters: integer LEVEL (selection level)
!             integer IROW  (RTRA row number)
! selection levels:
!    0  maximum probable hypothesis
!    1  soft likelihood cut
!    2  medium likelihood cut
!    3  hard likelihood cut
! return value: TRUE, if IROW=valid row number && RTRA.flr>0 && obey
!               selection level criteria
!
! M. Staric, apr-2001
!
!-----------------------------------------------------------------------


      LOGICAL FUNCTION RICH_ELECTRON(LEVEL,IROW)

      IMPLICIT NONE
      INTEGER LEVEL             ! level (0,1,2,3)
      INTEGER IROW              ! RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER ILEV
      REAL PROB
      LOGICAL RICH_MAXPROB
      REAL CUT(3)
      SAVE CUT
      DATA CUT /.05, .5, .95/
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      RICH_ELECTRON=.FALSE.
      IF(IROW.LT.1.OR.IROW.GT.NRTRA()) RETURN
      IF(IRTRA_FLR(IROW).EQ.0) RETURN

      ILEV=MOD(LEVEL,4)
      IF(ILEV.EQ.0) THEN
         RICH_ELECTRON=RICH_MAXPROB(1,IROW)
      ELSE
         PROB=FRTRA_LRE(IROW)+FRTRA_LRMU(IROW)+FRTRA_LRPI(IROW)
         RICH_ELECTRON=PROB.GT.CUT(ILEV)
      ENDIF

      RETURN
      END



      LOGICAL FUNCTION RICH_MUON(LEVEL,IROW)

      IMPLICIT NONE
      INTEGER LEVEL             ! level (0,1,2,3)
      INTEGER IROW              ! RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER ILEV
      REAL PROB
      LOGICAL RICH_MAXPROB
      REAL CUT(3)
      SAVE CUT
      DATA CUT /.05, .5, .95/
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      RICH_MUON=.FALSE.
      IF(IROW.LT.1.OR.IROW.GT.NRTRA()) RETURN
      IF(IRTRA_FLR(IROW).EQ.0) RETURN

      ILEV=MOD(LEVEL,4)
      IF(ILEV.EQ.0) THEN
         RICH_MUON=RICH_MAXPROB(2,IROW)
      ELSE
         PROB=FRTRA_LRE(IROW)+FRTRA_LRMU(IROW)+FRTRA_LRPI(IROW)
         RICH_MUON=PROB.GT.CUT(ILEV)
      ENDIF

      RETURN
      END



      LOGICAL FUNCTION RICH_PION(LEVEL,IROW)

      IMPLICIT NONE
      INTEGER LEVEL             ! level (0,1,2,3)
      INTEGER IROW              ! RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER ILEV
      REAL PROB
      LOGICAL RICH_MAXPROB
      REAL CUT(3)
      SAVE CUT
      DATA CUT /.05, .5, .95/
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      RICH_PION=.FALSE.
      IF(IROW.LT.1.OR.IROW.GT.NRTRA()) RETURN
      IF(IRTRA_FLR(IROW).EQ.0) RETURN

      ILEV=MOD(LEVEL,4)
      IF(ILEV.EQ.0) THEN
         RICH_PION=RICH_MAXPROB(3,IROW)
      ELSE
         PROB=FRTRA_LRE(IROW)+FRTRA_LRMU(IROW)+FRTRA_LRPI(IROW)
         RICH_PION=PROB.GT.CUT(ILEV)
      ENDIF

      RETURN
      END



      LOGICAL FUNCTION RICH_KAON(LEVEL,IROW)

      IMPLICIT NONE
      INTEGER LEVEL             ! level (0,1,2,3)
      INTEGER IROW              ! RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER ILEV
      REAL PROB
      LOGICAL RICH_MAXPROB
      REAL CUT(3)
      SAVE CUT
      DATA CUT /.05, .3, .95/
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      RICH_KAON=.FALSE.
      IF(IROW.LT.1.OR.IROW.GT.NRTRA()) RETURN
      IF(IRTRA_FLR(IROW).EQ.0) RETURN

      ILEV=MOD(LEVEL,4)
      IF(ILEV.EQ.0) THEN
         RICH_KAON=RICH_MAXPROB(4,IROW)
      ELSE
         PROB=FRTRA_LRK(IROW)
         RICH_KAON=PROB.GT.CUT(ILEV)
      ENDIF

      RETURN
      END



      LOGICAL FUNCTION RICH_PROTON(LEVEL,IROW)

      IMPLICIT NONE
      INTEGER LEVEL             ! level (0,1,2,3)
      INTEGER IROW              ! RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER ILEV
      REAL PROB
      LOGICAL RICH_MAXPROB
      REAL CUT(3)
      SAVE CUT
      DATA CUT /.05, .3, .95/
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      RICH_PROTON=.FALSE.
      IF(IROW.LT.1.OR.IROW.GT.NRTRA()) RETURN
      IF(IRTRA_FLR(IROW).EQ.0) RETURN

      ILEV=MOD(LEVEL,4)
      IF(ILEV.EQ.0) THEN
         RICH_PROTON=RICH_MAXPROB(5,IROW)
      ELSE
         PROB=FRTRA_LRP(IROW)
         RICH_PROTON=PROB.GT.CUT(ILEV)
      ENDIF

      RETURN
      END



      LOGICAL FUNCTION RICH_MULTIE(LEVEL,IROW)

      IMPLICIT NONE
      INTEGER LEVEL             ! level (0,1,2,3)
      INTEGER IROW              ! RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      INTEGER ILEV, I, ID
      REAL PLKH(0:6)
      REAL PROB, P
      REAL CUT(3)
      SAVE CUT
      DATA CUT /.05, .5, .95/
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      RICH_MULTIE=.FALSE.
      IF(IROW.LT.1.OR.IROW.GT.NRTRA()) RETURN
      IF(IRTRA_FLR(IROW).EQ.0) RETURN

      PLKH(1)=FRTRA_LRE(IROW)
      PLKH(2)=FRTRA_LRMU(IROW)
      PLKH(3)=FRTRA_LRPI(IROW)
      PLKH(4)=FRTRA_LRK(IROW)
      PLKH(5)=FRTRA_LRP(IROW)
      PLKH(0)=1.-PLKH(1)-PLKH(2)-PLKH(3)-PLKH(4)-PLKH(5)
      PLKH(6)=MAX(FRTRA_LRME(IROW),0.)

      PROB=1.+PLKH(6)
      DO I=0,6
         PLKH(I)=PLKH(I)/PROB
      ENDDO

      ILEV=MOD(LEVEL,4)
      IF(ILEV.EQ.0) THEN
         ID=6
         P=PLKH(ID)+1.e-6
         DO I=0,6
            IF(PLKH(I).GT.P) THEN
               P=PLKH(I)
               ID=I
            ENDIF
         ENDDO
         RICH_MULTIE=ID.EQ.6
      ELSE
         PROB=PLKH(6)
         RICH_MULTIE=PROB.GT.CUT(ILEV)
      ENDIF

      RETURN
      END



      LOGICAL FUNCTION RICH_MAXPROB(IDP,IROW)

! returns TRUE, if particle IDP has maximal likelihood

      IMPLICIT NONE
      INTEGER IDP               ! particle ID (0,1,2,3,4,5)
      INTEGER IROW              ! valid RTRA row number

#include "arte/Arte.fi"
#include "auto/RTRA.fi"
      REAL PLKH(0:5), P
      INTEGER ID, I
#include "arte/Arte.ff"
#include "auto/RTRA.ff"

      PLKH(1)=FRTRA_LRE(IROW)
      PLKH(2)=FRTRA_LRMU(IROW)
      PLKH(3)=FRTRA_LRPI(IROW)
      PLKH(4)=FRTRA_LRK(IROW)
      PLKH(5)=FRTRA_LRP(IROW)
      PLKH(0)=1.-PLKH(1)-PLKH(2)-PLKH(3)-PLKH(4)-PLKH(5)
 
      ID=MIN(MAX(IDP,0),5)
      P=PLKH(ID)+1.e-6
      DO I=0,5
         IF(PLKH(I).GT.P) THEN
            P=PLKH(I)
            ID=I
         ENDIF
      ENDDO
      RICH_MAXPROB=ID.EQ.IDP

      RETURN
      END


