FTZFIT COMMENTS
*-- Author :   I.O.Skillicorn
      SUBROUTINE FTZFIT(TZZ,NPAR,ZVERT,IMSTDY)
**: FTZFIT.......SM. New routine for monitoring.                                                              
**----------------------------------------------------------------------                                      
C                                                                                                             
C                                                                                                             
C     TRY MODS TO FIT VELOCITY FACTOR TO RADIALS                                                              
C                                                                                                             
C                                                                                                             
C                                                                                                             
C     MODIFIED TO WORK ON PLANAR-BASED PATTERN RECOGNITION                                                    
C     NOTE ROUTINES OF SAME NAME IN GRFTRAC !!!!!!!!!!!!!!                                                    
C                                                                                                             
C                                                                                                             
C     FIT PLANAR TRACKS TO GET T0 , FILL VERTEX HISTOGRAMS...                                                 
C     IF IRPIK=1    USE RADIAL POINTS ALSO                                                                    
C     DMIN CUT INTRODUCED FOR PLANAR POINTS 25/2/92                                                           
C     PUT  (Z-200.) IN ALL TERMS   26/2/92                                                                    
C     CHANGE TO Z  28/2/92  TO GET MOMENTUM                                                                   
C     ADD RESIDUAL PRINT     2/3/92                                                                           
C     TRY DRIFT VELOCITY DETERMINATION.. ...DOES NOT WORK                                                     
C     DUE TO STRONG CORRELATION WITH TIME-ZERO                                                                
C                                                                                                             
C     27/4/92  INTRODUCE SECTION TO COMPARE SLOPE OF LINE SEGMENT                                             
C     IN VERTICAL WIRES WITH FITTED LINE BETWEEN PLANAR CHAMBERS.                                             
C     RATIO OF SLOPES SHOULD BE RATIO OF ASSUMED AND TRUE DRIFT                                               
C     VELOCITIES .  FIND A VALUE CIRCA 1.02   THIS VALUE                                                      
C     GIVES BETTER RESIDUALS IN PLANARS. COSTXT NOW HAS 42.15                                                 
C     FOR PLANARS                                                                                             
C     1 /5/92  INCLUDE SECTION FOR RADIALS                                                                    
C                                                                                                             
*KEEP,BOSMDL.                                                                                                 
C     ------BOSMDL                                                                                            
C     ------                                                                                                  
*KEEP,FRDIMS.                                                                                                 
*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                                                                                                             
C---                                                                                                          
*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,FPFVTX.                                                                                                 
*                                                                                                             
*KEND.                                                                                                        
*                                                                                                             
*FOR PLOTS...                                                                                                 
*KEEP,FRPLTA.                                                                                                 
*KEND.                                                                                                        
*FTTRAC RESULTS (SJM)                                                                                         
*KEEP,FRH3FT.                                                                                                 
*     Common for RETRAC results (SJM)                                                                         
*KEND.                                                                                                        
*                                                                                                             
*                                                                                                             
*                                                                                                             
*                                                                                                             
C      COMMON/FTVERT2/NNFTV,FLPP(10000),FMPP(10000),                                                          
C     1 FXPP(10000),FYPP(10000)                                                                               
*                                                                                                             
*KEEP,FPLSEG.                                                                                                 
C---                                                                                                          
C---                                                                                                          
*KEND.                                                                                                        
************************************************************************                                      
C       XXX ADDED 3/3/92                                                                                      
C                    TZ=TIME-ZERO                                                                             
C     ADDED  FOR RESIDUAL CHECK                                                                               
C     BANKS FOR PLANAR PATTERN RECOGNITION                                                                    
C     COMMON FOR RADIALS ASSOCIATED WITH PLANAR TRACKS                                                        
C     PLANAR SEGMENTS ASSOCIATED  WITH RADIALS                                                                
C     ISGG POINTS TO ROB'S SEGMENT BANKS                                                                      
************************************************************************                                      
C     GLOBAL FIT TO PLANAR LINE SEGMENTS                                                                      
C     NPAR=4 STRAIGHT LINE IN X-Z , Y-Z PLANES                                                                
C     NPAR=6 PARABOLAE IN X-Z, Y-Z PLANES                                                                     
C*********************************************************************                                        
C     WITH SUITABLE MODS CAN FIT ALL MODULES SIMULTANEOUSLY                                                   
C     (EACH MODULE TO A (DIFFERENT) STRAIGHT LINE )                                                           
C     OR EACH TRACK TO A STRAIGHT LINE OR PARABOLA)                                                           
C     MINIMUM NUMBER OF PLANAR POINTS                                                                         
C     NNPLA=25                                                                                                
C     PICK UP PLANAR POINTS ONLY IF DRIFT > DMIN                                                              
C     TO AVOID WRONG SIGN POINTS                                                                              
C     TO USE RADIAL POINTS IRPIK=1                                                                            
      CALL STEXT(IHX+270,4,' PROJECTED VERTEX  X       ' )                                             
      CALL BHS(IHX+270,0,50,-5.,5.)                                                                    
      CALL STEXT(IHX+271,4,' PROJECTED VERTEX  Y       ' )                                             
      CALL BHS(IHX+271,0,50,-5.0,5.0)                                                                  
      CALL STEXT(IHX+272,4,' 1/P FROM FIT              ' )                                             
      CALL BHS(IHX+272,0,50,-2.5,2.5)                                                                  
      CALL STEXT(IHX+273,4,' 1/P FROM FIT CHI < 5     ' )                                              
      CALL BHS(IHX+273,0,50,-2.5,2.5)                                                                  
      CALL STEXT(IHX+273,4,' 1/P FROM FIT CHI < 5     ' )                                              
      CALL BHS(IHX+273,0,50,-2.5,2.5)                                                                  
      CALL STEXT(IHX+280,4,' VFACTOR MEAS/PRED CHI < 5  PLANAR' )                                      
      CALL BHS(IHX+280,0,50,00.5,1.5)                                                                  
      CALL STEXT(IHX+281,4,'  SLOPE PREDICTED PLANAR         ' )                                       
      CALL BHS(IHX+281,0,50,-0.5,0.5)                                                                  
      CALL STEXT(IHX+282,4,'  SLOPE MEAS   PLANAR           ' )                                        
      CALL BHS(IHX+282,0,50,-0.5,0.5)                                                                  
      CALL STEXT(IHX+283,4,'  SLOPE MEAS -SLOPE PRED  PLANAR' )                                        
      CALL BHS(IHX+283,0,50,-0.1,0.1)                                                                  
      CALL STEXT(IHX+290,4,' VFACTOR MEAS/PRED CHI < 5  RADIAL' )                                      
      CALL BHS(IHX+290,0,50,00.5,1.5)                                                                  
      CALL STEXT(IHX+291,4,'  SLOPE PREDICTED RADIAL         ' )                                       
      CALL BHS(IHX+291,0,50,-0.5,0.5)                                                                  
      CALL STEXT(IHX+292,4,'  SLOPE MEAS   RADIAL           ' )                                        
      CALL BHS(IHX+292,0,50,-0.5,0.5)                                                                  
      CALL STEXT(IHX+293,4,'  SLOPE MEAS -SLOPE PRED  RADIAL' )                                        
      CALL BHS(IHX+293,0,50,-0.1,0.1)                                                                  
