// decDoc.cpp : implementation of the CDecDoc class
//
#include "stdafx.h"
#include "dec.h"
#include "Dialogs.h"
#include "decDoc.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDecDoc

IMPLEMENT_DYNCREATE(CDecDoc, CDocument)

BEGIN_MESSAGE_MAP(CDecDoc, CDocument)
	//{{AFX_MSG_MAP(CDecDoc)
	ON_COMMAND(ID_DECAY_REDUCE, OnDecayReduce)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDecDoc construction/destruction

CDecDoc::CDecDoc()
{
	pds  = NULL;
	dsON = FALSE;						//DecaySheet id 
//============ Edit Service
	EditFrameON = FALSE;
	pEditFrame =  NULL;
	nl[0] = 13; nl[1] = 10; nl[2] = 0;

//============= Image Service
	ImageFrameON = FALSE;
	pImageFrame =  NULL;
	ImagePathName = ImageName = "";
	ImageFileSelected = FALSE;

//============= Decay Service
	DecayFrameON = FALSE;
	pDecayFrame =   NULL;
	XPathName = XName = FPathName = FName = DecayPathName = DecayName= "";
	XFileSelected = FFileSelected = DecayFileSelected = FALSE;
	nopx = nopf = nopd = 0;
	for(int i=0; i<16; i++)
	{
		Xsel[i] = Fsel[i] = Decsel[i] = FALSE;
	  NoX     = NoF     = NoDec     = 0;
	}
	fmax = 0; xmax = 0;

//============ Calculations
	ScatFree = FALSE;				
	Lamp     = TRUE;				//====== Lamp as x 
	FProbe   = FALSE;				//====== Fluor probe as x 
	NOIter   = 40;
	Iter     = 0;
	Resid    = 0;
	AnalComp	 = 1;
	AnalComp = 1;
	Tcal		 = 0.01;
	EmThreshold = 0.5;
	for(i = 0; i<5; i++)
	{
		InTau[i] = Tau[i] = 0.5+0.5*i; 
		InAlp[i] = Alp[i] = 0.3333333;
	}
	for(i = 0; i<5; i++) EMtoEX[i] = 1;
	EmThreshold = 0;
	Scatt = 0;
	for(i = 0; i<5; i++) TauFree[i] = TRUE;
	WeightOption = 1;				//===== i.e. 1/f
	FitReady = FALSE;
	Iter = 0;
	Resid = 0; 

//===============DeacyPlot
	DecPlotBottom = 0;

}

CDecDoc::~CDecDoc()
{
	if(!(pds == NULL))
	{
		pds->DestroyWindow();		
		pds = NULL;	
	}
}

BOOL CDecDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CDecDoc serialization

