C
C $Id$
C $Author$
C
C Best fit of kaon track in the KEAT chambers.  This routine has to be called
C after umsana, as geometry is initialized there.  Also clusering and pattern
C recognition is done there.  We only do fitting.
C
      subroutine keatana()
*
      IMPLICIT NONE
*
#include "umsanapar.inc"
#include "umscommon.inc"
#include "umsana.inc"
#include "umsdata.inc"
#include "keatana.inc"
#include "keatdata.inc"
*
      integer
     $     itrk                                  ,! track index
     $     ivw                                   ,! view index 1..3
     $     ilp                                   ,! chamber projections 1..16
     $     ich                                   ,! chamber index 1..8
     $     icp                                   ,! plane in the chamber 1..6
     $     ispec                                 ,! spectrometer index 1..2
     $     ihit                                   ! cluster index

      integer
     $     imask(ncp,nch)                         ! cluster mask
      real 
     $     hit  (ncp,nch),                        ! hit coordinates 
     $     hite (ncp,nch)                         !   and errors

      real
     $     x, y, tx, ty,                          ! track slopes and intercepts
     $     chi2                                   ! track chi2

      logical first
      data    first /.true./
      save    first
      
      if (nttrk.ne.1) then
        nkeattrk = 0
        goto 9000                                 ! too few tracks quit
      endif

      do itrk=1,nttrk                             ! for each track
        do icp = 1, ncp                           ! for each plane
          do ich = 1, nch                         ! for each chamber

            ihit = iclptk(icp,ich,itrk)
            if (ihit.le.0) then
              hit  (icp,ich) = -999.0
              hite (icp,ich) =  999.0
              imask(icp,ich) = -1
            else
              hit  (icp,ich) = clstrc(ihit,icp,ich)
              hite (icp,ich) = isizcl(ihit,icp,ich)*nwrsp(ich)
              imask(icp,ich) = ihit
            endif

c            write(*,'(4(a,1x,i4),(a,1x,f10.4,1x,a,f7.3,1x,a,f5.3))') 
c     $           'itrk=  ', itrk,
c     $           'chamber= ', ich,
c     $           'plane= ', icp,
c     $           'ihit=', ihit,
c     $           'z=', zpcspc(icp,ich),
c     $           'hit=',  hit (icp,ich),
c     $           'hite=', hite(icp,ich)
          enddo                                   ! for each chamber
        enddo                                     ! for each plane
      enddo                                       ! for each track

c      call trkfts (zpcspc, hit, hite, cosp, sinp, imask, 1, 3, 0,
c     $     ncp, nch, tx, ty, x, y, chi2)
c      call trkfts (zpcspc, hit, hite, cosp, sinp, imask, 4, 6, 0,
c     $     ncp, nch, tx, ty, x, y, chi2)
      call trkfts (zpcspc, hit, hite, cosp, sinp, imask, 7, 8, 0,
     $     ncp, nch, tx, ty, x, y, chi2)
c      write(*,'(2(f8.4,1x),2(f6.3,1x),f5.2)') 
c     $     x, y, tx*1000, ty*1000, chi2

      nkeattrk    = 1
      keatptrk(1) = pmtmtk(4,1)
      keatxint(1) = x
      keatyint(1) = y
      keatxusl(1) = tx
      keatyusl(1) = ty
      keatchi2(1) = chi2
      
      return
*
 9000 continue
      return
*
      END

C
C $Log$
C
