FVZFIT COMMENTS
*-- Author :    Stephen Burke   07/05/92
      SUBROUTINE FVZFIT
*-----------------------------------------Updates 21/09/93-------                                             
**: FVZFIT.......SB. Ignore CT z-vertex if z=0.0.                                                             
*-----------------------------------------Updates 07/09/93-------                                             
**: FVZFIT 40000 SB. Don't make a z-vertex if error is too big.                                               
*-----------------------------------------Updates 26/07/93-------                                             
**: FVZFIT 30907 SB. Change monitoring histograms.                                                            
**: FVZFIT 30907 RP. Farm changes.                                                                            
*-----------------------------------------Updates 13/10/92-------                                             
**: FVZFIT 30907 SB. Compare FT with CT z-vertex.                                                             
*-----------------------------------------Updates 29/07/92-------                                             
**: FVZFIT 30907 SB. New monitoring histograms.                                                               
*-----------------------------------------Updates 06/05/92-------                                             
**: FVZFIT 30907 SB. Bank FTGR added to the E-list:                                                           
*!: FTGR   30907 SB. New bank with forward z-vertex.                                                          
**: FVZFIT 30907 SB. New deck to perform forward z-vertex fit.                                                
*-----------------------------------------Updates----------------                                             
**********************************************************************                                        
*                                                                    *                                        
* Fit a z-vertex from forward tracks                                 *                                        
*                                                                    *                                        
* Makes FTGR/FTGX banks:                                             *                                        
*                                                                    *                                        
* !                                                                  *                                        
*  TABLE  FTGR                   ! z-vertex from forward tracks      *                                        
* !                                                                  *                                        
* !   ATTributes:                                                    *                                        
* !   -----------                                                    *                                        
* !COL ATT-name FMT Min    Max   ! Comments                          *                                        
* !                                                                  *                                        
*   1  Z        F  -200.   200.  ! z                                 *                                        
*   2  dZ       F     0.   200.  ! sigma(z)                          *                                        
*   3  CHISQ    F     0.   INF   ! Chi-squared                       *                                        
*   4  NDF      I     0    INF   ! (Number of tracks used) - 1       *                                        
* !                                                                  *                                        
* !    RELations:                                                    *                                        
* !    ----------                                                    *                                        
* !COL REL.bank  TYPE  INT.bank  !Comments                           *                                        
* !                    (COL)                                         *                                        
* !                                                                  *                                        
* !                                                                  *                                        
*  END TABLE                                                         *                                        
*                                                                    *                                        
* !                                                                  *                                        
*  TABLE  FTGX                   ! pointers from FTGR to FTKR        *                                        
* !                                                                  *                                        
* !   ATTributes:                                                    *                                        
* !   -----------                                                    *                                        
* !COL ATT-name FMT Min    Max   ! Comments                          *                                        
* !                                                                  *                                        
* !                                                                  *                                        
* !    RELations:                                                    *                                        
* !    ----------                                                    *                                        
* !COL REL.bank  TYPE  INT.bank  !Comments                           *                                        
* !                    (COL)                                         *                                        
* !                                                                  *                                        
*   62  FTKR     D1T1            ! FTKR tracks giving vertex         *                                        
* !                                                                  *                                        
* ! FTGX is a list of all FTKR tracks used to create the FTGR        *                                        
* ! vertices. Note that there is no pointer from FTGR to FTGX,       *                                        
* ! so it is necessary to use the NDF values to calculate the        *                                        
* ! pointers.                                                        *                                        
* !                                                                  *                                        
*  END TABLE                                                         *                                        
*                                                                    *                                        
**********************************************************************                                        
*KEEP,FVSTEE.                                                                                                 
*KEEP,FVPAR.                                                                                                  
*KEEP,FVSCAL.                                                                                                 
* Various counters                                                                                            
*KEEP,FVWBI.                                                                                                  
* Work bank indices                                                                                           
*KEEP,BCS.                                                                                                    
*KEEP,BOSMDL.                                                                                                 
C     ------BOSMDL                                                                                            
C     ------                                                                                                  
*KEEP,STFUNCT.                                                                                                
*     index of element before row number IROW                                                                 
*     index of L'th element  of row number IROW                                                               
*     L'th integer element of the IROW'th row of bank with index IND                                          
*     L'th real element of the IROW'th row of bank with index IND                                             
*KEND.                                                                                                        
**********************************************************************                                        
      INFTKR = NLINK('FTKR',0)
         CALL ERRLOG(511,'S:FVZFIT: FTKR bank not found')                                              
