C   13/08/79 001181430  MEMBER NAME  LGCDIR   (SOURCE)      FORTRAN
      SUBROUTINE LGCDIR( NPPREC, NPR, NPCL)
C
C     S.YAMADA      06-11-78  15:20
C     LAST MODIFICATION  12-08-79  22:25
C     LAST MODIFICATION  18-01-80  14:30 BY Y.WATANABE
C              CALL TPCNST......... DO NOT CALL LGECOR IF MONTE CARLO.
C
C---- LEAD GLASS CLUSTER DIRECTION IS CALCULATED USING THE EVENT VERTEX.
C     ADATA(NPCLS+8--10) ARE FILLED. SEE BELOW.
C
C     THIS IS CALLED IN THE 2-ND STEP LG-ANALYSIS.
C    ******* BOS VERSION ********
C
C---- INPUT:
C         NPPREC=THE BOS POINTER TO THE PATTERN RECOGNITION RES.('PATR')
C                THE NEW FORMAT(VARIABLE LENGTH HEADER) IS ACCEPTED NOW.
C
C         NPR   =THE BOS POINTER TO THE RAW LG-ADC DATA ('ALGN')
C         NPCL  =THE BOS POINTER TO THE LG-RESULT BANK ('LGCL')
C
      IMPLICIT INTEGER *2 (H)
C
#include "cdata.for"
#include "clgwork2.for"
      COMMON /CLGDMS/ X0,RADIUS(6),RADSX0(6),THX0(4),
     $                ZEND(2),ZENDX0(2),ZWID(2),ZGAP(2),PHWID(2),
     $                ZECAP(4),ZECAPX(4),THECPX(2)
C
      COMMON /CLGPRM/ ITH,MAXCLS,IRLTHD,IRLTH2,IRLTH3, ZVRTX, DZVRTX
      COMMON /CLGMSB/ MSGVAL(5)
      COMMON /CLGCHG/ NCHIND,NSTEP,CXDCHG(9,100)
      DIMENSION JBCCHG(9,100)
      EQUIVALENCE (CXDCHG(1,1),JBCCHG(1,1))
C
C---- SEE  JADE COMPUTER-NOTE #14.
C     IDATA(NPCLS+1)=JBC, 0 FOR BARREL, -1 FOR BOTTOM, 1 FOR TOP
C     ADATA(NPCLS+2)=CLUSTER ENERGY IN GEV
C     ADATA(NCLST+3)=SIGMA(ENERGY)
C     ADATA(NPCLS+4)=WEIGHTED AVERAGE PHI
C     ADATA(NPCLS+5)=WEIGHTED AVERAGE Z
C     ADATA(NPCLS+6)=SIGMA PHI (WEIGHTED)
C     ADATA(NPCLS+7)=SIGMA Z (WEIGHTED)
C     IDATA(NPCLS+8)=NUMBER OF CORRESPONDING INNER TRACKS
C     ADATA(NPCLS+9-11)=DIRECTION COSINES CORRECTED FOR SHOWER DEPTH.
C
      INTEGER INDEX(3)/  9, 8,10/
C
      COMMON /CLGVRN/ NVRSN(20)
      DATA NVCODE/379090318/
C
C---- CHECK POINTERS
      IF(NPCL.LE.0) GO TO 90
C---- FIX POINTERS FOR CLUSTER DATA
      NPCGI = NPCL+IDATA(NPCL+1)-1
      NPCLS = NPCL+IDATA(NPCL+3)-1
C---- GET NO.OF CLUSTERS
      NCLST = IDATA(NPCGI+3)
C............ PUT IN BKGAUSS ETC...1-11-79
          NPHD = IBLN('HEAD')
          NPHD = IDATA(NPHD)
         CALL TPCNST ( NPHD )
         HRUN=HDATA(NPHD+NPHD+10)
