FPFSSG COMMENTS
*-- Author :    R. Henderson   24/10/94
      SUBROUTINE FPFSSG(NSMLS)
C-----------------------------------------------------------------------                                      
C                                                                                                             
C---  Routine finds 'SECONDARY' segments from two Clusters/Planes                                             
C---  + any number of previously unused digits.                                                               
C---  Firstly arrays are calculated which                                                                     
C---  flag clusters which have not already been used in segments                                              
C---  formed from three clusters.                                                                             
C---         (IUCLU(IPLANE,MAXCLU) = 0 if unused)                                                             
C---  Then for unused digits that have not yet been placed in a cluster.                                      
C---         (IUD(MAXHTS,NUMWPL) = 0 if not used)                                                             
C---                                                                                                          
C---  A search is made for all candidate line segments formed from                                            
C---  two unused clusters to find any that have digits (the information                                       
C---  from which can be expressed as a line) in the orientation not                                           
C---  yet contributing  with a Distance of Closest                                                            
C---  Approach of less than DDTOL. The closest digit is included in the                                       
C---  segment together with any which are within DIGTOL of the closest.                                       
C---                                                                                                          
C---  A new cluster is formed using the newly added digit(s).( NB.                                            
C---  If the cluster is formed of only one digit the cluster no longer                                        
C---  represents a plane.) The number of these created is stored in                                           
C---  COMMON/FPSTSG/ as  NSTC(9). Otherwise the normal Cluster counter                                        
C---  is incremented to include all clusters new and old.                                                     
C---                                                                                                          
C---  The new segments are fitted using FPFSTS. A count of the new                                            
C---  secondary segments is kept in COMMON/FPSTSG/ as NFSSEG(9) but                                           
C---  otherwise the normal counter NFSEG(9) is incremented to include                                         
C---  these new segments.                                                                                     
C---                                                                                                          
C---  A new disconnected set is found for all segments by FPSSGF.                                             
C---                                                                                                          
C---  COMMON/FPSTSG/NSTC(9)   --- Number of secondary clusters formed.                                        
C---                NFSSEG(3) --- Number of secondary Segments formed.                                        
C---                NFTSEG(3) --- Number of Tertiary segments formed.                                         
C---                                                                                                          
C---  The segments are ordered so all primary segments preceed                                                
C---  secondary and finary secondary preceed Tertiary)                                                        
C---                                                                                                          
C-----------------------------------------------------------------------                                      
C                                                                                                             
C                                                                                                             
*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---                                                                                                          
*KEND.                                                                                                        
C---                                                                                                          
*KEEP,FPH1WRK.                                                                                                
C--    *KEEP,FPCSEG.                                                                                          
C---                                                                                                          
C---                                                                                                          
C--    *KEEP,FPDIGI.                                                                                          
C---                                                                                                          
C--    *KEEP,FPDGI.                                                                                           
C---                                                                                                          
C--    *KEEP,FPSTID.                                                                                          
C---                                                                                                          
C--    *interface to real data                                                                                
C---.                                                                                                         
*KEEP,FPLSEG.                                                                                                 
C---                                                                                                          
C---                                                                                                          
*KEEP,FPLGEO.                                                                                                 
C---                                                                                                          
C---                                                                                                          
*KEEP,FPSTSG.                                                                                                 
C---                                                                                                          
*KEEP,FPCLUS.                                                                                                 
C---                                                                                                          
*KEND.                                                                                                        
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C---  Initialize arrays                                                                                       
C                                                                                                             
      CALL VZERO(IUD,MAXHTS*NUMWPL)                                                                    
C                                                                                                             
C---  Establish a list of clusters and digits available                                                       
C---  to form secondary segments                                                                              
C                                                                                                             
C                                                                                                             
C---  Firstly Digits                                                                                          
C                                                                                                             
C---  Loop over supermodules                                                                                  
C                                                                                                             
C                                                                                                             
C---  Loop over candidate line segments                                                                       
C                                                                                                             
C                                                                                                             
C---  Loop on clusters that form CANDIDATE line segments                                                      
C                                                                                                             
C                                                                                                             
C---  Find the planes.clusters numbers that make them up                                                      
C                                                                                                             
C                                                                                                             
C---  Loop on four wires in cluster                                                                           
C                                                                                                             
C                                                                                                             
C---  Set flag in IUD (Used Digits) to +/-1 from 0                                                            
C     (SIGN = DRIFT DIRECTION)                                                                                
C                                                                                                             
C---                                                                                                          
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C                                                                                                             
C---  Create list of used/unused clusters                                                                     
C                                                                                                             
      CALL VZERO(IUCLU,9*MAXCLU)                                                                       
