SUBROUTINE FTLINK
*-- Author : I.O. Skillicorn
      SUBROUTINE FTLINK
C     ORGANISE LINKING OF RADIAL MODULES                                                                      
C     AUTHOR   I.O.SKILLICORN                                                                                 
C     21/5/91  REDUCE SIZE                                                                                    
*   SUNDRY ARRAY DIMENSIONS...                                                                                
*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)                                      
*                                                                                                             
*                                                                                                             
*KEND.                                                                                                        
*   VERTEX INFO...                                                                                            
*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...)                                       
*KEND.                                                                                                        
*                                                                                                             
*                                                                                                             
*     LOCAL ARRAYS...                                                                                         
      COMMON /FLINK3/LNK3(MAXTRK,3)                                     
*************************************************************                                                 
C      COMMON/FTWSEG/NIG,NNPTS(MAXTRK),NIRADG(36,MAXTRK),                                                     
C     1SDRADN(36,MAXTRK)                                                                                      
*************************************************************                                                 
      DIMENSION ZI12(MAXTRK)                                            
      DIMENSION ZI13(MAXTRK)                                            
      DIMENSION ZI23(MAXTRK)                                            
      DIMENSION ZI123(MAXTR3)                                           
      DIMENSION TRI(MAXTRK),TPA(MAXTRK,3)                               
      DIMENSION TZA(MAXTRK,3),TCH(MAXTRK)                               
      DIMENSION CF(MAXTR3,3),IUSED(MAXTR3,3)                            
      DIMENSION INP(MAXTRK)                                             
      DIMENSION RPC(MAXTRK),PH(MAXTRK),RPS(MAXTRK)                      
      DIMENSION NPTS(MAXTRK)                                            
      DIMENSION LL12(MAXTRK,2),RPC12(MAXTRK),RPS12(MAXTRK)              
      DIMENSION PH12(MAXTRK),CH12(MAXTRK)                               
C     DIMENSION IP12(36,MAXTRK),SD12(36,MAXTRK)                                                               
      DIMENSION LL13(MAXTRK,2),RPC13(MAXTRK)                            
      DIMENSION RPS13(MAXTRK),PH13(MAXTRK),CH13(MAXTRK)                 
C     DIMENSION IP13(36,MAXTRK),SD13(36,MAXTRK)                                                               
      DIMENSION LL23(MAXTRK,2),RPC23(MAXTRK)                            
      DIMENSION RPS23(MAXTRK),PH23(MAXTRK),CH23(MAXTRK)                 
C     DIMENSION IP23(36,MAXTRK),SD23(36,MAXTRK)                                                               
      DIMENSION LL123(MAXTR3,3),RPC123(MAXTR3)                          
      DIMENSION RPS123(MAXTR3),PH123(MAXTR3)                            
      DIMENSION CH123(MAXTR3)                                           
                                                                        
      PI2=6.2831853                                                     
      IG=0                                                              
      N12=0                                                             
      N13=0                                                             
      N23=0                                                             
C     =1 FOR UNUSED SEGMENTS                                                                                  
      NUN=0                                                             
      DO 2010 I=1,MAXTR3                                                
      DO 2010 J=1,3                                                     
      IUSED(I,J)=0                                                      
 2010 CONTINUE                                                          
      DO 2103 J=1,MAXTRK                                                
      LNK3(J,1)=0                                                       
      LNK3(J,2)=0                                                       
      LNK3(J,3)=0                                                       
      GPA(J,1)=0.                                                       
      GZA(J,1)=0.                                                       
      NPTS(J)=0                                                         
C      NNPTS(J)=0                                                                                             
      DO 2102 I=1,36                                                    
      SDRADG(I,J)=0                                                     
      IRADG(I,J)=0                                                      
C      NIRADG(I,J)=0                                                                                          
 2102 CONTINUE                                                          
 2103 CONTINUE                                                          
                                                                        
                                                                        
C     JOIN  TRACK SEGMENTS                                                                                    
      CALL FTJN3(CF,IUSED,N123,LL123,RPC123,RPS123,PH123,
     1 CH123,            ZI123)                                         
                                                                        
      CALL FTJN23(CF,IUSED,N23,LL23,RPC23,RPS23,PH23,
     1 CH23,          ZI23)                                             
      CALL FTJN12(CF,IUSED,N12,LL12,RPC12,RPS12,PH12,
     1 CH12,          ZI12)                                             
      CALL FTJN13(CF,IUSED,N13,LL13,RPC13,RPS13,PH13,
     1 CH13,          ZI13)                                             
                                                                        
                                                                        
                                                                        
