Module assigntowindow_mod

module assigntowindow_mod

        ! Uses
    use Precision_mod
    use Namelist_mod
    use Chambers_mod
    use Det_Geom_mod
    use Tdc_mod
    use Unp_mod
    use Hists_mod
    use Pattern_mod
    use Cluster_mod
    use Pattern_Log_mod
    use Xtalk_mod, ONLY: DC_IsXtalk, PC_IsXtalk
    use windowstat_mod
    use evalwin_mod
    use windex_mod
    use windowcalc_mod

        ! Subroutines and functions
    public subroutine WindowClearBJ (first_win, last_win)
    private subroutine GetWindowPlaneLimits (iwindow, minpc, maxpc, mindc, maxdc)
    private subroutine GetDeltaCuts (iwindow, deltau, deltav, deltasig, removedelta)
    private subroutine AssignHitsToWindow (iwindow)
    private subroutine GetFirstHitOnWire (itdc, inextwire, tdcP, tstart, tend, iwire, iplane)
    private subroutine AssignHitsToJimsWindow (iwindow)
    private subroutine SetupEmptyOddWindow (ipcwin)
    private subroutine SetupEvenWindow (ipcwin)
    private subroutine SetupEmptyLastWindow ()
    public subroutine AssignToWindows ()
    private subroutine CheckOverlapForTrigger ()
    private subroutine ExtractBeameOverlap ()
    private subroutine ExtractBeameToWindow (iwindow, ifit)

end module assigntowindow_mod
==============================================================================
 Date: December, 2003
 Name: assigntowindow_mod
------------------------------------------------------------------------------
 Description:

 This module contains routines used to sort hits into various windows.  These
 routines used to be in windowbj_mod.
==============================================================================

Author: Blair JAmieson

Version: 1.1


Description of Subroutines and Functions

WindowClearBJ

public subroutine WindowClearBJ (first_win, last_win)
    integer (kind=i4), INTENT(in) :: first_win
    integer (kind=i4), INTENT(in) :: last_win
    ! Calls: ClearCluster
end subroutine WindowClearBJ
 Subroutine WindowClearBJ( first_win, last_win )
============================================================================
Author: Art Olin
Description: Just clear the counters and the window parameters 
but not the hit and wire numbers.
Modified to also clear clusters-JRM June 21, 2000
Modified to clear new components and now used - ML July 2002.
Added clearing of nDeltas - RPM August 2003
============================================================================

GetWindowPlaneLimits

private subroutine GetWindowPlaneLimits (iwindow, minpc, maxpc, mindc, maxdc)
    integer (kind=i4), INTENT(in) :: iwindow
    integer (kind=i4), INTENT(out) :: minpc
    integer (kind=i4), INTENT(out) :: maxpc
    integer (kind=i4), INTENT(out) :: mindc
    integer (kind=i4), INTENT(out) :: maxdc
    ! Calls: GlobalPlaneMinMaxToCH
end subroutine GetWindowPlaneLimits
 SUBROUTINE GetWindowPlaneLimits
------------------------------------------------------------------
 Author: Blair
 Date: Jan. 2003
 Inputs: iwindow - the window you want to know limits on
 Outputs: minpc, maxpc, mindc, maxdc - the plane limits
 Description:
 This subroutine converts the global plane limits in the window
 structure into pc, and dc plane limits.

GetDeltaCuts

private subroutine GetDeltaCuts (iwindow, deltau, deltav, deltasig, removedelta)
    integer (kind=i4), intent(in) :: iwindow
    real (kind=r4), intent(out) :: deltau
    real (kind=r4), intent(out) :: deltav
    real (kind=r4), intent(out) :: deltasig
    logical, intent(out) :: removedelta
end subroutine GetDeltaCuts

AssignHitsToWindow

private subroutine AssignHitsToWindow (iwindow)
    integer (kind=i4), INTENT(in) :: iwindow
    ! Calls: GetDeltaCuts, GetFirstHitOnWire, GetWindowPlaneLimits
end subroutine AssignHitsToWindow
 SUBROUTINE AssignHitsToWindow( iwindow )
------------------------------------------------------------------
 Author: Blair
 Date: Jan. 2003
 Inputs: iwindow - the window you want to add hits to
 Description:
 This subroutine loops through all the PC and DC hits and adds 
 ones withing the window time limits to the window.  It also checks
 plane limits -- if the hits are outside the window plane limits, 
 but within the window times, then the hits are put into 
 iwindow-1.

 Modifications:
  Mar. 2003 - Try to extract deltas into previous window

GetFirstHitOnWire

private subroutine GetFirstHitOnWire (itdc, inextwire, tdcP, tstart, tend, iwire, iplane)
    integer (kind=i4), INTENT(INOUT) :: itdc
    integer (kind=i4), INTENT(OUT) :: inextwire
    type (tdc_TYPE), POINTER, dimension (:) :: tdcP
    real (kind=r4), INTENT(IN) :: tstart
    real (kind=r4), INTENT(IN) :: tend
    integer (kind=i4), INTENT(IN) :: iwire
    integer (kind=i4), INTENT(IN) :: iplane
