       REAL FUNCTION findped
*********************************************************
*                                                       *
* This file was generated by HUWFUN.                    *
*                                                       *
*********************************************************
*
*     Ntuple Id:      1    
*     Ntuple Title:   Tracks
*     Creation:       04/06/02 14.52.04
*
*********************************************************
*
      LOGICAL         CHAIN
      CHARACTER*128   CFILE
      INTEGER         IDNEVT,NCHEVT,ICHEVT
      REAL            OBS(13)
*
      COMMON /PAWIDN/ IDNEVT,OBS
      COMMON /PAWCHN/ CHAIN, NCHEVT, ICHEVT
      COMMON /PAWCHC/ CFILE
*
*--   Ntuple Variable Declarations
*
      REAL energy
      INTEGER event,index,reel,status1,wpep
*
      COMMON /PAWCR4/ energy,event,index,reel,status1,wpep
*
*--   Local variables
*
      INTEGER Noent
      INTEGER i,j,k,itow,nev
      INTEGER eta,phi,pmt,we
      INTEGER numped (0:1,0:23,0:22)
      REAL ped(0:1,0:23,0:22)
      REAL rms(0:1,0:23,0:22)
      DOUBLE sumped(0:1,0:23,0:22),sumped2(0:1,0:23,0:22)
      DATA nev/0/
*
*--   Enter user code here
*
        nev = nev + 1
*

      if(nev.eq.1)then
       Call Hnoent(1,Noent)
       print *,Noent
       do i = 0,1
        do j = 0,23
         do k = 0,22  
           sumped(i,j,k)=0.
           sumped2(i,j,k)=0.
           numped(i,j,k)=0
           ped(i,j,k)=0.
           rms(i,j,k)=0.
         enddo
        enddo
       enddo
      endif
*
      eta = wpep/128
      phi = (wpep-eta*128)/4
      pmt = (wpep-eta*128-phi*4)/2
      we  = wpep-eta*128-phi*4-pmt*2      
*
      itow = 2*(eta-10) + pmt
      if(nev.lt.Noent)then
*
*--    First find pedestal
*
       if (energy .gt. 60000) energy =0.
       if(reel .gt.-100 .and. reel.lt.500)then
         numped(we,phi,itow) = numped(we,phi,itow) + 1
         sumped(we,phi,itow) = sumped(we,phi,itow) + energy
         sumped2(we,phi,itow) = sumped2(we,phi,itow) + energy*energy
       endif
*
*     Evaluate pedestal and RMS
*
      elseif(nev.eq.Noent)then
*
       if(reel.gt.-100 .and. reel.lt.500)then
         numped(we,phi,itow) = numped(we,phi,itow) + 1
         sumped(we,phi,itow) = sumped(we,phi,itow) + energy
         sumped2(we,phi,itow) = sumped2(we,phi,itow) + energy*energy
       endif
*
        do i=0,1
         do j=0,23
          do k=0,22
           ped(i,j,k) = 0.
           rms(i,j,k) = 0.
           if(numped(i,j,k).gt.0)then
            ped(i,j,k) = sumped(i,j,k)/numped(i,j,k)
            if((sumped2(i,j,k) - numped(i,j,k) * 
     &       ped(i,j,k)*ped(i,j,k)).gt.0.)
     &       rms(i,j,k) = sqrt((sumped2(i,j,k) - 
     &       numped(i,j,k)*ped(i,j,k)*ped(i,j,k))/
     &       (numped(i,j,k)-1))
           endif
           write(21,9001)i,j,k,numped(i,j,k),ped(i,j,k),rms(i,j,k)
9001       format(4I5,2F12.6)      
          enddo
         enddo
        enddo
*
      endif
      find = 1.
*
      END
