*****************************************************************************
      INTEGER FUNCTION CALOL2CM()
c
      IMPLICIT NONE
C
      INCLUDE 'INTEST.TXT'
C
      REAL PIANO(22), VARFIT(2)
      REAL SHIFT
      REAL TG(2), BAR(2,NPLA)
      REAL DISTX, DISTY, Y(NPLA), YY(NPLA)
      REAL RIG, RMASS
      REAL CHECK, ENER, CX, CY
      REAL EINF, ESUP,ENPLANEX,ENPLANEY,TENPLAX,TENPLAY
      REAL hmemor(9000000)
c
      INTEGER gtr
      INTEGER j, m, nn, i
      INTEGER IBAR(2,NPLA), NPFIT(2)
      INTEGER Iquest(100), IWPL(2),CHTRACK
c
      COMMON / TAGLIOEN / EINF, ESUP, ENER(2)
      SAVE / TAGLIOEN /
C
      COMMON / SHIFT / SHIFT
      SAVE / SHIFT /
C
      COMMON / ANGOLO / BAR, IBAR
      SAVE / ANGOLO /
C
      COMMON / WHERE / CX, CY, PIANO
      SAVE / WHERE /
C
      COMMON / GENERAL / RIG, RMASS
      SAVE / GENERAL /
C
      COMMON / CH / CHECK
      SAVE / CH /
C
      COMMON / CALOFIT / VARFIT, NPFIT, IWPL,CHTRACK
      SAVE / CALOFIT /
C
      COMMON / pawcd / hmemor
      save / pawcd /
C
      Common / QUESTd / Iquest
      save / questd /
C
C Begin !
C
      CALOL2CM = 0;
      RMASS = 0.938
C
C Z position of Y planes, Y1 = 0., Y2 = -8.09, Y3 = -18.18, ...
C
      PIANO(1) = 0.
      DO I = 2, 22
         IF ( MOD(I,2).EQ.0 ) THEN 
            PIANO(I) = PIANO(I-1) - 8.09
         ELSE
            PIANO(I) = PIANO(I-1) - 10.09
         ENDIF 
      ENDDO
C
      CALL VZERO(DEXY,2*LENSEV)
      CALL VZERO(BAR,2*NPLA)
      CALL VZERO(IBAR,2*NPLA)
      CALL VZERO(CBAR,2*NPLA)
      CALL VZERO(CIBAR,2*NPLA)
      CALL VZERO(QQ,4)
      CALL VZERO(Y,NPLA)
      CALL VZERO(YY,NPLA)      
      VARFIT(1) = 0.
      VARFIT(2) = 0.
      NPFIT(1) = 0.
      NPFIT(2) = 0.
      VARCFIT(1) = 0.
      VARCFIT(2) = 0.
      NPCFIT(1) = 0.
      NPCFIT(2) = 0.
      ELEN = 0.
      SELEN = 0.
      NSTRIP = 0.
      QTOT = 0.
      NX22 = 0.
      QX22 = 0.
      QMAX = 0.
      GTR = 0
      CHTRACK = 0
C     
      DISTX = 0.
      DISTY = 0.
C
      PLANEMAX(1) = 0
      PLANEMAX(2) = 0
      TENPLAX = 0. 
      TENPLAY = 0. 
C
      IF (GOOD2.EQ.0.AND.TRIGTY.LT.2) goto 9696
C
C     CALCULATE QTOT, NSTRIP, QQ, NX22, QX22, QMAX AND PLANEMAX
C
      DO I = 1,22
         ENPLANEY = 0.
         ENPLANEX = 0.         
         DO J = 1,96
            IF ( MOD(I,2).NE.0 ) THEN
               IF ( ESTRIP(2,I,J).GT.EMIN ) THEN
                  DEXY(2,I,J) = ESTRIP(2,I,J)
                  ENPLANEY = ENPLANEY + DEXY(2,I,J)
                  NSTRIP = NSTRIP + 1.
                  QTOT = QTOT + DEXY(2,I,J)
                  IF (I.LT.11) QQ(1) = QQ(1) + DEXY(2,I,J)
               ENDIF
               IF ( ESTRIP(1,I,J).GT.EMIN ) THEN
C               IF ( ESTRIP(1,I+1,J).GT.EMIN ) THEN
C                  
c                  print *,' i ',i,' i + 1 ',I+1
C                  DEXY(1,I,J) = ESTRIP(1,I+1,J)
                  DEXY(1,I,J) = ESTRIP(1,I,J)
C
                  ENPLANEX = ENPLANEX + DEXY(1,I,J)
                  NSTRIP = NSTRIP + 1.
                  QTOT = QTOT + DEXY(1,I,J)
                  if (i.lt.11) QQ(2) = QQ(2) + DEXY(1,I,J)
