      subroutine steb2(n,dat,eb,w,xm,xe) ! bernd berg, march 6, 1990.
c  input:   data (gaussian), error bars  and  (optional) weight
c           factors. (if weight factors are not given: put   
c           w(1).le.0  and weight factors will we calculated 
c           from the error bars eb.)
c  output:  mean value xm and its error bar xe.
c           weight factors are returned normalized to one.
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      dimension dat(n),eb(n),w(n)
c
c calculation of weights from error bars 
c (if weights are not explicitly given)
      if(w(1).le.zero) then
        do i=1,n
          w(i)=one/eb(i)**2
        end do
      end if
c
c normalize weight factors to one:
      wnorm=zero
      do i=1,n
        wnorm=wnorm+w(i)
      end do
      wnormi=one/wnorm
      do i=1,n
        w(i)=wnormi*w(i)
      end do
c
c weighted mean value:
      xm=zero
      do i=1,n
        xm=xm+w(i)*dat(i)
      end do
c
c weighted error:
      xe=zero
      do i=1,n
        xe=xe+w(i)**2*eb(i)**2
      end do       
      xe=sqrt(xe)
c
c variance:
      xv=xe*sqrt((n*one))
c
      return
      end