C                                                                                                             
C---  Loop over supermodules                                                                                  
C                                                                                                             
C                                                                                                             
C---  Loop over segments                                                                                      
C                                                                                                             
C---  Remove those outside disconnected set                                                                   
C                                                                                                             
C                                                                                                             
C---                                                                                                          
C                                                                                                             
C                                                                                                             
C---    Set IUCLU to 1 if used                                                                                
C                                                                                                             
C---                                                                                                          
C                                                                                                             
C---  Having established the unused CLS and Digits find all                                                   
C---  distances of closest approach                                                                           
C                                                                                                             
      CALL VZERO(NFSSEG,3)                                                                             
      CALL VZERO(NFTSEG,3)                                                                             
C                                                                                                             
C---  Loop over supermodule                                                                                   
C                                                                                                             
C                                                                                                             
C---  Loop on candidate line segments                                                                         
C                                                                                                             
*                                                                                                             
* SB mod to help with large events                                                                            
*                                                                                                             
            CALL ERRLOG(216,'W:FPFSSG: .GT. MAXSEG segments found')                                    
C                                                                                                             
C---  Calculate the angle wrt to axis of candidate line segment                                               
C                                                                                                             
C                                                                                                             
C---                                                                                                          
C                                                                                                             
C                                                                                                             
C---  Check that neither plane in candidate line segment used                                                 
C                                                                                                             
C                                                                                                             
C---  Calculate which wires are to be searched for digits                                                     
C---  They must be the wires which have not contributed to CLS                                                
C                                                                                                             
*                                                                                                             
* SB mod to help with large events                                                                            
*                                                                                                             
     &      CALL ERRLOG(214,'W:FPFSSG: Cluster limit reached')                                         
C                                                                                                             
C                                                                                                             
C---  Calculate w of cluster in missing orientation                                                           
C                                                                                                             
C---                                                                                                          
C---                                                                                                          
C---                                                                                                          
C                                                                                                             
C---  Now find unused digitizations on these wires                                                            
C                                                                                                             
      CALL VZERO(DMINW,4)                                                                              
      CALL VZERO(IDIGM,4)                                                                              
C---                                                                                                          
C---                                                                                                          
C                                                                                                             
C---  Loop over all digits on wires                                                                           
C                                                                                                             
C                                                                                                             
C                                                                                                             
C---  Remove any digits already used                                                                          
C                                                                                                             
C                                                                                                             
C---  Loop on sign of drifts                                                                                  
C                                                                                                             
C---                                                                                                          
*      DRIFT = (DRIW(IDIG,IWW)*(-1.0)**(ISD-1) + DW(IDIG,IWW)) * 10.0                                         
C---                                                                                                          
C                                                                                                             
C---  Now make vectors TO and PARALLEL to digit information                                                   
C                                                                                                             
C---                                                                                                          
C---                                                                                                          
C                                                                                                             
C---  Make vector TO and PARALLEL to CLS                                                                      
C                                                                                                             
C---                                                                                                          
C                                                                                                             
C---  Now calculate distance of closest approach of digit to CLS                                              
C                                                                                                             
C      CALL FPDCA(TODIG,VDIG,TOCLS,VCLS,DIST)                                                                 
C New, hopefully faster version                                                                               
      CALL FPNDCA(TODIG,VDIG,TOCLS,VCLS,DIST)
C---                                                                                                          
C---                                                                                                          
C---                                                                                                          
C                                                                                                             
C---  Store distance to closest digit on wire plane                                                           
C                                                                                                             
C                                                                                                             
C---  Find closest                                                                                            
C                                                                                                             
      CALL VZERO(IWLIS,4)                                                                              
C---                                                                                                          
C                                                                                                             
C---  Find any within a tolerance of closest                                                                  
C                                                                                                             
C                                                                                                             
C---  Create secondary clusters and segments                                                                  
C                                                                                                             
C                                                                                                             
C---  Create new clusters                                                                                     
C                                                                                                             
                   CALL ERRLOG(214,'W:FPFSSG: Cluster limit reached')                                  
                   CALL VZERO( TCYUV(1,IPMISS,NTC(IPMISS)),4)                                          
                   CALL VZERO(TCYUVW(1,IPMISS,NTC(IPMISS)),4)                                          
                   CALL VZERO(IDGISM(1,IPMISS,NTC(IPMISS)),4)                                          
*                       TCYUV(IWW,IPMISS,NTC(IPMISS)) =                                                       
*     1                 (DRIW(IDIG,IWIR)*(-1.0)**(ISGN-1) +                                                   
*     2                 DW(IDIG,IWIR)) * 10.0                                                                 
C                                                                                                             
C---  Fit newly created cluster with candidate line segment                                                   
C---  to form new (secondary segment)                                                                         
C                                                                                                             
            CALL FPFSTS(ID1,ID2,ID3,ISM,PCHI,CHISQ,NDF,PARSGN,ERRSGN)
C                                                                                                             
C---  Reform disconnected segment sets                                                                        
C                                                                                                             
      CALL FPSSGF(.TRUE.)
*