# Simple Example of Using KWFIT

In this section I give a brief example (of anal1, anal2, anal3 routines) of how kwfit is used, from initialization to fitting, for building D0 --> K pi combinations. All calls to kwfit routines are hyperlinked to the documentation for those routines.

```      subroutine anal1

*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*  Called at beginning of job
*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

c     Initialization of kwfit
call kset_init

return
end```

```      subroutine anal2

*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*  Called once per run
*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

implicit none

#include "seq/clinc/anclev.inc"

integer I
logical ltesla
double precision beam_pos(3), beam_sig(3)
double precision bfield, bdir(3)
real bscale

data bdir/0., 0., 1./
*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

c     Set beam position & width
c     You need to do this in case you make vertices weighted by the
c     beam spot
c     All quantities must be double precision
do I=1,3
beam_pos(I) = dble(bmpos(I,1))  !dble function actually not needed
beam_sig(I) = dble(bmpos(I,2))
enddo
call kset_beam_position(beam_pos, beam_sig)

c     Set B field (including bfudge correction) and direction
c     Remember that bfie is in kGauss & set ltesla accordingly
call bfudge(runn, bscale)
bfield = dble(bfie * bscale)  !dble function actually not needed
ltesla = .FALSE.
call kset_bfield(bfield, bdir, ltesla)

return
end```

```      subroutine anal3

*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*  Called once per event
*  >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

implicit none

#include "seq/clinc/anclev.inc"

c     Externals
double precision kget_track_mass
external         kget_track_mass

c     Local variables
integer I, type, error, update, list_d0(2), option_d0(2)
integer ipi, iK, npi, nK, list_pi(100), list_K(100)
logical lcovar, ldedx, lvtx
double precision w_K(10), w_pi(10), w_d0(10), chisq
double precision chisq_d0, chisq_mass, mass_d0
double precision vtx(3), Vvtx(3,3)

c     Clear the kwfit track list every event
call kset_clear

c     Get CD tracks
call getcd

c     Make list of pions and kaons from CD tracks. We want
c     the covariance matrix built but no dE/dx correction is
c     necessary because the tracks have already been Kalman fit.
c     The list of kwfit tracks is returned in list_pi and list_K
lcovar = .TRUE.
ldedx = .FALSE.
type = 3
call kfil_track_cd_all(type, ldedx, lcovar, npi, list_pi, error)
type = 4
call kfil_track_cd_all(typr, lcovar, lcovar, nK, list_K, error)

c     Now loop over K, pi lists and build D0's
do iK=1,nK
call kget_track_param(list_K(iK), w_K)
do ipi=1,npi
call kget_track_param(list_pi(ipi), w_pi)
mass_d0 = kutl_mass2(w_k, w_pi)

c     Find the vertex of the K-pi pair. Do not update the tracks at this time.
update = 0
list_d0(1) = list_K(iK)
list_d0(2) = list_pi(ipi)
lvtx = .FALSE.
call kvtx_unknown(2, list_d0, update, lvtx, vtx, Vvtx, chisq, error)

c     If the chisq is OK, update the input tracks. The update will cause the
c     track parameters to be adjusted in such a way as to make them pass through
c     the new vertex point (However, the (x, y, z) location of the tracks
c     will not necessarily be at the vertex point. To make that happen, you
c     must call ktrk_move_point_bend to move the tracks and covariance matrices
c     to the new vertex point). The covariance matrices of the tracks are not
c     changed.
if(chisq .lt. 10.) call kfit_update_tracks(1)

c     Alternatively, you can build a D0 particle with a vertex constraint
c     (to a previously unknown position). The chisq_d0 is from the vertex
c     constraint. The D0 track parameters and covariance matrix are evaluated
c     at the vertex point.
option_d0(1) = 2
option_d0(2) = 2
update = 0
lvtx = .FALSE.

c     First create a new track entry then fill it with the virtual particle
call kfil_track_create(kd0)
call kvir_vertex_unknown(2, list_d0, option_d0, update, lvtx, vtx,
*                             chisq_d0, kd0, error)

c     Check the Kpi mass. If OK, apply a mass constraint forcing the D0 to
c     have the correct mass. The idea is to improve the D0 track parameters
c     so that the D0 can be used in subsequent fits.
mass_d0 = kget_track_mass(kd0)
if(abs(mass_d0 - 1.8654) .lt. 0.010) then
update = 2
call kfit_mass(kd0, update, 1.865, chisq_mass, error)
endif
enddo
enddo

return
end```