        SUBROUTINE PARTID(ITRKK,IFLAG,Ipero)
*************************************************************
*   The subroutine is used to to do particle identification *
* using the dEdX and tof information.  Considering the      *
* seperation ability of dEdX, we only use dEdX information  *
* when momenta less than 0.8GeV.                            *
*...........................................................*
*                                                           *
*   The tof information is corrected by subroutine toffsig  *
* and dEdX information is corrected by subroutine dedxcorp. *
*                                                           *
*   All the correction are the experical fitting of data    *
* from 50M J/psi data taken in 1999-2000 and 2000-2001.     *
* Then all the parameter depend on the reriod data and the  *
* reconstruction version. So the routine for correction only*
* will be used for 50M J/psi data taking in 1999-2000 and   *
* 2000-2001, thereconstruction version 102   *
*...........................................................*
*                                                           *
*   Because the routine is used to do PID for charged, we   *
* require sub1 and sub3 have information and at least one of*
* sub4 and sub7 has information.                            *
*   About the tof information, only barrel tof information  *
* can be used ,and the tof quality is required eq 1.0 2.0   *
* 7.0 or 10.0.                                              *
*   About dEdX information, only when mementa is less than  *
*  0.8Gev, and the hit munber hit .gt.10 and dedx quality   *
*  1 can be used.                                           *
*   The subroutine only can be used for RUN13691-RUN16835   *
* of Jpsi data taken in 1999-2000, and  RUN17009-RUN19149   *
* taken in 2000-2001.                                       *
*...........................................................*
*                                                           *
*   INPUT :                                                 *
*      ITRKK  ,  the track number of particle               *
*      IPERO  ,  the flag for differnt run set MC. data     *
*                1--the first run set of 24M j/psi data     *
*                2--the second run set of 27M j/psi data    *
*                3--the totly 50M j/psi data                *
*                if you analysis the first run set of 24M   *
*                j/psi data, ipero=1                        *
*   OUTPUT :                                                *
*      IFLAG ,  the paticle kind number                     *
*      1--for kaon, 2--for pion, 3--for proton              *
*...........................................................*
*   IDGDDEDX=1  , dedx information is used in PID           *
*   IDGDTOF=1   , tof information is used in PID            *
*   IDGDCOM=0   , none of dedx and tof info. be used in PID *
*          =1   , one  of dedx and tof info. be used in PID *
*          =2   , both of dedx and tof info. be used in PID *
*...........................................................*
*   TOFCHI      , the chi square of tof  information       *
*   DEDXCHI     , the chi square of dedx information       *
*   COMCHI      , the sum of chi square of dedx and tof     *
*                                                           *
*...........................................................*
*   A common block partid.inc is used at the process        *
*                                                           *
*         Author :  Peng Hai ping       USTC                *
*                             2001.2.20                     *
*************************************************************
      INCLUDE 'trklst.inc'
      INCLUDE 'evlst.inc'
      INCLUDE 'partid.inc'
       REAL EMASS(3),TOFQUALA(4)
       INTEGER IDEDXCH
       DATA IDEDXCH/10/
       DATA EMASS/0.4937,0.1396,0.9383/
       DATA TOFQUALA/1.0,2.0,8.0,10.0/
       IFLAG=0
       IDGDTOF=0
       IDGDDEDX=0
       IDGDCOM=0
       DO I=1,3
          TOFCHI(I)=999.
          DEDXCHI(I)=999.
          COMCHI(I)=999.
          PROBILITY(I)=-999.
       ENDDO
       XPIK=-999.
       ISUB1=TRKSUB(1,ITRKK)
       ISUB3=TRKSUB(3,ITRKK)
       ISUB4=TRKSUB(4,ITRKK)
       ISUB7=TRKSUB(7,ITRKK)
c
C*.....if not charged tracks, or no dedx and 
c*.....tof information then reject.
c
       IF(ISUB3.LE.0.OR.ISUB1.LE.0)RETURN
       IF(ISUB4.LE.0.AND.ISUB7.LE.0)RETURN
       PTOT=TRK(ISUB3+7)
       PZ=TRK(ISUB3+6)
       COSZ=PZ/PTOT
       ICHARG=ITRK(ISUB1+4)
c
C*......now get tof information.
c
       IF(ISUB4.GT.0)THEN
c
C*......only the barrel tof information can be used.....
c
           ICOUNT=ITRK(ISUB4+3)
           IF(ICOUNT.EQ.0.OR.ICOUNT.GT.48)GOTO 101
