*
* $Id: thelix.F,v 1.1 1999/04/07 14:14:15 poulard Exp $
*
* $Name: offline-00-00-31 $
*
#include <commons/config.h>
*
        SUBROUTINE THELIX (IG,P1,P2,F,DF,R,DR,BF,KINE,IPR)
*                                                                      *
*----------------------------------------------------------------------*
*                                                                      *
* The main program for pattern recognition in Atlas inner tracker      *
* for track with Pt above .5 GeV/c                                     *
* Program start from track candidates found in straw tracker TRT and   *
* by using Kalman filter procedure propogate last one to precision     *
* trackers (SIT,SITV,GAAS,MSGC,PIXB,PIXE)                              *
*                                                                      *
* Input parameters : IG     - geometry of the tracker                  *
*                    P1     - min. transferse momentum of the track    *
*                             for preselection                         *
*                    P2     - min. transferse momentum of the track    *
*                             for track bank                           *
*                    F      - mean azimuthal angle                     *
*                    DF     - half width of the azimuthal region       *
*                    R      - mean rapidity                            *
*                    DR     - half width of the rapidity region        *
*                    BF     - condition for brem. fit                  *
*                    BF     = 0  no brem fit                           *
*                    BF     = 1  brem fit only for e-tracks            *
*                    BF     = 2  brem fit for all tracks               *
*                    KINE   = 0  reconstruct all track without         *
*                                KINE information                      *
*                    KINE   = 1  reconstruct all track by using        *
*                                KINE information                      *
*                    KINE   < 0  reconstruct only track with # KINE    *
*                                with KINE information                 *
*                    KINE   = 100001                                   *
*                                recostruct all track by using         *
*                                KINE information and take into account*
*                                only hits with the same KINE number   *
*                    KINE   =-100000-N                                 *
*                                reconstruct only track with KINE = N  *
*                                and select hit belong only given track*
*                    IPR    - condition for print                      *
* Output information in common /CHELEX/                                *
*                                                                      *
*   NHEB       - total number found tracks                             *
*---------------------                                                 *
*   HEB  (K,N) - K=1,23,N=1,NHEB output helixes                        *
*                parameters in point closest to vertex with last hit   *
*   HEB  (1,N) - X                                                     *
*   HEB  (2,N) - Y                                                     *
*   HEB  (3,N) - Z                                                     *
*   HEB  (4,N) - F azimuthal angle of the track dir.                   *
*   HEB  (5,N) - T ratio Pl/Pt                                         *
*   HEB  (6,N) - C curvature*charge                                    *
*   HEB  (7,N) - Azimuthal angle system coordinates                    *
*   HEB  (8,N) = 1 if X is a parameter (barrel counter)                *
*              = 3 if Z is a parameter (endcap counter)                *
* HEB (9-23,N) - Elements of the low triangle of covariance matrix     *
*                                                                      *
*  if X is a parameter    if Z is a parameter                          *
*                                                                      *
*       Y  Z  F  T  C          X  Y  F  T  C                           *
*   Y   9 10 12 15 19      X   9 10 12 15 19                           *
*   Z  10 11 13 16 20      Y  10 11 13 16 20                           *
*   F  12 13 14 17 21      F  12 13 14 17 21                           *
*   T  15 16 17 18 22      T  15 16 17 18 22                           *
*   C  19 20 21 22 23      C  19 20 21 22 23                           *
*---------------------                                                 *
*   HEBO (K,N) - K=1,23,N=1,NHEB output helixes                        *
*                parameters in outer radii with last hit               *
*---------------------                                                 *
*   HEBI ( K,N) - K=1,9 addition helix information                     *
*   HEBI ( 1,N) = 1 if Mu fit (without brem recovery)                  *
*               = 2 if E  fit (with    brem recovery)                  *
*   HEBI ( 2,N) - Number digits from precision counters                *
*   HEBI ( 3,N) - Kine track number produced most number hits          *
*   HEBI ( 4,N) - Number hits produced by kine track                   *
*                 with number HEBI(3,N)                                *
*   HEBI ( 5,N) - Total Xi2 of the fit in precision counters           *
*   HEBI ( 6,N) - Number TRT straw-hit  on the track direction         *
*   HEBI ( 7,N) - Number empty TRT straws                              *
*   HEBI ( 8,N) - Number TIME hits from TRT                            *
*   HEBI ( 9,N) - Number TRD hits ( with energy above 5 KeV)           *
*   HEBI (10,N) - Number pixel   hits with KINE = HEBI(3,N)            *
*   HEBI (11,N) - Number pixel   hits with KINE = 0                    *
*   HEBI (12,N) - Number pixel   hits with another KINE                *
*   HEBI (13,N) - Number silicon hits with KINE = HEBI(3,N)            *
*   HEBI (14,N) - Number silicon hits with KINE = 0                    *
*   HEBI (15,N) - Number silicon hits with another KINE                *
*  IHEBI (16,N) = 000  0000  0000  000000000000                        *
*     Detector    PIXB PIXE  SCTT     ZSCT                             *
*     Layer       123  1234  1234  123456789....                       *
*   HEBI (17,N) = 1 if TRT information take into account for the fit   *
*   HEBI (18,N) - total number of common hits with other tracks        *
*   HEBI (19,N) - total number of common hits with other tracks in     *
*                 pixels                                               *
*   HEBI (20,N) - Xi2/NDF of fit procedure through TRT                 *
*   HEBI (21,N) - Number precision layers with ambiguous clusters      *
*   HEBI (22,N) - Xi2/NDF of the fit in precision counters             *
*                 with Gauss distribution                              *
*   HEBI (23,N) - number straw-hits produced by kine track HEBI(3,N)   *
*   HEBI (24,N) - number straw-hits with kine = 0                      *
*   HEBI (25,N) - Number B-layer pixel hits with KINE = HEBI(3,N)      *
*   HEBI (26,N) - Number B-layer pixel hits with KINE = 0              *
*   HEBI (27,N) - Number B-layer pixel hits with another KINE          *
*   HEBI (28,N) - total number common hits with other tracks for B-lay *
*---------------------                                                 *
*   HEBA (2,K,N) - K=1,HEBI(2,N) precision digits address              *
*   HEBA (1,K,N) - detector ID*100+Layer number                        *
*   HEBA (2,K,N) - Digit address in ZEBRA                              *
*---------------------                                                 *
* For helix parameters convertion to perigee presentation USERS can    *
* use program THEPERIG and for (R,Fr,Z,B,T,C)-presentation can         *
* use program THEOUTPR.                                                *
* Author : I.Gavrilenko                                                *
*----------------------------------------------------------------------*
*                                                                      *
#include "commons/typing.inc"
#include "atrecon/xcdes/ncmltc.inc"
#include "atrecon/xcdes/checond.inc"
#include "atrecon/xcdes/comtime.inc"
*
        REAL    P1,P2,F,DF,R,DR,BF,T1,T2
        INTEGER IPR(10),KINE,IER,IH,IBREM,IG
        REAL    timing
