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

void graphmulticol(){
  //Define parameters common to all analysis configurations
	const Int_t points = 10;
	char printname[256];
	char graphname1[256];
	char xTitle[256];
	char yTitle[256];
	char GraphText1[256];
	char histTitle[256];
	char output_line[256];

	gStyle->SetStatColor(0);//All this stuff makes backgrounds, etc.
	gStyle->SetTitleColor(0);//transparent for printing purposes...
	gStyle->SetCanvasColor(0);
	gStyle->SetPadColor(0);
	gStyle->SetPadBorderMode(0);
	gStyle->SetCanvasBorderMode(0);
	gStyle->SetFrameBorderMode(0);
	gStyle->SetPalette(1);
	gStyle->SetOptStat(kFALSE);
	gStyle->SetOptFit(0000);
	gStyle->SetPadGridX(kTRUE);
	gStyle->SetPadGridY(kTRUE);
	TGaxis::SetMaxDigits(4);

	ifstream infile1("120_Hz_FF.txt");
	ifstream infile2("240_Hz_FF.txt");

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

	for(int i=0; i<points; i++){
		infile1 >> Run[0] >> HV[0] >> PreAmp[0] >> Navg[0] >> PedAve[0] >> PedRMS[0] >> nonLin[0] >> Error[0] >> nonLinpV[0] >> Chi2p6df[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<points; i++){
		infile2 >> Run[1] >> HV[1] >> PreAmp[1] >> Navg[1] >> PedAve[1] >> PedRMS[1] >> nonLin[1] >> Error[1] >> nonLinpV[1] >> Chi2p6df[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];
	}

		infile1.close();
		infile2.close();

  TCanvas *c = new TCanvas("c","nonLinearity vs HV",0,0,900,600);
  gPad->SetLogy(0);
  gPad->SetLogx(0);
  gStyle->SetOptStat(kFALSE);
  TH2F *hr1;
  hr1 = new TH2F("hr1","",100,-800,-400,100,-3,1);
  gStyle->SetTitleX(0.18);
  sprintf(xTitle,"HV (V)");  
  sprintf(yTitle,"nonLinearity (%)");
  hr1->GetYaxis()->SetLabelColor(1);
  hr1->GetYaxis()->SetTitleColor(1);
  hr1->SetYTitle(yTitle);
  hr1->GetXaxis()->CenterTitle();
  hr1->GetYaxis()->CenterTitle();
  hr1->GetYaxis()->SetTitleOffset(1.3);
  hr1->GetXaxis()->SetLabelColor(1);
  hr1->GetXaxis()->SetTitleColor(1);
  hr1->SetXTitle(xTitle);
  hr1->Draw();

  TPaveLabel *title1 = new TPaveLabel(0.20,0.91,0.60,0.96,"nonLinearity vs HV for PMT#2, 10.0nA LL","NDC");
  title1->SetBorderSize(0);
  title1->SetTextColor(kBlack);
  title1->SetTextSize(0.80);
  title1->SetFillColor(0);
  title1->Draw();
  TGraphErrors *gr1 = new TGraphErrors(points,myHV[0],mynonLin[0],erx[0],ery[0]);
  gr1->SetLineColor(1);
  gr1->SetMarkerStyle(20);
  gr1->SetMarkerColor(1);
  gr1->SetMarkerSize(0.55);
  gr1->Draw("P");
  TF1 *f1 = new TF1("f1","pol1",-800,-400);
  f1->SetLineWidth(2);
  f1->SetLineColor(1);
  gr1->Fit("f1","R"); 

  TGraphErrors *gr2 = new TGraphErrors(points,myHV[1],mynonLin[1],erx[1],ery[1]);
  gr2->SetLineColor(2);
  gr2->SetMarkerStyle(20);
  gr2->SetMarkerColor(2);
  gr2->SetMarkerSize(0.55);
  gr2->Draw("P");
  TF1 *f2 = new TF1("f2","pol1",-800,-400);
  f2->SetLineWidth(2);
  f2->SetLineColor(2);
  gr2->Fit("f2","R"); 

  TLegend *leg1 = new TLegend(0.70,0.80,0.90,0.90);
  leg1->SetBorderSize(0);
  leg1->SetFillStyle(0);
  leg1->AddEntry(gr1,"120 Hz FF","EP");
  leg1->AddEntry(gr2,"240 Hz FF","EP");
  leg1->Draw();
  sprintf(printname,"nonLinearity_vs_HV.png");
  c->Print(printname);

  TCanvas *c1 = new TCanvas("c1","nonLinearity per volt vs HV",0,0,900,600);
  gPad->SetLogy(0);
  gPad->SetLogx(0);
  gStyle->SetOptStat(kFALSE);
  sprintf(graphname1,"");
  TH2F *hr2;
  hr2 = new TH2F("hr2","",100,-800,-400,100,-3,1);
  gStyle->SetTitleX(0.18);
  sprintf(xTitle,"HV (V)");  
  sprintf(yTitle,"nonLinearity (%) per volt");
  hr2->GetYaxis()->SetLabelColor(1);
  hr2->GetYaxis()->SetTitleColor(1);
  hr2->SetYTitle(yTitle);
  hr2->GetXaxis()->CenterTitle();
  hr2->GetYaxis()->CenterTitle();
  hr2->GetYaxis()->SetTitleOffset(1.3);
  hr2->GetXaxis()->SetLabelColor(1);
  hr2->GetXaxis()->SetTitleColor(1);
  hr2->SetXTitle(xTitle);
  hr2->Draw();

  TPaveLabel *title2 = new TPaveLabel(0.20,0.91,0.60,0.96,"nonLinearity per volt vs HV for PMT#2, 10.0nA LL","NDC");
  title2->SetBorderSize(0);
  title2->SetTextColor(kBlack);
  title2->SetTextSize(0.80);
  title2->SetFillColor(0);
  title2->Draw();
  TGraphErrors *gr1 = new TGraphErrors(points,myHV[0],mynonLinpV[0]);
  gr1->SetLineColor(1);
  gr1->SetMarkerStyle(20);
  gr1->SetMarkerColor(1);
  gr1->SetMarkerSize(0.55);
  gr1->Draw("P");
  TGraphErrors *gr2 = new TGraphErrors(points,myHV[1],mynonLinpV[1]);
  gr2->SetLineColor(2);
  gr2->SetMarkerStyle(20);
  gr2->SetMarkerColor(2);
  gr2->SetMarkerSize(0.55);
  gr2->Draw("P");

  TLegend *leg1 = new TLegend(0.15,0.15,0.35,0.35);
  leg1->SetBorderSize(0);
  leg1->SetFillStyle(0);
  leg1->AddEntry(gr1,"120 Hz FF","P");
  leg1->AddEntry(gr2,"240 Hz FF","P");
  leg1->Draw();
  sprintf(printname,"nonLinearity_per_volt_vs_HV.png");
  c1->Print(printname);

  TCanvas *c2 = new TCanvas("c2","nonLinearity vs PreAmp",0,0,900,600);
  gPad->SetLogy(0);
  gPad->SetLogx(0);
  gStyle->SetOptStat(kFALSE);
  sprintf(graphname1,"");
  TH2F *hr3;
  hr3 = new TH2F("hr3","",100,0,2.5,100,-3,1);
  gStyle->SetTitleX(0.18);
  sprintf(xTitle,"PreAmp (MOhm)");  
  sprintf(yTitle,"nonLinearity (%)");
  hr3->GetYaxis()->SetLabelColor(1);
  hr3->GetYaxis()->SetTitleColor(1);
  hr3->SetYTitle(yTitle);
  hr3->GetXaxis()->CenterTitle();
  hr3->GetYaxis()->CenterTitle();
  hr3->GetYaxis()->SetTitleOffset(1.3);
  hr3->GetXaxis()->SetLabelColor(1);
  hr3->GetXaxis()->SetTitleColor(1);
  hr3->SetXTitle(xTitle);
  hr3->Draw();

  TPaveLabel *title3 = new TPaveLabel(0.20,0.91,0.60,0.96,"nonLinearity vs preAmp for PMT#2, 10.0nA LL","NDC");
  title3->SetBorderSize(0);
  title3->SetTextColor(kBlack);
  title3->SetTextSize(0.80);
  title3->SetFillColor(0);
  title3->Draw();
  TGraphErrors *gr1 = new TGraphErrors(points,myPreAmp[0],mynonLin[0]);
  gr1->SetLineColor(1);
  gr1->SetMarkerStyle(20);
  gr1->SetMarkerColor(1);
  gr1->SetMarkerSize(0.55);
  gr1->Draw("P");
  TGraphErrors *gr2 = new TGraphErrors(points,myPreAmp[1],mynonLin[1]);
  gr2->SetLineColor(2);
  gr2->SetMarkerStyle(20);
  gr2->SetMarkerColor(2);
  gr2->SetMarkerSize(0.55);
  gr2->Draw("P");

  TLegend *leg1 = new TLegend(0.70,0.80,0.90,0.90);
  leg1->SetBorderSize(0);
  leg1->SetFillStyle(0);
  leg1->AddEntry(gr1,"120 Hz FF","P");
  leg1->AddEntry(gr2,"240 Hz FF","P");
  leg1->Draw();
  sprintf(printname,"nonLinearity_vs_PreAmp.png");
  c2->Print(printname);

  return ;
	}