#include <TH1.h>
#include <TH2.h>
#include <TCanvas.h>
#include <fstream>

void graphmulticol(){
  //Define parameters common to all analysis configurations
	const Int_t points = 11;

	gStyle->SetOptStat(kFALSE);
	gStyle->SetOptFit(0000);
	gStyle->SetPadGridX(kTRUE);
	gStyle->SetPadGridY(kTRUE);
	TGaxis::SetMaxDigits(4);

	ifstream infile1("015nALL.txt");
	ifstream infile2("018nALL.txt");
	ifstream infile3("03nALL.txt");
	ifstream infile4("35nALL.txt");
	ifstream infile5("7nALL.txt");
	ifstream infile6("10nALL.txt");

	float Run[6], HV[6], PreAmp[6], Navg[6], PedAve[6], PedRMS[6], nonLin[6], nonLinpV[6], Error[6], Chi2p6df[6], I_out[6];
	Double_t myHV[6][points], mynonLin[6][points], erx[6][points], ery[6][points], mynonLinpV[6][points], myPreAmp[6][points], my_Iout[6][points];
	char header[256];
	for(int h = 0; h<11; h++) {//read the header info
		infile1 >> header;
		infile2 >> header;
		infile3 >> header;
		infile4 >> header;
		infile5 >> header;
		infile6 >> header;
	}

	for(int i=0; i<8; i++){
		infile1 >> Run[0] >> HV[0] >> PreAmp[0] >> Navg[0] >> PedAve[0] >> PedRMS[0] >> nonLin[0] >> Error[0] >> nonLinpV[0] >> Chi2p6df[0] >> I_out[0];
		myHV[0][i] = HV[0];
		mynonLin[0][i] = nonLin[0];
		erx[0][i] = 0;
		ery[0][i] = Error[0];
		mynonLinpV[0][i] = nonLinpV[0];
		myPreAmp[0][i] = PreAmp[0];
	}
	for(int i=0; i<8; i++){
		infile2 >> Run[1] >> HV[1] >> PreAmp[1] >> Navg[1] >> PedAve[1] >> PedRMS[1] >> nonLin[1] >> Error[1] >> nonLinpV[1] >> Chi2p6df[1] >> I_out[1];
		myHV[1][i] = HV[1];
		mynonLin[1][i] = nonLin[1];
		erx[1][i] = 0;
		ery[1][i] = Error[1];
		mynonLinpV[1][i] = nonLinpV[1];
		myPreAmp[1][i] = PreAmp[1];
	}
	for(int i=0; i<10; i++){
		infile3 >> Run[2] >> HV[2] >> PreAmp[2] >> Navg[2] >> PedAve[2] >> PedRMS[2] >> nonLin[2] >> Error[2] >> nonLinpV[2] >> Chi2p6df[2] >> I_out[2];
		myHV[2][i] = HV[2];
		mynonLin[2][i] = nonLin[2];
		erx[2][i] = 0;
		ery[2][i] = Error[2];
		mynonLinpV[2][i] = nonLinpV[2];
		myPreAmp[2][i] = PreAmp[2];
cout << "HV = " << myHV[2][i] << "	" << "nonLin = " << mynonLin[2][i] << endl;
	}
	for(int i=0; i<11; i++){
		infile4 >> Run[3] >> HV[3] >> PreAmp[3] >> Navg[3] >> PedAve[3] >> PedRMS[3] >> nonLin[3] >> Error[3] >> nonLinpV[3] >> Chi2p6df[3] >> I_out[3];
		myHV[3][i] = HV[3];
		mynonLin[3][i] = nonLin[3];
		erx[3][i] = 0;
		ery[3][i] = Error[3];
		mynonLinpV[3][i] = nonLinpV[3];
		myPreAmp[3][i] = PreAmp[3];
	}
	for(int i=0; i<8; i++){
		infile5 >> Run[4] >> HV[4] >> PreAmp[4] >> Navg[4] >> PedAve[4] >> PedRMS[4] >> nonLin[4] >> Error[4] >> nonLinpV[4] >> Chi2p6df[4] >> I_out[4];
		myHV[4][i] = HV[4];
		mynonLin[4][i] = nonLin[4];
		erx[4][i] = 0;
		ery[4][i] = Error[4];
		mynonLinpV[4][i] = nonLinpV[4];
		myPreAmp[4][i] = PreAmp[4];
	}
	for(int i=0; i<7; i++){
		infile6 >> Run[5] >> HV[5] >> PreAmp[5] >> Navg[5] >> PedAve[5] >> PedRMS[5] >> nonLin[5] >> Error[5] >> nonLinpV[5] >> Chi2p6df[5] >> I_out[5];
		myHV[5][i] = HV[5];
		mynonLin[5][i] = nonLin[5];
		erx[5][i] = 0;
		ery[5][i] = Error[5];
		mynonLinpV[5][i] = nonLinpV[5];
		myPreAmp[5][i] = PreAmp[5];
	}
		infile1.close();
		infile2.close();
		infile3.close();
		infile4.close();
		infile5.close();
		infile6.close();

  TCanvas *c = new TCanvas("c","nonLinearity vs HV",0,0,900,600);
  gPad->SetLogy(0);
  gPad->SetLogx(0);
  TH2F *hr1;
  hr1 = new TH2F("hr1","",100,-1020,-480,100,-1,1.5);
  hr1->SetTitle("nonLinearity vs HV for PMT ZK5401;HV (V);nonLinearity(%)");
  hr1->GetXaxis()->CenterTitle();
  hr1->GetYaxis()->CenterTitle();
  hr1->Draw();
  TF1 *f1 = new TF1("f1","pol1",-1020,-480);

  TGraphErrors *gr1 = new TGraphErrors(8,myHV[0],mynonLin[0],erx[0],ery[0]);
  gr1->SetLineColor(1);
  gr1->SetMarkerStyle(20);
  gr1->SetMarkerColor(1);
  gr1->SetMarkerSize(0.55);
  gr1->Draw("P");
  f1->SetLineWidth(2);
  f1->SetLineColor(1);
  gr1->Fit("f1","R");

  TGraphErrors *gr2 = new TGraphErrors(8,myHV[1],mynonLin[1],erx[1],ery[1]);
  gr2->SetLineColor(2);
  gr2->SetMarkerStyle(20);
  gr2->SetMarkerColor(2);
  gr2->SetMarkerSize(0.55);
  gr2->Draw("P");
  f1->SetLineWidth(2);
  f1->SetLineColor(2);
  gr2->Fit("f1","R");

  TGraphErrors *gr3 = new TGraphErrors(10,myHV[2],mynonLin[2],erx[2],ery[2]);
  gr3->SetLineColor(3);
  gr3->SetMarkerStyle(20);
  gr3->SetMarkerColor(3);
  gr3->SetMarkerSize(0.55);
  gr3->Draw("P");
  f1->SetLineWidth(2);
  f1->SetLineColor(3);
  gr3->Fit("f1","R"); 

  TGraphErrors *gr4 = new TGraphErrors(11,myHV[3],mynonLin[3],erx[3],ery[3]);
  gr4->SetLineColor(4);
  gr4->SetMarkerStyle(20);
  gr4->SetMarkerColor(4);
  gr4->SetMarkerSize(0.55);
  gr4->Draw("P");
  f1->SetLineWidth(2);
  f1->SetLineColor(4);
  gr4->Fit("f1","R"); 

  TGraphErrors *gr5 = new TGraphErrors(8,myHV[4],mynonLin[4],erx[4],ery[4]);
  gr5->SetLineColor(6);
  gr5->SetMarkerStyle(20);
  gr5->SetMarkerColor(6);
  gr5->SetMarkerSize(0.55);
  gr5->Draw("P");
  f1->SetLineWidth(2);
  f1->SetLineColor(6);
  gr5->Fit("f1","R");

  TGraphErrors *gr6 = new TGraphErrors(7,myHV[5],mynonLin[5],erx[5],ery[5]);
  gr6->SetLineColor(7);
  gr6->SetMarkerStyle(20);
  gr6->SetMarkerColor(7);
  gr6->SetMarkerSize(0.55);
  gr6->Draw("P");
  f1->SetLineWidth(2);
  f1->SetLineColor(7);
  gr6->Fit("f1","R");

  TLegend *leg1 = new TLegend(0.70,0.70,0.90,0.90);
  leg1->SetBorderSize(0);
  leg1->SetFillStyle(0);
  leg1->AddEntry(gr1,"0.15nALL","EP");
  leg1->AddEntry(gr2,"0.18nALL","EP");
  leg1->AddEntry(gr3,"0.3nALL","EP");
  leg1->AddEntry(gr4,"3.5nALL","EP");
  leg1->AddEntry(gr5,"7nALL","EP");
  leg1->AddEntry(gr6,"10nALL","EP");

  leg1->Draw();
  c->SaveAs("nonLinearity_vs_HV.png");

  return ;
	}