SUBROUTINE FTVDET
*-- Author :    I.O.Skillicorn
      SUBROUTINE FTVDET
C     DRIFT VELOCITY DETERMINATION PER TRACK FOR RADIALS                                                      
*KEEP,FRDIMS.                                                                                                 
      PARAMETER (MAXHTS=200)                                            
      PARAMETER (NUMWPL=36)                                             
      PARAMETER (MAXTRK=200)                                            
      PARAMETER (MXTTRK=900)                                            
      PARAMETER (MAXTR3=200)                                            
      PARAMETER (MAXHPW=2)                                              
      PARAMETER (MAXDIG=2000)                                           
      PARAMETER (NUMRWR=1727)                                           
      PARAMETER (NUMPWR=1151)                                           
*KEEP,FH1WORK.                                                                                                
       COMMON/FGMIOS/                                                   
*    Planar geometry                                                                                          
     + ZPP(140),C(140),S(140),ITYPE(140),WZERO(140),WSPACE,             
*                                                                                                             
*    Radial geometry                                                                                          
     + ZP(36),PHW(36),WS(36)                                            
*                                                                                                             
       COMMON/H1WORK/                                                   
*    Radial data...                                                                                           
     + WW(MAXHTS,36),DRI(MAXHTS,36),RM(MAXHTS,36),                      
     + NDP(36),  NW(MAXHTS,36), DWS(MAXHTS,36),                         
*                                                                                                             
*    Planar Data                                                                                              
     + NDPW(NUMWPL),DW(MAXHTS,NUMWPL),                                  
     + DRIW(MAXHTS,NUMWPL),NDW(MAXHTS,NUMWPL),                          
     + WWP(MAXHTS,NUMWPL),                                              
     + IPHOLE(MAXHTS,NUMWPL),                                           
*                                                                                                             
*    Pointers into DIGI bank for IOS labelled hits                                                            
     +  IPFRRE(MAXHTS,36),IPFRPE(MAXHTS,36),NFRRE,NFRPE,                
     +  IRPIOS(MAXDIG,2), IPPIOS(MAXDIG,2),                             
*                                                                                                             
*    Track segment data                                                                                       
     + NTRAKS(3),IRPT(12,MAXTRK,3),SDRFT(12,MAXTRK,3),                  
*                                                                                                             
*    Fit data                                                                                                 
     + PCOSL(MAXTRK,3),PSINL(MAXTRK,3),PHZL(MAXTRK,3),                  
     + DPCOSL(MAXTRK,3),DPSINL(MAXTRK,3),                               
     + DPHZL(MAXTRK,3),CHSQ(MAXTRK,3),RZI(MAXTRK,3),                    
     + RPCOSG(MAXTRK),RPSING(MAXTRK),                                   
     + PHZG(MAXTRK),CC(3,MAXTRK),ZIG(MAXTRK),                           
     + IRADG(36,MAXTRK),PHIG(36,MAXTRK),                                
     + IG,SDRADG(36,MAXTRK),                                            
     + R1,Z1,RFIT(MAXTRK,3),                                            
     + CHG(MAXTRK),                                                     
     + PPA(MAXTRK,3),  ZZA(MAXTRK,3),                                   
     + GPA(MAXTRK,3),GZA(MAXTRK,3)                                      
*                                                                                                             
*                                                                                                             
*KEEP,FPTVTX.                                                                                                 
      COMMON/VERTVV/ZV ,XVV,YVV                                         
**the common/VERTEX/ becomes /VERTVV/ (in analogy to /VERTFF/) on the                                         
** 17/6/91, since it is in conflict with the VERTEX module (g.bernardi)                                       
** (note that all these common names should start by F in this deck...)                                       
*KEEP,FRH3FT.                                                                                                 
*     Common for RETRAC results (SJM)                                                                         
      COMMON/FRH3FT/ IRN(36,MAXTRK),SDN(36,MAXTRK),                     
     +              IRP(36,MAXTRK),SDP(36,MAXTRK),                      
     +              IG2,IGTTRK(MAXTRK),                                 
     +              CHISQ(MAXTRK),NUMDF(MAXTRK),                        
     +              FITX(MAXTRK),FITY(MAXTRK),FITZ(MAXTRK),             
     +              FITTH(MAXTRK),FITPH(MAXTRK),                        
     +              FITCU(MAXTRK),FTCOV(15,MAXTRK)                      