C     WRITE(*,*)' FTZFIT MIN # PLANAR PTS=  ',NNPLA                                                           
C     COUNTS DATA POINTS                                                                                      
C     COUNTS TRACK SEGMENTS                                                                                   
C     LOOP TRACKS                                                                                             
C     WRITE(*,*)' NPP ',NPP                                                                                   
C     WRITE(*,*)' FTZFIT K IRP,IRN ',K                                                                        
CC    PRINT 1000,(IPP(JJ,K),JJ=1,36)                                                                          
CC    PRINT 1001,(SPP(JJ,K),JJ=1,36)                                                                          
CC    PRINT 1000,(IRR(JJ,K),JJ=1,36)                                                                          
CC    PRINT 1001,(SRR(JJ,K),JJ=1,36)                                                                          
C     TO FIT EACH TRACK FOR T0 - THESE  LINES  ADDED------------                                              
C     --------------------------------------TO HERE                                                           
C     MODULES                                                                                                 
C     ORIENTATIONS                                                                                            
C     WIRES /ORIENTATION                                                                                      
CTEMP IF(IGSEG.NE.3)GOTO3010                                                                                  
C     GOOD SEGMENT- AT LEAST 2 POINTS/PLANAR ORIENTATION                                                      
C     SUM SEGMENTS                                                                                            
C     TO FIT WHOLE TRACK--- ADD NEXT LINE ----------                                                          
C     WHOLE TRACK FITTED AS STRAIGHT LINE WITH T0 FIT                                                         
C     5 PARAMETER FIT                                                                                         
C------------------------------------------------------------                                                 
C     FILL POINTS                                                                                             
C     IF(DRIW(J,IPL).LT.DMIN)GOTO3050                                                                         
C     IF(IITART.LT.10)WRITE(*,*)' FTZFIT  THET PLA ',THET(NNDATA)                                             
C     DY/DZ                                                                                                   
C     YZ                                                                                                      
C     DX/DZ                                                                                                   
C     XZ                                                                                                      
C     PLUS                                                                                                    
C     TWO TERMS TO GIVE CURVATURE---RY,RX--------------                                                       
C     ------------------------------------------------                                                        
C                                                                                                             
**********************************************************                                                    
C     DOWN WEIGHT PLANAR MODULE TO BE STUDIED                                                                 
C     IF(IMM.EQ.0)SSIG(NNDATA)=SSIG(NNDATA)*100.                                                              
C     IF(IMM.EQ.1)SSIG(NNDATA)=SSIG(NNDATA)*100.                                                              
C     IF(IMM.EQ.2)SSIG(NNDATA)=SSIG(NNDATA)*100.                                                              
**********************************************************                                                    
C     STORE NNDATA VALUE FOR PLANARS                                                                          
C     ADD POINT COUNT CHECK INSTEAD OF SEGMENT COUNT                                                          
C     WRITE(*,*)' FTZFIT NPLA ',NPLA                                                                          
C     PICK UP RADIALS .............................................                                           
C     ADDED 3/3/92                                                                                            
C     IF(IITART.LT.10)WRITE(*,*)' FTZFIT THET RAD',THET(NNDATA),                                              
C    1   WW(J,IPL)                                                                                            
C     DY/DZ                                                                                                   
C     YZ                                                                                                      
C     DX/DZ                                                                                                   
C     XZ                                                                                                      
C     PLUS                                                                                                    
C     TWO TERMS TO GIVE CURVATURE---RY,RX--------------                                                       
C     ------------------------------------------------                                                        
C     CORRECT DRIFT BY A FACTOR FOR WRONG VELOCITY                                                            
C     CORRECT DRIFT BY A FACTOR FOR WRONG VELOCITY                                                            
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
******************************************************************                                            
C     DOWN-WEIGHT RADIAL MODULE TO BE STUDIED   IM=0,1,2                                                      
C     IM=2 GIVES SYSTEMATIC SHIFT AT LOW MOMENTA AND SIG=2.0                                                  
C     DUE TO EXTRAPOLATION FROM PLANARS                                                                       
**    IF(IM.EQ.0)SSIG(NNDATA)=SSIG(NNDATA)*100.                                                               
**    IF(IM.EQ.1)SSIG(NNDATA)=SSIG(NNDATA)*100.                                                               
**    IF(IM.EQ.2)SSIG(NNDATA)=SSIG(NNDATA)*100.                                                               
******************************************************************                                            
C     NOTE ANGLE SELECTION FOR RESIDUALS IN FXLFIT                                                            
******************************************************************                                            
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C.................................END RADIAL PICKUP SECTION                                                   
C     NEXT     LINE  FOR FIT /TRACK  -------------------------                                                
*********************************************                                                                 
*********************************************                                                                 
*********************************************                                                                 
C     IF NO RADIAL DATA DO NOT FIT ADDITIONAL PARAMETER                                                       
C     TO SET TO NORMAL 6 PARAMETER FIT UNCOMMENT NEXT LINE                                                    
CC    IF(NPAR.EQ.6)MATOT=NTT*6                                                                                
C     TO SET TO NORMAL 4 PARAMETER FIT UNCOMMENT NEXT LINE                                                    
CC    IF(NPAR.EQ.4)MATOT=NTT*4                                                                                
******************************************************                                                        
******************************************************                                                        
******************************************************                                                        
C     FIT W= (YZ+YD*(Z- 00.)+YR*(Z- 00.)**2 )*COS(THETA)                                                      
C           -(XZ+XD*(Z- 00.)+XR*(Z- 00.)**2 )*SIN(THETA)                                                      
C     WRITE(*,*) NPLA,(IPRES(KK),KK=1,NNDATA)                                                                 
C     F1=DY , F3=DX                                                                                           
C     X,Y INTERCEPTS AT Z=ZVERT                                                                               
C     TRACKS ALONG Y(X) AXIS DETERMINE X(Y) VERTEX.                                                           
C                                                                                                             
C     -------------------------------------------------------                                                 
C     SECTION TO CALCULATE 1/PZ FOR TEST PURPOSES                                                             
C     SIGN CHANGE NEEDED FOR 'X' PARABOLA TERM                                                                
      CALL   SHS(IHX+272,0,RPZ)                                                                        