c
C*......only certain tof quality can be used..
c
           TQUAB=TRK(ISUB4+8)
           DO ICHE=1,4
             IF(TQUAB.EQ.TOFQUALA(ICHE))IDGDTOF=1
           ENDDO
           IF(IDGDTOF.EQ.0)GOTO 101
           TQQ=TRK(ISUB4+7)
           DO I=1,3
             TOFF=0.0
             TSIG=0.2
             TIME=TRK(ISUB4+4)
             eLENGTH=TRK(ISUB4+14)
             BETAA=PTOT/SQRT(PTOT**2+EMASS(I)**2)
             TIMEXP=eLENGTH/BETAA/0.3
             DTAT=TIME-TIMEXP
             IF(IRUN.Ge.13690.AND.IRUN.LE.16836)THEN
               CALL TOFFSIG(1,I,PTOT,COSZ,TQQ,ICOUNT,ICHARG,TOFF,TSIG)
             ENDIF
             IF(IRUN.Ge.17009.and.IRUN.LE.19149)THEN
               CALL TOFFSIG(2,I,PTOT,COSZ,TQQ,ICOUNT,ICHARG,TOFF,TSIG)
             ENDIF
             IF(IRUN.LT.0)THEN
              IF(IPERO.EQ.1)THEN
               CALL TOFFSIGMC018(I,PTOT,COSZ,TQQ,ICOUNT,
     +                                              ICHARG,TOFF,TSIG)
              ELSE IF(IPERO.EQ.2)THEN
               CALL TOFFSIGMC019(I,PTOT,COSZ,TQQ,ICOUNT,
     +                                              ICHARG,TOFF,TSIG)
              ELSE IF(IPERO.EQ.3)THEN
               CALL TOFFSIGMC020(I,PTOT,COSZ,TQQ,ICOUNT,
     +                                              ICHARG,TOFF,TSIG)
              ELSE
                PRINT *,'**THE INPUT PARAMTER HAVE SOME PROBLEM**'
              ENDIF
             ENDIF
             DTAT=DTAT-TOFF
             TOFCHI(I)=(DTAT/TSIG)**2
           ENDDO
       ENDIF
 101   CONTINUE
c
C*.....now get the dedx information...
c
       IF(ISUB7.GT.0)THEN
c
C*......dedx can be used only when quality .eq. 1 and ndedx .gt.10...
c
         NDEDX=ITRK(ISUB7+3)
         IOK=ITRK(ISUB7+4)
         IF(IOK.NE.1.OR.NDEDX.LE.IDEDXCH)GOTO 202
c
C*......dedx can be used only when momenta less than 0.8gev
c
         IF(PTOT.GT.0.8)GOTO 202
         IDGDDEDX=1
         DO I=1,3
            IF(I.EQ.1) XSP=TRK(ISUB7+18)
            IF(I.EQ.2) XSP=TRK(ISUB7+17)
            IF(I.EQ.3) XSP=TRK(ISUB7+19)
            IF(IRUN.GE.13690.AND.IRUN.LE.16836)THEN
              CALL DEDXCORP(1,I,PTOT,COSZ,OFFSET,RESOL)
            ENDIF
            IF(IRUN.GE.17009.AND.IRUN.LE.19149)THEN
              CALL DEDXCORP(2,I,PTOT,COSZ,OFFSET,RESOL)
            ENDIF
            IF(IRUN.LT.0)THEN
              CALL DEDXCORPMC(I,PTOT,COSZ,OFFSET,RESOL)
            ENDIF
          if(icharg.eq.1) then
            xxsp(i) = XSP
            xdedxoff(i) = OFFSET
            xdedxsig(i) = RESOL
          else
            yxsp(i) = XSP
            ydedxoff(i) = OFFSET
            ydedxsig(i) = RESOL 
          endif
            XSP=XSP-OFFSET
            DEDXCHI(I)=(XSP/RESOL)**2
         ENDDO
       ENDIF
 202   CONTINUE
c
C*....now combin the information of dedx and tof
c
       IDGDCOM=IDGDTOF+IDGDDEDX
       IF(IDGDCOM.EQ.0)RETURN
       IF(IDGDCOM.EQ.1.AND.IDGDTOF.EQ.1)THEN
          DO I=1,3
            COMCHI(I)=TOFCHI(I)
            PROBILITY(I)=PROB(COMCHI(I),IDGDCOM)
          ENDDO
       ENDIF
       IF(IDGDCOM.EQ.1.AND.IDGDDEDX.EQ.1)THEN
          DO I=1,3
            COMCHI(I)=DEDXCHI(I)
            PROBILITY(I)=PROB(COMCHI(I),IDGDCOM)
          ENDDO
       ENDIF
       IF(IDGDCOM.EQ.2)THEN
          DO I=1,3
             COMCHI(I)=DEDXCHI(I)+TOFCHI(I)
             PROBILITY(I)=PROB(COMCHI(I),IDGDCOM)
          ENDDO
       ENDIF
c
C*.......now calcute the xpik
c
C PENGHP ADD 2001/07/09,Dr. yuanye find the bug
c
        PROBTEMP=PROBILITY(1)+PROBILITY(2)
        IF(PROBTEMP.LE.0.00001)PROBTEMP=0.00001
C
        XPIK=PROBILITY(1)/PROBTEMP
C        XPIK=PROBILITY(1)/(PROBILITY(1)+PROBILITY(2))
c
C*.......now comparethe weight of all particles and get the id #
c
       IF(COMCHI(1).LE.COMCHI(2).AND.COMCHI(1).LE.COMCHI(3))
     &         IFLAG=1
       IF(COMCHI(2).LE.COMCHI(1).AND.COMCHI(2).LE.COMCHI(3))
     &         IFLAG=2
       IF(COMCHI(3).LE.COMCHI(1).AND.COMCHI(3).LE.COMCHI(2))
     &         IFLAG=3

       RETURN
       END