C     FILL BANK WITH TRACKS                                                                                   
      IG=0                                                              
      IF(N123.NE.0)THEN                                                 
      DO 100 I=1,N123                                                   
      IF(LL123(I,1).EQ.0)GOTO100                                        
      IG=IG+1                                                           
      IF(IG.GT.MAXTRK) IG=MAXTRK                                        
      RPCOSG(IG)=RPC123(I)                                              
      RPSING(IG)=RPS123(I)                                              
      PHZG(IG)=PH123(I)                                                 
      ZIG(IG)=ZI123(I)                                                  
      CHG(IG)=CH123(I)                                                  
      GZA(IG,1)=ZZA(I,1)                                                
      GPA(IG,1)=PPA(I,1)                                                
      GZA(IG,2)=ZZA(I,2)                                                
      GPA(IG,2)=PPA(I,2)                                                
      GZA(IG,3)=ZZA(I,3)                                                
      GPA(IG,3)=PPA(I,3)                                                
C     STORE LINK POINTERS                                                                                     
      LNK3(IG,1)=LL123(I,1)                                             
      LNK3(IG,2)=LL123(I,2)                                             
      LNK3(IG,3)=LL123(I,3)                                             
C      WRITE(*,*)' 123 ',IG,RPCOSG(IG),RPSING(IG)                                                             
C     DO105 N=1,36                                                                                            
C     IF(IP123(N,I).GT.0)NPTS(IG)=NPTS(IG)+1                                                                  
C     IRADG(N,IG)=IP123(N,I)                                                                                  
C     SDRADG(N,IG)=SD123(N,I)                                                                                 
C105  CONTINUE                                                                                                
 100  CONTINUE                                                          
      ENDIF                                                             
                                                                        
      IF(N23.NE.0)THEN                                                  
      DO 110 I=1,N23                                                    
      IF(LL23(I,1).EQ.0)GOTO110                                         
      IG=IG+1                                                           
      IF(IG.GT.MAXTRK)IG=MAXTRK                                         
      RPCOSG(IG)=RPC23(I)                                               
      RPSING(IG)=RPS23(I)                                               
      PHZG(IG)=PH23(I)                                                  
      ZIG(IG)=ZI23(I)                                                   
      CHG(IG)=CH23(I)                                                   
C     STORE LINK POINTERS                                                                                     
      LNK3(IG,1)=0                                                      
      LNK3(IG,2)=LL23(I,1)                                              
      LNK3(IG,3)=LL23(I,2)                                              
C      WRITE(*,*)'  23 ',IG,RPCOSG(IG),RPSING(IG)                                                             
C     DO115 N=1,36                                                                                            
C     IRADG(N,IG)=IP23(N,I)                                                                                   
C     IF(IP23(N,I).GT.0)NPTS(IG)=NPTS(IG)+1                                                                   
C     SDRADG(N,IG)=SD23(N,I)                                                                                  
C115  CONTINUE                                                                                                
 110  CONTINUE                                                          
      ENDIF                                                             
                                                                        
      IF(N12.NE.0)THEN                                                  
      DO 120 I=1,N12                                                    
      IF(LL12(I,1).EQ.0)GOTO120                                         
      IG=IG+1                                                           
      IF(IG.GT.MAXTRK)IG=MAXTRK                                         
      RPCOSG(IG)=RPC12(I)                                               
      RPSING(IG)=RPS12(I)                                               
      PHZG(IG)=PH12(I)                                                  
      ZIG(IG)=ZI12(I)                                                   
      CHG(IG)=CH12(I)                                                   
C     STORE LINK POINTERS                                                                                     
      LNK3(IG,1)=LL12(I,1)                                              
      LNK3(IG,2)=LL12(I,2)                                              
      LNK3(IG,3)=0                                                      
