FTFHPL COMMENTS
*-- Author : I.O. Skillicorn
      SUBROUTINE FTFHPL(NPLA,IPA,SD,IPP,SDP,
*                                                                                                             
*     Calling sequence changed. Error in R-z intercept put out                                                
*     Unused arguments removed. Overall 'Chisq' added.                                                        
*                                                                                                             
C     LEAST SQUARES FIT IN R-Z, PHI-Z  HELIX FRAME                                                            
C     UPDATED FROM FT205PP  3/12/91                                                                           
C     INCLUDES PLANARS IN R-Z FIT                                                                             
C     INCLUDES PLANARS IN PHI-Z FIT - REMOVED                                                                 
C     AUTHOR   I.O.SKILLICORN                                                                                 
C     SET FOR FAST FILTER                                                                                     
C     STR LINE FIT IN R-Z                                                                                     
C     STR LINE FIT IN PHI-Z                                                                                   
C                                                                                                             
C     VERTEX CORRECTED - FIT IN HELIX COORDS TO ALLOW FOR AN OFFSET                                           
C                        VERTEX XVV,YVV                                                                       
C                                                                                                             
C    CORRECTED FOR OFFSET RADIALS                                                                             
*                                                                                                             
C     *************************************************************                                           
C     REJECT R-Z FITS WITH UNREASONABLE INTERCEPT    SEE ZCUT                                                 
C     DISABLED 6/6/90                                                                                         
C     ******************************************************                                                  
C     FOR THREE MODULE FIT ONLY   ABS(ZINT) GT ZCUT                                                           
C     CORRECTED TO AVOID DIVIDE CHECK  11/6/90                                                                
C     VARIABLE ANGLE PLANARS  1/11/90                                                                         
*                                                                                                             
*KEEP,FRDIMS.                                                                                                 
*KEEP,FH1WORK.                                                                                                
*    Planar geometry                                                                                          
*                                                                                                             
*    Radial geometry                                                                                          
*                                                                                                             
*    Radial data...                                                                                           
*                                                                                                             
*    Planar Data                                                                                              
*                                                                                                             
*    Pointers into DIGI bank for IOS labelled hits                                                            
*                                                                                                             
*    Track segment data                                                                                       
*                                                                                                             
*    Fit data                                                                                                 
*                                                                                                             
*                                                                                                             
*KEEP,FPTVTX.                                                                                                 
**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,FRWERR.                                                                                                 
*KEND.                                                                                                        
*                                                                                                             
*                                                                                                             
C     TO USE Z-VERTEX  IVERTX=1                                                                               
C                                                                                                             
C     NOTE THAT XV,YV MUST ALWAYS BE USED TO GET CORRECT HELIX FRAME -                                        
C     NOMINAL BEAM POSITION WOULD BE USED IN PRACTISE .                                                       
*                                                                                                             
C     Z-VERTEX NEED NOT BE USED . IT'S USE RESULTS IN A BETTER DEFINITIO                                      
C     OF THE R-Z SLOPE - FEWER PATREC  ERRORS - BETTER D(1/P)                                                 
C     USE PLANARS IN FINAL PHI-Z FIT TO IMPROVE R   : IPL=1                                                   
*                                                                                                             
C     WRITE(*,*)' FTFHPL ERRORS ',ERRVL,ERRV,ERRP,ERRRX                                                       
C     Z INTERCEPT CUT *******************                                                                     
C     ***********************************                                                                     
C      TO CALCULATE R  USE:                                                                                   
C      R=RD*Z +RO                                                                                             
*                                                                                                             
C     TO CALCULATE PHI USE:                                                                                   
C     PHI=Z*RPCOS+PHZ                                                                                         
*                                                                                                             
*                                                                                                             
*                                                                                                             
C     FIT R-Z    IN HELIX COORDS - NO VERTEX CONSTRAINT                                                       
*                                                                                                             
C*********************************************************                                                    
C     ADD IN VERTEX                                                                                           
C      PRINT1003,II,J,XX(II),YY(II),WP(II)                                                                    
C*********************************************************                                                    
*                                                                                                             
C     J IS POINT NO.     K=PLANE NO.                                                                          
C     RADIALS                                                                                                 
************************************************************************                                      
*     CORRECT FOR ERRORS IN POSITION OF RADIAL HERE                                                           
************************************************************************                                      
*                                                                                                             
*                                                                                                             
C     LAB PHI                                                                                                 
C     LAB R                                                                                                   
C     CORRECT FOR  DISPLACEMENT OF RADIAL                                                                     
C     HELIX R                                                                                                 
*---------------------------------------------                                                                
C      PRINT1003,II,J,XX(II),YY(II),WP(II)                                                                    
*---------------------------------------------                                                                
C     PLANARS                                                                                                 
*      AL=ATAN2(S(K),C(K))                                                                                    
C     1/11/90    VARIABLE ANGLE PLANARS                                                                       
C     TO AVOID DIV. BY 0                                                                                      
*---------------------------------------------                                                                
C      PRINT1003,II,J,XX(II),YY(II),WP(II)                                                                    
*---------------------------------------------                                                                
*                                                                                                             
      CALL FTLFTW(XX,YY,WP,II,0,2,RZS,RZII,ET,DRZS,DRZI,COVR)
C     RZS-SLOPE,RZII-INTERCEPT                                                                                
C      WRITE(*,*)' RZS,RZII ',RZS,RZII                                                                        
C      FOR STOREAGE OF HELIX R                                                                                
*                                                                                                             
C     CALCULATE CHI**2 OF FIT TO STR LINE IN R  -Z                                                            
*        For an overall 'Chi-squared'...                                                                      
*                                                                                                             
*                                                                                                             
*                                                                                                             
*                                                                                                             
*                                                                                                             
C     CALCULATE PHI BASED ON FITTED R  - IN LAB                                                               
C      MAKE A FIT TO  HELIX PHI                                                                               
C     J IS POINT NO.     K=PLANE NO.                                                                          
C     WRITE(*,*)' FTFHPL J,K, DR RM ',II,J,K,DRI(J,K),RM(J,K)                                                 
*                                                                                                             
*                                                                                                             
************************************************************************                                      
*     CORRECT FOR ERRORS IN POSITION OF RADIAL HERE                                                           
************************************************************************                                      
C***********************************************************************                                      
*                                                                                                             
C     USE  HELIX R - THIS INCLUDES PLANARS IF IPL=1                                                           
C     CONVERT HELIX R TO LAB R                                                                                
C***********************************************************************                                      
*                                                                                                             
C     PHI WITH FITTED R  - IN LAB - TO GET MORE ACCURACY IN PHI                                               
C     ..................................................................                                      
*                                                                                                             
C     WRITE(*,*)' FHLX ERROR',(DRI(J,K)*SD(K)+WS(K)),RR                                                       
C     LAB PHI WITH FITTED R                                                                                   
C     TO HELIX COORDS                                                                                         
************************************************************************                                      
*     CORRECT FOR ERRORS IN POSITION OF RADIAL HERE                                                           
*     EFFECTIVELY XVV, YVV ARE CHANGED                                                                        
************************************************************************                                      
*                                                                                                             
C     NEXT LINE IS PHI HELIX  CALCULATED WITH FITTED R                                                        
C     RECALCULATE  CORRECTIONS -  USE RESULT OF PREVIOUS FIT                                                  
*                                                                                                             
C     ------------------------------------------------------------                                            
C     THE  ERROR SEEMS TO BE CALCULATED CORRECTLY ( ON AVERAGE )                                              
C     FOR 3 AND 2 MODULES EVENTS WITH THE FOLLOWING WEIGHT                                                    
C     ( IT MUST OVERESTIMATE THE ERROR DUE TO DR HOWEVER)                                                     
*------------------------------------------                                                                   
C      PRINT1002,J,II,XX(II),YY(II),WP(II),RR                                                                 
*------------------------------------------                                                                   
*******                                                                                                       
*     Try to repair Phi-discontinuites...                                                                     
*******                                                                                                       
C                                                                                                             
      CALL FTLFTW(XX,YY,WP,II,0,2,RPCOS,PHZ,EL,DRPCOS,DPHZ,COVP)
C     RPCOS-SLOPE PHI-Z,PHZ-INTERCEPT  PHI AXIS                                                               
*                                                                                                             
*------------------------------------------                                                                   
CDEB  PRINT5000,RPCOS,PHZ,DRPCOS,DPHZ                                                                         
*------------------------------------------                                                                   
*                                                                                                             
C     GUARD AGAINST RPCOS<< ERROR                                                                             
****  IF(ABS(RPCOS).LT.DRPCOS/10.)RPCOS=DRPCOS/10.                                                            
C                                                                                                             
C                                                                                                             
C     CALCULATE CHI**2 OF FIT TO STR LINE IN PHI-Z                                                            
*        For an overall 'Chi-squared'...                                                                      
C     WRITE(*,*)' RPCOS,PHZ,CH ',RPCOS,PHZ,CH                                                                 
*------------------------------------------                                                                   
C     PRINT 1000,PP,DL,DM,DN,DRPCOS*100.,PCOS,PSIN,PHZ                                                        
*------------------------------------------                                                                   
*                                                                                                             
*