!----------------------------------------------------------------------
! set windows on cerenkov angle
! M. Staric, jan-2000, changed: nov-2000
!
! SUBROUTINE SET_RICWIN(ITRA)  must be called prior to BGR_ESTIMATE for all tracks
! LOGICAL FUNCTION IN_NARROW_WIN(THC)
! LOGICAL FUNCTION IN_WIDE_WIN(THC)
! LOGICAL FUNCTION IN_NARROW_WIN2(THC2)
! LOGICAL FUNCTION IN_WIDE_WIN2(THC2)
! SUBROUTINE SET_RWIN(NN,THC,WIN,X2_MAX,X1,X2,N,SUM,SUM1)
!----------------------------------------------------------------------

      SUBROUTINE SET_RICWIN(ITRA)

#include "riter/RIC_TRA.fi"
#include "riter/RIC_PAR.fi"
#include "riter/RIC_WIN.fi"
      DATA N_AB /0/, N_CD /0/

      IF(ITRA.GT.NTRA) RETURN
      NHY=MIN(NHYP,6)
! narrow windows
      SIGMA=SQRT(SIG_M16**2+(T_SIG(1,ITRA)**2+T_SIG(2,ITRA)**2)/2.)
      SWIN=SWIN_SIZE*SIGMA
      CALL SET_RWIN(NHY,THC(0,ITRA),SWIN,THC_LIM,A,B,N_AB,S_AB,S_ABL)
      DO I=1,N_AB
         A2(I)=A(I)**2
         B2(I)=B(I)**2
      ENDDO
! wide windows
      DWIN=DWIN_SIZE*SWIN
      CALL SET_RWIN(NHY,THC(0,ITRA),DWIN,THC_LIM,C,D,N_CD,S_CD,S_CDL)
      DO I=1,N_CD
         C2(I)=C(I)**2
         D2(I)=D(I)**2
      ENDDO
      IF(S_CD.LE.S_AB*1.1) RECO_FLAG(ITRA)=0
! for background estimate
      IF(S_AB.NE.0.AND.RECO_FLAG(ITRA).NE.0) THEN
         AX3(ITRA)=1./S_AB
         FAB3(ITRA)=S_AB/(S_CD-S_AB)
         AX1(ITRA)=1./S_ABL
         FAB1(ITRA)=S_ABL/(S_CDL-S_ABL)
      ELSE
         AX3(ITRA)=0.
         FAB3(ITRA)=0.
         AX1(ITRA)=0.
         FAB1(ITRA)=0.
      ENDIF                                                                     

      RETURN
      END


      LOGICAL FUNCTION IN_NARROW_WIN(THC)

      REAL THC

#include "riter/RIC_WIN.fi"

      IN_NARROW_WIN=.FALSE.
      DO I=1,N_AB
         IN_NARROW_WIN=THC.GT.A(I).AND.THC.LT.B(I)
         IF(IN_NARROW_WIN) RETURN
      ENDDO
      RETURN
      END


      LOGICAL FUNCTION IN_WIDE_WIN(THC)

      REAL THC

#include "riter/RIC_WIN.fi"

      IN_WIDE_WIN=.FALSE.
      DO I=1,N_CD
         IN_WIDE_WIN=THC.GT.C(I).AND.THC.LT.D(I)
         IF(IN_WIDE_WIN) RETURN
      ENDDO
      RETURN
      END


      LOGICAL FUNCTION IN_NARROW_WIN2(THC2)

      REAL THC2

#include "riter/RIC_WIN.fi"

      IN_NARROW_WIN2=.FALSE.
      DO I=1,N_AB
         IN_NARROW_WIN2=THC2.GT.A2(I).AND.THC2.LT.B2(I)
         IF(IN_NARROW_WIN2) RETURN
      ENDDO
      RETURN
      END


      LOGICAL FUNCTION IN_WIDE_WIN2(THC2)

      REAL THC2

#include "riter/RIC_WIN.fi"

      IN_WIDE_WIN2=.FALSE.
      DO I=1,N_CD
         IN_WIDE_WIN2=THC2.GT.C2(I).AND.THC2.LT.D2(I)
         IF(IN_WIDE_WIN2) RETURN
      ENDDO
      RETURN
      END



      SUBROUTINE SET_RWIN(NN,THC,WIN,X2_MAX,X1,X2,N,SUM,SUM1)
 
      INTEGER NN                ! number of hypotheses
      REAL THC(0:NN-1)          ! cerenkov angle for other,e,mu,pi,K,p
      REAL WIN                  ! window width
      REAL X2_MAX               ! upper limit for X2
      REAL X1(*), X2(*)         ! lower, upper limit of intervals
      INTEGER N                 ! number of intervals
      REAL SUM                  ! integral (cub.)
      REAL SUM1                 ! integral (lin.)
 
      N=0
      SUM=0.
      SUM1=0.
      I=1                       ! other is ignored (should have thc=0)
      DO WHILE(THC(I).LE.0)
         I=I+1
         IF(I.GT.NN-1) RETURN
      ENDDO
      N=N+1
      X2(N)=MIN(THC(I)+WIN/2.,X2_MAX)
      X1(N)=MIN(MAX(THC(I)-WIN/2.,0.),X2_MAX)
      I1=I+1
      DO I=I1,NN-1
         IF(THC(I).GT.0) THEN
            X=THC(I)+WIN/2.
            IF(X.LT.X1(N)) THEN                                                    
               N=N+1
               X2(N)=X
            ENDIF
            X1(N)=MAX(THC(I)-WIN/2.,0.)
         ENDIF
      ENDDO

      DO I=1,N
         SUM=SUM+X2(I)**4-X1(I)**4
         SUM1=SUM1+X2(I)**2-X1(I)**2
      ENDDO
      SUM=SUM/4
      SUM1=SUM1/2
 
      RETURN
      END
  

