       REAL FUNCTION findhad
*********************************************************
*                                                       *
* 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 eta,phi,pmt,we
      INTEGER i,j,k,l,m
      INTEGER itow,nev
      INTEGER ii,jj,kk
      INTEGER mpeak(60,0:1,0:23)
      INTEGER nbig(60,0:1,0:23,0:22,0:1)
      INTEGER reelcnt(60,0:1,0:23,0:22,0:1)
      INTEGER numped(0:1,0:23,0:22)
      INTEGER indx(60,0:1,0:23,0:22)
      INTEGER chindx(158),chlist(60)
      INTEGER ind,nlo,nhi,nmid,indlo,indhi
      INTEGER ich,ichan,numch
      INTEGER rtype
      INTEGER ntow
      INTEGER gar,ent
      INTEGER igar(60,0:1,0:23,0:22)
      REAL avg22(60,0:1,0:23)
      REAL aver22(60,0:1,0:23,10000)
      REAL pklo,pkhi,sum22,diftest
      REAL ped(0:1,0:23,0:22)
      REAL rms(0:1,0:23,0:22)
      REAL sumsig(60,0:1,0:23,0:22)
      REAL avg(60,0:1,0:23,0:22,0:1),big(60,0:1,0:23,0:22,0:1)
      REAL big22(60,0:1,0:23)
      REAL pkmin,bigp
      LOGICAL found
      DATA nev/0/
*
*--   Enter user code here
*
        DATA CHINDX/1,2,4,6,16,0,6,12,19,1,2,4,6,16,0,6,12,19,      ! 1-18
     &             1,2,4,6,16,0,6,12,19,99,99,99,99,1,2,4,6,16,      ! 19-36
     &             0,6,12,19,1,2,4,6,16,0,6,12,19,1,2,4,6,16,        ! 37-54
     &             0,6,12,19,1,2,4,6,16,0,6,12,19,1,2,4,6,16,        ! 55-72
     &             0,6,12,19,1,2,4,6,16,0,6,12,19,99,99,99,99,       ! 73-89
     &             1,2,4,6,16,0,6,12,19,1,2,4,6,16,0,6,12,19,        ! 90-107
     &             1,2,4,6,16,0,6,12,19,                             ! 108-116
     &             3,5,7,8,9,10,11,12,13,14,15,17,18,19,20,21,22,23, ! 117-134
     &             99,99,99,                                         ! 135-137
     &             3,5,7,8,9,10,11,12,13,14,15,17,18,19,20,21,22,23, ! 138-155
     &             99,99,99/                                         ! 156-158
c


       nev = nev + 1
*
      rtype = 0
      if( (index.ge.10.and.index.le.18) .or.
     &    (index.ge.32.and.index.le.40) .or. 
     &    (index.ge.50.and.index.le.58) .or. 
     &    (index.ge.68.and.index.le.76) .or. 
     &    (index.ge.90.and.index.le.98) .or. 
     &    (index.ge.108.and.index.le.116) )
     &    rtype = 1 
*
      if(nev.eq.1)then
       Call Hnoent(1,Noent)
       pkmin = 1500.
*
       do i=0,1
        do j=0,23
         do k=0,22
          read(21,9001)ii,jj,kk,numped(i,j,k),ped(i,j,k),rms(i,j,k)
         enddo
        enddo
       enddo
9001   format(4I5,2F12.6)
*
       do i = 1,60
        chlist(i) = 0
        do j = 0,1
         do k = 0,23
          mpeak(i,j,k) = 0
          big22(i,j,k) = 0.
          do l = 0,22  
           indx(i,j,k,l) = 0
           sumsig(i,j,k,l) = 0.
           igar(i,j,k,l) = 0
           do m = 0,1
             big(i,j,k,l,m) = -999.   
             nbig(i,j,k,l,m) = 0
           enddo
          enddo
         enddo
        enddo
       enddo
       numch = 0
      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
*
c
c store the index channel numbers in chlist in consecutive order
c

        found=.false.
        do ich=1,60
          if(index.eq.chlist(ich))then
            found=.true.
            ichan=ich
          endif
        enddo
*
        if(.not.found)then
          if(numch.lt.60)numch=numch+1
          chlist(numch)=index
          ichan=numch
        endif
c

*
*--   Now start looking for peaks
*
*        if(nev.gt.350000)print *,nev,ichan,we,phi,itow,energy
        if( energy .gt. 60000)energy = 0.
        sumsig(ichan,we,phi,itow) = sumsig(ichan,we,phi,itow) + energy
*        if(mod(nev,5).eq.0)then
        indx(ichan,we,phi,itow) = indx(ichan,we,phi,itow) + 1
*
*     Check for begin of garaging
*
        igar(ichan,we,phi,itow)=0
        if(status1.ne.2)then
           igar(ichan,we,phi,itow)=1
        endif
*
        avg(ichan,we,phi,itow,igar(ichan,we,phi,itow)) = 
     &    sumsig(ichan,we,phi,itow)
        if(itow.eq.22.and.indx(ichan,we,phi,itow).lt.10000)
     &    aver22(ichan,we,phi,indx(ichan,we,phi,itow)) =
     &    sumsig(ichan,we,phi,itow)

        sumsig(ichan,we,phi,itow) = 0.
