C   27/07/79 C9081301   MEMBER NAME  TPVTX    (JADEGS)      FORTRAN
      SUBROUTINE TPVTX( NPPR )
C
C     S.YAMADA   18-07-79  09:00
C     LAST MODIFICATION   27-07-79  15:00
C                        MODIFIED FOR GRAPHICS USE, WITH /CWORK1/
C                                       J.OLSSON  29.07.79
C
C---- FIND THE VERTEX IN THE (X,Y) PROJECTION.
C
      COMMON /BCS/ IDATA(12000)
      DIMENSION ADATA(12000)
      EQUIVALENCE (IDATA(1),ADATA(1))
C
      COMMON /CWORK1/ MARK(40), CS(40),SN(40),X0(40),Y0(40), C(40),
     $               XV,YV, XX(40),YY(40),  XQ(40),
     $               A(40),B(40), A2B2(40), DIS2(40),
     $               NL, AL(40),BL(40), XYFIT(5), DIS2FT(40), IER, MODE
C
      COMMON /CTPVTX/ JCIRCL, RADMIN, DIS2MX,  NPCUT1,NPCUT2, COSCUT
      DATA  DMIN/1.E-20/, CNVVTX/4.E-4/, NTRYLM/10/
      DATA  RING2S/1.444E5/
C
      CMAX = 0.5/RADMIN
C
      XV = 0.
      YV = 0.
      IER = 0
      MODE = 0
      NTRY = 0
      DIS2LM = DIS2MX
        DO 400 K=1,5
  400   XYFIT(K) = 0.
C
C---- CHECK THE NUMBER OF INNER TRACKS.
      NTRK = IDATA(NPPR+2)
      IF(NTRK.LE.0) GO TO 100
C
C---- SET THE POINTER TO THE 1-ST TRACK
      NB = NPPR+IDATA(NPPR+1)
      NB0 = NB
      NSTEP = IDATA(NPPR+3)
C
C
        DO 1 I=1,NTRK
C----   COPY TRACK PARAMETERS
        CALL TPVTRC( I, NB)
C
C----   REMOVE LOW MOMENTUM TRACKS
        IF( 1.0-CS(I) ) 10,11,11
   10   IF( ABS(C(I))-RADMIN )  9,12,12
   11   IF( ABS(C(I))-CMAX ) 12,12,9
C
C----   CKECK NO.OF FITTED POINTS
C----   REMOVE TOO SHORT TRACKS IN THE FUTURE FITTING.
   12   IF(ADATA(NB+5)**2+ADATA(NB+6)**2-RING2S) 6,7,7
    6   IF(IDATA(NB+24)-NPCUT1) 9,8,8
    7   IF(IDATA(NB+24)-NPCUT2) 9,8,8
    8   MARK(I) = 0
        GO TO 1
    9   MARK(I) = -1
    1   NB = NSTEP+NB
C
C---- CALCULATE THE TANGENT LINE EQ.
    2 CALL TPVTNG(NTRK)
C
C---- FIT THE COMMON VERTEX
C     MOVE THE LINE PARAMETERS(A,B) TO AL AND BL ARRAYS.
      NL = 0
        DO 3 I=1,NTRK
        IF(MARK(I).LT.0) GO TO 3
C----   REMOVE TOO APART TRACKS.
   31   IF( DIS2(I)-DIS2LM ) 32,33,33
   32   NL = NL+1
        AL(NL) = A(I)
        BL(NL) = B(I)
        MARK(I) = 1
C///////////////////////////////
C       WRITE(6,6004) I,NL,AL(NL),BL(NL)
C6004   FORMAT(' I,NL,AL,BL=',2I4,2E10.3)
C///////////////////////////////
        GO TO 3
   33   MARK(I) = 0
C//////////////////////
C       WRITE(6,6005) I,DIS2(I), DIS2LM
C6005   FORMAT(' I,DIS2 =',I4,E12.3,'  DIS2LM=',E10.3)
C//////////////////////
    3   CONTINUE