c                  IF (I.EQ.21) THEN
c                     NX22 = NX22 + 1.
c                     QX22 = QX22 + DEXY(1,I,J)
c                  ENDIF
               ENDIF
            ENDIF
            IF ( MOD(I,2).EQ.0 ) THEN
               IF (ESTRIP(2,I,J).GT.EMIN) THEN
                  DEXY(2,I,J) = ESTRIP(2,I,J)
                  ENPLANEY = ENPLANEY + DEXY(2,I,J)
                  NSTRIP = NSTRIP + 1.
                  QTOT = QTOT + DEXY(2,I,J)
                  if (i.lt.11) QQ(3) = QQ(3) + DEXY(2,I,J)
               ENDIF
               IF (ESTRIP(1,I,J).GT.EMIN) THEN
C               IF (ESTRIP(1,I-1,J).GT.EMIN) THEN
C                  
c                  print *,' i ',i,' i - 1 ',I-1
C                  DEXY(1,I,J) = ESTRIP(1,I-1,J)
                  DEXY(1,I,J) = ESTRIP(1,I,J)
C
                  ENPLANEX = ENPLANEX + DEXY(1,I,J)
                  NSTRIP = NSTRIP + 1.
                  QTOT = QTOT + DEXY(1,I,J)
                  IF (I.EQ.22) THEN
                     NX22 = NX22 + 1.
                     QX22 = QX22 + DEXY(1,I,J)
                  ENDIF
                  IF (I.LT.11) QQ(4) = QQ(4) + DEXY(1,I,J)
               ENDIF
            ENDIF
C
            IF (DEXY(1,I,J).GT.QMAX) QMAX = DEXY(1,I,J)
            IF (DEXY(2,I,J).GT.QMAX) QMAX = DEXY(2,I,J)
C
         ENDDO
         IF (ENPLANEX.GT.TENPLAX) THEN
            PLANEMAX(1) = I
            TENPLAX = ENPLANEX
         ENDIF
         IF (ENPLANEY.GT.TENPLAY) THEN
            PLANEMAX(2) = I
            TENPLAY = ENPLANEY
         ENDIF
      ENDDO
C
C     FIT THE TRACK INSIDE THE CALORIMETER
C 
      if (GOOD2.EQ.1.OR.TRIGTY.GE.2) then
         CALL CLUSTER     
         CALL DIRECTION(TG)
         NPCFIT(1) = NPFIT(1)
         NPCFIT(2) = NPFIT(2)
C     
         DO M = 1,2
C
            IF (NPCFIT(M).GE.2) THEN
               VARCFIT(M) = VARFIT(M)
               IF (M.EQ.1) THEN
                  IMPX = (CX - XALIG)/10.
                  TANX = TG(1)
               ELSE
                  IMPY =( CY - YALIG)/10. 
                  TANY = TG(2)
               ENDIF
C               
               DO I = 1,NPLA     
                  NN = 1
C                  NN = 0
C                  IF (M.EQ.2) NN = 1
                  IF (MOD(I,2).EQ.NN) THEN
                     SHIFT = +0.5
                  ELSE
                     SHIFT = -0.5
                  ENDIF
C     
                  IF (M.EQ.1) THEN
                     DISTX = PIANO(I) - 5.81
                     Y(I) = DISTX * TG(1) + CX
                     BAR(M,I) = Y(I)
                     CBAR(M,I) = (Y(I) - XALIG)/10.        
C     
                  ELSE
                     DISTY = PIANO(I)                
                     YY(I) = DISTY * TG(2) + CY
                     BAR(M,I) = YY(I)
                     CBAR(M,I) = (-YALIG + YY(I))/10.
C     
                  ENDIF
                  CALL LASTRISCIA(BAR(M,I),IBAR(M,I))
                  CIBAR(M,I) = IBAR(M,I)
                  IF (ibar(m,i).EQ.-1) THEN 
                     CHTRACK = CHTRACK + 1
                  ELSE
                     IWPL(M) = IWPL(M) + 1
                  ENDIF
               ENDDO            
            ENDIF
C
         ENDDO
      ELSE
         GOTO 9696
      ENDIF
C
C     CALCULATE ELEN AND SELEN
C
      ELEN =  40.82 * QTOT * 1.06 * 1E-4  
      IF (ELEN.GT.0.) THEN
         SELEN = ELEN * (0.01183 + 0.121 / SQRT(ELEN))
      ELSE
         SELEN = 0.
      ENDIF
C
 9696 CONTINUE
C
 45   continue

 50   continue

      return
      END


