      REAL FUNCTION bcal()
      IMPLICIT NONE
      INCLUDE ?

      LOGICAL TRUE, FALSE, FIRST, BASIC_CUT,CER_CUT,CER_OK
      INTEGER I,J,K,CER_TDC_HI,CER_TDC_LOW,CER_GOOD(40),M
      REAL CER_SANE_TIME_WALK(8)
      DATA CER_SANE_TIME_WALK/24000,24500,24000,25000,
     ,     15000,15000,14000,14000/
      DATA FIRST /.TRUE./
      SAVE FIRST

      TRUE = .TRUE.
      FALSE = .FALSE.
      CER_OK=FALSE
      DO I=1,40
         CER_GOOD(I)=0
      ENDDO

      CER_TDC_HI=-2050
      CER_TDC_LOW=-2450

      BASIC_CUT = BTRIGTYPE.EQ.4.and.NCLUST64.EQ.1.AND.ECLUST(1).GT.1

      DO I=1,CER_HIT
         IF(CER_NUM(I).LT.9)THEN
            CER_TDC(I) = CER_TDC(I) +
     ,           CER_SANE_TIME_WALK(CER_NUM(I))/FLOAT(CER_ADCC(I))
         ENDIF
      ENDDO

      
      IF(FIRST)THEN
         first = false
         WRITE(*,*) 'BOOKING da HISTOGRAMS'
         CALL HBOOK1(30000,'CER MIR 5 TDC',100,-2400,-2000,0.0)

 
         CALL HBOOK1(20008,'bcal tdctime 5 2,8',100,300.0,400,0.0)
         CALL HBOOK1(20009,'bcal tdctime 5 2,9',100,300.0,400,0.0)
         CALL HBOOK1(20010,'bcal tdctime 5 2,10',100,300.0,400,0.0)
         CALL HBOOK1(20011,'bcal tdctime 5 2,11',100,300.0,400,0.0)
         CALL HBOOK1(20012,'bcal tdctime 5 2,12',100,300.0,400,0.0)
         CALL HBOOK1(20013,'bcal tdctime 5 2,13',100,300.0,400,0.0)
         CALL HBOOK1(20014,'bcal tdctime 5 2,14',100,300.0,400,0.0)
         CALL HBOOK1(20015,'bcal tdctime 5 2,15',100,300.0,400,0.0)
         CALL HBOOK1(20016,'bcal tdctime 5 2,16',100,300.0,400,0.0)
         CALL HBOOK1(20017,'bcal tdctime 5 2,17',100,300.0,400,0.0)



         CALL HBOOK1(20108,'bcal adc 5 2,8',100,1000.0,10000,0.0)
         CALL HBOOK1(20109,'bcal adc 5 2,9',100,1000.0,10000,0.0)
         CALL HBOOK1(20110,'bcal adc 5 2,10',100,1000.0,10000,0.0)
         CALL HBOOK1(20111,'bcal adc 5 2,11',100,1000.0,10000,0.0)
         CALL HBOOK1(20112,'bcal adc 5 2,12',100,1000.0,10000,0.0)
         CALL HBOOK1(20113,'bcal adc 5 2,13',100,1000.0,10000,0.0)
         CALL HBOOK1(20114,'bcal adc 5 2,14',100,1000.0,10000,0.0)
         CALL HBOOK1(20115,'bcal adc 5 2,15',100,1000.0,10000,0.0)
         CALL HBOOK1(20116,'bcal adc 5 2,16',100,1000.0,10000,0.0)
         CALL HBOOK1(20117,'bcal adc 5 2,17',100,1000.0,10000,0.0)



         call hbook2(40008,'tdc vs adc 5 2,8',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40009,'tdc vs adc 5 2,9',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40010,'tdc vs adc 5 2,10',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40011,'tdc vs adc 5 2,11',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40012,'tdc vs adc 5 2,12',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40013,'tdc vs adc 5 2,13',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40014,'tdc vs adc 5 2,14',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40015,'tdc vs adc 5 2,15',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40016,'tdc vs adc 5 2,16',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(40016,'tdc vs adc 5 2,17',100,1000,10000,100,
     ,        300,400,0.0)
         call hbook2(30001,'bcal 5 x y',60,-60,60,110,-110,110,0.0)

         
      ENDIF



      IF(BASIC_CUT)THEN
C     CHECK FOR CERENKOV HIT IN MIRROR
         DO I=1,CER_HIT
            CER_CUT=CER_NUM(I).EQ.5.AND.CER_TDC(I).LT.CER_TDC_HI.AND.
     ,           CER_TDC(I).GT.CER_TDC_LOW
            IF(CER_CUT)THEN
               CER_GOOD(I)=1
            ENDIF
         ENDDO

         DO I=1,CER_HIT
            IF(CER_GOOD(I).EQ.1)THEN
               CER_OK=TRUE
               CALL HF1(30000,float(CER_TDC(I)),1.0)
            ENDIF
         ENDDO

         IF(CER_OK)THEN
            DO I=1,NCLUST64
               call hfill(30001,xclust(i),yclust(i),1.0)
c looping over groups of 64               
               do M=8,17
                  DO J=1,6
                     if(icol64hit(j,i).eq.2.and.irow64hit(j,i)
     ,                    .eq.M)then
                        if(a64(J,i).gt.0)then
                           CALL HF1(20100+M,a64(J,i),1.0)
                        endif
                        DO K=1,8
                           IF(TCELL64(J,K,I).GT.0)THEN
                              CALL HF1(20000+M,TCELL64(J,K,i),1.0)
                              if(a64(J,i).gt.0)then
                                 CALL HFill(40000+M,a64(J,i),
     ,                                TCELL64(J,K,i),1.0)
                              endif 
                           ENDIF
                        ENDDO
                     endif
                  ENDDO
               enddo
            ENDDO
         ENDIF
      ENDIF

      END

C         call hf1(20001,float(nclust64),1.0)
C         DO I=1,NCLUST64
C           if(ncell64clust(I).gt.0.and.ncell64clust(I).lt.
C   ,           7)then
C            call hf1(20002,float(ncell64clust(I)),1.0)
C           endif
C           do j=1,6
C           if(nhit64clust(j,I).gt.0)then
C              call hf1(20005,float(nhit64clust(j,I)),1.0)
C            endif
C         enddo
C            DO J=1,6
C               if(irow64hit(J,I).gt.0)then
C                  call hf1(20003,float(irow64hit(J,I)))
C               endif
C              DO K=1,8
C                  if(tcell64(J,K,I).gt.0)then
C                     call hf1(20004,tcell64(J,K,I))
C                  endif
C               ENDDO
C            ENDDO
C         ENDDO

         