C
C---- VERTEX FINDING PROGRAM
      IF( NL ) 100,100,40
   40 CALL TPVSLC( COSCUT )
      IF( MODE-2 ) 1000,61,62
C
C----  NEARLY PARARELL TWO TRACKS
   61 CALL TPVPRL
      GO TO 1000
C
      DATA SIGMA/1.0/
   62 CALL TPXYVX( NL, AL, BL, SIGMA, XYFIT, DIS2FT, IER)
C
C---- CHECK CONVERSION
      IF(NTRY.LT.1) GO TO 4
      RELDIF = (XV-XYFIT(1))**2+(YV-XYFIT(2))**2
C---- CHECK THE ABSOLUTE DIFFERENCE
      IF(RELDIF.LT.CNVVTX) GO TO 1000
      AVV = 0.25*((XV+XYFIT(1))**2+(YV+XYFIT(2))**2)
      IF(AVV.LE.0.) AVV = DMIN
      RELDIF = RELDIF/AVV
C---- CHECK THE RELATIVE DIFFERENCE
      IF( RELDIF.LT.CNVVTX) GO TO 1000
C
C---- REPEAT VERTEX FIT
    4 XV = XYFIT(1)
      YV = XYFIT(2)
      IF(XYFIT(5).GT.0.)   DIS2LM = 16.0*XYFIT(5)
      IF(DIS2LM.GT.DIS2MX) DIS2LM = DIS2MX
      IF(DIS2LM.LT.100.)   DIS2LM = 100.
      NTRY = NTRY+1
      IF(NTRY.GT.NTRYLM) GO TO 91
C---- RECALCULATE THE TANGENT LINES AT THE SHORTEST POINT FROM THE FIT
C     VERTEX.
C     GET THE CLOSEST POINT FROM THE VERTEX ON EACH TRACK.
        DO 5 I=1,NTRK
        IF( 1.0-CS(I) ) 50,55,55
   50   DX = XV-X0(I)
        DY = YV-Y0(I)
        DS = SQRT(DX**2+DY**2)
        IF(DS) 52,52,51
   51   RATIO = C(I)/DS
        XX(I) = X0(I)+RATIO*DX
        YY(I) = Y0(I)+RATIO*DY
        GO TO 5
C----   VERTEX IS ON THE CENTER. TRY  (0,0).
   52   XX(I) = 0.
        YY(I) = 0.
        GO TO 5
C
   55   AXBY = A(I)*XV-B(I)*YV
        XX(I) = A(I)*(AXBY+B(I)**2)/A2B2(I)
        YY(I) = B(I)*(-AXBY+A(I)**2)/A2B2(I)
    5   CONTINUE
      IF(IER) 1000,2,1000
C
C---- NO CONVERSION
   91 WRITE(6,691)
  691 FORMAT(' NO VERTEX CONV.')
C
C---- VERTEX POINT IS DEFINED IN THE (X,Y) PLANE.
 1000 NB = NB0
        DO 1001 I=1,NTRK
        X1 = ADATA(NB+5)
        Y1 = ADATA(NB+6)
        IF(A(I)*(X1-XYFIT(1))-B(I)*(Y1-XYFIT(2))) 1002,1003,1003
 1002   S = -1.
        GO TO 1004
 1003   S = 1.
 1004   A2B2(I) = SQRT(A2B2(I))
        A(I) = S*A(I)/A2B2(I)
        B(I) = -S*B(I)/A2B2(I)
        NB = NB+NSTEP
 1001   CONTINUE
  100 RETURN
      END
C
C
C*******************************************************************
C
      BLOCK DATA
C
      COMMON /CTPVTX/ JCIRCL, RADMIN, DIS2MX,  NPCUT1,NPCUT2, COSCUT
C
C---- INITIALIZATION OF PARAMETERS FOR THE VERTEX FITTING
C
C**** JCIRCL = FLAG TO FORCE CIRCLE PARAMETERS ALSO FOR PARABOLA FITTED
C              TRACKS.
      DATA JCIRCL/1/
