SUBROUTINE FPTRDT
*-- Author : S.J. Maxfield
      SUBROUTINE FPTRDT
*D: FPTRDT...... SM. New definition of dead wire flag. (Phase 2!)                                             
**----------------------------------------------------------------------                                      
**: FPTRDT 40000 SM. New definition of dead wire flag.                                                        
**----------------------------------------------------------------------                                      
**: FPTRDT 30907 RP. Farm changes.                                                                            
**----------------------------------------------------------------------                                      
**: FPTRDT 30104 SM. reject bad hits flagged by FRLOCO.                                                       
**----------------------------------------------------------------------                                      
*     Unpack Digitisations from bank FRRE.                                                                    
*     Create intermediate bank FRLC containing local                                                          
*     coordinates                                                                                             
*                                                                                                             
*     Store hits in IOS COMMONs for Pattern Recognition                                                       
*                                                                                                             
*     Called once per event. Needs previous call to FTCORG                                                    
*     to create corrected geometry bank FRG1                                                                  
*                                                                                                             
*                                                                                                             
*KEEP,BCS.                                                                                                    
      INTEGER      NHROW,NHCOL,NHLEN                                    
      PARAMETER   (NHROW = 2, NHCOL = 1, NHLEN=2)                       
      INTEGER      NBOSIW                                               
      PARAMETER   (NBOSIW=1000000)                                      
      INTEGER      IW(NBOSIW)                                           
      REAL         RW(NBOSIW)                                           
      COMMON /BCS/ IW                                                   
      EQUIVALENCE (RW(1),IW(1))                                         
      SAVE   /BCS/                                                      
*KEEP,CNSTBF.                                                                                                 
      INTEGER   LW(NBOSIW)                                              
      REAL      SW(NBOSIW)                                              
      EQUIVALENCE (RW(1),IW(1),LW(1),SW(1))                             
*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.                                                                                                        
      COMMON/FERROR/ERRDR(MAXHTS, 36), ERRRM(MAXHTS, 36),               
     +              ERPDR(MAXHTS, 36), IERRF(MAXHTS, 36),               
     +                                 IERPF(MAXHTS, 36)                
                                                                        
*     Max allowable bad hit flag...(To be gotten from bank sometime)                                          
      PARAMETER(IFRBAD=128)                                             
                                                                        
      PARAMETER(NBN=0)                                                  
*     Locators for FRG1 bank                                                                                  
      PARAMETER(IPDEAD=1)                                               
      PARAMETER(IPRPHP=2)                                               
      PARAMETER(IPRSTP=3)                                               
      PARAMETER(IPRPHM=5)                                               
      PARAMETER(IPRSTM=6)                                               
*     Locators for FRLC bank                                                                                  
      PARAMETER(IPRCLN=1)                                               
      PARAMETER(IPRDRF=2)                                               
      PARAMETER(IPRERD=3)                                               
      PARAMETER(IPRRAD=4)                                               
      PARAMETER(IPRERR=5)                                               
      PARAMETER(IPRSGW=6)                                               
                                                                        
      LOGICAL FIRST                                                     
      DATA FIRST/.TRUE./                                                
                                                                        
*KEEP,STFUNCT.                                                                                                
*     index of element before row number IROW                                                                 
      INDR(IND,IROW)=IND+2+IW(IND+1)*(IROW-1)                           
*     index of L'th element  of row number IROW                                                               
      INDCR(IND,L,IROW)=INDR(IND,IROW) + L                              
*     L'th integer element of the IROW'th row of bank with index IND                                          
      IBTAB(IND,L,IROW)=IW(INDCR(IND,L,IROW))                           
*     L'th real element of the IROW'th row of bank with index IND                                             
      RBTAB(IND,L,IROW)=RW(INDCR(IND,L,IROW))                           
*KEEP,FTFUNCT.                                                                                                
*     Statement functions for RADIAL Chamber data access.                                                     
*     Using Channel Number J                                                                                  
                                                                        
