C   11/07/79 412041827  MEMBER NAME  LGANAL0  (SOURCE)      FORTRAN
      SUBROUTINE LGANAL
C
C     S.YAMADA  31-10-78  14:35
C     LAST MODIFICATION    01-08-79  24:00  IZ=0&31 KILLED
C     LAST MODIFICATION    07-01-79  24:00  NEG ENERGY TO 20000
C                                                 Y.WATANABE
C.....THE #WORDS/CLUSTER IS NOW 16..  13/6/80     Y.WATANABE
C     LAST MOD.     04-12-84   18:25    WORK SPACE CLEAR (5200->5520)
C                                                 S.YAMADA
C---- MAIN SUBROUTINE FOR THE 1-ST STEP L-GLASS ANALYSIS.
C---- *******  MODIFIED FOR BOS *******
C
      IMPLICIT INTEGER *2 (H)
C
#include "cdata.for"
C
#include "clgwork1.for"
C---- NCLST = NO.OF THE LOCATED CLUSTERS.
C---- MAPCL(J)=THE INDEX OF THE FIRST MEMBER OF THE J-TH CLUSTER
C               IN THE HLGADC. (THE FIRST 51 WORDS OF CLMPRP)
C
      COMMON /CLGPRM/ ITH,MAXCLS,IRLTHD,IRLTH2,IRLTH3, ZVRTX,DZ
C
      COMMON /CLGMSB/ MSGVAL
C
      DATA NMLGCL/'LGCL'/,  LGANVR/1/,  MESS/0/
C
C
C---- FOR THE FIRST CALL PRINT THE LG-CLUSTER FINDING PARAMETERS.
      DATA IINIT/0/
      IF(IINIT.GT.0) GO TO 14
      IINIT=1
      IPLGCL=IBLN(NMLGCL)
14    CONTINUE
C
      IF(MESS) 1001,1000,1001
 1000 CALL LGMESG( 2, 0)
      MESS = 1
C
C
C---- FILL HEADER AND CONSTANTS IN THE GENERAL LG-INF. OF THE EVENT.
 1001 IDENT(1) = LGANVR
      IDENT(2) = IDATTM(DUM)
C
C---- CLEAR CLUSTER RESULTS ARRAY
      CALL SETSL(NCLST,0,5520,0)
C
C---- SET NO.OF WORDS/CLUSTER
      NWPCL = 16
C---- SET THE FLAG OF LG-ANALYSIS STEP-1.
      IFLAG(2) = 1
C
C*******************************************************************
C
C       LEAD GLASS CLUSTER FINDING
C
C     LG-ADC DATA IS MOVED FROM 'ALGN' TO /CWORK/.
      CALL LGDCPY( NP, IFLAG(1))
C
C
C---- CHECK IF THERE IS ANY LG-DATA.
      IF(NP.LE.0) GO TO 5
C---- IF DATA FORMAT ERROR IS FOUND,SKIP ANALYSIS AND MAKE AN EMPTY RES.
C     IFLAG(1)=1,IF NOT CALIBRATED.
C     IFLAG(1)=2,IF NOT ALL DATA ARE COPIED DUE TO THE BUFFER SIZE.
C     IFLAG(1)=1024,IF FORMAT CONVRSION IS NOT DONE.
C
      IF(IFLAG(1).GE.1024) GO TO 5
C
C#####KILL IZ=0&31 RINGS BECAUSE THEY ARE NOT REAL.
      NHIT=LNG-3
      IF(NHIT.LE.0) GO TO 85
      IF(HLGADC(1,1).GE.2688) HPOINT(2)=1
      DO 80 I=1,NHIT
      HPH=HLGADC(2,I)
      IF(HPH.LT.0) HLGADC(2,I)=20000
      IZ=HLGADC(1,I)
      IF(IZ.GT.2687) GO TO 80
      IZ=MOD(IZ,32)
      IF(IZ.EQ.0.OR.IZ.EQ.31) HLGADC(2,I)=0
C          PROTECTION HERE TO CORRECT NEGATIVE ENRGY DUE TO OVERFLOW.
80    CONTINUE
85    CONTINUE
C######
C---- CLUSTER FINDING    (NCLST,NCLBEC(1-3) ARE FILLED. MAP IS MADE.)
      CALL LGCCTL(IFLAG(1))
C
C---- CLUSTER POSITIONS AND ENERGIES ARE CALCULATED FOR THE BARREL AND
C     END CAPS.
      IF(NCLBEC(1)) 2,2,1
    1 CALL LGCLPB
    2 IF(NCLBEC(2)+NCLBEC(3)) 4,4,3
    3 CALL LGCLPC
C
C---- TOTAL ENERGY IS CALCULATED.
    4 ETOT(1) = ETOT(2)+ETOT(3)+ETOT(4)
C
C
C****************************************************************
C---- COPY LG-RESULT INTO A NEW BANK/LGCL/.
C
C---- FIX POINTERS OF THREE PARTS.
    5 NPNT(1) = 5
      NPNT(2) = 26
      NPNT(3) = NCLST+27
      NPNT(4) = NPNT(3)+NWPCL*NCLST
C
      IF(NCLST.EQ.0 .OR. NCLST.EQ. MAXCLS ) GO TO 10
C---- SHIFT DATA TO FILL THE GAP BETWEEN MAPCL AND CLSPRP
      CALL MVCL(CLMPRP(NCLST+2),0,CLMPRP(MAXCLS+2),0, 4*NCLST*NWPCL)
C
   10 LNGCL = NPNT(4)-1
C
C---- CREATE THE LG-CLUST BANK.
      NPLGCL=IDATA(IPLGCL)
      IF(NPLGCL.LT.1) GO TO 12
      MSGVAL = 1
      CALL LGMESG( 2, 3)
      CALL BDLS(NMLGCL,1)
C
12    CALL BCRE(NPLGCL,NMLGCL,1,LNGCL,&91,IER)
      CALL BSAW( 1, NMLGCL)
      IF(IER.EQ.0) GO TO 20
C
C---- BCRE ENDS WITH AN ERROR.
C     THE 'LGCL' EXISTS ALREADY.
      MSGVAL = IER
      CALL LGMESG( 2, 3)
C
C---- COPY
   20 CALL BSTR( NPLGCL, NPNT, LNGCL)
      GO TO 30
C
C---- NOT ENOUGH SPACE IN /BCS/.
   91 IFLAG(1) = IFLAG(1)+100
      MSGVAL = IER
      CALL LGMESG( 2, 2)
C
C---- OVERWRITE 'ALGN'-BANK WITH THE SORTED LG-ADC'S.
   30 IF(NP.GT.0) CALL MVCL(IDATA,4*NP,HNORD,0,4*LNG)
C
  100 RETURN
      END
