C----------------------------------------------------
C
C  This file contains:
C
C   Subroutine SUPERFUG
C
C
C   SUPERFUG is used to apply dE/dx corrections to a 
C   charged track's momentum, and to correct the track
C   error matrix for dE/dx and multiple scattering, 
C   according to a specified mass hypothesis.
C
C  Input:
C     R*4   XMAS        Mass hypothesis (GeV/c**2);
C     R*4   TKPAR(6)    Vector of track parameters:
C                       phi, k (=1/Pxy), s (=tanlam), 
C                       xm, ym, zm;
C     I*4   ICHG        Charge;
C     R*4   BC          = 0.029979 * B field at 
C                       (xm,ym,zm);
C     R*4   PM(5)       Vector containing Px, Py, 
C                       Pz, Pxyz, Pxy;
C     R*4   SECLM(4)    Ith element = sec(lambda)**I;
C     R*4   ERROR(15)   Error matrix (taken from 
C                       TRK(I3+20) et seq.);
C     R*4   RM          = sqrt(xm**2 + ym**2);
C     I*4   MFIT        = Least-significant byte 
C                       of MFITWD;
C     I*4   MZFIT       = Next-to-least-significant
C                       byte of MFITWD;
C     I*4   IFIT(2)     R-phi layer maps = 
C                       ITRK(I3+35), ITRK(I3+36);
C     I*4   CD          CD CODE = ITRK(I3+40).
C
C Output:
C           TKPAR(6)    Corrected track parameters;
C           ERROR(15)   Corrected error matrix.
C
C   The following track parameters are corrected:
C            phi, k, xm, and ym.
C
C------------------------------------------------
C
C  Miniature set of COULOM-like constants:
C
C  U        = exponent in D. Hitlin formula
C  XMCS     = (0.0141 GeV)**2 for Multiple Scattering
C  NSCATR   = number of scatterers under consideration
C
C            In the following arrays, the scatterers
C               are numbered as follows:
C                  1. Beampipe
C                  2. Little Chamber
C                  3. Inner wall of Main DC
C                  4. Gas and wires of DC layers 2-5
C
C  RSCATR   = radius of mid-scatterer from beam axis
C  DPMIN    = d(momentum) for each scatterer (from 
C             Hitlin) for a minimum-ionizing particle
C  ALPHAT   = total alpha*t for each scatterer, 
C             determined from COULOM COMMON. These 
C             numbers are not necessarily consistent 
C             with the DPMIN values, but it's not 
C             worth worrying about.
C  RADL     = number of radiation lengths 
C             in each scatterer
C  A seperate subroutine at BES    
C             H.B.Lan,  Jan. 14,1994
C
C  B. Lowery Cosmetic fix to range error when 
C            INDEX1 = 940 just expand dimension 
C            of f1,f2 by 1.    May 18, 1994
C
C  as the energy loss correction and error matrix
C  correction are not correct for the low momentum
C  in the old telesis package,we use Mike Kelsey's 
C  BPFRAC subroutine to do energy loss correction in
C  IVERSION .ge.86,but ne.831.It is pointed out by
C  James Panetta& Li Xinhua
C  Huibin Lan  Nov. , 13,1994
c
c  Adopted BESII's RSCATR,DPMIN,ALPHAT,RADL instead 
C  of BESI's.(line114-118),dE/dX correction for 
C  charged tracks' momentum(LINE1237).  
c          LU Feng      20/Jan./2000
C
c  MDC error matrix of J/psi data from 13500-16844
C  had been re-scaled run by run.
C  reference:
C     <<STUDY ON BESII TELESIS>>
C            BES Annual Meeting, Jun. 2000, DaLian
C     <<BESII TELESIS ON CHARGED CHANNEL>>
C            BES Basic software Memo,on Web
c                         LU Feng      22/July/2000
C  Pull distribution corrections RUN by Run for Jpsi data
C  from run17209 to run19149
C           XU Guofa, YUAN Jianming
C                     Apr. 2001          
C------------------------------------------------------------------------------
      Subroutine SUPERFUG(xmas,
     > tkpar,ichg,bc,pm,seclm,error,rm,mfit,mzfit,ifit,cd,iversion)
C     Implicit None

cluf
      INCLUDE 'evlst.inc'
      External bpcorr2
      real bpcorr2,L1,L2,L3
cluf

      Real xmas, tkpar(6), bc, pm(5), seclm(4), error(15), rm
clhb
      real tkpar1,tkpar2,tkpar3,thta
      Integer ichg, mfit, mzfit, ifit(2), cd, iversion
      Real bumin,ptot0,beta0,frac0,ptot1,beta1,frac1,delth2
      Real dphidk,delphi,delxi,sigk,sigphi,sigxi
      Integer i,j,index0,index1,i1,i2,im
CBL   Real*8 f1(0:940), f2(0:940)
      Real*8 f1(0:941), f2(0:941)
      Real*8 scr1(0:10), scr2(0:10)
      Real*8 u, b, g
      Real*8 q0, q1, a1, a2, am
      Integer nscatr
      Real twopi, xmcs
      Real rscatr(4), dpmin(4), alphat(4), radl(4)
      Logical first
      Logical debug
      Logical super
      Data first / .true. /
      Data debug / .false. /
      Data twopi / 6.2831853 /
      Data u / 2.74d0 /
      Data xmcs / 1.99e-4 /
      Data nscatr / 4 /