*     Module, Wedge-pair and Z-plane numbers...                                                               
      IRMOD(J) = J/288                                                  
      IRWDP(J) = (J-IRMOD(J)*288)/12                                    
      IRZPL(J) =  J-IRMOD(J)*288-IRWDP(J)*12                            
*     Statement function for obtaining WEDGE numbers(0-47) of                                                 
*     wires at plus and minus ends of Cell numbers                                                            
      IRWPL(J) = (IRWDP(J)/2)*4 + (MOD(IRWDP(J),2))                     
      IRWMI(J) = MOD(IRWPL(J) + 34,48)                                  
*     Statement function for obtaining IOS wire number (1-36)                                                 
      IRIOSW(J) = IRMOD(J)*12 + IRZPL(J) + 1                            
                                                                        
*     Statement functions for PLANAR Chamber data access.                                                     
*     Using Channel Number J                                                                                  
                                                                        
*     Module, orientation, W-cell and Z-plane numbers...                                                      
      IPMOD(J)  = J/384                                                 
      IPORI(J)  = (J-IPMOD(J)*384)/128                                  
      IPWCL(J)  = (J-IPMOD(J)*384-IPORI(J)*128)/4                       
      IPZPL(J)  = (J-IPMOD(J)*384-IPORI(J)*128-IPWCL(J)*4)              
                                                                        
*     IPSMD in range 0:8 Planar module number.                                                                
      IPSMD(J)  = IPMOD(J)*3 + IPORI(J)                                 
*                                                                                                             
*     IOS wire number (runs from 0 to 36)                                                                     
      IPIOSW(J) = IPSMD(J)*4 + IPZPL(J) + 1                             
                                                                        
* SB plane numbers (1-72) from cell number                                                                    
      IPSBW(J) = 24*IPMOD(J) + 4*IPORI(J) + IPZPL(J) + 1                
      IRSBW(J) = 24*IRMOD(J)              + IRZPL(J) + 13               
                                                                        
* Module, orientation, wire and (typical) cell number from plane                                              
* number in the range 1-72 (planars, radials and combined)                                                    
      IPMSB(J)  = (J - 1)/24                                            
      IPOSB(J)  = (J - 24*IPMSB(J) - 1)/4                               
      IPZSB(J)  = J - 24*IPMSB(J) - 4*IPOSB(J) - 1                      
      IPCLSB(J) = 384*IPMSB(J) + 128*IPOSB(J) + IPZSB(J)                
                                                                        
      IRMSB(J)  = (J - 1)/24                                            
      IRZSB(J)  = J - 24*IRMSB(J) - 13                                  
      IRCLSB(J) = 288*IRMSB(J) + IRZSB(J)                               
                                                                        
      IRADSB(J) = (J - 24*((J-1)/24) - 1)/12                            
      ICELSB(J) = IRADSB(J)*IRCLSB(J) + (1 - IRADSB(J))*IPCLSB(J)       
*KEEP,STFCLW.                                                                                                 
* statement functions acting on the BOS COMMON LW                                                             
*     index of element before row number LWROW                                                                
      LNDR(LND,LWROW) = LND + 2 + LW(LND+1)*(LWROW-1)                   
*     index of L-th element of row number LWROW                                                               
      LNDCR(LND,L,LWROW)=LNDR(LND,LWROW)+L                              
*     L-th integer element of the LWROW'th row in bank with index LND                                         
      LBTAB(LND,L,LWROW)=LW(LNDCR(LND,L,LWROW))                         
*     L-th real element of the LWROW'th row in bank with index LND                                            
      SBTAB(LND,L,LWROW)=SW(LNDCR(LND,L,LWROW))                         
*                                                                                                             
*KEND.                                                                                                        
*                                                                                                             
      IF(FIRST) THEN                                                    
         FIRST  = .FALSE.                                               
         IQFRLC = NAMIND('FRLC')
         IQFRG1 = NAMIND('FRG1')
      ENDIF                                                             
                                                                        
