	subroutine confit(violat)
c
	include '/u1/gcf/e350/crystal/cb/beam'
	include '/u1/gcf/e350/crystal/cb/hotsun'
	include '/u1/gcf/e350/crystal/cb/irspec'
	include '/u1/gcf/e350/crystal/cb/parms'
	include '/u1/gcf/e350/crystal/cb/it'
	include '/u1/gcf/e350/crystal/cb/pperpd'
	include '/u1/gcf/e350/crystal/cb/igr'
	include '/u1/gcf/e350/crystal/cb/where'
	include '/u1/gcf/e350/crystal/cb/wenbin'
	include '/u1/gcf/e350/crystal/cb/clouds'
	include '/u1/gcf/e350/crystal/cb/type'
	include '/u1/gcf/e350/crystal/cb/datbin'
	include '/u1/gcf/e350/crystal/cb/norm'
	include '/u1/gcf/e350/crystal/cb/abssiz'
	include '/u1/gcf/e350/crystal/cb/const'
	external regge,regal,resp,respa,tfixed,bmfunc
	include '/u1/gcf/e350/crystal/cb/x'
	dimension deriv(10)
	logical violat
c
	iprint=1
	isavf1=nobeam
	isavf2=moint
	violat=.false.
	if (.not.fitbem) goto 1
	pinc=param(ibeam)
c	this upper limit when fitting beam energy used to be 102 at 100 gev.
c	doubled but not critical - only keeps nonlinear fit in reasonable
c	range
	if (pinc.lt.204.) goto 6
	violat=.true.
	param(ibeam)=204.
	pinc=204.
6	call bemset
1	if (.not.fitsga) goto 11
	if (abs(param(isigma)/sigman-1.).lt.1.) goto 2
	violat=.true.
	param(isigma)=sigman
2	sigma=param(isigma)
11	if (ntr.eq.0) goto 17
	do 13 i=1,ntr
	if (.not.ftnorm(i)) goto 44
	if (i.eq.2.and.(.not.nfs)) goto 44
	if (param(inorm(i)).gt.0.) goto 44
	violat=.true.
	param(inorm(i))=1.e-7
44	if (.not.fitalp(1)) goto 13
	if (iusalp(i).ne.i) goto 13
	if (abs(param(ialpha(i))).lt.5.) goto 13
	violat=.true.
	param(ialpha(i))=alfain(i)
13	continue
c
17	if (.not.ftbrem) goto 14
	pbb=param(ibrem)
c
14	if (.not.fixed(1)) goto 23
	if (.not.fitfix) goto 23
c	changed to -100 from 0 in a desparate attempt to make things work.
	if (param(ifixed).gt.-100.) goto 24
	violat=.true.
	param(ifixed)=1.e-7
	goto 23
24	pff=param(ifixed)
	print 999,pff
999	format(' debug pff='g14.3)
	if (fxlo.lt.1.e-6.and.fxhi.gt.1.e9) goto 23
c	calculate integral over fixed mass term
	noint=noinfx
	it=1
	igr=jpart(igrp(it))
	ello=e1(it)
	ia=ie1(it)
	ib=ie2(it)
	thf=0.
	do 20 i=ia,ib
	call fixlim(ello,de(it),ido)
	if (ido.eq.1) goto 20
	thf=thf+calct(ello,tfixed,resp,bmfunc)
20	ello=ello+de(it)
	print 998,thf
998	format(' debug  thf'g14.3)
	if (thf.gt.fxlo) goto 18
	violat=.true.
	param(ifixed)=fxlo*param(ifixed)/thf
	goto 23
18	if (thf.lt.fxhi) goto 23
	violat=.true.
	param(ifixed)=fxhi*param(ifixed)/thf
c
23	do 19 it=1,notrig
	plimp=plim2(1)
	jp=jpart(igrp(it))
	ia=ie1(it)
	ib=ie2(it)
	ello=e1(it)
	do 4 i=ia,ib
	pperpd=ptoe(i,it)
	t=0.
	nobeam=1
	moint=1
c	changed from 100 gev value of 79.99
	if(ello.gt.159.99) nobeam=isavf1
	if(ello.gt.159.99) moint=isavf2
	call bemset
	call wgtint(0.,de(it),moint,val1(1,it),wgt1(1,it))
	do 25 ik=1,npar
