      REAL FUNCTION bcal2()
      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)
      INTEGER M,CER_MIR_GOOD(8)
      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

      DO I=1,8
         CER_MIR_GOOD(I)=0
      ENDDO

      CER_TDC_HI=-2000
      CER_TDC_LOW=-2500

      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 ver 2'
         CALL HBOOK1(10000,'CER TDC',100,-2500,-2000,0.0)
         CALL HBOOK2(20001,'bcal64 ADC vs row col1',19,1,20,
     ,        100,1000.0,10000,0.0)
         CALL HBOOK2(30001,'bcal64 ADC vs row col2',19,1,20,
     ,        100,1000.0,10000,0.0)
         CALL HBOOK2(20002,'bcal64 TDC vs row col1',19,1,20,
     ,        40,310.0,350,0.0)
         CALL HBOOK2(30002,'bcal64 TDC vs row col2',19,1,20,
     ,        40,310.0,350,0.0)
Correlating the hits and mirrors. Breaking into quadrants
         CALL HBOOK2(21001,'bcal64 TDC vs row col1',19,1,20,
     ,        40,310.0,350,0.0)
         CALL HBOOK2(31001,'bcal64 TDC vs row col2',19,1,20,
     ,        40,310.0,350,0.0)
      ENDIF



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

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

         IF(CER_OK)THEN
            DO I=1,NCLUST64
c looping over groups of 64               
               DO J=1,6                 
                  DO K=1,8
                     if(icol64hit(j,i).NE.0.and.a64(J,i).gt.0.and.
     ,                 irow64hit(j,i).NE.0.AND.TCELL64(J,K,I).NE.0)then
                        IF(CER_MIR_GOOD(1).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.2.AND.IROW64HIT(J,I).GE.
     ,                       1.AND.IROW64HIT(J,I).LE.5)THEN
                           CALL HFill(31001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(2).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.1.AND.IROW64HIT(J,I).GE.
     ,                       1.AND.IROW64HIT(J,I).LE.5)THEN
                           CALL HFill(21001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(3).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.2.AND.IROW64HIT(J,I).GE.
     ,                       5.AND.IROW64HIT(J,I).LE.10)THEN
                           CALL HFill(31001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(4).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.1.AND.IROW64HIT(J,I).GE.
     ,                       5.AND.IROW64HIT(J,I).LE.10)THEN
                           CALL HFill(21001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(5).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.2.AND.IROW64HIT(J,I).GE.
     ,                       10.AND.IROW64HIT(J,I).LE.15)THEN
                           CALL HFill(31001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(6).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.1.AND.IROW64HIT(J,I).GE.
     ,                       10.AND.IROW64HIT(J,I).LE.15)THEN
                           CALL HFill(21001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(7).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.2.AND.IROW64HIT(J,I).GE.
     ,                       15.AND.IROW64HIT(J,I).LE.19)THEN
                           CALL HFill(31001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ELSEIF(CER_MIR_GOOD(8).EQ.1.AND.
     ,                       icol64hit(j,i).EQ.1.AND.IROW64HIT(J,I).GE.
     ,                       15.AND.IROW64HIT(J,I).LE.19)THEN
                           CALL HFill(21001,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ENDIF
                  ENDIF
                  ENDDO
               ENDDO
            ENDDO


            DO I=1,NCLUST64
c looping over groups of 64               
               DO J=1,6
                  if(icol64hit(j,i).eq.1.and.a64(J,i).gt.0.and.
     ,                 irow64hit(j,i).GE.1)then                     
                     CALL HFILL(20001,float(irow64hit(j,i))
     ,                    ,a64(J,i),1.0)
                     DO K=1,8
                        IF(TCELL64(J,K,I).GT.0)THEN
                           CALL HFill(20002,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ENDIF
                     ENDDO
                  ENDIF
                  if(icol64hit(j,i).eq.2.and.a64(j,i).gt.0.and.
     ,                irow64hit(j,i).GE.1)then
                     CALL HFILL(30001,float(irow64hit(j,i))
     ,                    ,a64(J,i),1.0)
                     DO K=1,8
                        IF(TCELL64(J,K,I).GT.0)THEN
                           CALL HFill(30002,float(irow64hit(j,i)),
     ,                          TCELL64(J,K,i),1.0)
                        ENDIF
                     ENDDO
                  ENDIF
               ENDDO
            ENDDO         
         ENDIF
      ENDIF

      END



         