*                                                                                                             
      IFRLC = NLINK('FRLC',NBN)
      IFRG1= IW(IQFRG1)                                                 
      IF(IFRG1.EQ.0) THEN                                               
        CALL ERRLOG(107,'S:FPTRDT: FRG1 bank not found')                                               
        RETURN                                                          
      ENDIF                                                             
      IF(IFRLC.EQ.0) THEN                                               
        RETURN                                                          
      ENDIF                                                             
      NFRLC = IW(IFRLC+2)                                               
      IF(NFRLC.LE.0) THEN                                               
        RETURN                                                          
      ENDIF                                                             
                                                                        
*     Extract Hits...                                                                                         
      DO 300  K= 1, NFRLC                                               
                                                                        
        ICLNUM=        IBTAB(IFRLC,IPRCLN,K)                            
        IDEAD =        LBTAB(IFRG1,IPDEAD,ICLNUM+1)                     
                                                                        
*       Reject alleged hits from dead wires and hits marked bad                                               
*       in FRLOCO...                                                                                          
        ISGNW =        IBTAB(IFRLC,IPRSGW,K)                            
        ISGTS =        MOD(ISGNW,2)                                     
*       Is this end of the wire live?                                                                         
        IF(IDEAD.NE.1 .AND. IDEAD.NE.(ISGTS+2))  THEN                   
        IF(ISGNW .LT. IFRBAD) THEN                                      
                                                                        
         DRIFT =        RBTAB(IFRLC,IPRDRF,K)                           
         RADIUS=        RBTAB(IFRLC,IPRRAD,K)                           
         ERRDRF=        RBTAB(IFRLC,IPRERD,K)                           
         ERRRAD=        RBTAB(IFRLC,IPRERR,K)                           
                                                                        
         KWIR   =  IRIOSW(ICLNUM)                                       
*        Increment number of hits in this wire plane...                                                       
         NDP(KWIR)              =   NDP(KWIR) + 1                       
*        Skip if maximum hits exceeded                                                                        
         IF(NDP(KWIR) .GT. MAXHTS) THEN                                 
          CALL ERRLOG(101,'W:FPTRDT: MAX HITS exceeded ')                                              
          NDP(KWIR)              =   NDP(KWIR) - 1                      
         ELSE                                                           
          RM ( NDP(KWIR), KWIR)  =   RADIUS                             
          DRI( NDP(KWIR), KWIR)  =   DRIFT                              
          ERRRM( NDP(KWIR), KWIR)=   ERRRAD                             
          ERRDR( NDP(KWIR), KWIR)=   ERRDRF                             
          IERRF( NDP(KWIR), KWIR)=   ISGNW                              
*         Wedge number of this hit, Phi and stagger                                                           
*         of sense wire closest this hit.                                                                     
          IF(MOD(ISGNW,2).EQ.0) THEN                                    
            NW(NDP(KWIR),KWIR)     =   IRWPL(ICLNUM)+ 1                 
            WW(NDP(KWIR),KWIR)     =   SBTAB(IFRG1,IPRPHP,ICLNUM+1)     
            DWS(NDP(KWIR), KWIR)   =   SBTAB(IFRG1,IPRSTP,ICLNUM+1)     
          ELSE                                                          
            NW(NDP(KWIR),KWIR)     =   IRWMI(ICLNUM)+ 1                 
            WW(NDP(KWIR),KWIR)     =   SBTAB(IFRG1,IPRPHM,ICLNUM+1)     
            DWS(NDP(KWIR), KWIR)   =   SBTAB(IFRG1,IPRSTM,ICLNUM+1)     
          ENDIF                                                         
*         relations between IOS labelling and FRRE bank...                                                    
          IPFRRE(NDP(KWIR),KWIR)  =   K                                 
          IF(K.LE.MAXDIG) THEN                                          
           IRPIOS(K,1)            =   KWIR                              
           IRPIOS(K,2)            =   NDP(KWIR)                         
          ENDIF                                                         
                                                                        
         ENDIF                                                          
                                                                        
        ENDIF                                                           
        ENDIF                                                           
*      Next HIT...                                                                                            
 300  CONTINUE                                                          
                                                                        
                                                                        
      RETURN                                                            
      END                                                               
*