C**** RADMIN = MIN.RADIUS OF THE TRACK TO BE USED FOR THE VERTEX
C                   FIT IN MM.
      DATA RADMIN/ 740./
C
C**** DIS2MX = SQUARED MAX.DIST.OF A TRACK IN MM**2
      DATA DIS2MX/1.0E4/
C**** NPCUT1 = MIN.NO. OF HITS IF THE TRACK STARTS IN THE R1-CHAMBER
      DATA NPCUT1/11/
C**** NPCUT2 = MIN.NO OF HITS ON THE TRACK IF IT STARTS AFTER THE R1.
      DATA NPCUT2/16/
C**** COSCUT = THE MAX.<ABS(COS(AL))> OF ALL SELECTED TRACK PAIRSIN
C              (X,Y) PROJECTION TO START A FIT.
C              IF <ABS(COS(AL))>.GT.COSCUT,THE AVERAGE FOOT-POINTS ARE
C              TAKEN.
      DATA COSCUT/0.90/
      END
      SUBROUTINE TPVTRC( I, NB )
C
C     S.YAMADA  18-07-79  18:55
C     LAST MODIFICATION  25-07-79   00:25
C
C---- COPY THE TRACK FIT PARAMETERS OF THE I-TH TRACK FROM 'PATR' TO
C     THE /CWORK1/.
C
      COMMON /BCS/ IDATA(12000)
      DIMENSION ADATA(12000)
      EQUIVALENCE (IDATA(1),ADATA(1))
C
C
      COMMON /CWORK1/ MARK(40), CS(40),SN(40),X0(40),Y0(40), C(40),
     $               XV,YV, XX(40),YY(40),  XQ(40),
     $               A(40),B(40), A2B2(40), DIS2(40),
     $               NL, AL(40),BL(40), XYFIT(5), DIS2FT(40), IER, MODE
C
      COMMON /CTPVTX/ JCIRCL, RADMIN, DIS2MX,  NPCUT1,NPCUT2, COSCUT
C
C---- BOTH PARABOLA AND CIRCLE FIT MAY BE USED.
C     IF(JCIRCL>0) PARABOLA PARAMETERS ARE CONVERTED INTO CIRCLE ONES.
      IF(IDATA(NB+18)-1) 20,20,21
C
C----   CIRCLE FIT CASE
   20 CS(I) = 10.
      SN(I) = 0.
      RAD = ADATA(NB+19)
      IF(ABS(RAD).LT.DMIN ) RAD = SIGN(DMIN,RAD)
      C(I) = 1.0/RAD
      DIS = C(I)+ADATA(NB+20)
      X0(I) = DIS*COS(ADATA(NB+21))
      Y0(I) = DIS*SIN(ADATA(NB+21))
      GO TO 22
C
C----   PARABOLA CASE
   21 IF(JCIRCL) 30,30,31
   30 CS(I) = COS(ADATA(NB+19))
      SN(I) = SIN(ADATA(NB+19))
      X0(I) = ADATA(NB+20)
      Y0(I) = ADATA(NB+21)
      C(I) = ADATA(NB+22)
      GO TO 22
C
C---- PARABOLA TO CIRCLE CONV.
   31 CS(I) = 10.0
      SN(I) = 0.
      IF(ADATA(NB+22)) 32,33,32
   32 C(I) = 0.5/ADATA(NB+22)
      GO TO 34
   33 C(I) = 1.E6
   34 X0(I) = ADATA(NB+20) - C(I)*SIN(ADATA(NB+19))
      Y0(I) = ADATA(NB+21) + C(I)*COS(ADATA(NB+19))
      C(I)  = ABS( C(I) )
C
C
   22 XX(I) = XV
      YY(I) = YV
C
      RETURN
      END
      SUBROUTINE TPVTNG(NTRK)
