SUBROUTINE FTMERG
*-- Author :   I.O.Skillicorn
      SUBROUTINE FTMERG
**: FTMERG 30907 RP. Farm changes.                                                                            
**----------------------------------------------------------------------                                      
*                                                                                                             
*     Merge lists of Radial-based and Planar-based tracks                                                     
*     resolving conflicts.                                                                                    
*         - Planar-based tracks win disputed segments                                                         
*  ***    - Radial-based tracks only accepted if verified by at                                               
*           at least one planar.                                                                              
*  ***    - Keep radial only tracks if 1 or 2 module.                                                         
*                                                                                                             
*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.                                                                                                        
                                                                        
                                                                        
                                                                        
*     POINTERS TO RADIAL SEGMENTS FORMING TRACKS                                                              
      COMMON /FLINK3/LNK3(MAXTRK,3)                                     
                                                                        
*     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 FOR PLANAR FOUND TRACKS                                                                          
      COMMON/FTPPBK/NPP,IPP(36,100),CHPP(100),LP(3,100)                 
      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                                                                
C     ISGG POINTS TO ROB'S SEGMENT BANKS                                                                      
      COMMON /FPSEG1/ ISGG(3,MAXTRK)                                    
      COMMON /FPSEG2/ ISGP(3,MAXTRK),IUZP(MAXSEG,3),IUZR(MAXTRK,3)      
      COMMON /FPSEG3/ ISGR(3,MAXSEG)                                    
C            RADIAL  REJECT , UNUSED  , RADIAL VERIFIED BY PLANAR                                             
      COMMON/FTRFLG/ IBRR(MAXTRK),IBPP(100),IVRR(MAXTRK)                
      DIMENSION PSEG(4)                                                 
      PARAMETER (PI2=6.2831853)                                         
      DATA ISTART/0/                                                    
                                                                        
                                                                        
      CALL VZERO(IUZP,3*MAXSEG)                                                                        
      CALL VZERO(IUZR,3*MAXTRK)                                                                        
      CALL VZERO(IBRR,IG)                                                                              
      CALL VZERO(IVRR,IG)                                                                              
      CALL VZERO(IBPP,NPP)                                                                             
                                                                        
                                                                        
      DO 100 I=1,IG                                                     
      DO 200 J=1,NPP                                                    
      DO 110 II=1,3                                                     
C     CHECK COMMON RADIAL SEGMENT                                                                             
      IF(LNK3(I,II).EQ.LRR(II,J).AND.LRR(II,J).NE.0)THEN                
      CALL SHS(713,0,2.01)                                                                             
      GOTO 250                                                          
      ENDIF                                                             
 110  CONTINUE                                                          
      DO 120 II=1,3                                                     
C     CHECK COMMON PLANAR SEGMENT                                                                             
      IF(ISGG(II,I).EQ.LPP(II,J).AND.LPP(II,J).NE.0)THEN                
      CALL SHS(713,0,3.01)                                                                             
      GOTO 250                                                          
      ENDIF                                                             
 120  CONTINUE                                                          
      GOTO200                                                           
 250  CONTINUE                                                          
C     HERE WE HAVE EITHER RADIAL OR PLANAR SEGMENT IN COMMON                                                  
C     BETWEEN RADIAL AND PLANAR FOUND TRACKS                                                                  
C     MARK RADIAL LINKED TRACK AS BAD                                                                         
C     NEXT LINE COUNTS RADIALS FOUND MORE THAN ONCE BY PLANARS                                                
       IF(IBRR(I).EQ.1)CALL SHS(713,0,4.51)                             
      IBRR(I)=1                                                         
      CALL SHS(713,0,4.01)                                                                             
 200  CONTINUE                                                          
 100  CONTINUE                                                          
                                                                        
                                                                        
                                                                        
                                                                        
*-----Debug---------------------------------------------------                                                
*     WRITE(*,*)' ***FTMERG***'                                                                               
*     WRITE(*,*)IG,' RADIAL TRACKS FTADD'                                                                     
*                                                                                                             
*     Mark planar and radial segments on verified radial tracks                                               
*     used. Label the verified radial tracks in the IG list                                                   
*     Kill off bad links...                                                                                   
      DO 300 I=1,IG                                                     
      CALL SHS(711,0,5.01)                                                                             
      IF(IBRR(I).EQ.0)THEN                                              
       IF((ISGG(1,I)+ISGG(2,I)+ISGG(3,I)).NE.0)THEN                     
C       RADIAL TRACK VERIFIED BY PLANAR SEGMENT - GOOD TRACK                                                  
        IVRR(I)=1                                                       
        IF(LNK3(I,1).NE.0)IUZR(LNK3(I,1),1)=1                           
        IF(LNK3(I,2).NE.0)IUZR(LNK3(I,2),2)=1                           
        IF(LNK3(I,3).NE.0)IUZR(LNK3(I,3),3)=1                           
        IF(ISGG(1,I).NE.0)IUZP(ISGG(1,I),1)=1                           
        IF(ISGG(2,I).NE.0)IUZP(ISGG(2,I),2)=1                           
        IF(ISGG(3,I).NE.0)IUZP(ISGG(3,I),3)=1                           
                                                                        