C
C     Data for MARK3
c      Data rscatr /    0.077,    0.115,    0.145,    0.409 /
c      Data dpmin  / 0.441e-3, 0.490e-3, 0.129e-3, 0.155e-3 /
c      Data alphat /  1.78e-5,  1.95e-5,  0.22e-5,  0.68e-5 /
c      Data radl   /  4.03e-3,  7.10e-3,  0.53e-3,  0.00e-3 /
C
C     Data for BES  (see Chensj's note book)
cc      Data rscatr /    0.077,    0.121,    0.155,    0.375 /
cc      Data dpmin  / 0.911e-3, 0.963e-3, 0.742e-3, 0.166e-3 /
cc      Data alphat /  3.98e-5,  3.93e-5,  3.06e-5,  0.75e-5 /
C      Data radl   /  10.4e-3,  13.0e-3,  9.00e-3,  0.00e-3 /
cc      Data radl   / 22.47e-3,  13.0e-3,  9.00e-3,  0.00e-3 /

C-LUF  Data for BESII
      Data rscatr /0.0483   ,    0.0926,     0.155,    0.375 /
      Data dpmin  /0.4112e-3, 0.4258e-3,  0.742e-3, 0.166e-3 /
      Data alphat /1.78e-5  ,   1.95e-5,   3.06e-5,  0.75e-5 /
      Data radl   /5.36e-3  ,  10.32e-3, 11.071e-3,  0.00e-3 /

      super = .true.

  10  continue
C
clhb-------use bpfrac to do dedx energy loss correction-----
c-luf------no dedx energy loss corection,just save data-----
      tkpar1=tkpar(1)
      tkpar2=tkpar(2)
      tkpar3=tkpar(3)
      if(tkpar2.eq.0.)return
      psav=sqrt(1.+tkpar3**2)/tkpar2
      thta=acos(tkpar3/sqrt(1.+tkpar3**2))
clhb---------------------------------------------------------
      IF (.not. first) go to 80

      Call telever

C
C   Initialization on first call: perform numerical
C   integrations using trapezoid rule.
C   SCR1 and SCR2 are scratch arrays.
C   BUMIN is needed to convert DPMIN into the Ai.

      scr1(10) = 0.d0
      scr2(10) = 0.d0
      f1(0) = 0.d0
      f2(0) = 0.d0

      DO 70 i=1,940
        scr1(0) = scr1(10)
        scr2(0) = scr2(10)

        DO 60 j=1,10
          b = dble(float(10*i - 10 + j))/10000.d0
          g = 1.d0 / dsqrt(1.d0 - b*b)
          scr1(j) = b**u * g**3
          scr2(j) = b**(u-4.d0) * g
  60    continue

        f1(i) = f1(i-1) + (scr1(0)+scr1(10))/20000.d0
        f2(i) = f2(i-1) + (scr2(0)+scr2(10))/20000.d0

        DO 65 j=1,9
          f1(i) = f1(i) + scr1(j)/10000.d0
          f2(i) = f2(i) + scr2(j)/10000.d0
  65    continue
  70  continue
      bumin = 0.940**sngl(u)
      first = .false.

      write (6,75)
  75  format (/ ' > FUDGE initialized.' /)

C
C--------------------------------------------------------------------C
C
  80  continue
C
C   Correct the error matrix for multiple scattering in the
C   DC gas.
C
      if (iversion.le.7) goto 100
      Call SUPERGAS(xmas,
     > tkpar,ichg,bc,pm,seclm,error,rm,mfit,mzfit,ifit,cd)

      IF (.not. debug) go to 100
      write (6,90) tkpar, error
  90  format (' After GSCATR:' /
     >        '  TKPAR:',6F11.7 /
     >        '  ERROR:',F11.7 / 8X,2F11.7 / 8X,3F11.7
     >                         / 8X,4F11.7 / 8X,5F11.7 )
 100  continue
C
C   Prepare to correct for hard scatterers:
C
      ptot0 = pm(4)
      beta0 = ptot0 / sqrt(xmas**2+ptot0**2)
C
C   Loop over scatterers:
C
      DO 300 i=nscatr,1,-1

        IF (debug) then
          write (6,1100) i, ptot0, beta0
1100      format (' >>>>  Entering scatterer',I2,':' /
     >            '                  P0:',F10.6 /
     >            '               beta0:',F10.6 )
        endif

C
C   Make dE/dx correction:
C
        index0 = int(1000.*beta0)
        IF (index0 .ge. 940) then
          go to 200

        else
          frac0 = 1000.*beta0 - float(index0)

          q0 = f1(index0) + dble(frac0)*(f1(index0+1)-f1(index0))
          q1 = q0 + dble(dpmin(i)*bumin*seclm(1)/xmas)

          IF (debug) then
            write (6,1130) index0, frac0, q0, q1
1130        format ('              index0:',I10 /
     >              '               frac0:',F10.6 /
     >              '                  Q0:',F10.6 /
     >              '                  Q1:',F10.6)
          endif

          IF (q1 .ge. f1(940)) then
            go to 200

          else
C
C   Full calculation for beta < 0.94:
C
            i1 = index0
            i2 = 940
            a1 = f1(i1)
            a2 = f1(i2)

 150        continue

            im = (i1+i2)/2
            am = f1(im)

            IF (debug) then
              write (6,1150) i1, a1, im, am, i2, a2
1150          format (' i1 a1 im am i2 a2:',3(i6,f10.6))
            endif

            IF (q1 .gt. am) then
              i1 = im
              a1 = am
            else
              i2 = im
              a2 = am
            endif

            IF (i2-i1 .gt. 1) go to 150

            beta1 = (float(i1) + sngl((q1-a1)/(a2-a1)))/1000.
            ptot1 = xmas*beta1/sqrt(1.-beta1**2)

            IF (debug) then
              write (6,1170) ptot1, beta1
1170          format ('                  P1:',F10.6 /
     >                '               beta1:',F10.6)
            endif

            index1 = int(1000.*beta1)
            frac1 = 1000.*beta1 - float(index1)
            q0 = f2(index0) + dble(frac0)*(f2(index0+1)-f2(index0))
            q1 = f2(index1) + dble(frac1)*(f2(index1+1)-f2(index1))
            delth2 = xmcs*radl(i)*sngl(q1-q0)/dpmin(i)/bumin/xmas

            IF (debug) then
              write (6,1171) index1, frac1
1171          format ('              INDEX1:',I10 /
     >                '               FRAC1:',F10.6)
              write (6,1180) q0, q1, delth2
1180          format ('                  Q0:',F10.6 /
     >                '                  Q1:',F10.6 /
     >                '              DELTH2:',F10.6)
            endif

          endif
        endif
        go to 250

 200    continue
C
C  Ultra-relativistic particles:
C
        ptot1 = ptot0 + dpmin(i)*seclm(1)
        beta1 = ptot1 / sqrt(xmas**2+ptot1**2)
        delth2 = xmcs*radl(i)*seclm(1) / (beta1*ptot1)**2
C
C--------------------------------------------------------------------C
C
 250    continue
C
C  Correction in phi and xi:
C  [Notice that xi = XM*sin(phi) - YM*cos(phi).]
C
        dphidk = float(ichg)*rscatr(i)*bc
        delphi = dphidk*seclm(1)*(1./ptot1 - 1./ptot0)
        delxi = rscatr(i)*delphi/2.

        tkpar(4) = tkpar(4) + sin(tkpar(1))*delxi
        tkpar(5) = tkpar(5) - cos(tkpar(1))*delxi

        tkpar(1) = tkpar(1) + delphi
        IF (tkpar(1) .lt. 0.)    tkpar(1) = tkpar(1) + twopi
        IF (tkpar(1) .gt. twopi) tkpar(1) = tkpar(1) - twopi

        ptot0 = ptot1
        beta0 = beta1


        sigk = 2.*alphat(i)*seclm(1) / beta1**3 / ptot1**2
        sigphi = dphidk*sigk
        sigxi  = sigphi*rscatr(i)/2.
        if(iversion.le.7)goto 300
C
C  Correct error matrix for dE/dx losses.
C
C  d phi d phi
        error(1) = error(1) + sigphi**2

C  d phi dk
        error(2) = error(2) + sigk*sigphi

C  dk    dk
        error(3) = error(3) + sigk**2

C  d phi d xi
        error(7) = error(7) + sigphi*sigxi

C  dk    d xi
        error(8) = error(8) + sigk*sigxi

C  d xi  d xi
        error(10) = error(10) + sigxi**2

C
C--------------------------------------------------------------------C
C
C  Correct error matrix for multiple scattering.
C
C  d phi d phi
        error(1) = error(1) + delth2*seclm(2)

C  ds    ds
        error(6) = error(6) + delth2*seclm(4)

C  d phi d xi
        error(7) = error(7) + delth2*seclm(2)*rscatr(i)

C  d xi  d xi
        error(10) = error(10) + delth2*seclm(2)*rscatr(i)**2

C  ds    d eta
        error(13) = error(13) + delth2*seclm(4)*rscatr(i)

C  d eta d eta
        error(15) = error(15) + delth2*seclm(4)*rscatr(i)**2

 300  continue

cluf----------------------------------------------------begin
c-  re-scale MDC error matrix


c--for DATA          

             if(irun.gt.0) then

              if(irun.ge.13500.and.irun.le.13520) then
                L1=1.388
                L2=1.789
                L3=2.463
cxugf              end if
              else if(irun.ge.13521.and.irun.le.13540) then
                L1=1.338
                L2=1.571
                L3=2.449
c              end if
              else if(irun.ge.13541.and.irun.le.13555) then
                L1=1.306
                L2=1.553
                L3=2.013
c              end if

              else if(irun.ge.13556.and.irun.le.13580) then
                L1=1.275
                L2=1.524
                L3=1.921
c              end if

              else if(irun.ge.13581.and.irun.le.13605) then
                L1=1.293
                L2=1.545
                L3=1.922
c              end if

              else if(irun.ge.13606.and.irun.le.13625) then
                L1=1.360
                L2=1.616
                L3=2.045
c              end if

              else if(irun.ge.13626.and.irun.le.13655) then
                L1=1.33
                L2=1.57
                L3=2.077
c              end if

              else if(irun.ge.13656.and.irun.le.13690) then
                L1=1.316
                L2=1.613
                L3=2.092
c              end if

              else if(irun.ge.13691.and.irun.le.13710) then
                L1=1.277
                L2=1.500
                L3=1.754
c              end if


              else if(irun.ge.13711.and.irun.le.13740) then
                L1=1.191
                L2=1.300
                L3=1.508
c              end if

              else if(irun.ge.13741.and.irun.le.13790) then
                L1=1.216
                L2=1.315
                L3=1.559
c              end if


              else if(irun.ge.13791.and.irun.le.13855) then
                L1=1.346
                L2=1.612
                L3=1.834
c              end if

              else if(irun.ge.13856.and.irun.le.13865) then
                L1=1.298
                L2=1.446
                L3=1.693
c              end if

              else if(irun.ge.13866.and.irun.le.13940) then
                L1=1.252
                L2=1.347
                L3=1.565
c              end if

              else if(irun.ge.13941.and.irun.le.13950) then
                L1=1.233
                L2=1.458
                L3=1.597
c              end if

              else if(irun.ge.13951.and.irun.le.13975) then
                L1=1.282
                L2=1.462
                L3=1.710
c              end if

              else if(irun.ge.13976.and.irun.le.13985) then
                L1=1.310
                L2=1.539
                L3=1.714
c              end if

              else if(irun.ge.13986.and.irun.le.14015) then
                L1=1.295
                L2=1.45
                L3=1.71
c              end if

              else if(irun.ge.14016.and.irun.le.14030) then
                L1=1.353
                L2=1.581
                L3=1.718
c              end if

              else if(irun.ge.14031.and.irun.le.14055) then
                L1=1.33
                L2=1.547
                L3=1.726
c              end if

              else if(irun.ge.14056.and.irun.le.14080) then
                L1=1.324
                L2=1.475
                L3=1.694
c              end if

              else if(irun.ge.14081.and.irun.le.14095) then
                L1=1.345
                L2=1.534
                L3=1.663
c              end if

              else if(irun.ge.14096.and.irun.le.14140) then
                L1=1.276
                L2=1.452
                L3=1.688
c              end if

              else if(irun.ge.14141.and.irun.le.14145) then
                L1=1.387
                L2=1.727
                L3=1.926
c              end if

              else if(irun.ge.14146.and.irun.le.14150) then
                L1=1.356
                L2=1.744
                L3=2.142
c              end if

              else if(irun.ge.14151.and.irun.le.14155) then
                L1=1.284
                L2=1.527
                L3=1.680
c              end if

              else if(irun.ge.14156.and.irun.le.14160) then
                L1=1.242
                L2=1.460
                L3=1.56
c              end if

              else if(irun.ge.14161.and.irun.le.14200) then
                L1=1.27
                L2=1.427
                L3=1.644
c              end if

              else if(irun.ge.14201.and.irun.le.14220) then
                L1=1.28
                L2=1.385
                L3=1.583
c              end if

              else if(irun.ge.14221.and.irun.le.14275) then
                L1=1.246
                L2=1.320
                L3=1.557
c              end if

              else if(irun.ge.14276.and.irun.le.14280) then
                L1=1.13
                L2=1.261
                L3=1.526
c              end if

              else if(irun.ge.14281.and.irun.le.14295) then
                L1=1.25
                L2=1.38
                L3=1.59
c              end if

              else if(irun.ge.14296.and.irun.le.14300) then
                L1=1.232
                L2=1.343
                L3=1.521
c              end if

              else if(irun.ge.14301.and.irun.le.14310) then
                L1=1.264
                L2=1.384
                L3=1.548
c              end if

              else if(irun.ge.14311.and.irun.le.14320) then
                L1=1.25
                L2=1.321
                L3=1.579
c              end if

              else if(irun.ge.14321.and.irun.le.14325) then
                L1=1.17
                L2=1.334
                L3=1.379
c              end if

              else if(irun.ge.14326.and.irun.le.14330) then
                L1=1.194
                L2=1.312
                L3=1.558
c              end if

              else if(irun.ge.14331.and.irun.le.14345) then
                L1=1.321
                L2=1.462
                L3=1.688
c              end if

              else if(irun.ge.14346.and.irun.le.14360) then
                L1=1.285
                L2=1.332
                L3=1.579
c              end if

              else if(irun.ge.14361.and.irun.le.14380) then
                L1=1.214
                L2=1.295
                L3=1.547
c              end if

              else if(irun.ge.14381.and.irun.le.14425) then
                L1=1.261
                L2=1.357
                L3=1.621
c              end if

              else if(irun.ge.14426.and.irun.le.14440) then
                L1=1.223
                L2=1.292
                L3=1.571
c              end if

              else if(irun.ge.14441.and.irun.le.14495) then
                L1=1.233
                L2=1.327
                L3=1.595
c              end if

              else if(irun.ge.14496.and.irun.le.14550) then
                L1=1.258
                L2=1.341
                L3=1.54
c              end if

              else if(irun.ge.14551.and.irun.le.14565) then
                L1=1.185
                L2=1.247
                L3=1.491
c              end if

              else if(irun.ge.14566.and.irun.le.14580) then
                L1=1.238
                L2=1.297
                L3=1.532
c              end if

              else if(irun.ge.14581.and.irun.le.14590) then
                L1=1.229
                L2=1.306
                L3=1.606
c              end if

              else if(irun.ge.14591.and.irun.le.14600) then
                L1=1.212
                L2=1.274
                L3=1.466
c              end if

              else if(irun.ge.14601.and.irun.le.14610) then
                L1=1.224
                L2=1.352
                L3=1.537
c              end if

              else if(irun.ge.14611.and.irun.le.14625) then
                L1=1.201
                L2=1.262
                L3=1.535
c              end if

              else if(irun.ge.14626.and.irun.le.14650) then
                L1=1.239
                L2=1.320
                L3=1.572
c              end if

              else if(irun.ge.14651.and.irun.le.14655) then
                L1=1.215
                L2=1.311
                L3=1.442
c              end if

              else if(irun.ge.14656.and.irun.le.14675) then
                L1=1.247
                L2=1.294
                L3=1.581
c              end if

              else if(irun.ge.14676.and.irun.le.14705) then
                L1=1.243
                L2=1.254
                L3=1.546
c              end if

              else if(irun.ge.14706.and.irun.le.14725) then
                L1=1.207
                L2=1.312
                L3=1.498
c              end if

              else if(irun.ge.14726.and.irun.le.14750) then
                L1=1.205
                L2=1.288
                L3=1.492
c              end if

              else if(irun.ge.14751.and.irun.le.14795) then
                L1=1.236
                L2=1.314
                L3=1.527
c              end if

              else if(irun.ge.14796.and.irun.le.14800) then
                L1=1.164
                L2=1.192
                L3=1.473
c              end if

              else if(irun.ge.14801.and.irun.le.14815) then
                L1=1.232
                L2=1.310
                L3=1.558
c              end if

              else if(irun.ge.14816.and.irun.le.14830) then
                L1=1.234
                L2=1.31
                L3=1.528
c              end if

              else if(irun.ge.14831.and.irun.le.14860) then
                L1=1.234
                L2=1.318
                L3=1.552
c              end if

              else if(irun.ge.14861.and.irun.le.14870) then
                L1=1.25
                L2=1.277
                L3=1.591
c              end if

              else if(irun.ge.14871.and.irun.le.14875) then
                L1=1.222
                L2=1.36
                L3=1.452
c              end if

              else if(irun.ge.14876.and.irun.le.14880) then
                L1=1.231
                L2=1.251
                L3=1.496
c              end if

              else if(irun.ge.14881.and.irun.le.14885) then
                L1=1.198
                L2=1.275
                L3=1.48
c              end if

              else if(irun.ge.14886.and.irun.le.14900) then
                L1=1.186
                L2=1.241
                L3=1.514
c              end if

              else if(irun.ge.14901.and.irun.le.14915) then
                L1=1.232
                L2=1.272
                L3=1.487
c              end if

              else if(irun.ge.14916.and.irun.le.14925) then
                L1=1.207
                L2=1.322
                L3=1.533
c              end if

              else if(irun.ge.14926.and.irun.le.14955) then
                L1=1.219
                L2=1.312
                L3=1.562
c              end if

              else if(irun.ge.14956.and.irun.le.15000) then
                L1=1.224
                L2=1.318
                L3=1.563
c              end if

              else if(irun.ge.15001.and.irun.le.15020) then
                L1=1.242
                L2=1.311
                L3=1.538
c              end if

              else if(irun.ge.15021.and.irun.le.15025) then
                L1=1.245
                L2=1.177
                L3=1.528
c              end if

              else if(irun.ge.15026.and.irun.le.15040) then
                L1=1.225
                L2=1.151
                L3=1.393
c              end if

              else if(irun.ge.15041.and.irun.le.15045) then
                L1=1.319
                L2=1.244
                L3=1.352
c              end if

              else if(irun.ge.15046.and.irun.le.15060) then
                L1=1.199
                L2=1.2
                L3=1.449
c              end if

              else if(irun.ge.15061.and.irun.le.15080) then
                L1=1.232
                L2=1.293
                L3=1.601
c              end if

cxugf              else if(irun.ge.15021.and.irun.le.15065) then
cxugf                L1=L1*1.031
cxugf                L2=L2*1.062
cxugf                L3=L3*1.023
cxugf              end if



              else if(irun.ge.15081.and.irun.le.15095) then
                L1=1.263
                L2=1.335
                L3=1.515
c              end if

              else if(irun.ge.15096.and.irun.le.15105) then
                L1=1.141
                L2=1.270
                L3=1.513
c              end if

              else if(irun.ge.15106.and.irun.le.15115) then
                L1=1.218
                L2=1.304
                L3=1.503
c              end if

              else if(irun.ge.15116.and.irun.le.15125) then
                L1=1.197
                L2=1.285
                L3=1.497
c              end if

              else if(irun.ge.15126.and.irun.le.15165) then
                L1=1.222
                L2=1.318
                L3=1.508
c              end if

              else if(irun.ge.15166.and.irun.le.15170) then
                L1=1.18
                L2=1.206
                L3=1.394
c              end if

              else if(irun.ge.15171.and.irun.le.15195) then
                L1=1.25
                L2=1.335
                L3=1.574
c              end if

              else if(irun.ge.15196.and.irun.le.15205) then
                L1=1.227
                L2=1.28
                L3=1.577
c              end if

              else if(irun.ge.15206.and.irun.le.15255) then
                L1=1.23
                L2=1.331
                L3=1.585
c              end if

              else if(irun.ge.15256.and.irun.le.15290) then
                L1=1.222
                L2=1.304
                L3=1.514
c              end if

              else if(irun.ge.15291.and.irun.le.15295) then
                L1=1.166
                L2=1.328
                L3=1.541
c              end if

              else if(irun.ge.15296.and.irun.le.15460) then
                L1=1.226
                L2=1.305
                L3=1.534
c              end if

              else if(irun.ge.15461.and.irun.le.15540) then
                L1=1.23
                L2=1.291
                L3=1.496
c              end if

              else if(irun.ge.15541.and.irun.le.15570) then
                L1=1.218
                L2=1.321
                L3=1.533
c              end if

              else if(irun.ge.15571.and.irun.le.15640) then
                L1=1.212
                L2=1.29
                L3=1.505
c              end if

              else if(irun.ge.15641.and.irun.le.15685) then
                L1=1.236
                L2=1.362
                L3=1.558
c              end if

              else if(irun.ge.15686.and.irun.le.15700) then
                L1=1.196
                L2=1.282
                L3=1.526
c              end if

              else if(irun.ge.15701.and.irun.le.15705) then
                L1=1.169
                L2=1.355
                L3=1.41
c              end if

              else if(irun.ge.15706.and.irun.le.15780) then
                L1=1.243
                L2=1.328
                L3=1.576
c              end if

              else if(irun.ge.15781.and.irun.le.15795) then
                L1=1.275
                L2=1.417
                L3=1.689
c              end if

              else if(irun.ge.15796.and.irun.le.15810) then
                L1=1.292
                L2=1.553
                L3=1.75
c              end if

              else if(irun.ge.15811.and.irun.le.15830) then
                L1=1.241
                L2=1.362
                L3=1.549
c              end if

              else if(irun.ge.15831.and.irun.le.15860) then
                L1=1.208
                L2=1.308
                L3=1.542
c              end if

              else if(irun.ge.15861.and.irun.le.15965) then
                L1=1.231
                L2=1.328
                L3=1.563
c              end if

              else if(irun.ge.15966.and.irun.le.15980) then
                L1=1.255
                L2=1.373
                L3=1.616
c              end if

              else if(irun.ge.15981.and.irun.le.16045) then
                L1=1.214
                L2=1.313
                L3=1.512
c              end if

              else if(irun.ge.16046.and.irun.le.16050) then
                L1=1.189
                L2=1.246
                L3=1.513
c              end if

              else if(irun.ge.16051.and.irun.le.16095) then
                L1=1.202
                L2=1.33
                L3=1.525
c              end if

              else if(irun.ge.16096.and.irun.le.16220) then
                L1=1.227
                L2=1.312
                L3=1.544
c              end if

              else if(irun.ge.16221.and.irun.le.16240) then
                L1=1.273
                L2=1.426
                L3=1.787
c              end if

              else if(irun.ge.16241.and.irun.le.16245) then
                L1=1.405
                L2=1.622
                L3=1.92
c              end if

              else if(irun.ge.16246.and.irun.le.16255) then
                L1=1.464
                L2=1.766
                L3=2.362
c              end if

              else if(irun.ge.16256.and.irun.le.16260) then
                L1=1.244
                L2=1.439
                L3=1.658
c              end if

              else if(irun.ge.16261.and.irun.le.16320) then
                L1=1.261
                L2=1.411
                L3=1.605
c              end if


              else if(irun.ge.16321.and.irun.le.16335) then
                L1=1.245
                L2=1.349
                L3=1.545
c              end if


              else if(irun.ge.16336.and.irun.le.16375) then
                L1=1.262
                L2=1.414
                L3=1.598
c              end if


              else if(irun.ge.16376.and.irun.le.16400) then
                L1=1.252
                L2=1.365
                L3=1.618
c              end if

              else if(irun.ge.16401.and.irun.le.16405) then
                L1=1.223
                L2=1.231
                L3=1.603
c              end if

              else if(irun.ge.16406.and.irun.le.16415) then
                L1=1.222
                L2=1.401
                L3=1.654
c              end if

              else if(irun.ge.16416.and.irun.le.16425) then
                L1=1.264
                L2=1.468
                L3=1.744
c              end if

              else if(irun.ge.16426.and.irun.le.16430) then
                L1=1.283
                L2=1.477
                L3=1.774
c              end if

              else if(irun.ge.16431.and.irun.le.16535) then
                L1=1.263
                L2=1.415
                L3=1.632
c              end if

              else if(irun.ge.16536.and.irun.le.16575) then
                L1=1.275
                L2=1.408
                L3=1.617
c              end if

              else if(irun.ge.16576.and.irun.le.16600) then
                L1=1.226
                L2=1.366
                L3=1.561
c              end if

              else if(irun.ge.16601.and.irun.le.16640) then
                L1=1.235
                L2=1.411
                L3=1.6
c              end if

              else if(irun.ge.16641.and.irun.le.16660) then
                L1=1.242
                L2=1.38
                L3=1.583
c              end if

              else if(irun.ge.16661.and.irun.le.16675) then
                L1=1.218
                L2=1.442
                L3=1.621
c              end if

              else if(irun.ge.16676.and.irun.le.16735) then
                L1=1.261
                L2=1.415
                L3=1.652
c              end if

              else if(irun.ge.16736.and.irun.le.16750) then
                L1=1.285
                L2=1.471
                L3=1.638
c              end if

              else if(irun.ge.16751.and.irun.le.16790) then
                L1=1.266
                L2=1.409
                L3=1.598
c              end if

              else if(irun.ge.16791.and.irun.le.16810) then
                L1=1.226
                L2=1.4
                L3=1.571
c              end if

              else if(irun.ge.16811.and.irun.le.16844) then
                L1=1.255
                L2=1.374
                L3=1.606
c              end if

c---XUGF, YUANJM ------
              else if(irun.ge.17209.and.irun.le.17216) then
                L1=1.258
                L2=1.351
                L3=1.475
              else if(irun.ge.17217.and.irun.le.17228) then
                L1=1.295
                L2=1.390
                L3=1.591
              else if(irun.ge.17229.and.irun.le.17251) then
                L1=1.298
                L2=1.399
                L3=1.595
              else if(irun.ge.17252.and.irun.le.17254) then
                L1=1.284
                L2=1.334
                L3=1.484
              else if(irun.ge.17255.and.irun.le.17275) then
                L1=1.270
                L2=1.396
                L3=1.519
              else if(irun.ge.17276.and.irun.le.17290) then
                L1=1.271
                L2=1.377
                L3=1.573
              else if(irun.ge.17291.and.irun.le.17298) then
                L1=1.342
                L2=1.495
                L3=1.719
              else if(irun.ge.17299.and.irun.le.17303) then
                L1=1.292
                L2=1.457
                L3=1.666
              else if(irun.ge.17304.and.irun.le.17306) then
                L1=1.215
                L2=1.362
                L3=1.500
              else if(irun.ge.17307.and.irun.le.17315) then
                L1=1.248
                L2=1.310
                L3=1.539
              else if(irun.ge.17316.and.irun.le.17318) then
                L1=1.214
                L2=1.350
                L3=1.539
              else if(irun.ge.17319.and.irun.le.17327) then
                L1=1.228
                L2=1.309
                L3=1.517
              else if(irun.ge.17328.and.irun.le.17331) then
                L1=1.254
                L2=1.349
                L3=1.519
              else if(irun.ne.17332) then
                L1=1.239
                L2=1.307
                L3=1.516
              else if(irun.ge.17333.and.irun.le.17338) then
                L1=1.247
                L2=1.319
                L3=1.513
              else if(irun.ge.17339.and.irun.le.17362) then
                L1=1.252
                L2=1.332
                L3=1.532
              else if(irun.ge.17363.and.irun.le.17379) then
                L1=1.258
                L2=1.345
                L3=1.534
              else if(irun.ge.17380.and.irun.le.17389) then
                L1=1.278
                L2=1.337
                L3=1.551
              else if(irun.ge.17390.and.irun.le.17401) then
                L1=1.296
                L2=1.430
                L3=1.706
              else if(irun.ge.17402.and.irun.le.17406) then
                L1=1.0
                L2=1.0
                L3=1.0
              else if(irun.ge.17407.and.irun.le.17412) then
                L1=1.258
                L2=1.331
                L3=1.543
              else if(irun.ge.17413.and.irun.le.17422) then
                L1=1.208
                L2=1.274
                L3=1.419
              else if(irun.ge.17423.and.irun.le.17442) then
                L1=1.287
                L2=1.354
                L3=1.507
              else if(irun.ge.17443.and.irun.le.17449) then
                L1=1.253
                L2=1.367
                L3=1.555
              else if(irun.ge.17450.and.irun.le.17451) then
                L1=1.234
                L2=1.332
                L3=1.532
              else if(irun.ge.17452.and.irun.le.17469) then
                L1=1.270
                L2=1.370
                L3=1.554
              else if(irun.ge.17470.and.irun.le.17472) then
                L1=1.332
                L2=1.447
                L3=1.578
              else if(irun.ge.17473.and.irun.le.17480) then
                L1=1.239
                L2=1.326
                L3=1.508
              else if(irun.ge.17481.and.irun.le.17502) then
                L1=1.243
                L2=1.331
                L3=1.497
              else if(irun.ge.17503.and.irun.le.17528) then
                L1=1.245
                L2=1.308
                L3=1.493
              else if(irun.ge.17529.and.irun.le.17533) then
                L1=1.222
                L2=1.283
                L3=1.498
              else if(irun.ge.17534.and.irun.le.17559) then
                L1=1.282
                L2=1.344
                L3=1.518
              else if(irun.ge.17560.and.irun.le.17576) then
                L1=1.241
                L2=1.306
                L3=1.490
              else if(irun.ge.17577.and.irun.le.17591) then
                L1=1.242
                L2=1.310
                L3=1.513
              else if(irun.ge.17592.and.irun.le.17604) then
                L1=1.237
                L2=1.318
                L3=1.489
              else if(irun.ge.17605.and.irun.le.17613) then
                L1=1.227
                L2=1.287
                L3=1.479
              else if(irun.ge.17614.and.irun.le.17627) then
                L1=1.228
                L2=1.301
                L3=1.493
              else if(irun.ge.17628.and.irun.le.17670) then
                L1=1.247
                L2=1.313
                L3=1.327
              else if(irun.ge.17671.and.irun.le.17696) then
                L1=1.270
                L2=1.334
                L3=1.527
              else if(irun.ge.17697.and.irun.le.17718) then
                L1=1.287
                L2=1.336
                L3=1.492
              else if(irun.ge.17719.and.irun.le.17729) then
                L1=1.251
                L2=1.330
                L3=1.482
              else if(irun.ge.17730.and.irun.le.17736) then
                L1=1.285
                L2=1.294
                L3=1.446
              else if(irun.ge.17737.and.irun.le.17742) then
                L1=1.287
                L2=1.309
                L3=1.478
              else if(irun.ge.17743.and.irun.le.17754) then
                L1=1.255
                L2=1.299
                L3=1.468
              else if(irun.ge.17755.and.irun.le.17757) then
                L1=1.227
                L2=1.281
                L3=1.437
              else if(irun.ge.17758.and.irun.le.17773) then
                L1=1.235
                L2=1.317
                L3=1.510
              else if(irun.ge.17774.and.irun.le.17790) then
                L1=1.248
                L2=1.311
                L3=1.500
              else if(irun.ge.17791.and.irun.le.17800) then
                L1=1.247
                L2=1.322
                L3=1.524
              else if(irun.ge.17801.and.irun.le.17822) then
                L1=1.249
                L2=1.320
                L3=1.502
              else if(irun.ge.17823.and.irun.le.17825) then
                L1=1.217
                L2=1.289
                L3=1.474
              else if(irun.ge.17826.and.irun.le.17847) then
                L1=1.237
                L2=1.300
                L3=1.492
              else if(irun.ge.17848.and.irun.le.17851) then
                L1=1.244
                L2=1.311
                L3=1.457
              else if(irun.ge.17852.and.irun.le.17865) then
                L1=1.293
                L2=1.335
                L3=1.521
              else if(irun.ge.17866.and.irun.le.17871) then
                L1=1.281
                L2=1.254
                L3=1.504
              else if(irun.ge.17872.and.irun.le.17888) then
                L1=1.285
                L2=1.352
                L3=1.501
              else if(irun.ge.17889.and.irun.le.17895) then
                L1=1.274
                L2=1.328
                L3=1.500
              else if(irun.ge.17896.and.irun.le.17899) then
                L1=1.257
                L2=1.346
                L3=1.533
              else if(irun.ge.17900.and.irun.le.17906) then
                L1=1.271
                L2=1.338
                L3=1.495
              else if(irun.ge.17907.and.irun.le.17935) then
                L1=1.278
                L2=1.348
                L3=1.517
              else if(irun.ge.17936.and.irun.le.17962) then
                L1=1.254
                L2=1.319
                L3=1.466
              else if(irun.ge.17963.and.irun.le.17966) then
                L1=1.287
                L2=1.330
                L3=1.515
              else if(irun.ge.17967.and.irun.le.18005) then
                L1=1.310
                L2=1.406
                L3=1.582
              else if(irun.ge.18006.and.irun.le.18016) then
                L1=1.306
                L2=1.372
                L3=1.528
              else if(irun.ge.18017.and.irun.le.18037) then
                L1=1.322
                L2=1.398
                L3=1.562
              else if(irun.ge.18038.and.irun.le.18048) then
                L1=1.313
                L2=1.379
                L3=1.570
              else if(irun.ge.18049.and.irun.le.18066) then
                L1=1.295
                L2=1.378
                L3=1.572
              else if(irun.ge.18067.and.irun.le.18085) then
                L1=1.304
                L2=1.355
                L3=1.558
              else if(irun.eq.18086) then
                L1=1.322
                L2=1.346
                L3=1.503
              else if(irun.ge.18087.and.irun.le.18093) then
                L1=1.412
                L2=1.501
                L3=1.806
              else if(irun.ge.18094.and.irun.le.18096) then
                L1=1.455
                L2=1.939
                L3=2.463
              else if(irun.ge.18097.and.irun.le.18122) then
                L1=1.324
                L2=1.464
                L3=1.711
              else if(irun.ge.18123.and.irun.le.18133) then
                L1=1.305
                L2=1.365
                L3=1.545
              else if(irun.ge.18134.and.irun.le.18156) then
                L1=1.314
                L2=1.365
                L3=1.558
              else if(irun.ge.18157.and.irun.le.18182) then
                L1=1.265
                L2=1.322
                L3=1.508
              else if(irun.ge.18183.and.irun.le.18197) then
                L1=1.287
                L2=1.346
                L3=1.520
              else if(irun.ge.18198.and.irun.le.18221) then
                L1=1.266
                L2=1.339
                L3=1.524
              else if(irun.ge.18222.and.irun.le.18245) then
                L1=1.262
                L2=1.309
                L3=1.494
              else if(irun.ge.18246.and.irun.le.18264) then
                L1=1.280
                L2=1.373
                L3=1.505
              else if(irun.ge.18265.and.irun.le.18289) then
                L1=1.296
                L2=1.388
                L3=1.546
              else if(irun.ge.18290.and.irun.le.18296) then
                L1=1.0
                L2=1.0
                L3=1.0
              else if(irun.ge.18297.and.irun.le.18310) then
                L1=1.267
                L2=1.354
                L3=1.543
              else if(irun.ge.18311.and.irun.le.18326) then
                L1=1.272
                L2=1.354
                L3=1.532
              else if(irun.ge.18327.and.irun.le.18354) then
                L1=1.302
                L2=1.378
                L3=1.549
              else if(irun.ge.18355.and.irun.le.18389) then
                L1=1.280
                L2=1.346
                L3=1.517
              else if(irun.ge.18390.and.irun.le.18393) then
                L1=1.275
                L2=1.386
                L3=1.486
              else if(irun.ge.18394.and.irun.le.18411) then
                L1=1.277
                L2=1.356
                L3=1.538
              else if(irun.ge.18412.and.irun.le.18441) then
                L1=1.306
                L2=1.394
                L3=1.548
              else if(irun.ge.18442.and.irun.le.18443) then
                L1=1.244
                L2=1.297
                L3=1.398
              else if(irun.ge.18444.and.irun.le.18452) then
                L1=1.258
                L2=1.353
                L3=1.498
              else if(irun.ge.18453.and.irun.le.18463) then
                L1=1.265
                L2=1.325
                L3=1.489
              else if(irun.ge.18464.and.irun.le.18483) then
                L1=1.269
                L2=1.332
                L3=1.480
              else if(irun.ge.18484.and.irun.le.18486) then
                L1=1.275
                L2=1.341
                L3=1.509
              else if(irun.eq.18487) then
                L1=1.245
                L2=1.361
                L3=1.662
              else if(irun.ge.18488.and.irun.le.18506) then
                L1=1.306
                L2=1.345
                L3=1.547
              else if(irun.ge.18507.and.irun.le.18518) then
                L1=1.296
                L2=1.381
                L3=1.562
              else if(irun.ge.18519.and.irun.le.18522) then
                L1=1.302
                L2=1.383
                L3=1.567
              else if(irun.ge.18523.and.irun.le.18545) then
                L1=1.296
                L2=1.377
                L3=1.540
              else if(irun.ge.18546.and.irun.le.18591) then
                L1=1.314
                L2=1.391
                L3=1.590
              else if(irun.ge.18592.and.irun.le.18597) then
                L1=1.276
                L2=1.332
                L3=1.511
              else if(irun.ge.18598.and.irun.le.18604) then
                L1=1.264
                L2=1.339
                L3=1.558
              else if(irun.ge.18605.and.irun.le.18633) then
                L1=1.283
                L2=1.360
                L3=1.544
              else if(irun.ge.18634.and.irun.le.18641) then
                L1=1.346
                L2=1.467
                L3=1.696
              else if(irun.ge.18642.and.irun.le.18648) then
                L1=1.309
                L2=1.572
                L3=1.609
              else if(irun.ge.18649.and.irun.le.18678) then
                L1=1.275
                L2=1.351
                L3=1.554
              else if(irun.eq.18679) then
                L1=1.276
                L2=1.284
                L3=1.520
              else if(irun.ge.18680.and.irun.le.18701) then
                L1=1.288
                L2=1.369
                L3=1.539
              else if(irun.eq.18702) then
                L1=1.226
                L2=1.329
                L3=1.412
              else if(irun.ge.18703.and.irun.le.18731) then
                L1=1.228
                L2=1.329
                L3=1.412
              else if(irun.ge.18732.and.irun.le.18746) then
                L1=1.262
                L2=1.329
                L3=1.494
              else if(irun.ge.18747.and.irun.le.18771) then
                L1=1.268
                L2=1.333
                L3=1.577
              else if(irun.ge.18772.and.irun.le.18777) then
                L1=1.283
                L2=1.358
                L3=1.607
              else if(irun.ge.18778.and.irun.le.18779) then
                L1=1.231
                L2=1.306
                L3=1.486
              else if(irun.ge.18780.and.irun.le.18794) then
                L1=1.269
                L2=1.365
                L3=1.573
              else if(irun.ge.18795.and.irun.le.18823) then
                L1=1.263
                L2=1.333
                L3=1.497
              else if(irun.ge.18824.and.irun.le.18825) then
                L1=1.206
                L2=1.222
                L3=1.453
              else if(irun.ge.18826.and.irun.le.18827) then
                L1=1.253
                L2=1.357
                L3=1.542
              else if(irun.ge.18828.and.irun.le.18841) then
                L1=1.286
                L2=1.364
                L3=1.666
              else if(irun.ge.18842.and.irun.le.18846) then
                L1=1.248
                L2=1.317
                L3=1.486
              else if(irun.ge.18847.and.irun.le.18866) then
                L1=1.280
                L2=1.355
                L3=1.513
              else if(irun.ge.18867.and.irun.le.18885) then
                L1=1.273
                L2=1.361
                L3=1.546
              else if(irun.ge.18886.and.irun.le.18891) then
                L1=1.264
                L2=1.368
                L3=1.528
              else if(irun.ge.18892.and.irun.le.18906) then
                L1=1.291
                L2=1.382
                L3=1.523
              else if(irun.ge.18907.and.irun.le.18912) then
                L1=1.255
                L2=1.325
                L3=1.483
              else if(irun.ge.18913.and.irun.le.18931) then
                L1=1.323
                L2=1.415
                L3=1.615
              else if(irun.ge.18932.and.irun.le.18956) then
                L1=1.305
                L2=1.396
                L3=1.541
              else if(irun.ge.18957.and.irun.le.18962) then
                L1=1.287
                L2=1.392
                L3=1.512
              else if(irun.ge.18963.and.irun.le.18980) then
                L1=1.294
                L2=1.366
                L3=1.537
              else if(irun.ge.18981.and.irun.le.18992) then
                L1=1.298
                L2=1.377
                L3=1.503
              else if(irun.ge.18993.and.irun.le.19006) then
                L1=1.278
                L2=1.366
                L3=1.523
              else if(irun.ge.19007.and.irun.le.19025) then
                L1=1.279
                L2=1.356
                L3=1.513
              else if(irun.ge.19026.and.irun.le.19052) then
                L1=1.258
                L2=1.331
                L3=1.513
              else if(irun.ge.19053.and.irun.le.19073) then
                L1=1.278
                L2=1.349
                L3=1.534
              else if(irun.ge.19074.and.irun.le.19093) then
                L1=1.270
                L2=1.344
                L3=1.507
              else if(irun.ge.19094.and.irun.le.19117) then
                L1=1.251
                L2=1.325
                L3=1.509
              else if(irun.ge.19118.and.irun.le.19135) then
                L1=1.271
                L2=1.335
                L3=1.477
              else if(irun.ge.19136.and.irun.le.19149) then
                L1=1.266
                L2=1.354
                L3=1.520
              else
                L1=1.000
                L2=1.000
                L3=1.000
              end if

              if(irun.ge.15021.and.irun.le.15065) then
                L1=L1*1.031
                L2=L2*1.062
                L3=L3*1.023
              end if

c--- xugf,yuanjm -----
c-for MC
             else
cxugf               if(irun.lt.0) then
               L1=1.0028
               L2=1.2235
               L3=1.3344
             end if

             error(1)=error(1)*L1*L1
             error(2)=error(2)*L1*L2
             error(3)=error(3)*L2*L2
             error(4)=error(4)*L1*L3
             error(5)=error(5)*L2*L3
             error(6)=error(6)*L3*L3
cluf-------------------------------------------------end

      if(iversion.eq.86.and.iversion.ne.831)then

clhb----------------mike kelsey's dedx correction-----------------------
        idp=4
        if(xmas.le.0.01)idp=2
        if(abs(xmas-0.10566).le.0.005)idp=3
        if(abs(xmas-0.49365).le.0.005)idp=5
        if(abs(xmas-0.93827).le.0.005)idp=6
        bp=bpfrac(idp,psav,thta)
        if(bp.eq.0.)bp=1.
        tkpar(2)=tkpar2/bp
        tkpar(3)=tkpar3
        tkpar(1)=tkpar1
         return
clhb-------------------------------------------------------------------
        endif
c      tkpar(2) = seclm(1) / ptot0

cluf----------------BESII dedx correction-----------------------begin
      if(iversion.gt.100.and.iversion.ne.831)then
        idp=4
        if(abs(xmas-0.000511).le.0.0003) idp=2
        if(abs(xmas-0.10566).le.0.005) idp=3
        if(abs(xmas-0.49365).le.0.005) idp=5
        if(abs(xmas-0.93827).le.0.005) idp=6

c--dE/dX correction for 1/Pxy
        bp=bpcorr2(idp,psav,thta)/psav
        if(bp.eq.0.) bp=1.
        tkpar(2)=tkpar2/bp
        tkpar(3)=tkpar3
        tkpar(1)=tkpar1
         return
        endif
cluf---------------------------------------------------------------end
      return
      end
