C   16/07/79 011130328  MEMBER NAME  LGCALB   (SOURCE)      FORTRAN
      SUBROUTINE LGCALB(*)
C
C     S.YAMADA   09-03-79  10:50
C     LAST MODIFICATI0N  14-11-79  01:35 BY Y.WATANABE
C     LAST MODIFICATI0N  27-08-80  08:35 BY Y.WATANABE
C                INTERCHANGE 293 AND 613.
C     LAST MODIFICATI0N  12-11-80  13:35 BY Y.WATANABERUNS=4865-5666
C                INTERCHANGE AMP CABLE FOR THE LAST ADC OF THE BARREL.
C---- LG-ADC CALIBRATION SUB.TO SIMULATE THE LHO'S SYSTEM.
C
C---- TAKEDA'S  LGERSE IS CALLED TO FIX UP BAD CHANNELS
      IMPLICIT INTEGER *2 (H)
C
#include "cdata.for"
C
#include "clgwork1.for"
#include "clgkalb.for"
C
C     BOUNDARY LIST
      INTEGER*2 HCHLST(4)/0,2688,2784,2880/
      DATA INITP/0/
      IF(INITP.GT.0) GO TO 5
      INITP=1
      IP= IBLN('ALGL')
      IPNPHD= IBLN('HEAD')
5     CONTINUE
      LNG = 3
C     CALL CLOC( NP, 'ALGL',0 )
      NP=IDATA(IP)
      IF(NP.LE.0) RETURN
C---- IDATA(NP)=# OF DATA IN HDATA INCLUDING SUB-POINTERS
C               AND THE NORD-STATUS WORD.
      LNG = IDATA(NP)
      NNP = NP+NP
C
C
C---- EXAMINE IF FORMAT CHANGE IS ALREADY DONE. EXAMINE POINTERS
C
      IF(HDATA(NNP+3).NE.1) GO TO 110
      IPO = 1
      DO 10 K=1,3
      IPK = HDATA(NNP+K+3)
      IF(IPK.LT.IPO) GO TO 110
      IF(MOD(IPK,2).NE.1) GO TO 110
      IPO = IPK
10    CONTINUE
      IF((IPO-1)/2+3.NE.LNG) GO TO 110
C
C---- FORMAT IS ALREADY CHANGED.
C     DATA IS SIMPLY COPIED  AFTER THE LENGTH CHECK.
      LNGC = 4*LNG
      IF(LNGC.GT.11532) LNGC=11532
      CALL MVCL(HNORD,0,HDATA,4*NP,LNGC)
      IF(2883-LNG) 120,20,20
C
C---- CALIBRATION CHECK
20    IF(HNORML.EQ.0) GO TO 30
      WRITE(6,600) HNORML
600   FORMAT(' (LGCALB) LG-DATA IS ALREADY CALIBRATED.HNORML=',I6)
      GO TO 50
C
30    NHIT = LNG-3
      IF(NHIT.LE.0) GO TO 95
C
C---- SET THE NORMALIZATION FLAG
C     CF.  HNORM=1, FOR ONE-DIMENSIONAL M.C DATA
C     CF.  HNORM=2, FOR THREE-DIMENSIONAL M.C DATA
      NNORML = 3
C
C---- ERASE ALWAYS FIRED COUNTERS
50    HRUN = 0
C     IPNPHD = IBLN( 'HEAD' )
      NNPHD = IDATA(IPNPHD)
      NNPHD=NNPHD+NNPHD
      IF(NNPHD) 60,60,70
60    WRITE(6,610) NNPHD
610   FORMAT('0 ***** (LGCALB) ''HEAD'' IS MISSING, HNPHD=',I6,'  LGERSE
     $ IS NOT CALLED')
      GO TO 80
C
70    HEXP = HDATA(NNPHD+9)
      HRUN = HDATA(NNPHD+10)
      CALL LGERSE( HEXP,HRUN,&80 )
80    NP=NCAL(2)
      NPH=NP+NP+1
      J=0
C     INITIAL SET OF THE DETECTOR PART COUNTER KPOS
      KPOS=1
      DO 90 N=1,NHIT
      MEV=HLGADC(2,N)
      IF(MEV.LT.1) GO TO 90
      HCHN=HLGADC(1,N)
C..................INTERCHANGED CABLES.
      HCHO=HCHN
      IF(HCHN.EQ.293) HCHO=613
      IF(HCHN.EQ.613) HCHO=293
C...........AN AMP CABLE HAD BEEN INTERCHANGED OCT-NOV '80 RUNS
      IF(HRUN.LT.4992) GO TO 81
      IF(HRUN.GT.5667) GO TO 81
      IF(HCHN.GE.2664.AND.HCHN.LE.2687) HCHO=HCHN-24
      IF(HCHN.GE.2640.AND.HCHN.LE.2663) HCHO=HCHN+24
81    HCHN=HCHO
C..........................
      NP=NPH+HCHN+HCHN
      IPED=HCAL(NP)+8
      IPED=ISHFTR(IPED,4)
      MEV=MEV*HCAL(NP+1)+512
      MEV=ISHFTR(MEV,10)-IPED
      IF(MEV.LT.1) GO TO 90
      J=J+1
      IF(MEV.GT.32000) MEV=32000
C        IN CASE IT OVERFLOWS......
      HLGADC(2,J)=MEV
      HLGADC(1,J)=HCHN
C---  SET NEW POINTERS
82    IF(HCHN-HCHLST(KPOS)) 90,84,84
84    HPOINT(KPOS)=J+J-1
      KPOS=KPOS+1
      GO TO 82
90    CONTINUE
C        SET POINTERS CORRECTLY.
      DO 92 KK=KPOS,4
92    HPOINT(KK)=J+J+1
C
      LNG=J+3
      LNGC=LNG*4
      HNORML = HNORML + 10000
C
C---- CREATE THE NORMALIZED LG-ADC BANK.
95    LNG = LNGC/4
      CALL BDLS('ALGN',1)
      CALL BCRE(NPALGN,'ALGN',1,LNG,&140,IER)
      CALL BSAW( 1, 'ALGN')
      CALL BSTR( NPALGN, HNORD, LNG )
100   RETURN
C
C
C---- WRONG POINTERS
110   WRITE(6,620)
620   FORMAT(' *** WRONG DATA AFTER REFORMATTING ***')
C     CALL HPRS( 'HEAD',0)
C     CALL HPRS( 'ALGL',0)
      RETURN 1
C
C
C---- TOO LONG DATA
120   WRITE(6,630) LNG, LNGC,HEXP,HRUN,HDATA(NNPHD+11)
630   FORMAT(2X,20(1H*),'DATA IS TOO LONG******LNG,LNGC=',2I6,
     1 5X,'EXP#,RUN#,EV#=',3I8)
      DO 130 K=2,4
        IF( HPOINT(K).GT.5761 ) HPOINT(K) = 5761
130     CONTINUE
      RETURN 1
C
C---- ERROR IN CREATING 'ALGN'
140   WRITE(6,640) IER
640   FORMAT(' NOT ENOUGH SPACE IN /BCS/ TO PUT ALGN. IER=',5I5)
      RETURN 1
      END
