#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("ZK4033.txt");
	ifstream infile2("ZK5363.txt");
	ifstream infile3("ZK5365.txt");
	ifstream infile4("ZK5369.txt");
	ifstream infile5("ZK5370.txt");
	ifstream infile6("ZK5401.txt");
	ifstream infile7("ZK5407.txt");
	ifstream infile8("ZK5553.txt");
	ifstream infile9("ZK5555.txt");


	float Run[9], HV[9], PreAmp[9], Navg[9], PedAve[9], PedRMS[9], nonLin[9], nonLinpV[9], Error[9], Chi2p6df[9], I_out[9];
	Double_t myHV[9][points], mynonLin[9][points], erx[9][points], ery[9][points], mynonLinpV[9][points], myPreAmp[9][points], my_Iout[9][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;
		infile7 >> header;
		infile8 >> header;
		infile9 >> header;
	}

	for(int i=0; i<9; 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<9; 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<8; 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<10; 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<10; 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];
	}
	for(int i=0; i<6; i++){
		infile7 >> Run[6] >> HV[6] >> PreAmp[6] >> Navg[6] >> PedAve[6] >> PedRMS[6] >> nonLin[6] >> Error[6] >> nonLinpV[6] >> Chi2p6df[6] >> I_out[6];
		myHV[6][i] = HV[6];
		mynonLin[6][i] = nonLin[6];
		erx[6][i] = 0;
		ery[6][i] = Error[6];
		mynonLinpV[6][i] = nonLinpV[6];
		myPreAmp[6][i] = PreAmp[6];
	}
	for(int i=0; i<6; i++){
		infile8 >> Run[7] >> HV[7] >> PreAmp[7] >> Navg[7] >> PedAve[7] >> PedRMS[7] >> nonLin[7] >> Error[7] >> nonLinpV[7] >> Chi2p6df[7] >> I_out[7];
		myHV[7][i] = HV[7];
		mynonLin[7][i] = nonLin[7];
		erx[7][i] = 0;
		ery[7][i] = Error[7];
		mynonLinpV[7][i] = nonLinpV[7];
		myPreAmp[7][i] = PreAmp[7];
	}
	for(int i=0; i<11; i++){
		infile9 >> Run[8] >> HV[8] >> PreAmp[8] >> Navg[8] >> PedAve[8] >> PedRMS[8] >> nonLin[8] >> Error[8] >> nonLinpV[8] >> Chi2p6df[8] >> I_out[8];
		myHV[8][i] = HV[8];
		mynonLin[8][i] = nonLin[8];
		erx[8][i] = 0;
		ery[8][i] = Error[8];
		mynonLinpV[8][i] = nonLinpV[8];
		myPreAmp[8][i] = PreAmp[8];
	}

		infile1.close();
		infile2.close();
		infile3.close();
		infile4.close();
		infile5.close();
		infile6.close();
		infile7.close();
		infile8.close();
		infile9.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,-0.4,0.8);
  hr1->SetTitle("nonLinearity vs HV for 0.3nA LL;HV (V);nonLinearity(%)");
  hr1->GetXaxis()->CenterTitle();
  hr1->GetYaxis()->CenterTitle();
  hr1->Draw();
  TF1 *f1 = new TF1("f1","pol1",-1020,-480);

  TGraphErrors *gr1 = new TGraphErrors(9,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(9,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(8,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(10,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(10,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"); 

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

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

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

  TLegend *leg1 = new TLegend(0.70,0.70,0.90,0.90);
  leg1->SetBorderSize(0);
  leg1->SetFillStyle(0);
  leg1->AddEntry(gr1,"ZK4033_0.3nALL","EP");
  leg1->AddEntry(gr2,"ZK5363_0.3nALL","EP");
  leg1->AddEntry(gr3,"ZK5365_0.3nALL","EP");
  leg1->AddEntry(gr4,"ZK5369_0.3nALL","EP");
  leg1->AddEntry(gr5,"ZK5370_0.3nALL","EP");
  leg1->AddEntry(gr6,"ZK5401_0.3nALL","EP");
  leg1->AddEntry(gr7,"ZK5407_0.3nALL","EP");
  leg1->AddEntry(gr8,"ZK5553_0.3nALL","EP");
  leg1->AddEntry(gr9,"ZK5555_0.3nALL","EP");

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

  return ;
	}