* Count events                                                                                                
* Zero work bank index                                                                                        
* Quick check to see if there are any forward tracks                                                          
* Create a work bank to store z values and weights                                                            
      CALL WBANK(IW,INFVWK,NFTKR,*9000)                                                                
      CALL HCDIR('//PAWC/FVFIT',' ')                                                                   
* Write primary/secondary flag into word 7 (IPTYPE)                                                           
      IF (LTRUTH) CALL FVTRUE(INFTKR)
* Get the nominal z-vertex                                                                                    
*                                                                                                             
* Loop over forward tracks, and calculate z0                                                                  
*                                                                                                             
* Extrapolate to vertex region and calculate z0 and weight                                                    
         CALL FVXTRP(RW(INDCR(INFTKR,1,JFT)),ZNOM,LPRIM,Z0,WZ0,IFAIL)
* Store in work bank                                                                                          
* If the weight is zero, the track will be ignored                                                            
* Take the weighted mean of the z values                                                                      
      CALL FVZWM(INFTKR,NFTKR,ZNOM,.TRUE.,FVVEC,IFAIL)
         CALL SHS(21,0,0.)                                                                             
* Create the FTGR bank ...                                                                                    
      INFTGR = NBANK('FTGR',0,6)
         CALL ERRLOG(512,'S:FVZFIT: Unable to create FTGR')                                            
* ... and fill it                                                                                             
      CALL UCOPY(FVVEC,IW(INFTGR+3),4)                                                                 
      CALL BLIST(IW,'E+','FTGR')                                                                       
      CALL BLIST(IW,'E+','FTGX')                                                                       
* Monitoring histograms                                                                                       
      CALL SHS(21,0,FLOAT(NDF+1))                                                                      
      CALL SHS(22,0,FVVEC(1))                                                                          
      CALL SHS(23,0,FVVEC(2))                                                                          
         CALL SHS(24,0,CHISQ/FLOAT(NDF))                                                               
         CALL SHS(25,0,CHPROB)                                                                         
         CALL SHS(24,0,-1.)                                                                            
         CALL SHS(25,0,-1.)                                                                            
* Compare with the CxKV z-vertex                                                                              
               CALL SHS(28,0,FVVEC(1)-CTZ)                                                             
     &            CALL SHS(29,0,(FVVEC(1)-CTZ)/FVVEC(2))                                               
      IF (LRESID) CALL FVCHEK(FVVEC)
* Now do "secondary" vertices                                                                                 
      CALL FVZWM(INFTKR,NFTKR,ZNOM,.FALSE.,FVVEC,IFAIL)
         INFTGR = NBANK('FTGR',0,2+4*NVERT)
            CALL ERRLOG(513,'S:FVZFIT: Unable to extend FTGR bank')                                    
         CALL UCOPY(FVVEC,IW(INDCR(INFTGR,1,NVERT)),4)                                                 
* Monitoring histograms                                                                                       
         CALL SHS(41,0,FLOAT(NDF+1))                                                                   
         CALL SHS(42,0,FVVEC(1))                                                                       
         CALL SHS(43,0,FVVEC(2))                                                                       
            CALL SHS(44,0,CHISQ/FLOAT(NDF))                                                            
            CALL SHS(45,0,CHPROB)                                                                      
            CALL SHS(44,0,-1.)                                                                         
            CALL SHS(45,0,-1.)                                                                         
* Truncate FTGX                                                                                               
      INFTGX = NLINK('FTGX',0)
      IF (INFTGX.GT.0) INFTGX = NBANK('FTGX',0,2+IW(INFTGX+2))
* Make sure work banks are dropped!                                                                           
      CALL WDROP(IW,INFVWK)                                                                            
* Reset IPTYPE to 2                                                                                           
*                                                                                                             
* Some CT diagnostics                                                                                         
*                                                                                                             
         CALL ERRLOG(514,'W:FVZFIT: No SVX bank')                                                      
         CALL ERRLOG(515,'W:FVZFIT: No primary vertex!')                                               
      CALL HFILL(100,DZ,0.,1.)                                                                         
* The FTGR bank must exist if possible                                                                        
      INFTGR = NLINK('FTGR',0)
         INFTGR = NBANK('FTGR',0,2)
            CALL BLIST(IW,'E+','FTGR')                                                                 
            CALL ERRLOG(516,'S:FVZFIT: Unable to create FTGR bank')                                    
      INFTGX = NLINK('FTGX',0)
         INFTGX = NBANK('FTGX',0,2)
            CALL BLIST(IW,'E+','FTGX')                                                                 
            CALL ERRLOG(517,'S:FVZFIT: Unable to create FTGX bank')                                    
      CALL ERRLOG(518,'S:FVZFIT: Work bank creation failed')                                           
*