25	deriv(ik)=0.
	if (ntr.eq.0) goto 3
c
c	calculate theory and derivatives for regge terms
	noint=nointr
	do 10 j=1,ntr
	if (jrspec.eq.0) goto 22
	x1=x1a(j)
	x2=x2a(j)
22	continue
	call reglim(ello,de(it),ido,j,fixed(igrp(it)))
	if (ido.ne.0) goto 10
	const=conal(j)
	if (fitalp(1)) alpha=param(ialpha(iusalp(j)))
	if (.not.fitalp(1)) alpha=alfain(iusalp(j))
	tp=calct(ello,regge,resp,bmfunc)
	if (ftnorm(j)) goto 8
	if (j.ne.2) call abort
	if (jp.le.2) pp=plimp
	if (jp.eq.3.or.jp.eq.5) pp=plimp*param(inorm(1))
	if (jp.eq.4.or.jp.eq.6) pp=0.
	goto 9
8	pp=param(inorm(j))
	if ((jpart(igrp(it)).eq.4.or.jpart(igrp(it)).eq.6).and.j.eq.2)
& goto 21
	if (j.eq.2.and.(.not.nfs).and.jpart(1).eq.1) goto 15
	deriv(inorm(j))=tp
	goto 9
21	pp=0.
	deriv(inorm(j))=0.
	goto 9
15	deriv(inorm(j))=alimf2*sin(2.*pp)*plimp*tp
	pp=(alimf2*(sin(pp))**2+alimf1)*plimp
9	t=t+tp*pp
	if (fitalp(1)) deriv(ialpha(iusalp(j)))=deriv(ialpha(iusalp(j)))
& +pp*calct(ello,regal,resp,bmfunc)
	if (fitbem) deriv(ibeam)=deriv(ibeam)+pp*calcs(ello,regge,
& resp,bmfunc)
	if (fitsga) deriv(isigma)=deriv(isigma)+pp*calct(ello,regge,
& respa,bmfunc)
10	continue
c
c	calculate theory and derivatives for fixed mass terms
3	if (.not.fixed(igrp(it))) goto 5
	noint=noinfx
	igr=jpart(igrp(it))
	call fixlim(ello,de(it),ido)
	if (ido.ne.0) goto 5
	tp=calct(ello,tfixed,resp,bmfunc)
	t=t+tp
	if (fitfix) deriv(ifixed)=tp/pff
	if (fitbem) deriv(ibeam)=deriv(ibeam)+calcs(ello,tfixed,
& resp,bmfunc)
	if (fitsga) deriv(isigma)=deriv(isigma)+calct(ello,
& tfixed,respa,bmfunc)
c
5	if (.not.ftbrem) goto 7
c	calculate bremstrahlung term
c	if (ido.ne.0) goto 7
c	tp=calct(ello,brem,resp,bmfunc)
c	t=t+tp*pbb
c	deriv(ibrem)=tp
c	if (fitbem) deriv(ibeam)=deriv(ibeam)+pbb*calcs(ello,brem,
c& resp,bmfunc)
c	if (fitsga) deriv(isigma)=deriv(isigma)+pbb*calct(ello,brem,
c& respa,bmfunc)
c
7	if (ftest(i,it).gt.t) goto 27
	ext=sqrt(amax1(ftest(i,it),binb(i,it))/t)
	if (ext.gt.1.) goto 27
	err=ebinb(i,it)*ext
	do 29 ik=1,npar
29	deriv(ik)=deriv(ik)*err*(1.0-0.5*(1.-binb(i,it)/t))
	goto 30
27	err=ebinb(i,it)*amin1(1.,sqrt(amax1(ftest(i,it),binb(i,it))/
& amax1(ftest(i,it),t)))
	do 31 ik=1,npar
31	deriv(ik)=deriv(ik)*err
30	value=err*(t-binb(i,it))
	call accum(value,deriv)
4	ello=ello+de(it)
19	continue
	nobeam=isavf1
	moint=isavf2
	call bemset
	do 16 i=1,notrig
16	call wgtint(0.,de(i),moint,val1(1,i),wgt1(1,i))
	return
c
	end