C
C     S.YAMADA  21-03-79  14:25
C     LAST MODIFICATION    15-05-79  20:50
C
C---- OBTAIN THE TANGENT LINE EQUATIONS OF THE FITTED CURVES.
C
C---- INPUT:  NTRK=NO.OF TRACKS FILLED INTO THE COMMON.
C             CS,SN,X0,Y0,C,XX,YY  MUST BE FILLED
      COMMON /CWORK1/ MARK(40), CS(40),SN(40),X0(40),Y0(40), C(40),
     $               XV,YV, XX(40),YY(40),  XQ(40),
     $               A(40),B(40),A2B2(40), DIS2(40),
     $               NL, AL(40),BL(40), XYFIT(5), DIS2FT(40)
C
C---- IN CASE OF CIRCLE FIT
C     CS > 1.00, SHOWS THAT THE FIT PARAMETERS ARE FOR CIRCLE FIT
C     SN         NOT USED
C     X0,Y0      CENTER COORDINATE.
C     C          RADIUS
C     --- EQUATION ----    (X-X0)**2+(Y-Y0)**2=C**2
C     XV,YV: VERTEX POINT
C     XX,YY      POINT TO CALULATE THE TANGENT
C
C---- IN CASE OF PARABOLA FIT
C     CS,SN:SIN AND COS OF THETA(PARABOLA INCLINATION ANGLE)
C     X0,Y0:XQ=0,YQ=0 POINT IN THE (X,Y) COORDINATE.
C     C:    PARABOLA PARAMETER  YP=C*XP**2
C     XV,YV: VERTEX POINT
C     XX,YY: POINT TO CALULATE XQ,WHERE THE PARABOLA TANGENT IS OBTAINED
C     XQ:    AT (XQ,XQ**2) THE TANGENT IS CALCULATED.
C
C---- OUTPUTS
C
C     A,B:   THE TANGENT LINE EQUATION IN THE (X,Y) COORDINATE.
C     DIS2:  DISTANCE FROM THE VERTEX TO THE TANGENT LINE.
C
      DATA DMIN/1.E-20/
C
      IF(NTRK.LE.0) RETURN
        DO 1 I=1,NTRK
        IF(CS(I).LE. 1.0) GO TO 2
C----   CASE OF CIRCLE FIT
        DX = XX(I)-X0(I)
        IF(ABS(DX).LT.DMIN ) DX = SIGN(DMIN,DX)
        DY = YY(I)-Y0(I)
        IF(ABS(DY).LT.DMIN ) DY = SIGN(DMIN,DY)
        DS = SQRT(DX**2+DY**2)
        DD = DS*C(I)+DX*X0(I)+DY*Y0(I)
        GO TO 3
C
C----   PARABOLA CASE
    2   XQ(I) = (XX(I)-X0(I))*CS(I)+(YY(I)-Y0(I))*SN(I)
C
C----   TANGENT AT XP=XQ,   YP = C*XQ*(2.*XP - XQ)
C
C----   THE SAME LINE IN THE (X,Y) COORDINATE, X/A+Y/B=1.0
C
        DX = 2.0*C(I)*XQ(I)*CS(I)+SN(I)
        IF( ABS(DX).LT.DMIN ) DX = SIGN(DMIN,DX)
        DY = 2.0*C(I)*XQ(I)*SN(I)-CS(I)
        IF( ABS(DY).LT.DMIN ) DY = SIGN(DMIN,DY)
        DD = DX*X0(I) + DY*Y0(I) + C(I)*XQ(I)**2
    3   A(I) = DD/DX
        B(I) = DD/DY
        B2 = B(I)**2
        A2 = A(I)**2
        A2B2(I) = A2+B2
        IF( A2B2(I).LT.DMIN ) A2B2(I) = DMIN
        DIS2(I) = (A(I)*B(I)-B(I)*XV-A(I)*YV)**2/A2B2(I)
