cmz   22/10/2000    by penghp
        SUBROUTINE TOFPID(I,IFLAG,Ipero)
********************************************************
*   This subroution is used to identify pi/k/p using   *
* TOF information, for Tqual for certain values will   *
* be used.                                             *
*   The TOF time offset is corrected by TOFFSIG for    *
* different particle hoporperses. and TOFFSIG also get *
* time resolution for all particles.                   *
*   The detector performance may change according      *
* to time and the reconstruction version. so the       *
* ROUTINE for correcting TOF is use for J/psi data     *
* collected  at 99-00 and reconstruction Version 102.  *
*    The subroutines TOFFSIGMC018, TOFFSIGMC019 and    *
* TOFFSIGMC020  are used to do correction  the TOF     *
* time and get resolution for the MC data of all kinds *
* of particles, they are correspond to the first run   *
* set of 24M j/psi data, the second run set of 27M     *
* j/psi data and all 50M j/psi data respectively       *
*                                                      *
* Input  : I     : the track #                         *
*          Ipero : the flag of MC data for different   *
*                  run  set                            *
*                  1---the first run set of 24M j/psi  *
*                  2---the second run set of 27M j/psi *
*                  3---the totly 50M j/psi             *
*                 if the MC. data is simulate the first*
*                 run set of 24M j/psi data ipero=1    *
*                                                      *
* Output : IFLAG ,the identify result                  *
*             1-for kaon,2-for pion,3-for proton       *
*             0-out the 3 sigma                        *
*          /COMMON/TOFWGT, all kind of particles wgt   *
*          XPIK WTK/(WTPI+WTK),varible recommmended    *
*          to cut on pi/k seperation                   *
*                                                      *
*  Author:  Peng Haiping  (USTC)                       *
*           Li Haibo      (IHEP)                       *
*******************************************************
      INCLUDE 'tofwgt.inc'
      INCLUDE 'trklst.inc'
      INCLUDE 'evlst.inc'
         INTEGER I,IFLAG
         REAL emass(3),TOFW(3)
         REAL TOFCUT1,TOFQUALA(5)
         INTEGER IDGDTOF
         DATA TOFCUT1/900.0/
         DATA emass/0.4937,0.1396,0.938/
         DATA TOFQUALA/1.0,2.0,7.0,8.0,10.0/
         DO 10 J=1,3
          TOFW(J)=0
 10      CONTINUE
         IFLAG=0
C................................TOFID
C....FOR WILL USE THE INFORMATIN OF MON,PATH,TIME,CHARG,
C....THEN DENAND ISUB1,ISUB3,ISUB4 MUST HAVE INFORMATION.
        ISUB1=TRKSUB(1,I)
        ISUB4=TRKSUB(4,I)
        ISUB3=TRKSUB(3,I)
        IF(ISUB1.LE.0)GOTO 50
        IF(ISUB4.LE.0)GOTO 50
        IF(ISUB3.LE.0)GOTO 50
        TQUAB=TRK(ISUB4+8)
        IDGDTOF=0
c......tof quality require..........
        DO ICHE=1,5
          IF(TQUAB.EQ.TOFQUALA(ICHE))IDGDTOF=1
        ENDDO
        if(IDGDTOF.eq.0)goto 50
        PMON=TRK(ISUB3+7)
        PZ=TRK(ISUB3+6)
        COSZ=PZ/PMON
        TQQ=TRK(ISUB4+7)
        ITT=ITRK(ISUB4+3)
        ICHARG=ITRK(ISUB1+4)
C.....ONLY THE BARREL TOF INFORMATION CAN BE USED
        IF(ITT.EQ.0.OR.ITT.GT.48)GOTO 50
        DO J=1,3
          TIME=TRK(ISUB4+4)
C.....FOR THE REAL DATA..............
          IF(IRUN.Ge.13690.AND.IRUN.LE.16836)THEN
           CALL TOFFSIG(1,J,PMON,COSZ,TQQ,ITT,ICHARG,TOFF,TSIG)
          ENDIF
          IF(IRUN.Ge.17009.and.IRUN.LE.19149)THEN
           CALL TOFFSIG(2,J,PMON,COSZ,TQQ,ITT,ICHARG,TOFF,TSIG)
          ENDIF
C.....FOR THE MC. DATA...........
          IF(IRUN.LE.0)THEN
           IF(IPERO.EQ.1)THEN
            CALL TOFFSIGMC018(J,PMON,COSZ,TQQ,ITT,ICHARG,TOFF,TSIG)
           ELSE IF(IPERO.EQ.2)THEN
            CALL TOFFSIGMC019(J,PMON,COSZ,TQQ,ITT,ICHARG,TOFF,TSIG)
           ELSE IF(IPERO.EQ.3)THEN
            CALL TOFFSIGMC020(J,PMON,COSZ,TQQ,ITT,ICHARG,TOFF,TSIG)
           ELSE
            PRINT *,'***TOFPID HAVE SOME PROBLEM, THE INPUT PARA HAVE
     &              SOME PROBLEM**'
            STOP
           endif
          ENDIF
        if(icharg.eq.1) then
          xtime(j) = TIME
          xtimeoff(j) = TOFF
          TIME=TIME-TOFF
          PMON=TRK(ISUB3+7)
          BETAA=PMON/SQRT(PMON**2+eMASS(J)**2)
          PATH=TRK(ISUB4+14)
          TEXP=PATH/BETAA/0.3
          xtimee(j) = TEXP
          xtimesig(j) = TSIG
          DTAT=TIME-TEXP
          TOFW(J)=(DTAT/TSIG)**2
        else
          ytime(j) = TIME   
          ytimeoff(j) = TOFF  
          TIME=TIME-TOFF
          PMON=TRK(ISUB3+7)
          BETAA=PMON/SQRT(PMON**2+eMASS(J)**2)
          PATH=TRK(ISUB4+14)
          TEXP=PATH/BETAA/0.3
          ytimee(j) = TEXP   
          ytimesig(j) = TSIG 
          DTAT=TIME-TEXP
          TOFW(J)=(DTAT/TSIG)**2
        endif
        ENDDO
        IF(TOFW(1).LE.TOFW(2).AND.TOFW(1).LE.TOFW(3).AND.
     &                               TOFW(1).LE.TOFCUT1)IFLAG=1
        IF(TOFW(2).LE.TOFW(1).AND.TOFW(2).LE.TOFW(3).AND.
     &                               TOFW(2).LE.TOFCUT1)IFLAG=2
        IF(TOFW(3).LE.TOFW(2).AND.TOFW(3).LE.TOFW(1).and.
     &                               TOFW(3).LE.TOFCUT1)IFLAG=3

        TOFWTK=EXP(-0.5*TOFW(1)**2)
        TOFWTPI=EXP(-0.5*TOFW(2)**2)
        TOFWTP=EXP(-0.5*TOFW(3)**2)
        PROBK=PROB(TOFW(1),1)
        PROBPI=PROB(TOFW(2),1)
        PROBP=PROB(TOFW(3),1)
c        XPIK=TOFWTK/(TOFWTK+TOFWTPI)
 50    CONTINUE
        RETURN
        END
