Problem in plotting with fitted parameters and missing chann

Dear all,
I have used the TString for reading the data file and TMinuit is used for fitting the data but finally i have ploted the graph with fitted parameters , then it is giving erroer regarding K is not defined earlierr and in drawing the data ploting it is not plotting with full data and it is missing the first channel of the data to plot.
Hope to hear from you all soon.
please help in this problem…vivek

[code]

#include
#include
#include <iostream.h>
#include <fstream.h>
#include
#include
#include
#include “TGraph.h”
#include “TH1.h”
#include “TF1.h”
#include “TLegend.h”
#include “TCanvas.h”
#include “TMinuit.h”

using namespace std;
gROOT->Reset();

Float_t counts[1000],chan[1000],time[1000],error[1000];
Float_t fitcounts[1000],fittime[1000];
Float_t errorx[1000]=0;
Int_t lowchan=1;
Int_t hichan=12;
Int_t bin=1;
Int_t nchan=(hichan-lowchan)/bin; //Number of channels in spectrum
Float_t calib1=1;
Float_t calib2=0;

int vivek_fit();
//---------------------------------------------------------------------------
int main()
{
return vivek_fit();
}
//-------------------------------------------------------------------------------

void fcn(Int_t &npar, Double_t *gin, Double_t &f, Double_t par, Int_t iflag)
{
Int_t i;
//calculate chisquare
Double_t chisq = 0;
Double_t delta;
for (i=0;i<nchan;i++)
{
if(error[i]!=0.)then
{
delta = (counts[i]-func(time[i],par))/error[i];
chisq += delta
delta;
}
}
f = chisq;
}
//-------------------------------------------------------------------------------

Double_t func(float ttime,Double_t *par)
{
Double_t value=(1.5)*cos(par[0]+par[1]ttime)+par[2];
return value;
}
//------------------------------------------------------------------------------------
int vivek_fit() {
Int_t i,j,k;
Float_t temp, sum;
TString data_file(“test1.dat”);
cout<<data_file.Data()<<endl;
ifstream istream (data_file.Data());
k=0;
for(i=0;i<=hichan;i=i+bin) {
sum=0;
for(j=0;j<bin;j++) {
istream>>temp;
sum=sum+temp;
}
if(i>lowchan) {
counts[k]=sum;
chan[k]=i+bin;
time[k]=calib1
chan[k]+calib2;
error[k]=sqrt(counts[k]);
cout<<" nchan " <<nchan<<" i “<<i <<“k” <<k << time << time[k] << “chan”
<< chan[k]<<” " << counts[k] <<endl;
k++;
}
}

//make the graph of the data

TCanvas *c1 = new TCanvas(“c1”,“Spin rotation spectrum”,200,10,800,400);
TGraph *gr1 = new TGraphErrors(nchan,time,counts,0,error);
gr1->SetMarkerStyle(21);
gr1->SetMarkerSize(0.5);
gr1->SetTitle(“Spin rotation spectrum”);
gr1->GetXaxis()->SetTitle(“Time in ns”);
gr1->GetYaxis()->SetTitle(“R(t)”);
gr1->Draw(“AP”) ;

//initialize TMinuit with a maximum of 10 params
TMinuit *gMinuit = new TMinuit(10);
gMinuit->SetFCN(fcn);
Double_t arglist[10];
Int_t ierflg = 0;
arglist[0] = 1;
gMinuit->mnexcm(“SET ERR”, arglist ,1,ierflg);

// Set starting values and step sizes for parameters
static Double_t vstart[3] = {.01,0.01,0.01};
static Double_t step[3] = {0.01 ,0.01,0.01};
gMinuit->mnparm(0, “Phase”, vstart[0], step[0], -10.,10.,ierflg);
gMinuit->mnparm(1, “Omega”, vstart[1], step[1], 0.,100.,ierflg);
gMinuit->mnparm(2, “Bkgd”, vstart[1], step[1], 100.,200.,ierflg);

// Now ready for minimization step
arglist[0] = 500;
arglist[1] = 1.;
gMinuit->mnexcm(“MIGRAD”, arglist ,2,ierflg);

// To scan the data // gminuit->mnexcm(“SCAN”, arglist,22ierflg);

// Print results
Double_t amin,edm,errdef;
Int_t nvpar,nparx,icstat;
gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
gMinuit->mnprin(2,amin);

// Gets the fitted parameters from minuit (RESULTS) and print
Int_t i,k;
Double_t ferr[3], fpar[3];
cout <<endl<<endl<<endl<<"Final Parameters: “<<endl;
for(Int_t i=0; i<3;i++)
{
gMinuit->GetParameter(i,fpar[i], ferr[i]);
cout<<i <<” “<< fpar[i]<<” “<<ferr[i]<<” "<<endl;
}
k=0;
for (i=0;i<nchan;i++)
{
fittime[k]=calib1*i;
fitcounts[k]=func(fittime[k],fpar);
K++;
}

TGraph *gr2 = new TGraph(hichan-lochan,fittime,fitcounts);
gr2->SetMarkerStyle(21);
gr2->SetMarkerSize(0.5);
gr2->Draw(“L”) ;
return();
}[/code]

Your file is full of C++ errors. To discover them, I suggest to run your script.C file with the ACLIC comnpiler in ROOT, doing
root > .L script.C+

Rene