C          USE IT TO DECIDE DATA OR MONTE CARLO.
C
      NVRSN(9) = NVCODE
C
C     CHECK IF LGANAL IS CALLED FOR THIS JOB
      IF(IDATA(NPCGI+17).EQ.2) CALL LGREIN(NPCL,NPR)
C
C---- CLEAR THE MATCH TABLE
      CALL SETSL( NCHCL2, 0,1448, 0)
C
C---- SET THE FLAG TO SHOW THAT THE 2-ND STEP ANALYSIS IS DONE.
      IDATA(NPCGI+17) = 2
C
C
      IF( NPR.LE.0 ) GO TO 4
C
C---- INNER TRACK CONNECTION IS DONE IF 'PATR' IS THERE.
      IF(NPPREC.LE.0) GO TO 4
C
      NCHPRE = IDATA(NPPREC+2)
      NLPTRC = IDATA(NPPREC+3)
      NPTR = NPPREC+1+IDATA(NPPREC+1)
C
C---- SKIP CH.TRACING,IF LG-CLUSTER IS NOT THERE.(TO SAVE TIME)
      IF( NCLST.GT.0 ) GO TO 6
      NCHCLS = NCHPRE
      NPOINT = 0
      NCHCL2 = NCHPRE
      GO TO 7
C
    6 CALL ILCTRC(NCHPRE,NLPTRC,IDATA(NPTR),ADATA(NPTR))
      CALL LGCHC2
C
C---- CONNECT SIMULATED CHARGE CLUSTERS AND OBSERVED ONES.
      NPMAP = NPCL+IDATA(NPCL+2)
      NPALGN = NPR*2+7
C
C---- NOTICE THAT IN THE INTOLG LG-ADC MAP IS TREATED AS HDATA(2,2).
      CALL INTOLG(NPCL,IDATA(NPMAP),HDATA(NPALGN))
C
C---- SET THE FLAG OF INNER TRACK CONNECTION
    7 IDATA(NPCGI+19) = 1
C
    4 IF( NCLST ) 100,100,8
C
C---- GET NO.OF WORDS FOR EACH CLUSTER.
    8 LSTEP = IDATA(NPCGI+21)
C
C---- VERSION NO. FOR ENERGY CORRECTION SUBROUTINE IS SET.
      CALL LGECR0( NVECOR )
C---- SHOWER ENERGY CORRECTION IS MADE
      IDATA(NPCGI+18) = NVECOR
      IF(NVECOR)42,42,41
C---- CLEAR TOTAL ENERGY TO MAKE THE SUM OF CORRECTED ENERGIES
41    CALL SETSL(ADATA(NPCGI+7),0,36,0.)
42    NEND = NCLST
      IF(NEND.GT.MAXCLS) NEND = MAXCLS
C
C---- VERTEX OF THE EVENT IS COPIED INTO ZVRTX
      CALL CLOC( NPVX, 'TPVX',1 )
      ZVRTX = 0.
      DZVRTX = 0.
      IF(NPVX.LE.0) GO TO 5
C----   SET THE VERTEX FLAG
      IDATA(NPCGI+20) = HDATA(2*NPVX+2)
      ZVRTX = ADATA(NPVX+4)
C----   LIMIT THE VERTEX POSITION RANGE FOR SAFETY.
      IF( ZVRTX.LT.ZEND(1) ) ZVRTX = ZEND(1)
      IF( ZVRTX.GT.ZEND(2) ) ZVRTX = ZEND(2)
      DZVRTX = ADATA(NPVX+7)
    5 CONTINUE
C
C
        DO 1 N=1,NEND
C----   CHARGE( NO.OF CONNECTED CHARGED TRACK*100+ THE INDEX OF THE 1-ST
C               CANDIDATE)
        IDATA(NPCLS+8) = HCLLSO(1,N)*100+HCLLSO(2,N)
      JEG=0
