      REAL FUNCTION RIDUCI

      IMPLICIT REAL (A-Z)

      INTEGER nb_ntuple, icount
      PARAMETER (nb_ntuple=6)
      character filename

      INCLUDE 'comcheck.txt'

      integer evnum, i,j,k,pre, inf,sup
      real calbase, valore, bas, dedx(2,22,96)

      COMMON /CALO_DAT/evnum,dedx

      VECTOR PEDE(2,22,96)
      vector matmip(2,22,96)

*
*--   Enter user code here
*

      icount = icount + 1
      if (icount.eq.1) then
c         print *,'Filename to save :'
c         read (*,40) filename
      CALL HROPEN(42,'NTUPLE','./tmp.rz','n',6144,istat)
      
 40      FORMAT(A40)

         CALL HBNT(11,'New Calo',' ')
         CALL HBSET('BSIZE',lrec,ierr)

***     /* Book ntuple variables */
         CALL HBNAME(11,'CALO_DAT',evnum,'evnum:I,dedx(2,22,96):R')

      endif  
C
      call vzero(dedx,2*22*96)
C
      do i=1,2
         do j=1,22
            pre = 0
            do k=1,96
               if (mod(k+15,16).eq.0) pre = pre + 1
               if (dexyc(i,j,k).gt.0) then
                  calbase = 0.
                  if (base(i,j,pre).eq.0.) then
                     inf = (pre - 1) * 16 + 1
                     sup = pre * 16 
                     do m = inf, sup
                        valore = valore + dexyc(i,j,m) 
                     enddo
                     bas = valore / 16.
c                     bas = base(i,j,pre)
                  else
                     bas = base(i,j,pre)
                  endif
                  calbase=(dexyc(i,j,k)-bas-pede(i,j,k))/matmip(i,j,k)
                  if (calbase.gt.0.7) dedx(i,j,k) = calbase
               endif
c               if (cal_rms(i,j,k).ne.0.)
c     &         print *,'rms ',cal_rms(i,j,k),' i=',i,' j=',j,' k=',k
            enddo
         enddo
      enddo           
      evnum = iev
C         
      call hfnt(11)
C
      END