*KEEP,FPPRAM.                                                                                                 
C                                                                                                             
C---  MAXSEG is maximum number of segments per supermodule                                                    
C---  MAXCON is maximum number of amibiguous segments associatable with                                       
C---         one segment                                                                                      
C---  LIMSTO is maximum number of 2 cluster planes intersections to be                                        
C---         stored per supermodule                                                                           
C---  MSEGLM is maximum number of clusters that can be found before                                           
C---         connectivity considered                                                                          
C---  MAXCLU is maximum number of clusters that can be found after                                            
C---         forming non-connected set    MUST BE 50 IF RUN WITH OLD RCW                                      
C---         (cluster = 3/4 digits found in a straight line in one                                            
C---          4-wire orientation)                                                                             
C                                                                                                             
      PARAMETER (MAXSEG = 200)                                          
      PARAMETER (MAXCON = 100)                                          
      PARAMETER (LIMSTO = 5000)                                         
      PARAMETER (MSEGLM = 150)                                          
      PARAMETER (MAXCLU = 50)                                           
C---                                                                                                          
*KEEP,FPLSEG.                                                                                                 
C---                                                                                                          
      COMMON /FPLSEG / PW(12,MAXSEG,3)   , PWC(12,MAXSEG,3)     ,       
     1                 PRCHI(MAXSEG,3)   , NFSEG(3)             ,       
     2                 XYDXY(4,MAXSEG,3) , EXYDXY(4,4,MAXSEG,3) ,       
     3                 ZSEG(2,MAXSEG,3)  ,                              
     4                 ASEGIN(MAXSEG,3)  , ISEGIN(5,MAXSEG,3)   ,       
     5                 MASKSG(MAXSEG,3)  , IDGISG(12,MAXSEG,3)          
C---                                                                                                          
*KEEP,FPTFLG.                                                                                                 
      COMMON/FPTFLG/IPLAR, NIT, IREZ, LUNFP, NPLMAX                     
*KEEP,FPTPAR.                                                                                                 
      COMMON/FPTPAR/PCUT1, PCUT2, PCUT3, CXP, CYP,                      
     +              DRPCT1, DRPCT2, DRPCT3,                             
     +              DRCUT1, DRCUT2, DRCUT3                              
*KEEP,FDIFLG.                                                                                                 
      COMMON/LDIAG/IDIAG,IDIAG2,MAXPRT,IDOHIS,PMCUT                     
*KEND.                                                                                                        
                                                                        
*     Common for track parameter errors...                                                                    
      COMMON /FTRERR/ DSLPHI, DPHZER, COVP, DSLPR, DRZER, COVR          
                                                                        
                                                                        
*     Common for segment numbers...                                                                           
      COMMON /FPSEGN/ ISG(3,MAXTRK)                                     
                                                                        
*     COMMON FOR PLANAR PATREC  ...                                                                           
      COMMON/FTPPBK/NPP,IPP(36,100),CHPP(100),LP(3,100)                 
      COMMON/FPPFIT/PSSS(100),PISS(100),RSSS(100),RISS(100)             
C     POINTER TO RADIAL ASSOCIATED WITH NPP'TH PLANAR                                                         
      COMMON/FPPTR/LR(3,100)                                            
      COMMON/FPLNK/KTIP(3,50),LPP(3,100)                                
C     COMMON FOR RADIALS ASSOCIATED WITH PLANAR TRACKS                                                        
      COMMON/FTRRBK/IRR(36,MAXTRK),SRR(36,MAXTRK),LRR(3,MAXTRK)         
*     Local arrays...                                                                                         
      DIMENSION IUSEDP(MAXTRK,36), IUSEG( MAXSEG, 3)                    
      DIMENSION RSEG(4),PSEG(4)                                         
      PARAMETER(PI2=6.2831853)                                          
      DO 100 K=1,NPP                                                    