C     IF(ABS(RPZ).LT.0.5)THEN                                                                                 
C     ENDIF                                                                                                   
***   WRITE(*,*)' FTZFIT  1/PZ  =  ',RPZ,S1,S2,SS                                                             
C     -------------------------------------------------------                                                 
C     CHECK VELOCITY DETERMINATION - VERTICAL WIRES -PLANAR                                                   
C     FIT  SLOPE                                                                                              
      CALL FTLFT(XXX,YYY,KK,0,SLPM,B,E)
C     PREDICTED SLOPE                                                                                         
      CALL SHS(IHX+281,0.,SLPP)                                                                        
      CALL SHS(IHX+282,0.,SLPM)                                                                        
C     WRITE(*,*)' SLOPE MEAS PRED  P',SLPM,SLPP,SLPM/SLPP                                                     
C     -------------------------------------------------------                                                 
C     ----AS FILTER FORTRAN  (IOSXA ) SOME LINES REMOVED  4/5/92                                              
C     CHECK VELOCITY DETERMINATION -  RADIAL                                                                  
C     MODULES WITHIN PLANAR SYSTEM ONLY                                                                       
C     FIRST POINT                                                                                             
C     LAST POINT                                                                                              
C     FIT  SLOPE                                                                                              
      CALL FTLFT(XXX,YYY,KK,0,SLPM,B,E)
C     PREDICTED SLOPE RELATIVE TO ANODE PLANE                                                                 
      CALL SHS(IHX+291,0.,SLPP)                                                                        
      CALL SHS(IHX+292,0.,SLPM)                                                                        
C     WRITE(*,*)' SLOPE MEAS PRED R',SLPM,SLPP,SLPM/SLPP                                                      
C     NEXT TWO LINES FOR FIT ALL SEGMENTS                                                                     
CO    MATOT=NTT*4                                                                                             
CO    IF(NTT.NE.0)CALL FXLFIT                                                                                 
*