      PROGRAM decay_e
C ========================================================================
* To run the program, modify timeget.sh to specify the input file.
* This simple analysis 
*     (a) opens the input file
*     (b) loops over all the events
*     (d) applies a window on the event time
*     (e) writes out an output file of selected events
* The shell is over-commented to also help as an 
* introduction to FORTRAN.
C ========================================================================
C
C     This states that no variables will be implicitely declared
C     to be integers or floats. It is a good idea to always do this.
C     --------------------------------------------------------------
      IMPLICIT NONE
C
C     These files have declarations for Super-K variables
C     kept in COMMON blocks. You can find the files
C     in a directory such as ~superk/skrep/00b/inc/.
C     ----------------------------------------------
#include "skparm.h"
#include "skhead.h"
#include "sktq.h"
#include "apmring.h"
C
C     This is for the hbook package:
C     ------------------------------
      INTEGER NWPAWC
      REAL H
      PARAMETER (NWPAWC = 10000)
      COMMON/PAWC/H(NWPAWC)
      integer lunhbk,icycle
C
C     Here we declared local variables:
C     ---------------------------------
      INTEGER LUNIN, LUNOUT 
      INTEGER IEVENT, ISAVED,  IERR 
      INTEGER NGTH,SZWIN
      REAL DISTTH,TIMTH,WINTH
      logical good_ev
      integer mixed,mode
      real mode_r
      
      DATA LUNIN /10/
      DATA LUNOUT/20/


C
C This is end of the declaration (setup) section of the program.
C What follows are lines of code, executed one at a time.
C ========================================================================


**************************************************************************


C     Initialization:
C     ---------------
      CALL KZINIT
      CALL SKOPTN('31,30,29,26,25,23,19,18')
      CALL SKOPENF(LUNIN, 0, 'Z', IERR)
      CALL SKOPENF(LUNOUT, 0, 'Z', IERR)
      CALL HLIMIT(-NWPAWC)
      IEVENT = 0
      mixed =0


C
C     Read time window (format is HOUR MIN-minimum MIN-maximum):
C     ----------------------------------------------------------
      OPEN(44,FILE='threshold.card',FORM='FORMATTED')
      READ(44,*) DISTTH,TIMTH,WINTH,NGTH,SZWIN
      CLOSE(44)

C
C     Open histogram file, book histograms:
C     -------------------------------------

c      call hrfile(lunout,'test','N')
      CALL BOOK_HISTO
C
C     Loop over all events, until the end-of-file.
C     At the end-of-file, jump to line 1010
C     -------------------------------------
      DO WHILE (.TRUE.)
C
C        Read in next event:
C        -------------------
         CALL SKREAD(LUNIN,*1010,*1010,*900,*900)
 900     CONTINUE
C
C        Print some text to screen:
C        --------------------------


************************************************************************
c Calling aprstnbk:

* clear commons and read AP bank
      apnring=0
      call apclrall

c This 4 is what Ed told me. Need to be checked later on!
      call aprstbnk(4)
      write(*,*) '****** WARNING need to check input of aprstbnk ******'
      
      

***********************************************************************

c From here to next *** line I can do my modifications:

        
        write(*,*)'=============================='
        write(*,*)'Start new event', nevsk
      
         good_ev = .false.
         mode=0 
         mode_r=0
c         write(*,*)'DISTTH',DISTTH,' TIMTH',TIMTH,' WINTH',WINTH
         call getelectron(DISTTH,TIMTH,WINTH,NGTH,
     &        SZWIN,good_ev,mixed)
c          call getelectron_b(DISTTH,TIMTH,WINTH,NGTH,good_ev,mixed)
         if (good_ev) then
            
            ISAVED = ISAVED + 1
            write(*,*) 'SAVED!!!', nevsk
            
            call getinfo_neut(mode)
            mode_r=mode
            call HFILL(3,mode_r,-100.0,100.0,0.0)
            CALL KZWRIT(LUNOUT)
c            call HFILL(1,isaved,0.0,100.0)

         else 
c     want to record some info about missed event:

            call getinfo_neut(mode)
            mode_r=mode
            call HF1(2,mode_r,1.)
            
            write(*,*)'===mode==', mode
         endif
         call HFILL(12,mode_r,-100.0,100.0,1.0)
C        If event is good, write event to output file:
C        ---------------------------------------------
c         IF (NTIMSK(1).EQ.HOURSELECT) THEN
c            IF ((NTIMSK(2).GE.MINMIN).AND.(NTIMSK(2).LE.MINMAX)) THEN
c               WRITE(*,77) NRUNSK, NSUBSK, NEVSK,
c     &         NDAYSK(1), NDAYSK(2), NDAYSK(3),
c     &         NTIMSK(1), NTIMSK(2), NTIMSK(3), NTIMSK(4), ' SAVED******'c
c
c               CALL KZWRIT(LUNOUT)
c               ISAVED = ISAVED + 1
c            END IF
c         END IF

*************************************************************************
C
C        Clear and increment some variables:
C        -----------------------------------
         CALL KZECLR
         IEVENT = IEVENT + 1
      END DO
77    FORMAT(1X,I6,1X,I4,1X,I8,' time = ',
     &       I2.2,'-',I2.2,'-',I2.2,':',
     &       I2.2,':',I2.2,':',I2.2,'.',I2,1X,A)

C
C     Jump here when end-of-file:
C     ---------------------------
 1010 CONTINUE
      CALL SKCLOSEF(LUNIN)
      CALL SKCLOSEF(LUNOUT)
C
C     Print out summary information:
C     ------------------------------
      WRITE(*,*) 'Number of events: read=',ievent,' saved=',isaved
C
C     Write out histogram file:
C     -------------------------
      CALL HRPUT(0,'decay_e.hbk','N')
      
      WRITE(*,*) 'See decay_e.hbk for histograms'


c      call hrout(0,icycle,' ')
c      call hrend('test')

C
C     Program is finished, exit with a brief message:
C     -----------------------------------------------
      STOP 'Decay_e program completed'
      END

      SUBROUTINE BOOK_HISTO
C ======================================================================
C This subroutine books the histograms.



c     call HBOOK1(1,'time_saved',100,3100.0,4100.0,0.0)
      call HBOOK1(1,'delta_saved',100,0.0,2000.0,0.0)
c     call HBOOK1(4,'time_missed',100,0.0,5000.0,0.0)
      call HBOOK1(4,'delta_missed',100,0.0,4000.0,0.0)
      call HBOOK1(2,'mode_missed',201,-100.0,100.0,0.0)
      call HBOOK1(3,'mode_saved',201,-100.0,100.0,0.0)
      call HBOOK1(5,'r_missed',100,0.0,2000.0,0.0)
      call HBOOK1(6,'z_missed',100,0.0,2500.0,0.0)
      call HBOOK1(7,'dist_missed',100,0.0,5000.0,0.0)
      call HBOOK1(8,'dist_saved',100,0.0,5000.0,0.0)
      call HBOOK1(9,'r_pmt_miss',100,0.0,2000.0,0.0)
      call HBOOK1(10,'z_pmt_miss',100,0.0,2500.0,0.0)
      call HBOOK1(12,'mode_all',201,-100.0,100.0,0.0)
      call HBOOK1(13,'ngt',20,0.0,20.0,0.0)
      call HBOOK1(20,'top all',3,0.0,3.0,0.0)
      call HBOOK1(21,'top through',3,0.0,3.0,0.0)
      call HBOOK1(30,'time_cluster',10,1000.0,6000.0,0.0)
C ======================================================================
      CONTINUE
      RETURN
      END