C     CHECK WHICH PLANAR MODULES ARE FOUND                                                                    
      IP1=0                                                             
      IP2=0                                                             
      IP3=0                                                             
      DO 70 IP=1,36                                                     
      IF(IP.GT.01.AND.IP.LE.12.AND.IPP(IP,K).NE.0)IP1=1                 
      IF(IP.GT.12.AND.IP.LE.24.AND.IPP(IP,K).NE.0)IP2=1                 
      IF(IP.GT.24.AND.IP.LE.36.AND.IPP(IP,K).NE.0)IP3=1                 
 70   CONTINUE                                                          
      SME=0.                                                            
      SEE=0.                                                            
      SSS=0.                                                            
      STZ=0.                                                            
      DO 50 ISM=1,3                                                     
      IP=LRR(ISM,K)                                                     
      IF(IP.EQ.0)GOTO50                                                 
      DO 22 IPL=1,12                                                    
C     CHECK RADIAL BETWEEN PLANAR SEGMENTS                                                                    
      IF(ISM.EQ.1.AND.(IP1.EQ.0.OR.IP2.EQ.0))GOTO22                     
      IF(ISM.EQ.2.AND.(IP2.EQ.0.OR.IP3.EQ.0))GOTO22                     
      JPL=IPL+(ISM-1)*12                                                
      NP=IRPT(IPL,IP,ISM)                                               
      IF(NP.NE.0)THEN                                                   
      RR=RSSS(K)*ZP(JPL)+RISS(K)                                        
      PHI=PSSS(K)*ZP(JPL)+PISS(K)                                       
      IF(PHI.LT.0.0)PHI=PHI+PI2                                         
      DRE=RR*SIN(PHI-WW(NP,JPL))                                        
      IF (DRE.NE.0.) THEN                                               
         SGN=DRE/ABS(DRE)                                               
      ELSE                                                              
         SGN = 1.                                                       
      ENDIF                                                             
      DRMM=SDRFT(IPL,IP,ISM)*DRI(NP,JPL)+DWS(NP,JPL)                    
      IF(ABS(DRE-DRMM).LT.0.5)THEN                                      
        IF(IP1.EQ.1.AND.IP2.EQ.1.AND.ISM.EQ.1)THEN                      
C       CALL SHS(704+ISM,0.,DRE-DRMM)                                                                         
        SEE=SEE+DRE*DRE                                                 
        SME=SME+DRMM*DRE                                                
        STZ=STZ+(DRMM-DRE)*SGN                                          
        SSS=SSS+1.0                                                     
        ENDIF                                                           
        IF(IP2.EQ.1.AND.IP3.EQ.1.AND.ISM.EQ.2)THEN                      
C       CALL SHS(704+ISM,0.,DRE-DRMM)                                                                         
        SEE=SEE+DRE*DRE                                                 
        SME=SME+DRMM*DRE                                                
        STZ=STZ+(DRMM-DRE)*SGN                                          
        SSS=SSS+1.0                                                     
        ENDIF                                                           
C       IF(IP2.EQ.1.AND.IP3.EQ.1.AND.ISM.EQ.3)THEN                                                            
C       CALL SHS(704+ISM,0.,DRE-DRMM)                                                                         
C       SEE=DRE*DRE                                                                                           
C       SME=DRMM*DRE                                                                                          
C       SSS=SSS+1.0                                                                                           
C       ENDIF                                                                                                 
      ENDIF                                                             
      ENDIF                                                             
 22   CONTINUE                                                          
 50   CONTINUE                                                          
      IF(SSS.GT.4)THEN                                                  
C     CALCULATE VELOCITY CORRECTION                                                                           
      VFAC=SME/SEE                                                      
      CALL SHS(761,0,VFAC)                                                                             
      TZ=STZ/SSS                                                        
      CALL SHS(762,0,TZ  )                                                                             
      ENDIF                                                             
 100  CONTINUE                                                          
      RETURN                                                            
      END                                                               
*