*
        if(avg(ichan,we,phi,itow,igar(ichan,we,phi,itow)) .gt.
     &     big(ichan,we,phi,itow,igar(ichan,we,phi,itow)))then

*
*--  Found new local maximum 
*
          big(ichan,we,phi,itow,igar(ichan,we,phi,itow)) = 
     &      avg(ichan,we,phi,itow,igar(ichan,we,phi,itow))
          nbig(ichan,we,phi,itow,igar(ichan,we,phi,itow)) = event
          reelcnt(ichan,we,phi,itow,igar(ichan,we,phi,itow)) = reel
        endif
*

      elseif(nev.eq.Noent)then
*        

*
*
* special case for tower 22
*
        do ichan=1,60
         if(chlist(ichan).ne.0)then
          if(chindx(chlist(ichan)).ne.1)then
           itow = 22
           do we = 0,1
            do phi = 0,23
              do ent = 1,10000
c
c first find maximum 
c
                if((aver22(ichan,we,phi,ent) .gt. 
     &           big22(ichan,we,phi)) .and. 
     &           ((aver22(ichan,we,phi,ent)-ped(we,phi,itow))
     &             .gt.pkmin))then

                 big22(ichan,we,phi) = aver22(ichan,we,phi,ent)
                 mpeak(ichan,we,phi) = ent
                endif
              enddo
            enddo
           enddo
          endif
         endif
        enddo

        do ichan=1,60
         if(chlist(ichan).ne.0)then
          if(chindx(chlist(ichan)).ne.1)then
           do we = 0,1
            do phi = 0,23
              if(mpeak(ichan,we,phi).ne.0)then
                 big22(ichan,we,phi) = 
     &             aver22(ichan,we,phi,mpeak(ichan,we,phi))
                 bigp = big22(ichan,we,phi) - ped(we,phi,itow)
                 ind = mpeak(ichan,we,phi)
                 do i=1,10000
                   ind=ind-1
                   if(ind.gt.0)then
                    pklo=aver22(ichan,we,phi,ind) - ped(we,phi,itow)
*                    print *,ichan,we,phi,ind,pklo
*                   write(22,9005)i,ind,pklo
                    if(pklo.lt.0.5*bigp)go to 350
                   endif
                 enddo
                 go to 370
350              continue
9005             format(2i5,f12.4)
                 indlo=ind
                 ind=mpeak(ichan,we,phi)
                 do i=1,10000
                  ind=ind+1
                  if(ind.lt.10000)then
                   pkhi=aver22(ichan,we,phi,ind) - ped(we,phi,itow)
                   if(pkhi.lt.0.5*bigp)go to 360
                  endif
                 enddo
                 go to 370
360              continue
                 indhi=ind
                 nmid=(indlo+indhi)/2
                 sum22=0.
                 ind=nmid
                 sum22=sum22 + 
     &             aver22(ichan,we,phi,ind) - ped(we,phi,itow)

                 do i=1,10
                  ind=nmid-i
                  if(ind.lt.10000)
     &               diftest=aver22(ichan,we,phi,ind) -ped(we,phi,itow)
*                 if(diftest.lt.0.9*bigp)print 9008,diftest,bigp
                 sum22=sum22 + 
     &             aver22(ichan,we,phi,ind) - ped(we,phi,itow)
                  ind=nmid+i
                  if(ind.lt.10000)
     &               diftest=aver22(ichan,we,phi,ind) -ped(we,phi,itow)
*                 if(diftest.lt.0.9*bigp)print 9008,diftest,bigp
                 sum22=sum22 + 
     &             aver22(ichan,we,phi,ind) - ped(we,phi,itow)
                 enddo
*
                 avg22(ichan,we,phi)=sum22/21.
*                 print *,ichan,we,phi,nmid,avg22(ichan,we,phi),sum22
*
370           continue
              endif
            enddo
           enddo
          endif
         endif
        enddo
*
9008    format(' ** low point **',2f12.4)
9010    format(3f12.4,3i5,f12.4)

        do ichan=1,60
         do we = 0,1
          do phi = 0,23
           do itow = 0,22
            do gar = 0,1
             if(chlist(ichan).ne.0)then
              if(chindx(chlist(ichan)).ne.1)then
               bigp = big(ichan,we,phi,itow,gar)
     &         - ped(we,phi,itow)
               if(bigp .gt. pkmin)then 
                write(22,9002)we,phi,itow,chindx(chlist(ichan)),rtype,
     &           bigp,nbig(ichan,we,phi,itow,gar),
     &           ped(we,phi,itow),rms(we,phi,itow),
     &           reelcnt(ichan,we,phi,itow,gar)
                 if(itow.eq.22)then
                  ntow = 23
                  bigp = avg22(ichan,we,phi)
                  write(22,9002)we,phi,ntow,chindx(chlist(ichan)),rtype,
     &             bigp,nbig(ichan,we,phi,itow,gar),
     &             ped(we,phi,itow),rms(we,phi,itow),
     &             reelcnt(ichan,we,phi,itow,gar)
                 endif
               endif
              endif
             endif
            enddo
           enddo
          enddo
         enddo
        enddo
      endif
*
9002  format(5I5,F12.4,I10,2F12.4,I10) 
      find = 1.
*
      END