C      WRITE(*,*)' 12  ',IG,RPCOSG(IG),RPSING(IG)                                                             
C     DO125 N=1,36                                                                                            
C     IRADG(N,IG)=IP12(N,I)                                                                                   
C     IF(IP12(N,I).GT.0)NPTS(IG)=NPTS(IG)+1                                                                   
C     SDRADG(N,IG)=SD12(N,I)                                                                                  
C125  CONTINUE                                                                                                
 120  CONTINUE                                                          
      ENDIF                                                             
                                                                        
      IF(N13.NE.0)THEN                                                  
      DO 130 I=1,N13                                                    
      IF(LL13(I,1).EQ.0)GOTO130                                         
      IG=IG+1                                                           
      IF(IG.GT.MAXTRK)IG=MAXTRK                                         
      RPCOSG(IG)=RPC13(I)                                               
      RPSING(IG)=RPS13(I)                                               
      PHZG(IG)=PH13(I)                                                  
      ZIG(IG)=ZI13(I)                                                   
      CHG(IG)=CH13(I)                                                   
C     STORE LINK POINTERS                                                                                     
      LNK3(IG,1)=LL13(I,1)                                              
      LNK3(IG,2)=0                                                      
      LNK3(IG,3)=LL13(I,2)                                              
C      WRITE(*,*)' 1 3 ',IG,RPCOSG(IG),RPSING(IG)                                                             
C     DO135 N=1,36                                                                                            
C     IRADG(N,IG)=IP13(N,I)                                                                                   
C     IF(IP13(N,I).GT.0)NPTS(IG)=NPTS(IG)+1                                                                   
C     SDRADG(N,IG)=SD13(N,I)                                                                                  
C135  CONTINUE                                                                                                
 130  CONTINUE                                                          
      ENDIF                                                             
      IF(NUN.NE.0)THEN                                                  
C     PICK UP UNUSED TRACK SEGMENTS                                                                           
      NIG=0                                                             
      DO 200 II=1,3                                                     
      NT=NTRAKS(II)                                                     
      IF(NT.EQ.0)GOTO200                                                
      IF(II.EQ.1)NS=0                                                   
      IF(II.EQ.2)NS=12                                                  
      IF(II.EQ.3)NS=24                                                  
C      DO 210 I=1,NT                                                                                          
C      IF(IUSED(I,II).EQ.1)GOTO210                                                                            
C      NIG=NIG+1                                                                                              
C     IG=IG+1                                                                                                 
C**********************************************************************                                       
C     IF(NIG.GT.MAXTRK)THEN                                                                                   
C     NIG=MAXTRK                                                                                              
C     NNPTS(NIG)=0                                                                                            
C     ENDIF                                                                                                   
C     IF(IG.GT.MAXTRK)THEN                                                                                    
C     IG=MAXTRK                                                                                               
C     NNPTS(IG)=0                                                                                             
C     ENDIF                                                                                                   
C**************************************************12/10/87************                                       
C      RPCOSG(IG)=PCOSL(I,II)                                                                                 
C      RPSING(IG)=PSINL(I,II)                                                                                 
C      PHZG(IG)=PHZL(I,II)                                                                                    
C      ZIG(IG)=RZI(I,II)                                                                                      
C     DO 215 N=1,12                                                                                           
C     NN=NS+N                                                                                                 
C     NIRADG(NN,NIG)=IRPT(N,I,II)                                                                             
C     IF(IRPT(N,I,II).GT.0)NNPTS(NIG)=NNPTS(NIG)+1                                                            
C     SDRADN(NN,NIG)=SDRFT(N,I,II)                                                                            
C215  CONTINUE                                                                                                
C 210  CONTINUE                                                                                               
 200  CONTINUE                                                          
      ENDIF                                                             
                                                                        
*-----Debug--------------------------------------                                                             
*     WRITE(6,'('' ***FTLINK>> '',I5, ''  tracks found'')')IG                                                 
*     DO 170 I=1,IG                                                                                           
*         PRINT 2330,I,(IRADG(J,I),J=1,36)                                                                    
*2330 FORMAT (' LINK ',1X,I3,1X,12I2,1X,12I2,1X,12I2,F6.2)                                                    
*170  CONTINUE                                                                                                
*GB   DO 175 I=1,NIG                                                                                          
*         PRINT 2330,I,(NIRADG(J,I),J=1,36)                                                                   
*175  CONTINUE                                                                                                
*------------------------------------------------                                                             
                                                                        
      RETURN                                                            
      END                                                               
*