*
* Conditions initialization
*
        CALL UZERO   (COND,1,100)
        COND (1)   = IG             ! Geometry number
        COND (2)   = ABS(P1)        ! Min. transferse momentum for selection
        COND (3)   = F              ! Mean azimuthal angle
        COND (4)   = ABS(DF)        ! Half width of azimuthal angle
        T1         = 1./TAN(2.*ATAN(EXP(-AMAX1(-10.,AMIN1(10.,R+DR)))))
        T2         = 1./TAN(2.*ATAN(EXP(-AMAX1(-10.,AMIN1(10.,R-DR)))))
        COND (5)   = AMIN1 (T1,T2)  ! Min Pl/Pt of the search
        COND (6)   = AMAX1 (T1,T2)  ! Max Pl/Pt of the search
        COND (7)   = BF             ! Condition for brem. fit
        COND (8)   = 0.             ! 0-usial,1-brem. fit
        COND (9)   = ABS(P2)        ! Min. transferse momentum for output
        COND (31)  = KINE           ! Use kine information for pattern
        CALL UZERO   (CERR,1,100  ) ! Errors buffer initialization
        CALL THEFILP (   0      )   ! Output track bank initialization
        CALL THECONS                ! Constant bank initialization
        CALL THECORR                ! Table of slop corrections preparation
*
        Call TIMED(timing)
        CALL THEDIGT                ! Read digits from TRT
        Call TIMED(timing)
        readTRT = readTRT + timing
*
        CALL THERRO  (   IER    )   ! Test errors list
        IF ( IER.NE.0    ) RETURN   ! Prolong reconstruction if no errors
*
* The main cycle of the pattern recognition start from TRT
*
10      CONTINUE
*
        Call TIMED(timing)
        CALL THESTRO (   IH     )   ! Primary track direction search in TRT
        Call TIMED(timing)
        roadTRT = roadTRT + timing
*
        IF( IH.EQ.0      ) GO TO 30 ! No more track candidates
*
        Call TIMED(timing)
        CALL THEDIGS ( IPR(1)   )   ! Read digits from precision detectors
*                                   ! by using initial helix
        Call TIMED(timing)
        readSCT = readSCT + timing
*
        CALL THEDIGC ( IPR(2)   )   ! Sort counters in propogation order
        Call TIMED(timing)
        sort = sort + timing
*
20      Call TIMED(timing)
        CALL THEFLSP ( IPR(3)   )   ! Track propogation through
                                    ! precision detectors
        Call TIMED(timing)
        if ( ibrem.eq.0 ) then
          trackSCT = trackSCT + timing
        else
          bremSCT  = bremSCT + timing
        endif
*
        CALL THEFILP (  1       )   ! Fill output track bank without TRT
*
        Call TIMED(timing)
        CALL THEFLST ( IPR(4)   )   ! Track propogation through TRT
        Call TIMED(timing)
        if ( ibrem.eq.0 ) then
          trackTRT = trackTRT + timing
        else
          bremTRT  = bremTRT + timing
        endif
*
        CALL THEFILT                ! Fill output track bank with TRT
*
        CALL THEBRE  ( IBREM    )   ! Brem. fit possibility investigation
*
        IF ( IBREM.NE.0  ) GO TO 20 ! Repeat fit with brem. conditions
                           GO TO 10 ! Go to next track candidate
30      CALL THERRO  (   IER    )   ! Test errors list
*
        Call TIMED(timing)
        CALL THELOOK                ! Tracks comparison
        Call TIMED(timing)
        compare = compare + timing
*
        Call TIMED(timing)
        CALL THEREPEAT              ! Repeat track search
        Call TIMED(timing)
        repeat = repeat + timing
*
        IF ( IPR(5).EQ.0 ) RETURN   ! Condition for print
        CALL THEBPRI (  IPR(5)  )   ! Print output track BANK
        RETURN
        END


