; $Id: fitinfo.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
FUNCTION fitinfo, fit, font=fontnum, ongraph=ongraph
;+ 
;	NAME: fitinfo
;
;	PURPOSE: Prints out a bunch of information concerning the
;		results of a MINUIT fit. See the MINUIT User's Manual for a description
;		of anything said here. Get it from Paul Schleger, if you can't find it.
;
;	CATEGORY: Internal function for TASCOM data analysis
;	CALLING SEQUENCE: string=fitinfo,fit,font=5,/ongraph
;	INPUT PARAMETERS: fit
;		fit is a structure which must have the following elements:
;			a :	The fit parameters
;			eplus :	The MINOS positive error bar	FLTARR(n_elements(a))
;			eminus: The MINOS negative error bar	FLTARR(n_elements(a))
;			eparab: The parabolic error bar		FLTARR(n_elements(a))
;			globcc: The global correlation coeff.	FLTARR(n_elements(a))
;			covar :	The external error matrix	FLTARR(total(vary),total(vary))
;			chisquared: The reduced chi-squared	FLOAT...
;			fmin  :	The best value for chi-squared found so far (NOTE: NOT reduced chisqr)
;			fedm  : The estimated distance to the minimum
;			errdef: The value of ERRDEF used for the calculation of the error bars. 
;				(Normally = 1.0)
;			istat :	An integer giving the status of the fit (See fitstat)
;			fitstat: A descriptive text specifying the status of the fit
;			npari : An integer giving the number of non-fixed parameters
;			nparx : An integer giving the index of the highest parameter number used
;			
;	COMMON BLOCKS: COMMON fitp,ngau,nlor,a,vary,pnames,gw,ufunct,nufun
;
;	fit={a:a,eplus:FLTARR(n_elements(a)),eminus:FLTARR(n_elements(a)),eparab:FLTARR(n_elements(a)),$
;	globcc:FLTARR(n_elements(a)),covar:FLTARR(TOTAL(vary),TOTAL(vary)),chisquared:chisquared,$
;	fmin:0.0,fedm:0.0,errdef:0.0,istat:0,fitstat:' ',npari:0,nparx:0}
;	MODIFICATION HISTORY: 
;		Paul Schleger, Riso National Laboratory, Oct 25. 1993
;-
COMMON fitp,ngau,nlor,a,vary,pnames,gw,ufunct,nufun
COMMON preferences, pref

;Check out the font
IF NOT keyword_set(fontnum) THEN fontnum=5
IF (fontnum LT 3) OR (fontnum GT 20) THEN fontnum=5

IF keyword_set(ongraph) THEN BEGIN
	fntstr = STRCOMPRESS('!'+STRING(fontnum,format='(I)'),/REMOVE_ALL) 	;Font used for data
	fntstr1 = '!4'								;Font used for titles
	IF !P.FONT EQ 0 THEN pmsym = STRCOMPRESS(fntstr+'!9!S!B!E-!R!A!I+!N',/REMOVE_ALL) $
	ELSE pmsym = '!M+'
	;Currently, this routine used the greek chi symbol, too
	IF !P.FONT EQ 0 THEN chisym = '!9c' ELSE chisym = '!7v'
	newline = '!c'
	TAB = '    '
END ELSE BEGIN
	fntstr = ' '
	fntstr1 = fntstr
	pmsym = '+/-'
	chisym = 'Chisq'
	newline = STRING('0d'XB)+STRING('0a'XB)
	TAB = STRING('09'XB)
END

formstr = '(G9.4)'				;Format for printing numbers

;First some checking
index=WHERE(vary,count)
IF (count EQ 0) THEN RETURN, 'MINUIT fit Info: No free fit parameters'
IF (count NE n_elements(fit.covar(0,*))) THEN RETURN, 'MINUIT fit Info: Invalid fit structure'

;line will contain the big string of stuff to print
line=strarr(6)
;First line will be some general info
line(0) = fntstr1+'MINUIT Fit Information'+newline
line(1) = fntstr1+'Chi-Squared:'+fntstr+STRING(fit.chisquared,FORMAT=formstr)+TAB+fntstr1+$
	'EDM:'+fntstr+STRING(fit.fedm,FORMAT=formstr)+newline+$
	fntstr1+'No. of non-fixed pars:'+fntstr+STRING(fit.npari,FORMAT='(I)')+fntstr1+$
	TAB+'ERRDEF'+fntstr+STRING(fit.errdef,formstr)+newline
line(2) = fntstr1+fit.fitstat+newline+newline

;Now print out the fit parameters and their error bars
line(3)=fntstr1+'MINUIT Fit Results'+newline
line(4)=fntstr+STRING('PARAMETER',' ','PARABOLIC','MINOS ERRORS    ',FORMAT='(3A11,A22)')+newline
line(5)=fntstr+STRING('NAME','VALUE','ERROR','POSITIVE','NEGATIVE',FORMAT='(5A11)')+newline
FOR i=0,n_elements(fit.a)-1 DO $
IF (vary(i)) THEN line=[line,STRING(getpname(i,ngau,nlor,pnames),'  ',fit.a(i),'  ',fit.eparab(i),'  ',$
	fit.eminus(i),'  ',fit.eplus(i),$
	FORMAT='(A12,A2,G9.4,A2,G9.4,A2,G9.4,A2,G9.4,A2)')+newline]

IF pref.multi_gauss EQ 0 THEN BEGIN	
;Now the correlation coefficients
line=[line,newline+fntstr1+'Correlation Coefficients'+newline]
;Calculate the Correlation coefficients from the external error matrix
;This is calculated by: Corr(i,j)=Covar(i,j)/SQRT(Covar(i,i)*Covar(j,j))
;Do it the "slow" way, because I'm tired and we don't ever have that many elements
corr=fltarr(count,count)
FOR i=0,count-1 DO FOR j=0,count-1 DO $
	corr(i,j)=fit.covar(i,j)/SQRT(fit.covar(i,i)*fit.covar(j,j))
stuff=fntstr+STRING(' ',FORMAT='(A11)')
FOR i=0,count-1 DO stuff=stuff+STRING(getpname(index(i),ngau,nlor,pnames),FORMAT='(A11)')
line=[line,stuff+newline]
FOR i=0,count-1 DO BEGIN
	stuff=fntstr+STRING(getpname(index(i),ngau,nlor,pnames),FORMAT='(A11)')
	FOR j=0,count-1 DO stuff=stuff+'  '+STRING(corr(i,j),FORMAT=formstr)
	line=[line,stuff+newline]
END
END

;Now concatenate into one big string
stuff=' '
FOR i=0,n_elements(line)-1 DO stuff=stuff+line(i)

RETURN, stuff+newline
END