void CDecDoc::Serialize(CArchive& ar)
{
	// CEditView contains an edit control which handles all serialization
	((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
}

/////////////////////////////////////////////////////////////////////////////
// CDecDoc diagnostics

#ifdef _DEBUG
void CDecDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CDecDoc::Dump(CDumpContext& dc) const		

{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDecDoc commands

//==========  Decay Service


void CDecDoc::GetXProfile()
{
	CEdit& red = pEditView->GetEditCtrl();
	//red.ReplaceSel("X profile");
	CString buffedit, s, sline;
	buffedit = "";
	ifile.open(XPathName, ios::in);
	nopx = 0;
	xmax = 0.0;
	do
	{
		sline.Empty();
		sline.Format("%6d",nopx);
		ifile.getline(buffer,255,'\n');
		x[NoX][nopx] = float(atof(buffer));
		if(xmax < x[NoX][nopx]) xmax = x[NoX][nopx];
		s.Format("%8.0f",x[NoX][nopx]);
		sline += s;
		sline += nl;
		if(!ifile.eof()) buffedit += sline;
		nopx++;
	} while (!ifile.eof());
	ifile.close();
	red.ReplaceSel(buffedit);
	red.SetSel(0,0);
	red.Clear();
	Xsel[NoX]	= TRUE;
	NoX++;
	NoChan = nopx - 1;
	if(xmax > fmax) vmax = xmax;
}

void CDecDoc::GetFProfile()
{
	CEdit& red = pEditView->GetEditCtrl();
	red.SetSel(0,-1);
	red.Clear();
	//red.ReplaceSel("F profile");
	CString buffedit, s, sline;
	buffedit = "";
	ifile.open(FPathName, ios::in);
	nopf = 0;
	fmax = 0.0;
	do
	{
		sline.Empty();
		sline.Format("%6d",nopf);
		if(Xsel[NoF])
		{
			s.Format("%8.0f",x[NoF][nopf]);
			sline += s;
		}
		ifile.getline(buffer,255,'\n');
		f[NoF][nopf] = float(atof(buffer));
		if(fmax < f[NoF][nopf]) fmax = f[NoF][nopf];
		s.Format("%8.0f",f[NoF][nopf]);
		sline  += s;
		sline += nl;
		if(!ifile.eof()) buffedit += sline;
		nopf++;
	} while (!ifile.eof());
	ifile.close();
	red.ReplaceSel(buffedit);
	red.SetSel(0,0);
	red.Clear();
	Fsel[NoF] = TRUE;
	NoF++;
	NoChan = nopf - 1;
	if(fmax > xmax) vmax = fmax;
}

void CDecDoc::GetDecayProfile()
{
	CEdit& red = pEditView->GetEditCtrl();
	red.SetSel(0,-1);
	red.Clear();
	CString buffedit, sline;
	CString sx[16],sf[16];
	int idata;
	double Noxc = 0, Nofc = 0;
	buffedit = "";
	ifile.open(DecayPathName, ios::in);
	nopd  = 0;
	NoDec = 0;
	fmax  = 0.0;  xmax = 0.0;
	do
	{
		sline.Empty();
		for(idata=0; idata<16; idata++)
		{
			sx[idata].Empty();
			sf[idata].Empty();
		}
		idata = 0;
		ifile.getline(buffer,255,'\n');
		for(int i=6; i<15; i++)
		{
			sx[idata] += buffer[i];
			sf[idata] += buffer[i + 8];
		}
		x[idata][nopd] = float(atof(sx[idata]));
		f[idata][nopd] = float(atof(sf[idata]));

		Noxc += x[idata][nopd];
		Nofc += f[idata][nopd];
	
		if(xmax < x[idata][nopd]) xmax = x[idata][nopd];
		if(fmax < f[idata][nopd]) fmax = f[idata][nopd];
		sline  = buffer;
		sline += nl;
		if(!ifile.eof()) buffedit += sline;
		nopd++;
	} while (!ifile.eof());
	ifile.close();
	if(Noxc > 0) EMtoEX[0] = Nofc/Noxc;
	red.ReplaceSel(buffedit);
	red.SetSel(0,0);
	red.Clear();
	Decsel[NoDec] = TRUE;
	NoF++;
	NoChan = nopd - 1;
	if(xmax > fmax) vmax = xmax;
		else vmax = fmax;
	AnalFirst = 0;
	AnalLast = NoChan;
}


//========  FLuorView Utilities

void CDecDoc::Ticks()
{
	if(xmax > fmax) vmax = xmax;
		else vmax = fmax;
	double rr = 1.0;
	if(vmax <= 10) rr = 1.0; 
		else if(vmax < 100) rr = float(0.1);
		else if(vmax < 1000) rr = 0.01;
		else if(vmax < 10000) rr = 0.001;
		else if(vmax < 100000) rr = 0.0001;
		else if(vmax < 1000000) rr = 0.00001;
	vmax *= rr;
	tickval = 0.125;
	if (vmax > 5) tickval = 1; 
		else if(fmax > 2.5) tickval = 0.5;
		else if(fmax > 1.25) tickval = 0.25;
  tickno = int(vmax/tickval) + 1;
	tickval /= rr;
	vmax = tickno * tickval;
}


//========  Calculations  ==========

void CDecDoc::Inver(int dim)
{
	int i,j,k,l;
	double  save,amax;
  int     ik[13], jk[13];

  for(k=0; k<dim; k++)			//find the largest element in rest of the matrix
	{
		i = -1;
		j = -1;
    amax = 0;
    while (j < k)
		{
      while (i < k)
			{
        for(i=k; i<dim; i++)
				{
					for(j=k; j<dim; j++)
          {
            if(fabs(matrix[i][j]) > fabs(amax))
						{
							amax = matrix[i][j];
              ik[k] = i;
              jk[k] = j;
            }
          }
				}
        i = ik[k];
			}
      if(i > k)
      {
        for(j=0; j<dim; j++)
				{
          save = matrix[k][j];
          matrix[k][j] = matrix[i][j];
          matrix[i][j] = save;
        }
      }
      j = jk[k];
		}
    if(j > k)
    {
      for(i=0; i<dim; i++)
      {
        save = matrix[i][k];
        matrix[i][k] = matrix[i][j];
        matrix[i][j] = -save;
      }
    }
    for(i=0; i<dim; i++)
			if (!(i == k)) matrix[i][k] = -matrix[i][k]/amax;
    for(i=0; i<dim; i++)
    {
      for(j=0; j<dim; j++)
      if((!(i == k)) && (!(j == k))) 
				matrix[i][j] += matrix[i][k] * matrix[k][j];
    }
    for(j=0; j<dim; j++) 
			if(!(j == k)) matrix[k][j] = matrix[k][j]/amax;
    matrix[k][k] = 1.0/amax;
	}
  for(l=0; l<dim; l++)
  {
    k = dim - l-1;
		j = ik[k];
    if(j > k)
		{
			for(i=0; i<dim; i++)
      {
				save = matrix[i][k];
				matrix[i][k] = -matrix[i][j];
				matrix[i][j] = save;
			}
		}
    i = jk[k];
    if(i > k)
		{
			for(j=0; j<dim; j++)
			{
				save = matrix[k][j];
				matrix[k][j] = -matrix[i][j];
				matrix[i][j] = save;
			}
    }
  }
}

void CDecDoc::StepFirstLamp()
{
	int idata = 0;			// Assume for now;
	int ip = 0;
	double ff[5] = {0}, w[5] = {0};
	double ei, eii, rip;
  int nscat = 2*AnalComp + 1;
  double lowlim = 0;
  double thalf = Tcal/2;	
	double fitt = 0;
  double limth = 0;
  int nfree		= 1;

	for(int ic = 0; ic < AnalComp; ic++)
	{
		w[ic] = exp(-Tcal/Tau[ic]);
		ff[ic] = 0;
	}
	nterm = 2*AnalComp;
  if(ScatFree) nterm = 2 * AnalComp + 1;
	Resid = 0;
  ei = 0; 
  for(ip = AnalFirst; ip < AnalLast+1; ip++)
  {
	  fitt = 0;
		eii = x[idata][ip];
    for(ic = 0; ic<AnalComp; ic++)
		{
			ff[ic] = (ff[ic] + thalf * ei) * w[ic] + thalf * eii;
      fitt = fitt + Alp[ic] * ff[ic];
		}
    if(ScatFree) fitt = fitt + Scatt * eii;
    fit[idata][ip] = float(fitt);
    if ((ip > FirstChann) && (f[idata][ip] > CountThreshold))
		{
      nfree++; 
      rip = f[idata][ip] - fitt;
      Resid += vari[idata][ip] * rip * rip;
		}
    ei = eii;
	}
  ResidLast = Resid/nfree;
}

void CDecDoc::StepLamp()
{
	int idata = 0;			// Assume for now;
	double ff[5] = {0}, df[5] = {0}, w[5] = {0}, tau2[5] = {0};
	double tAlp[5] = {0}, tTau[5] = {0}, tScatt;
	double  deriv[11] = {0}, matr[11][11] = {0}, coll[11] = {0};
	double matrin[11][11] = {0}, matres[11][11] = {0};
	double trialsol[11] = {0};
  int nscat = 2*AnalComp + 1;
  double lowlim = 0;
  double thalf = Tcal/2;
	int ic, jc;
  double ei, eii, rip;
	double fitt = 0;
  double limth = 0;
  int nfree		= 1, ip = 0;

	nterm = 2*AnalComp;
	Resid = 0;
	for(ic = 0; ic<AnalComp; ic++)
	{
		if(Tau[ic] < 0.001) Tau[ic] = 0.001;
    w[ic] = exp(-Tcal/Tau[ic]);
		tau2[ic] = 1/(Tau[ic] * Tau[ic]);
		ff[ic] = 0;
	}
	if(ScatFree) nterm = 2 * AnalComp + 1;
	ei = 0; 
  for(ip = AnalFirst; ip < AnalLast; ip++)
	{
    eii= x[idata][ip];
    for(ic = 0; ic < AnalComp; ic++)
		{
      ff[ic] = (ff[ic] + thalf * ei) * w[ic] + thalf*eii;
      deriv[AnalComp + ic] = ff[ic];
      if (TauFree[ic]) 
			{
        df[ic] = (df[ic] + Tcal * tau2[ic] * (ff[ic] + thalf * ei)) * w[ic];
        deriv[ic] = Alp[ic] * df[ic];
			}
		}
    if(ScatFree) deriv[nscat] = eii;
    if(1 == 1)											//modify later
		{
      nfree ++;
      rip = f[idata][ip] - fit[idata][ip];
			for(ic = 0; ic < nterm; ic++)
			{
				for(jc = 0; jc < nterm; jc++)
				matr[ic][jc] +=vari[idata][ip] * deriv[ic] * deriv[jc];
			}
      for(ic = 0; ic < nterm; ic++)
      coll[ic] += vari[idata][ip] * deriv [ic] * rip;
    }
    ei  = eii;
	}

  for(ic = 1; ic < nterm; ic++)
	{
	  if(matr[ic][ic] < 1e-15) matr[ic][ic] = 1e-15;
	}
  do
	{
		for(ic = 0; ic < nterm; ic++)
		{
			for(jc = 0; jc < nterm; jc++)
			matrix[ic][jc] = matr[ic][jc]/(sqrt(matr[ic][ic]) * sqrt(matr[jc][jc]));
		}
    for(ic = 0; ic < nterm; ic++)     
		matrix[ic][ic] = 1+lambda;

//		matrix[0][0] = 1;
//		matrix[0][1] = 2;
//		matrix[1][0] = 2;
//		matrix[1][1] = 1;

		for (ic=0; ic<nterm; ic++)
		for(jc=0; jc<nterm; jc++) matrin[ic][jc] = matrix[ic][jc];

		Inver(nterm);

		for(ic=0; ic<nterm; ic++)
		{
			for(jc=0; jc<nterm; jc++)
			{
				matres[ic][jc] = 0;
				for(int iic=0; iic<nterm; iic++) matres[ic][jc] += matrin[ic][iic]*matrix[iic][jc];
			}
		}
		
		for(ic = 0; ic < AnalComp; ic++) trialsol[ic] = Tau[ic];
		for(ic = 0; ic < AnalComp; ic++) trialsol[ic + AnalComp] = Alp[ic];
    trialsol[nscat] = Scatt;
    for(ic = 0; ic < nterm; ic++)
		{
			for(jc = 0; jc < nterm; jc++)  
      trialsol[ic] = trialsol[ic]+coll[jc]*matrix[ic][jc]/(sqrt(fabs(matr[ic][ic]))*sqrt(fabs(matr[jc][jc])));
		}
		if(ScatFree) tScatt = trialsol[nscat];
			else tScatt = Scatt;
    if(tScatt < 0) tScatt = 0;
    if(tScatt > 2) tScatt = 2;
    for(ic = 0; ic < AnalComp; ic++)
		{
			if(TauFree[ic]) tTau[ic] = trialsol[ic]; else tTau[ic] = Tau[ic];
      if(tTau[ic] < 0.001) tTau[ic] = 0.001;
      tAlp[ic] = trialsol[AnalComp + ic];
			if (tAlp[ic] > 1) tAlp[ic] = 1;
			if (tAlp[ic] < -1) tAlp[ic] = -1;
      w[ic] = exp(-Tcal/tTau[ic]);
			ff[ic] = 0;
		}

		Resid = 0;
    ei = 0;
    for(ip = AnalFirst; ip < AnalLast+1; ip++) 
    {
      fitt = 0;
      double eii = x[idata][ip];
      for(ic = 0; ic < AnalComp; ic++)
      {
        ff[ic] = (ff[ic] + thalf * ei) * w[ic] + thalf * eii;
        fitt = fitt + tAlp[ic] * ff[ic];
      }
      if(ScatFree) fitt = fitt + trialsol[nscat] * eii;
		else
			if(Scatt > 0) fitt = fitt + tScatt * eii;
      fit[idata][ip] = float(fitt);
		if(1 == 1) 
      {
        rip = f[idata][ip]-fit[idata][ip];
        rip = vari[idata][ip] * rip * rip;
        Resid = Resid + rip;
      }
      ei = eii;
    }
		lambda = lambda * 3;
		Resid = Resid/nfree;
	}while((Resid > ResidLast) && (lambda < 1e12));

	for(ic = 0; ic < AnalComp; ic++)
	{
		Tau[ic] = tTau[ic];
    Alp[ic] = tAlp[ic];
	}
	if(ScatFree) Scatt = tScatt;
  for(ic = 0; ic < AnalComp; ic++) 
	{
    if(TauFree[ic]) Sigma[ic] = sqrt(fabs(matrix[ic][ic]/matr[ic][ic]));
		else
    Sigma[ic] = 0;
    Sigma[AnalComp + ic] = sqrt(fabs(matrix[ic + AnalComp][ic + AnalComp]/matr[ic+AnalComp][ic + AnalComp]));
  }
  if(lambda > 0.3) lambda = lambda/3;
	for(ip = AnalFirst; ip < AnalLast+1; ip++) 
		res[idata][ip] = f[idata][ip] - fit[idata][ip];

}

void CDecDoc::DoWeight()
{
	int idata = 0, ichan;					//for now
  if(WeightOption == 0)
	{
		for(ichan = AnalFirst; ichan < AnalLast+1; ichan++) 
		vari[idata][ichan] = 1;
	}
	else if(WeightOption == 1)
	{
		for(ichan = AnalFirst; ichan < AnalLast+1; ichan++) 
    if(f[idata][ichan] > 10) vari[idata][ichan] = 1/f[idata][ichan];
    else vari[idata][ichan] = 1/10;
  }
  else if(WeightOption == 2)
	{
		for(ichan = AnalFirst; ichan < AnalLast+1; ichan++)
    if(fit[idata][ichan] > 1) vari[idata][ichan] = 1/fit[idata][ichan];
    else vari[idata][ichan] = 1;
	}
}

void CDecDoc::TausOut()
{
	pds->fit.fitin(Tau[0],				IDC_TAU0,         "%3.4f");
	pds->fit.fitin(Alp[0],				IDC_ALP0,         "%2.3f");
	if(AnalComp > 1)
	{
		pds->fit.fitin(Tau[1],      IDC_TAU1,         "%3.4f");
		pds->fit.fitin(Alp[1],      IDC_ALP1,         "%2.3f");
	} 
	else
	{
		pds->fit.blankin(IDC_TAU1);
		pds->fit.blankin(IDC_ALP1);
	}	
	if(AnalComp > 2)
	{
		pds->fit.fitin(Tau[2],      IDC_TAU2,         "%3.4f");
		pds->fit.fitin(Alp[2],      IDC_ALP2,         "%2.3f");
	} 
	else
	{
		pds->fit.blankin(IDC_TAU2);
		pds->fit.blankin(IDC_ALP2);
	}
	if(AnalComp > 3)
	{
		pds->fit.fitin(Tau[3],      IDC_TAU3,         "%3.4f");
		pds->fit.fitin(Alp[3],      IDC_ALP3,         "%2.3f");
	} 
	else
	{
		pds->fit.blankin(IDC_TAU3);
		pds->fit.blankin(IDC_ALP3);
	}		
	if(AnalComp > 4)
	{
		pds->fit.fitin(Tau[4],      IDC_TAU4,         "%3.4f");
		pds->fit.fitin(Alp[4],      IDC_ALP4,         "%2.3f");
	} 
	else
	{
		pds->fit.blankin(IDC_TAU4);
		pds->fit.blankin(IDC_ALP4);
	}
}

void CDecDoc::Iterate()
{
	//if(!(TimeShift == 0) DoTimeShift();
	CRect UpdateArea; 
	CRect region; 
	pDecayView->GetClientRect(&UpdateArea);
	
	UpdateArea.left +=50;
	UpdateArea.bottom -= 240;

	DoWeight(); 
  NFree = AnalLast - AnalFirst - nterm;
	int idata = 0;												//for now
	//for(int i = 0; i < AnalComp; i++) Alp[i] = 1.0/AnalComp;
	//for(i = 0; i < AnalComp; i++) if(Tau[i] == 0) Tau[i] = 1.0*i;
	//for(i = 0; i < AnalComp; i++) Alp[i] = Alp[i] * EMtoEX[idata]/Tau[i];
  if(Lamp) StepFirstLamp();
  ResidLast = Resid; 
  lambda = 0.1;
	for(Iter = 0; Iter < NOIter; Iter++)
	{
		StepLamp();
		FitReady = TRUE;
		pDecayView->InvalidateRect(UpdateArea,TRUE);
		pDecayFrame->UpdateWindow();
		ResidLast = Resid;
		pds->fit.fitin(Iter,	IDC_ITER,  "%5d");
		pds->fit.fitin(Resid,	IDC_CHISQ, "%3.8f");
		TausOut();
	}
}

void CDecDoc::OnDecayReduce()
{	
	if(pds == NULL)
	{
		pds = new CDecaySheet("DecaySheet",pDecayView);
		pds->Create(pDecayView, 0, 0); 
		pds->fit.pDecayView = pDecayView;
		pds->GetWindowRect(&dsPosition);
		dsVer = dsPosition.bottom - dsPosition.top;
		dsHor = dsPosition.right - dsPosition.left;
		pDecayFrame->GetWindowRect(&DecayFramePosition);
		pds->MoveWindow(0, 400, dsHor, dsVer,TRUE);
		DecPlotBottom = 230;
		pDecayFrame->Invalidate();
		pDecayFrame->UpdateWindow();
		pDecayFrame->BringWindowToTop();
		dsON = TRUE;
		FitPageIn();
	}	
	else Iterate();
}


void CDecDoc::FitPageIn()
{	
	int nID;
	int set;
	pds->fit.fitin(Tcal,        IDC_TCAL,         "%1.6f");
	pds->fit.fitin(AnalFirst,   IDC_ANALFIRST,    "%5d");
	pds->fit.fitin(AnalLast,    IDC_ANALLAST,     "%5d");
	pds->fit.fitin(EmThreshold, IDC_EM_THRESHOLD, "%3.3f");
	pds->fit.fitin(NOIter,      IDC_NOITER,       "%5d");
	TausOut();
	if(Lamp)          nID = IDC_LAMP_EXCITATION;
		else if(FProbe) nID = IDC_PROBE_EXCITATION;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(1);

	if (WeightOption				== 0) nID = IDC_NONEWEIGHT;
		else if (WeightOption == 1)	nID = IDC_EMWEIGHT;
		else if (WeightOption == 2)	nID = IDC_EMFITWEIGHT;
		else if (WeightOption == 3)	nID = IDC_FITWEIGHT;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(1);

	if(TauFree[0]) set = 1; else set = 0;		
	nID = IDC_TAUFREE0;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(set);
	if(TauFree[1]) set = 1; else set = 0;			
	nID = IDC_TAUFREE1;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(set);
	if(TauFree[2]) set = 1; else set = 0;			
	nID = IDC_TAUFREE2;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(set);
	if(TauFree[3]) set = 1; else set = 0;			
	nID = IDC_TAUFREE3;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(set);
	if(TauFree[4]) set = 1; else set = 0;			
	nID = IDC_TAUFREE4;
	pbutton = (CButton*)pds->fit.GetDlgItem(nID);
	pbutton->SetCheck(set);

	pds->fit.fitin(Iter,      IDC_ITER,       "%5d");
	pds->fit.fitin(Resid,     IDC_CHISQ,      "%3.8f");
	pds->fit.fitin(AnalComp,  IDC_NOCOMP,     "%2d");
}


void CDecDoc::FitPageOut()
{
	Tau[0] = pds->fit.fitout(IDC_TAU0);
	if (AnalComp >1) Tau[1] = pds->fit.fitout(IDC_TAU1);
	if (AnalComp >2) Tau[2] = pds->fit.fitout(IDC_TAU2);
	if (AnalComp >3) Tau[3] = pds->fit.fitout(IDC_TAU3);
	if (AnalComp >4) Tau[1] = pds->fit.fitout(IDC_TAU4);
	AnalComp =  int(pds->fit.fitout(IDC_NOCOMP));
	AnalFirst = int(pds->fit.fitout(IDC_ANALFIRST));
	AnalLast =  int(pds->fit.fitout(IDC_ANALLAST));
	EmThreshold =   pds->fit.fitout(IDC_EM_THRESHOLD);
	NOIter =    int(pds->fit.fitout(IDC_NOITER));
	Tcal        =   pds->fit.fitout(IDC_TCAL);
	
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_TAUFREE0);
	TauFree[0] = pbutton->GetCheck();
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_TAUFREE1);
	TauFree[1] = pbutton->GetCheck();
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_TAUFREE2);
	TauFree[2] = pbutton->GetCheck();
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_TAUFREE3);
	TauFree[3] = pbutton->GetCheck();
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_TAUFREE4);
	TauFree[4] = pbutton->GetCheck();

	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_NONEWEIGHT);
	if(pbutton->GetCheck()) WeightOption = 0;
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_EMWEIGHT);
	if(pbutton->GetCheck()) WeightOption = 1;
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_EMFITWEIGHT);
	if(pbutton->GetCheck()) WeightOption = 2;
	pbutton = (CButton*)pds->fit.GetDlgItem(IDC_FITWEIGHT);
	if(pbutton->GetCheck()) WeightOption = 3;

	OnDecayReduce();
}

void CDecDoc::DoFitDialog()
{
	
}


