SUBROUTINE FPTPDT
*-- Author : S.J. Maxfield
      SUBROUTINE FPTPDT
**: FPTPDT 40000 SM. New definition of dead wire flag.                                                        
**----------------------------------------------------------------------                                      
**: FPTPDT 30907 RP. Farm changes.                                                                            
**----------------------------------------------------------------------                                      
*                                                                                                             
*                                                                                                             
*     Unpack Digitisations from bank FRPE.                                                                    
*     Create intermediate bank FPLC 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 FPG1                                                                  
*                                                                                                             
*                                                                                                             
*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 FPG1 bank                                                                                  
      PARAMETER(IPDEAD=1)                                               
      PARAMETER(IPPPHP=2)                                               
      PARAMETER(IPPSTP=3)                                               
*     Locators for FPLC bank                                                                                  
      PARAMETER(IPPCLN=1)                                               
      PARAMETER(IPPDRF=2)                                               
      PARAMETER(IPPERD=3)                                               
      PARAMETER(IPPERF=4)                                               
                                                                        
      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.                                               
         IQFPG1 = NAMIND('FPG1')
         IQFPLC = NAMIND('FPLC')
      ENDIF                                                             
                                                                        
*                                                                                                             
      IFPG1= IW(IQFPG1)                                                 
      IFPLC = IW(IQFPLC)                                                
                                                                        
      IF(IFPG1.EQ.0) THEN                                               
        CALL ERRLOG(106,'S:FPTPDT: FPG1 bank not found')                                               
        RETURN                                                          
      ENDIF                                                             
      IF(IFPLC.EQ.0) THEN                                               
        RETURN                                                          
      ENDIF                                                             
      NFPLC = IW(IFPLC+2)                                               
      IF(NFPLC.LE.0) THEN                                               
        RETURN                                                          
      ENDIF                                                             
                                                                        
*     Extract Hits...                                                                                         
      DO 300  K= 1, NFPLC                                               
                                                                        
       ICLNUM=        IBTAB(IFPLC,IPPCLN,K)                             
       IDEAD =        LBTAB(IFPG1,IPDEAD,ICLNUM+1)                      
       IF(IDEAD .NE. 1) THEN                                            
                                                                        
         DRIFT =        RBTAB(IFPLC,IPPDRF,K)                           
         ERRDRF=        RBTAB(IFPLC,IPPERD,K)                           
         ISGNW =        IBTAB(IFPLC,IPPERF,K)                           
         IF(ISGNW .LT. IFRBAD) THEN                                     
                                                                        
*        IOS wire planes numbered 1-36 through 3 Modules                                                      
         KWIR   =  IPIOSW(ICLNUM)                                       
*        increment number of hits in this wire plane...                                                       
         NDPW(KWIR)              =   NDPW(KWIR) + 1                     
         IF(NDPW(KWIR) .GT. MAXHTS) THEN                                
          CALL ERRLOG(102,'W:FPTPDT: MAX HITS exceeded ')                                              
          NDPW(KWIR)              =   NDPW(KWIR) - 1                    
         ELSE                                                           
*         W-coordinate of wire...                                                                             
*         W-coordinate of wire...                                                                             
          DW ( NDPW(KWIR), KWIR)  =   SBTAB(IFPG1,IPPSTP,ICLNUM+1)      
*         Phi of hit wire...                                                                                  
          WWP( NDPW(KWIR), KWIR)  =   SBTAB(IFPG1,IPPPHP,ICLNUM+1)      
*         Drift in W, Error, flag...                                                                          
          DRIW( NDPW(KWIR), KWIR)  =  DRIFT                             
          ERPDR( NDPW(KWIR), KWIR) =  ERRDRF                            
          IERPF( NDPW(KWIR), KWIR) =  ISGNW                             
*         W-cell number of this hit...                                                                        
          KWCL = IPWCL(ICLNUM)                                          
          IF(KWCL.LE.15)THEN                                            
            NDW(NDPW(KWIR),KWIR) = KWCL                                 
          ELSE                                                          
            NDW(NDPW(KWIR),KWIR) = KWCL-6                               
          ENDIF                                                         
*                                                                                                             
          IF(KWCL.LE.15.AND.KWCL.GE.10)THEN                             
            IPHOLE(NDPW(KWIR),KWIR) = 1                                 
          ELSE IF(KWCL.LE.21.AND.KWCL.GE.16)THEN                        
            IPHOLE(NDPW(KWIR),KWIR) = -1                                
          ELSE                                                          
            IPHOLE(NDPW(KWIR),KWIR) = 0                                 
          ENDIF                                                         
*                                                                                                             
*         relations between IOS labelling and FRRE bank...                                                    
          IPFRPE(NDPW(KWIR),KWIR)  =   K                                
          IF(K.LE.MAXDIG) THEN                                          
           IPPIOS(K,1)             =   KWIR                             
           IPPIOS(K,2)             =   NDPW(KWIR)                       
          ENDIF                                                         
                                                                        
         ENDIF                                                          
                                                                        
         ENDIF                                                          
       ENDIF                                                            
                                                                        
 300  CONTINUE                                                          
                                                                        
      RETURN                                                            
      END                                                               
*