SUBROUTINE FPTREZ
*-- Author :    Stephen J. Maxfield  / I.O.Skillicorn
      SUBROUTINE FPTREZ(KTRK)
*                                                                                                             
*     Calculate residuals of Radial hits to PATREC tracks                                                     
*     and make lookagrams of them.                                                                            
*                                                                                                             
*D     Remove vertex shifts in segment residuals.                                                             
*                                                                                                             
*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,FPTFLG.                                                                                                 
      COMMON/FPTFLG/IPLAR, NIT, IREZ, LUNFP, NPLMAX                     
*KEND.                                                                                                        
*                                                                                                             
      COMMON/FERROR/ERRDR(MAXHTS, 36), ERRRM(MAXHTS, 36),               
     +              ERPDR(MAXHTS, 36), IERRF(MAXHTS, 36),               
     +                                 IERPF(MAXHTS, 36)                
      COMMON/CORRXY/CX1,CX2,CX3,CY1,CY2,CY3                             
      DIMENSION IRT(36),REZ(36),DRFT(12),ZEDW(12)                       
      DIMENSION RESID(12), RESR(12), RAD(12), DRFE(12), RMEAS(12)       
      PARAMETER(PHII=0.130899693)                                       
      PARAMETER(HPHII=PHII/2.)                                          
      PARAMETER(PI2=6.2831853)                                          
                                                                        
      IF(IREZ.GE.2) THEN                                                
                                                                        
      DO 1 I = 1, 36                                                    
                                                                        
*     Offsets for radials...                                                                                  
      IF(I.GE.1.AND.I.LE.12) THEN                                       
        CX=CX1                                                          
        CY=CY1                                                          
      ENDIF                                                             
      IF(I.GE.13.AND.I.LE.24) THEN                                      
        CX=CX2                                                          
        CY=CY2                                                          
      ENDIF                                                             
      IF(I.GE.25.AND.I.LE.36) THEN                                      
        CX=CX3                                                          
        CY=CY3                                                          
      ENDIF                                                             
                                                                        
      PHI=RPCOSG(KTRK)*ZP(I)+PHZG(KTRK)                                 
      RR=RPSING(KTRK)*ZP(I)+ZIG(KTRK)                                   
      IF(PHI.LT.0.0)PHI=PHI+PI2                                         
      IF(PHI.GT.PI2)PHI=PHI-PI2                                         
                                                                        
*     Jiggery-pokery to get right wedge number...                                                             
      XT=RR*COS(PHI)+XVV +CX                                            
      YT=RR*SIN(PHI)+YVV +CY                                            
      PHT=ATAN2(YT,XT)                                                  
      IF(PHT.LT.0.0)PHT=PHT+PI2                                         
      IF(PHT.GT.PI2)PHT=PHT-PI2                                         
      NNW=(PHT+HPHII -PHW(I))/PHII                                      
      NNW=NNW+1                                                         
                                                                        
                                                                        
      NP=NDP(I)                                                         
      DMIN=1000.                                                        
                                                                        
      DO 120 J=1,NP                                                     
                                                                        
       IF(DRI(J,I).GT.900.)GOTO120                                      
       IF(ABS(NW(J,I)-NNW).GT.1)GOTO120                                 
                                                                        
*      Expected drift corrected for shifts...                                                                 
       DRE=RR*SIN(PHI-WW(J,I))                                          
     1  +(YVV+CY)*COS(WW(J,I))-(XVV+CX)*SIN(WW(J,I))                    
                                                                        
