void fit_adc(const char *type="ADC",
const char *iname="HIST.root",
const char *oname="PS.ps"
)
{
gROOT->SetStyle("Plain");
gStyle->SetOptStat(0);

gSystem->Load("../libFemCalib.so");
TFile *input = new TFile(iname);

TPostScript *ofile = new TPostScript(oname,111);
TCanvas *fem = new TCanvas("fem","",600,800);
fem->Divide(2,4);
ofile->NewPage();

const int NPMT = 32;

double para0[NPMT];
double para1[NPMT];
double tmp;

int xmin = 200;
int xmax = 4000;

int j=1;
int femid=1;
char hist[50];
char file[50];
char histname[50];

TF1 *f1;

TH1F *h_slope;
TH1F *h_constant;

if(type=="ADC"){
h_slope = new TH1F("h_slope","Gain [dac/adc ch]",150,0,15);
h_constant = new TH1F("h_constant","Pedestal [adc [ch]]", 100,600,1100);
}

ofstream fout("qscdata.txt",ios::out);

for (int i=0; i<NPMT; i++){
if(type=="ADC"){
sprintf(hist,"adc_%d",i);
TH1F *fig = (TH1F*)gROOT->FindObject(hist);
fig->SetXTitle("DAC Value");
fig->SetYTitle("ADC [ch]");
sprintf(histname,"FEM%d_ADC_%dch",femid,j-1);
fig->SetTitle(histname);
fig->SetMarkerSize(0.5);
//fout.open("qscdata.txt",ios::out);
}
else if(type=="TDC0"){
sprintf(hist,"tdc0_%d",i);
TH1F *fia = (TH1F*)gROOT->FindObject(hist);
fia->SetXTitle("Delay [ps]");
fia->SetYTitle("TDC0 [ch]");
sprintf(histname,"FEM%d_TDC0_%dch",femid,j-1);
fia->SetTitle(histname);
fia->SetMarkerSize(0.5);
fout.open("tscdata0.txt");
}
else if(type=="TDC1"){
sprintf(hist,"tdc1_%d",i);
TH1F *fib = gROOT->FindObject(hist);
fib->SetXTitle("Delay [ps]");
fib->SetYTitle("TDC1 [ch]");
sprintf(histname,"FEM%d_TDC1_%dch",femid,j-1);
fib->SetTitle(histname);
fib->SetMarkerSize(0.5);
fout.open("tscdata1.txt");
}
else{
cout << "input ADC or TDC0 or TDC1 !" << endl;
return 0;
}

fem->cd(j);
gPad->SetGrid();

// frequency of external clock = 9.38258 [MHz]
// 1 clock of external clock = 106.5805 [ns]
// 1 delay of GTM = (1clk)*4/256 = [ns]

float clock = 106.5805;


float min=400;       <-  set minimun QVC
float max=3900;     <-  set maximun QVC

char num[256];
TText *t[2];
f1 = new TF1("f1","pol1");
f1->SetLineWidth(0.5);
f1->SetLineColor(2);
f1->SetParameters(0.,0.);
if(type=="ADC") {
fig->Draw();
//fig->Fit("f1","N","",min,max);
fig->Fit("f1","","",min,max);
cout << f1->GetParameter(0) << " " << 1/f1->GetParameter(1)<<" "<<f1->GetChisquare()/f1->GetNDF()<<endl;
fout <<i<<" "<<f1->GetParameter(0)<<" "<<1/f1->GetParameter(1)<<" "<<f1->GetChisquare()/f1->GetNDF()<<endl;
sprintf(num,"pedestal %f", f1->GetParameter(0));
t[0] = new TText(2000,500, num);
t[0]->SetTextSize(0.1);
t[0]->Draw("same");
sprintf(num,"slope [dac/ch] %f", 1/f1->GetParameter(1));
t[1] = new TText(2000,400, num);
t[1]->SetTextSize(0.1);
t[1]->Draw("same");
h_constant->Fill(f1->GetParameter(0));
h_slope->Fill(1/f1->GetParameter(1));
}
else if(type=="TDC0") {
fia->Draw();
fia->Fit("f1","N","",min*clock*4./256.,max*clock*4./256.);
cout << f1->GetParameter(0) << " " << f1->GetParameter(1)<<endl;
//fout << f1->GetParameter(0) << " " << f1->GetParameter(1)<<endl;
sprintf(num,"pedestal %f", f1->GetParameter(0));
t[0] = new TText(310,2300, num);
t[0]->SetTextSize(0.1);
t[0]->Draw("same");
sprintf(num,"slope [ps/ch] %f", -1*1000/f1->GetParameter(1));
t[1] = new TText(310,2000, num);
t[1]->SetTextSize(0.1);
t[1]->Draw("same");
}
else if(type=="TDC1") fib->Draw();

j++;

if(j%9==0 && i!=NPMT){
fem.Update();
ofile->NewPage();
cout << "FEM #" << femid << " was printed" << endl;
j=1;
femid++;
if(i==127)break;
}
}

fout.close();

fem->Clear();
ofile->NewPage();
fem->Divide(1,2);
fem->cd(1);
fem->Update();
h_constant->Draw();
fem->cd(2);
h_slope->Draw();
fem->Update();

ofile.Close();
}