      SUBROUTINE NSHOWER(IERR,RNUMX,RNUMY,ENERGIAX,ENERGIAY)
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : IT IDENTIFIES ELECTROMAGNETIC SHOWERS USING :
C-   CENTER OF GRAVITY IN THE FIRST HALF - SECOND HALF OF THE CALORIMETER .
C-   RNUMX AND RNUMY (X AND Y VIEW) ARE THE DISTANCES IN mm OF THE CLUSTERS
C-   CLOSEST TO THE TRACK NORMALIZED TO THE TRACK .  
C-   ENERGIAX AND ENERGIAY ARE THE ENERGY OF THESE CLUSTERS .
C-   THE CENTERS OF GRAVITY AND THE CLUSTERS COME FROM THE ROUTINE CLUSTER2 .
C-
C-   Inputs  :
C-   Outputs :
C-   Controls:
C-
C-   Created   3-NOV-1993   MIRKO BOEZIO
C-
C----------------------------------------------------------------------
      INCLUDE 'INTEST.TXT'

      REAL RX(2),RY(2)
      REAL RIL(NCHA/2)
C$      DIMENSION IP(6)
      REAL BAR(2,NPLA)
      REAL TG(2)
      REAL DAD
      INTEGER IBAR(2,NPLA)
      INTEGER ITL
      COMMON/VARIE/MAX
      SAVE /VARIE/
      COMMON/INIZIO/INPIA
      SAVE /INIZIO/
      COMMON / IMPULSO / QUAMOT
      SAVE /IMPULSO/
      COMMON/DOVE/NX,NY,LUNGH
      SAVE /DOVE/
      COMMON/TANGENTI/TG
      SAVE /TANGENTI/
      COMMON/ANGOLO/BAR,IBAR
      SAVE /ANGOLO/
C$      INTEGER NUM(10)
C$      REAL IMP(10)
      INTEGER LK(2)
C$      DATA (IMP(I),I=1,3) / 3*0 /,(NUM(I),I=1,3) / 3*0 /
C$      DATA (IMP(I),I=4,5) / 2*50 /,(NUM(I),I=4,5) / 2*4 /

C
      LPIANO = NPLA / 2 
      CALL CLUSTER2
C
      ITL = 0
      IALFA = 0
  499 CONTINUE
C
      DO I = 1,NCHA/2
        RIL(I) = 1000.
      ENDDO
C
      RX(1) = 0.
      RX(2) = 0.
      RY(1) = 0.
      RY(2) = 0.      
C
      DO M = 1,2
        DO J = 1,2
C
          DO I = 1,NCHA/2
            RIL(I) = 1000.
          ENDDO
C
          LK(J) = 0
C$$          IF (J.EQ.1) TL = 3.
C$$          IF (J.EQ.2) TL = (15. + 7.) / 2.
C$$          DAT = -PIANO * TL * TG(M)
C$$
C$$          DAD = BAR(M,1) + DAT
C
          IF (J.EQ.1) ITL = (LPIANO + 1) / 2
          IF (J.EQ.2) ITL = (NPLA + LPIANO + 1) / 2

          DAD = BAR(M,ITL) 
c          PRINT *,' DAD ',DAD,' M ',M,' ITL ',ITL
C
C$          CALL LASTRISCIA(DAD,NN)
C
          NGIR = NCL2(M,J)
          IF (NGIR.EQ.0) GO TO 555
          DO L = 1,NGIR
C$            CALL LASTRISCIA(CLUS2(M,J,L),IN)
C$            RIL(L) = FLOAT(ABS(NN - IN))
            RIL(L) = ABS(DAD - CLUS2(M,J,L))
          ENDDO
          MM = NCHA/2
          LK(J) = LVMIN(RIL,MM)
C
          IF (CLUS2(M,J,LK(J)+NCHA/2).GT.EMIN) THEN
C$            CALL MILLIM(NN,RR)
            IF (M.EQ.1) THEN
              ENERGIAX = ENERGIAX + CLUS2(M,J,LK(J)+NCHA/2)
              RX(J) = CLUS2(M,J,LK(J)) - DAD
c              print *,'m j lk(j) ',m,j,lk(j)
c              PRINT*,' RX: ',RX(J)
c              PRINT*,' DAD: ',DAD
c              PRINT*,' CLUS2: ',CLUS2(M,J,LK(J))
C$              RX(J) = CLUS2(M,J,LK(J)) - RR
            ENDIF
            IF (M.EQ.2) THEN
              ENERGIAY = ENERGIAY + CLUS2(M,J,LK(J)+NCHA/2)
              RY(J) = CLUS2(M,J,LK(J)) - DAD
C$              RY(J) = CLUS2(M,J,LK(J)) - RR
            ENDIF
          ENDIF
C
  555     CONTINUE
        ENDDO
C
        IF (M.EQ.1) RNUMX = ABS( RX(2) - RX(1) ) 
        IF (M.EQ.2) RNUMY = ABS( RY(2) - RY(1) ) 
c        PRINT*,' RX: ',RX(1),RX(2),' RY: ',RY(1),RY(2)
C
      ENDDO
C
      ESOGLIATOT = 50.
      EINT = 10.
C
      IF (ENERGIAX.EQ.0.OR.ENERGIAY.EQ.0) THEN
        II = 1
      ENDIF
C
      EINF = ENERGIAX - EINT
      ESUP = ENERGIAX + EINT
      ETOT = ENERGIAX + ENERGIAY
C
      IF (ENERGIAY.LT.EINF.OR.ENERGIAY.GT.ESUP.OR.
     &    ETOT.LT.ESOGLIATOT) GO TO 2
C
      GO TO 10
C
    2 IERR = 1
C
   10 CONTINUE

      RETURN
      END