*      Measured drifts...                                                                                     
       DWW= DRI(J,I)+DWS(J,I)                                           
       DTT=-DRI(J,I)+DWS(J,I)                                           
                                                                        
       DDW=DWW-DRE                                                      
       DDT=DTT-DRE                                                      
                                                                        
       IF(ABS(DDW).LT.DMIN)THEN                                         
        DMIN=ABS(DDW)                                                   
        DDD=DDW                                                         
        JMIN=J                                                          
       ENDIF                                                            
                                                                        
       IF(ABS(DDT).LT.DMIN)THEN                                         
        DMIN=ABS(DDT)                                                   
        JMIN=J                                                          
        DDD=DDT                                                         
       ENDIF                                                            
                                                                        
 120  CONTINUE                                                          
                                                                        
      IRT(I)=0                                                          
      REZ(I)=10.                                                        
      IF(DMIN.LT.5.0)THEN                                               
        IRT(I)=JMIN                                                     
        REZ(I)=DDD/0.0150                                               
      ENDIF                                                             
                                                                        
      IF(I.GE.1.AND.I.LE.12.AND.IRT(I).NE.0)THEN                        
        L=L+1                                                           
        IF(ABS(REZ(I)).LT.5.0) THEN                                     
          CALL SHS(116,0,REZ(I))                                                                       
          CALL SHS(119,0,REZ(I))                                                                       
        ENDIF                                                           
      ENDIF                                                             
                                                                        
      IF(I.GE.13.AND.I.LE.24.AND.IRT(I).NE.0)THEN                       
        LL=LL+1                                                         
        IF(ABS(REZ(I)).LT.5.0) THEN                                     
          CALL SHS(117,0,REZ(I))                                                                       
          CALL SHS(119,0,REZ(I))                                                                       
        ENDIF                                                           
      ENDIF                                                             
                                                                        
      IF(I.GE.25.AND.I.LE.36.AND.IRT(I).NE.0)THEN                       
        LLL=LLL+1                                                       
        IF(ABS(REZ(I)).LT.5.0) THEN                                     
          CALL SHS(118,0,REZ(I))                                                                       
          CALL SHS(119,0,REZ(I))                                                                       
        ENDIF                                                           
      ENDIF                                                             
                                                                        
 1    CONTINUE                                                          
                                                                        
      ENDIF                                                             
                                                                        
*     Now look at radial segment data...                                                                      
      DO 1001 I=1,3                                                     
      DO 1002 J=1,NTRAKS(I)                                             
      CH  = CHSQ(J,I)                                                   
      CALL SHS(241,0,CH)                                                                               
                                                                        
      NWED = -1                                                         
      JCNT = 0                                                          
      DO 1003 K=1,12                                                    
        JWIR = 12*(I-1) + K                                             
*       Track parameters for this track...                                                                    
        PHI = PCOSL(J,I)*ZP(JWIR)+PHZL(J,I)                             
        RR  = PSINL(J,I)*ZP(JWIR)+RZI(J,I)                              
        IF(PHI.LT.0.0)PHI=PHI+PI2                                       
        IF(PHI.GT.PI2)PHI=PHI-PI2                                       
                                                                        
        JP    = IRPT(K,J,I)                                             
        IF(JP .NE. 0) THEN                                              
          IF(NWED.LT.0) THEN                                            
            NWED = NW(JP,JWIR)                                          
          ENDIF                                                         
          IF(NW(JP,JWIR) .NE. NWED) GOTO 1003                           
*         IF(IERRF(JP,JWIR) .GT. 1) GOTO 1003                                                                 
                                                                        
          JCNT = JCNT + 1                                               
*         Z of wire...                                                                                        
          ZEDW(JCNT) = ZP(JWIR)                                         
                                                                        
          DS    = SDRFT(K,J,I)                                          
*         Measured drift...                                                                                   
          DRFT(JCNT) = DS*DRI(JP,JWIR)+DWS(JP,JWIR)                     
*         ...and radius (this has been corrected for Lorenz angle).                                           
          RMEAS(JCNT) = RM(JP,JWIR)                                     
                                                                        
*         Expected drift corrected for shifts...                                                              
          DRFE(JCNT)   = RR*SIN(PHI-WW(JP,JWIR))                        
          RESID(JCNT)  = DRFT(JCNT) - DRFE(JCNT)                        
                                                                        
*         Expected radius corrected for shifts...                                                             
          RAD(JCNT)   = RR*COS(PHI-WW(JP,JWIR))                         
          RESR(JCNT)  = RMEAS(JCNT) - RAD(JCNT)                         
                                                                        
        ELSE                                                            
          CALL SHS(209,0,FLOAT(JWIR))                                                                  
        ENDIF                                                           
                                                                        
 1003 CONTINUE                                                          
      IF(JCNT .GE.4) THEN                                               
      DO 1009 KK=1,JCNT                                                 
          CALL SHS(232+I,0,RESID(KK))                                                                  
          CALL SHS(236  ,0,RESID(KK))                                                                  
          CALL SHD(242  ,0,DRFE(KK),RESID(KK))                                                         
          CALL SHS(236+I,0,RESR(KK))                                                                   
          CALL SHS(240  ,0,RESR(KK))                                                                   
          CALL SHD(250  ,0,RMEAS(KK),RESR(KK))                                                         
 1009 CONTINUE                                                          
      ENDIF                                                             
                                                                        
      IF(JCNT .GE. 6) THEN                                              
*     Close to wire stuff...                                                                                  
      ENDIF                                                             
                                                                        
 1002 CONTINUE                                                          
 1001 CONTINUE                                                          
                                                                        
      RETURN                                                            
      END                                                               
*