C////////////////////
C
C       WRITE(6,6002) XX(I),YY(I),X0(I),Y0(I),CS(I),SN(I),XQ(I),
C    $              A(I),B(I),DIS2(I)
C6002   FORMAT(' XX,YY,X0,Y0,CS,SN,XQ,A,B,DIS2=',10E10.3)
C////////////////////////
C
    1   CONTINUE
      RETURN
      END
      SUBROUTINE TPVSLC( COSCUT )
C
C     S.YAMADA  01-04-79  18:05
C     LAST MODIFICATION   12-07-79  10:30
C
C---- SELECT THE MODE OF VERTEX FITTING.
C
      COMMON /CWORK1/ MARK(40), CS(40),SN(40),X0(40),Y0(40), C(40),
     $               XV,YV, XX(40),YY(40),  XQ(40),
     $               A(40),B(40), A2B2(40), DIS2(40),
     $               NL, AL(40),BL(40), XYFIT(5), DIS2FT(40), IER, MODE
      DIMENSION SQA2B2(40)
C
      MODE = NL
C
      IF(NL-2) 100,1,10
C
C---- CHECK PARAREL OR NOT.
    1 COST = ABS(AL(1)*AL(2)+BL(1)*BL(2))
      COST = COST/SQRT((AL(1)**2+BL(1)**2)*(AL(2)**2+BL(2)**2))
      IF(COST-COSCUT) 40,40,100
C
   10 MODE = 1
C---- CALCULATE THE AVERAGE COST
      NL1 = NL-1
      SCOST = 0.
      CONT = 0.
        DO 20 I=1,NL
        SQA2B2(I) = SQRT(AL(I)**2+BL(I)**2)
        IF(SQA2B2(I).EQ.0.) SQA2B2(I) = 1.E-20
   20   CONTINUE
        DO 11 I=1,NL1
        J1 = I+1
          DO 12 J=J1,NL
          COST = ABS(AL(I)*AL(J)+BL(I)*BL(J))/(SQA2B2(I)*SQA2B2(J))
          SCOST = SCOST+COST
   12     CONT = CONT+1.
   11   CONTINUE
      IF(CONT) 100,100,30
   30 COST = SCOST/CONT
      IF(COST-COSCUT) 40,40,50
   40 MODE = 3
      GO TO 100
   50 MODE = 2
  100 RETURN
      END
      SUBROUTINE TPVPRL
C
C     S.YAMADA  01-04-79  18:25
C     LAST MODIFICATION  02-04-79   11:05
C
C---- CALCULATE THE AVERAGE CLOSEST POINT FROM THE BEAM TO 2-TRACKS.
C     CALLED IN TPVTX1
C
C
      COMMON /CWORK1/ MARK(40), CS(40),SN(40),X0(40),Y0(40), C(40),
     $               XV,YV, XX(40),YY(40),  XQ(40),
     $               A(40),B(40), A2B2(40), DIS2(40),
     $               NL, AL(40),BL(40), XYFIT(5), DIS2FT(40), IER
C
        DO 4 K=1,4
    4   XYFIT(K) = 0.
      CNT = 0.
C
      IF(NL.LE.1) GO TO 90
C
        DO 5 I=1,NL
        B2 = BL(I)**2
        A2 = AL(I)**2
        ASBS = A2+B2
        IF(ASBS.LE.0.) ASBS = 1.E-20
        AB = AL(I)*BL(I)
        XS = AB*BL(I)/ASBS
        YS = AB*AL(I)/ASBS
        XYFIT(1) = XYFIT(1)+XS
        XYFIT(2) = XYFIT(2)+YS
        XYFIT(3) = XYFIT(3)+XS**2
        XYFIT(4) = XYFIT(4)+YS**2
        CNT = CNT+1.
    5   CONTINUE