C....USE 1/2 OF COUNTER THICKNESS IF IT IS A NONSHOWERING PARTICLE
      IF(HCLLSO(1,N)-1) 206,202,204
202   ELG=ADATA(NPCLS+2)
      IF(ELG.GT.0.6) GO TO 204
      NC=HCLLSO(2,N)
      PC=CXDCHG(9,NC)
      IF(ELG.GT.0.75*PC) GO TO 204
      JEG=2
      GO TO 206
204   JEG=1
C
206     IPART = IDATA(NPCLS+1)
        IF(IPART) 10,2,10
C
C----   BARREL
    2   ZAV = ADATA(NPCLS+5)
C
C
C----   AVERAGE DEPTH OF THE SHOWER IS CALCULATED BY ITERATION.
        CALL LGAVDP(ZAV,ADATA(NPCLS+2)*1000.,JEG,DEP)
C
C----   SET DIRECTION COSINES
    3   ZZ = ADATA(NPCLS+5)-ZVRTX
        TR = SQRT((RADIUS(3)+DEP)**2+ZZ**2)
        RXY = (RADIUS(3)+DEP)/TR
        ADATA(NPCLS+9) = RXY*COS(ADATA(NPCLS+4))
        ADATA(NPCLS+10) = RXY*SIN(ADATA(NPCLS+4))
        ADATA(NPCLS+11) = ZZ/TR
        GO TO 20
C
C----   END CAP (JEG=1 FOR SAFTY REASON)
   10   JEG=1
        ZAV=ZECAP(IPART+2)
C----     AVERAGE DEPTH OF THE SHOWER IS CALCULATEDBY ITERATION
        CALL LGAVDE(ZAV,NPCLS,JEG,DEP)
        ZZ = ZECAP(IPART+2)-ZVRTX+DEP*FLOAT(IPART)
        TR = SQRT(ADATA(NPCLS+4)**2+ADATA(NPCLS+5)**2+ZZ**2)
        ADATA(NPCLS+9) = ADATA(NPCLS+4)/TR
        ADATA(NPCLS+10) = ADATA(NPCLS+5)/TR
        ADATA(NPCLS+11) = ZZ/TR
C
C----   ENERGY CORRECTION
   20   CONTINUE
        IF(HRUN.GT.0) CALL LGECOR( ADATA(NPCLS+1), DEP, IFLAG )
        CALL LGEERR( IPART, ADATA(NPCLS+2), ADATA(NPCLS+3))
C
C
        IF(NVECOR) 22,22,21
C----        SUM UP CORRECTED SHOWER ENERGY
   21   IP = INDEX( IPART+2 )
        ADATA(NPCGI+IP) = ADATA(NPCGI+IP)+ADATA(NPCLS+2)
C
C----        SUM UP GAMMA ENERGY
   22   IF(IDATA(NPCLS+8).NE.0) GO TO 30
C----        COUNT NO.OF GAMMAS
        IDATA(NPCGI+11) = IDATA(NPCGI+11)+1
        IP = INDEX( IPART+2 )+5
        ADATA(NPCGI+IP) = ADATA(NPCGI+IP)+ADATA(NPCLS+2)
C
   30   CONTINUE
C----        CHANGE THE POINTER FOR THE NEXT CLUSTER.
        NPCLS = NPCLS+LSTEP
    1   CONTINUE
C
C---- SUM UP ALL ENERGIES.
      ADATA(NPCGI+7) = ADATA(NPCGI+8)+ADATA(NPCGI+9)+ADATA(NPCGI+10)
      ADATA(NPCGI+12) = ADATA(NPCGI+13)+ADATA(NPCGI+14)+ADATA(NPCGI+15)
      GO TO 100
C
C---- WRONG POINTER
   90 MSGVAL(1) = NPPREC
      MSGVAL(2) = NPR
      MSGVAL(3) = NPCL
      CALL LGMESG( 7, 1)
C
  100 RETURN
      END
