SUBROUTINE FREFIT
*-- Author :    I.O. Skillicorn   27/09/96
      SUBROUTINE FREFIT
*     REFIT TRACK MODEL INCLUDING PLANARS                                                                     
*     this routine updates the radial-based track parameters                                                  
*     using planar data. both phi-z and r-z are updated.                                                      
*     planars alone define r-z                                                                                
*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 IOS PLANAR LINK                                                                              
      COMMON/FTPS3/NS(3),SPAR(4,50,3),IPT(12,50,3),IPLA(12,50,3),       
     1  SGN(12,50,3),YYS(12,50,3),YYF(12,50,3)                          
                                                                        
      COMMON/FTPPBK/NPP,IPP(36,100),CHPP(100),LP(3,100)                 
C     THE SIGN OF THE PLANAR DRIFT IS FILLED IN FTADD                                                         
      COMMON/FTPPBS/SPP(36,100)                                         
      COMMON/FPPFIT/PSSS(100),PISS(100),RSSS(100),RISS(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)         
C     PLANAR SEGMENTS ASSOCIATED  WITH RADIALS                                                                
      COMMON /FPSEG1/ ISGG(3,MAXTRK)                                    
      COMMON /FLINK3/LNK3(MAXTRK,3)                                     
                                                                        
*     Local arrays...                                                                                         
      DIMENSION XX(100),YY(100),WP(100)                                 
      PARAMETER(PI2=6.283185307)                                        
      if(ig.eq.0)return                                                 
      DO 100 I=1,ig                                                     
      iF((isgg(1,i)+isgg(2,i)+isgg(3,i)).ne.0)then                      
c     at least one planar: - fit r-z                                    
      IC=0                                                              
      DO 20 ISM=1,3                                                     
      IPLAN=isgg(ISM,I)                                                 
      I1=(ISM-1)*12+1                                                   
      I2=ISM*12                                                         
C     PLANARS                                                                                                 
      IF(IPLAN.NE.0)THEN                                                
      DO 21 IP=I1,I2                                                    
      Z1=ZPP(IP)                                                        
      XF=XYDXY(1,IPLAN,ISM)/10.+Z1*XYDXY(3,IPLAN,ISM)                   
      YF=XYDXY(2,IPLAN,ISM)/10.+Z1*XYDXY(4,IPLAN,ISM)                   
      IC=IC+1                                                           
      XX(IC)=Z1                                                         
      YY(IC)=sqrt(xf**2+yf**2)                                          
      WP(IC)=1.                                                         
 21   CONTINUE                                                          
      ENDIF                                                             
 20   continue                                                          
c     fit r-z defined by planars                                        
      CALL FTLFTW(XX,YY,WP,IC,0,2,RS,RI,D1,D2,D3,D4)
c     replace radial-defined r-z by planar-defined r-z                  
      rpsing(i)=rs                                                      
      zig(i)=ri                                                         
      endif                                                             
      IC=0                                                              
      DO 200 ISM=1,3                                                    
      IPLAN=isgg(ISM,I)                                                 
      I1=(ISM-1)*12+1                                                   
      I2=ISM*12                                                         
C     PLANARS                                                                                                 
      IF(IPLAN.NE.0)THEN                                                
      DO 210 IP=I1,I2                                                   
      Z1=ZPP(IP)                                                        
      XF=XYDXY(1,IPLAN,ISM)/10.+Z1*XYDXY(3,IPLAN,ISM)                   
      YF=XYDXY(2,IPLAN,ISM)/10.+Z1*XYDXY(4,IPLAN,ISM)                   
      IC=IC+1                                                           
      XX(IC)=Z1                                                         
      YY(IC)=ATAN2(YF,XF)                                               
      IF(YY(IC).LT.0.0)YY(IC)=YY(IC)+PI2                                
      WP(IC)=1./(0.1/SQRT(XF**2+YF**2))                                 
 210  CONTINUE                                                          
      ENDIF                                                             
C     RADIALS                                                                                                 
      DO 220 IP=I1,I2                                                   
      Z1=ZP(IP)                                                         
C     RADIUS DEFINED BY  radials                                                                              
      RR=rpsing(I)*z1+zig(I)                                            
      J=irn(IP,I)                                                       
      IF(J.EQ.0)GOTO220                                                 
      IC=IC+1                                                           
      PHI=ATAN((DRI(J,IP)*sdn(IP,I)+DWS(J,IP))/RR) +WW(J,IP)            
      IF(PHI.LT.0.0)PHI=PHI+PI2                                         
      XX(IC)=Z1                                                         
      YY(IC)=PHI                                                        
      WP(IC)=1./(0.1/RR)                                                
 220  CONTINUE                                                          
 200  CONTINUE                                                          
      IF(IC.GT.1)THEN                                                   
      DO 250 JJ=2,IC                                                    
      DP=YY(JJ)-YY(JJ-1)                                                
      IF(DP.GT.0.0)THEN                                                 
      IF(ABS(DP).GT.ABS(DP-PI2))YY(JJ)=YY(JJ)-PI2                       
      ELSE                                                              
      IF(ABS(DP).GT.ABS(DP+PI2))YY(JJ)=YY(JJ)+PI2                       
      ENDIF                                                             
 250  CONTINUE                                                          
      ENDIF                                                             
C     REFIT PHI-Z INCLUDING planars                                                                           
C     WRITE(*,*)I,phzg(I),rpcosg(I)                                                                           
      CALL FTLFTW(XX,YY,WP,IC,0,2,PS,PI,D1,D2,D3,D4)
      phzg(I)=PI                                                        
      rpcosg(I)=PS                                                      
C     WRITE(*,*)I,phzg(I),rpcosg(I)                                                                           
 100  CONTINUE                                                          
      RETURN                                                            
      END                                                               
                                                                        
*