*-----Debug---------------------------------------------------                                                
*       Write(6,*) '       Verified Radial-based track:-'                                                     
*       PRINT 1001,I,(IRN(K,I),K=1,36),LNK3(I,1),LNK3(I,2),LNK3(I,3)                                          
*       PRINT 1001,I,(IRP(K,I),K=1,36),ISGG(1,I),ISGG(2,I),ISGG(3,I)                                          
                                                                        
        CALL SHS(711,0,2.01)                                                                           
       ELSE                                                             
C       UNVERIFIED - SEEN RADIALS ONLY- ?                                                                     
                                                                        
*-----Debug---------------------------------------------------                                                
*       Write(6,*) '       Unverified Radial-based track:-'                                                   
*       PRINT 1001,I,(IRN(K,I),K=1,36),LNK3(I,1),LNK3(I,2),LNK3(I,3)                                          
*       PRINT 1001,I,(IRP(K,I),K=1,36),ISGG(1,I),ISGG(2,I),ISGG(3,I)                                          
*1001 FORMAT(' RR',I2,3X,12I2,3X,12I2,3X,12I2,2X,3I2)                                                         
*1002 FORMAT(' RP',I2,3X,12I2,3X,12I2,3X,12I2,2X,3I2)                                                         
                                                                        
        CALL SHS(711,0,3.01)                                                                           
*       'Verify' this track also.                                                                             
        IVRR(I)=1                                                       
*       This assumes Radial tracks are at least two module...                                                 
        IF(LNK3(I,1).NE.0)IUZR(LNK3(I,1),1)=1                           
        IF(LNK3(I,2).NE.0)IUZR(LNK3(I,2),2)=1                           
        IF(LNK3(I,3).NE.0)IUZR(LNK3(I,3),3)=1                           
*       LNK3(I,1) = 0                                                                                         
*       LNK3(I,2) = 0                                                                                         
*       LNK3(I,3) = 0                                                                                         
        ISGG(1,I) = 0                                                   
        ISGG(2,I) = 0                                                   
        ISGG(3,I) = 0                                                   
       ENDIF                                                            
      ELSE                                                              
        LNK3(I,1) = 0                                                   
        LNK3(I,2) = 0                                                   
        LNK3(I,3) = 0                                                   
        ISGG(1,I) = 0                                                   
        ISGG(2,I) = 0                                                   
        ISGG(3,I) = 0                                                   
                                                                        
C     RADIAL BASED TRACK HAS SEGMENT IN COMMON WITH PLANAR BASED TRACK                                        
C     SHARE PLANAR SEGMENT WITH PLANAR TRACK - REJECT - IBRR=1                                                
C     SHARE RADIAL SEGMENT WITH PLANAR TRACK - REJECT- IBRR=1                                                 
*     PRINT 1005,I,(IRN(K,I),K=1,36),LNK3(I,1),LNK3(I,2),LNK3(I,3)                                            
*     PRINT 1006,I,(IRP(K,I),K=1,36),ISGG(1,I),ISGG(2,I),ISGG(3,I)                                            
      CALL SHS(711,0,4.01)                                                                             
      ENDIF                                                             
 300  CONTINUE                                                          
                                                                        
                                                                        
CC    WRITE(*,*)NPP,' PLANAR TRACKS '                                                                         
      DO 310 I=1,NPP                                                    
      IF( LRR(1,I).NE.0)IUZR( LRR(1,I),1)=1                             
      IF( LRR(2,I).NE.0)IUZR( LRR(2,I),2)=1                             
      IF( LRR(3,I).NE.0)IUZR( LRR(3,I),3)=1                             
      IF( LPP(1,I).NE.0)IUZP( LPP(1,I),1)=1                             
      IF( LPP(2,I).NE.0)IUZP( LPP(2,I),2)=1                             
      IF( LPP(3,I).NE.0)IUZP( LPP(3,I),3)=1                             
C     TRACKS BASED ON LINKED PLANAR SEGMENTS - ACCEPT AS GOOD                                                 
*     PRINT 1004,I,(IRR(K,I),K=1,36),LRR(1,I),LRR(2,I),LRR(3,I)                                               
*     PRINT 1003,I,(IPP(K,I),K=1,36),LPP(1,I),LPP(2,I),LPP(3,I)                                               
      CALL SHS(711,0,1.01)                                                                             
      IF(LRR(1,I).NE.0.OR.LRR(2,I).NE.0.OR.LRR(3,I).NE.0)THEN           
C     PLANAR VERIFIED BY RADIAL                                                                               
      CALL SHS(713,0,1.01)                                                                             
      ENDIF                                                             
C     FOR COMPLETENESS ADD DRIFT SIGNS FOR PLANAR AND RADIALS                                                 
      DO 311 IM=1,3                                                     
       IF(LPP(IM,I).NE.0)THEN                                           
        IP=LPP(IM,I)                                                    
        DO 312 II=1,12                                                  
         IOSP=IDGISG(II,IP,IM)                                          
         SPP(II+(IM-1)*12,I)=SIGN(1.0,FLOAT(IOSP))                      
 312    CONTINUE                                                        
       ENDIF                                                            
       IF(LRR(IM,I).NE.0)THEN                                           
        IP=LRR(IM,I)                                                    
       ENDIF                                                            
 311  CONTINUE                                                          
C                                                                                                             
 310  CONTINUE                                                          
                                                                        
      RETURN                                                            
      END                                                               
*