end subroutine GetFirstHitOnWire
SUBROUTINE GetFirstHitOnWire( itdc, inextwire, tdcP, tstart, tend, iwire, plane )
--------------------------------------------------------------------
 Search next hits (counting down backwards) after index itdc in tdc structure for
 earliest hit on wire iwire of plane iplane.  The hit must fall within
 the time window [tstart:tend].  The index to the earliest hit time is
 returned in itdc.  The index to the first hit in the structre for the next wire
 is also returned. 

AssignHitsToJimsWindow

private subroutine AssignHitsToJimsWindow (iwindow)
    integer (kind=i4), INTENT(in) :: iwindow
end subroutine AssignHitsToJimsWindow
 SUBROUTINE AssignHitsToJimsWindow( iwindow )
------------------------------------------------------------------
 Author: Blair
 Date: May. 2003
 Inputs: iwindow - the window you want to add hits to
 Description:
 This subroutine loops through all the PC and DC hits and adds 
 ones withing the window time limits to the window.... unless the
 previous even window was within DCOverlapTime... then it puts
 the hits from the end of the previous window to the end of
 DCOverlapTime into the previous odd window.  It doesnt check
 plane limits -- if the hits are outside the window plane limits, 
 but within the window times, then too bad so sad they still
 end up in this window.

SetupEmptyOddWindow

private subroutine SetupEmptyOddWindow (ipcwin)
    integer (kind=i4), intent(in) :: ipcwin
    ! Calls: WindowClearBJ
end subroutine SetupEmptyOddWindow
 SUBROUTINE SetupEmptyOddWindow( ipcwin )
------------------------------------------------------------------
 Author: Blair
 Date: Jan. 2003
 Inputs: ipcwin - the next track that will come after this empty
                  window
 Description:
 This subroutine sets window times and resets counters for the
 empty window that will come before track ipcwin.

SetupEvenWindow

private subroutine SetupEvenWindow (ipcwin)
    integer (kind=i4), INTENT(in) :: ipcwin
    ! Calls: WindowClearBJ
end subroutine SetupEvenWindow
 SUBROUTINE SetupEvenWindow( ipcwin )
------------------------------------------------------------------
 Author: Blair
 Date: Jan. 2003
 Inputs: ipcwin - the track number that will be put in this window
 Description:
 This subroutine sets window times and sets counters for a track
 window.

SetupEmptyLastWindow

private subroutine SetupEmptyLastWindow ()
    ! Calls: WindowClearBJ
end subroutine SetupEmptyLastWindow
 SUBROUTINE SetupEmptyLastWindow
------------------------------------------------------------------
 Author: Blair
 Date: Jan. 2003
 Description:
 This subroutine sets up an empty last window -- keeps the rest
 of the code later on happier.
  

AssignToWindows

public subroutine AssignToWindows ()
    ! Calls: AllEventWindowTypes, AssignHitsToJimsWindow, AssignHitsToWindow, CheckOverlapForTrigger, EvalWindowType, ExtractBeameOverlap, PrintWindowTypes, SetupEmptyLastWindow, SetupEmptyOddWindow, SetupEvenWindow
end subroutine AssignToWindows
 SUBROUTINE AssignToWindows
------------------------------------------------------------------
 Author: Blair
 Date: Jan. 2003
 Description:
 This is the main steering routine for setting up windows,
 evaluating window types, and assigning hits to windows.
  

CheckOverlapForTrigger

private subroutine CheckOverlapForTrigger ()
end subroutine CheckOverlapForTrigger

ExtractBeameOverlap

private subroutine ExtractBeameOverlap ()
    ! Calls: AssignHitsToWindow, DoClusteringWin, ExtractBeameToWindow, FirstGuessKOwindow, SetupEmptyOddWindow, SetupEvenWindow, helixfitFG
end subroutine ExtractBeameOverlap
 SUBROUTINE ExtractBeameOverlap
------------------------------------------------------------------
 Author: Blair
 Date: Mar. 2003
 Description:
 This subroutine fits one side of a beam positron
 or delta, and tries to remove hits from the other side

ExtractBeameToWindow

private subroutine ExtractBeameToWindow (iwindow, ifit)
    integer (kind=i4), INTENT(in) :: iwindow
    integer (kind=i4), INTENT(in) :: ifit
    ! Calls: GetWindowPlaneLimits
end subroutine ExtractBeameToWindow
 SUBROUTINE ExtractBeameToWindow( iwindow, ifit )
------------------------------------------------------------------
 Author: Blair
 Date: Mar. 2003
 Inputs: iwindow - the window you want to add hits to
         ifit - index into helixfit structure for fit to beam e+
 Description:
 This subroutine loops through all the PC and DC hits and adds 
 ones withing the window time limits to the window.  It also checks
 plane limits -- if the hits are outside the window plane limits, 
 but within the window times, then the hits are put into 
 iwindow-1.  Also check if the hits are near where fit to
 beam positron is -- try to put beam positron hits in iwindow-1.

 Note: This should only be called for even windows!