C
C---- TAKE THE AVERAGE
      IF(CNT.LE.0.) CNT=1.
      XYFIT(1) = XYFIT(1)/CNT
      XYFIT(2) = XYFIT(2)/CNT
      XYFIT(3) = XYFIT(3)/CNT-XYFIT(1)**2
      XYFIT(4) = XYFIT(4)/CNT-XYFIT(2)**2
      IF(XYFIT(3).LT.0.) XYFIT(3) = 0.
      XYFIT(3) = SQRT(XYFIT(3))
      IF(XYFIT(4).LT.0.) XYFIT(4) = 0.
      XYFIT(4) = SQRT(XYFIT(4))
      IER = 0
      RETURN
C
   90 IER = 1
      RETURN
      END
      SUBROUTINE TPXYVX( NL, A, B, SIGMA, XYVTX, DIS2, IER)
C
C      S.YAMADA    07-01-78  21:05
C     LAST MODIFICATION  10-01-79   09:15
C
C---- CALCULATE THE COMMON VERTEX FOR NL-LINES IN THE X-Y PLANE
C
C---- INPUTS
C     NL;        NO.OF INPUT LINES(>1)
C     A(I),B(I); X,Y INTERCEPTS OF THE I-TH LINE.
C     SIGMA;     EXPECTED DEVIATION FOR A LINE.
C
C---- OUTPUTS
C     XYVTX(1);   X COORDINATE OF THE LEAST SQUARE FITTEDVERTEX POINT.
C     XYVTX(2);   Y    "
C     XYVTX(3);   ERROR FOR X
C     XYVTX(4);   ERROR FOR Y
C     XYVTX(5);   SUM(DIS2)/SIGMA2       A KIND OF CHI-SQUARE

C                          VERTEX POINT
C     DIS2;    SQUARED DISTANCES FROM THE (XYVTX) TO THE INPUT LINES.
C     IER;     ERROR FLAG, =0 IF NOERROR.
C
      DIMENSION A(1),B(1),  XYVTX(2),DIS2(1)
C
      IF(NL.LE.1) GO TO 90
        DO 400 K=1,5
  400   XYVTX(K) = 0.
C
      SB2 = 0.
      SA2 = 0.
      SAB = 0.
      SA2B = 0.
      SAB2 = 0.
        DO 1 N=1,NL
        B2 = B(N)**2
        A2 = A(N)**2
        C = A2+B2
        IF(C.LE.1.E-30) C=1.E-30
        RC = 1./C
        DIS2(N) = RC
        AB = A(N)*B(N)
        SB2 = SB2+B2*RC
        SA2 = SA2+A2*RC
        SAB = SAB+AB*RC
        SA2B = SA2B+A2*B(N)*RC
        SAB2 = SAB2+AB*B(N)*RC
    1   CONTINUE
C
C---- THE EQUATION FOR X,Y
C     SB2*X + SAB*Y = SAB2
C     SAB*X + SA2*Y = SA2B
C
      C = SA2*SB2-SAB**2
      IF(C.EQ.0.0) GO TO 91
      XYVTX(1) = (SAB2*SA2-SA2B*SAB)/C
      XYVTX(2) = (SA2B*SB2-SAB2*SAB)/C
      IER = 0
C
C---- ERRORS OF (X,Y) ARE SET
      IF(SB2.GT.0.) XYVTX(3) = SIGMA/SQRT(SB2)
      IF(SA2.GT.0.) XYVTX(4) = SIGMA/SQRT(SA2)
C
C
C---- DISTANCE**2 FROM THE XY-VERTEX TO EACH INPUT LINE IS CALCULATED.
      SDIS2 = 0.
        DO 2 N=1,NL
        DIS2(N) = DIS2(N)*(A(N)*B(N)-B(N)*XYVTX(1)-A(N)*XYVTX(2))**2
        SDIS2 = SDIS2+DIS2(N)
    2   CONTINUE
      XYVTX(5) = SDIS2/SIGMA**2/(NL-1)
      RETURN
C
   90 WRITE(6,690) NL
  690 FORMAT(' TOO FEW LINES FOR TPXYVX-INPUTS,  NL=',I2)
      IER = 1
      RETURN
   91 IER = 2
      RETURN
      END
