#include <TGaxis.h>
#include <TCanvas.h>
#include <TPaveLabel.h>
#include <TPaveText.h>
#include "TGraphErrors.h"

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

   gStyle->SetPalette(1);
   gStyle->SetPadGridX(kTRUE);
   gStyle->SetPadGridY(kTRUE);
   TGaxis::SetMaxDigits(4);
   gStyle->SetOptStat(kFALSE);

   TString file = Form("summary.txt");
   ifstream infile(file);

   char header[256];
   for(int h = 0; h<11; h++) { //read the header info
    infile >> header;
  }
   Double_t Run[points], HV[points], PreAmp[points], Navg[points], PedAve[points], PedRMS[points], nonLin[points], nonLinpV[points], Error[points], Chi2p6df[points], I_out[points];
   Double_t RelativeWidth[points], myHV[points], nonLin_positive[points], Chi2[points], erX[points], erY[points], myPreAmp[points], myNavg[points], Vout[points], nonLinpV_positive[points], my_Iout[points];
   const int ndf = 6;
   Double_t Vperch = 76.29E-6; //Volts per channel

   for(int i=0;i<points;i++) {
   infile >> Run[i] >> HV[i] >> PreAmp[i] >> Navg[i] >> PedAve[i] >> PedRMS[i] >> nonLin[i] >> Error[i] >> nonLinpV[i] >> Chi2p6df[i] >> I_out[i];
   RelativeWidth[i] = PedRMS[i]/PedAve[i];
   myHV[i] = HV[i];
   nonLin_positive[i] = nonLin[i];
   nonLinpV_positive[i] = nonLinpV[i];
   Chi2[i] = Chi2p6df[i]/ndf;
   myPreAmp[i] = PreAmp[i];
   erX[i] = 0.0;
   erY[i] = Error[i];
   myNavg[i] = Navg[i];
   Vout[i] = Navg[i]*Vperch;
   my_Iout[i] = I_out[i];
   cout << Run[i]<<"	"<<myHV[i] <<"	"<< myPreAmp[i] <<"	"<< RelativeWidth[i] <<"	"<< Chi2[i]<< "	"<< nonLin_positive[i]<<"	"<< nonLinpV_positive[i]<<"	"<<Navg[i]<<"	"<<Vout[i]<< "	" << my_Iout[i] << endl;

  }

   infile.close();

   TCanvas *c1 = new TCanvas("c1","non-Linearity vs HV",0,0,700,500);

   TGraphErrors *gr1 = new TGraphErrors(points,my_Iout, nonLin_positive, erX, erY);
   gr1->SetTitle(Form("non-linearity vs Iout for ZK5555 @ 0.15nALL; Iout (#muA); nonLinearity(%%)"));
   TExec *ex1 = new TExec("ex1","HVoltage();");
   gr1->GetListOfFunctions()->Add(ex1);
   gr1->Draw("AP");
   gr1->GetXaxis()->CenterTitle();
   gr1->GetYaxis()->CenterTitle();
   TF1 *f1 = new TF1("f1","pol1",0,4);
   f1->SetLineWidth(2);
   f1->SetLineColor(46);
   gr1->Fit("f1","R"); 
   gPad->Modified();
   TString leg[points];
   leg[0]=Form("Vadc=%1.2fV, 2.0M#Omega(MAIN)",Vout[0]);
   leg[1]=Form("Vadc=%1.2fV, 2.0M#Omega(MAIN)",Vout[1]);
   leg[2]=Form("Vadc=%1.2fV, 4.0M#Omega(MAIN)",Vout[2]);
   leg[3]=Form("Vadc=%1.2fV, 4.0M#Omega(MAIN)",Vout[3]);
   leg[4]=Form("Vadc=%1.2fV, 10.0M#Omega(SNS)",Vout[4]);
   leg[5]=Form("Vadc=%1.2fV, 10.0M#Omega(SNS)",Vout[5]);
   leg[6]=Form("Vadc=%1.2fV, 25.0M#Omega(LUMI)",Vout[6]);
   leg[7]=Form("Vadc=%1.2fV, 25.0M#Omega(LUMI)",Vout[7]);
   leg[8]=Form("Vadc=%1.2fV, 50.0M#Omega(LUMI)",Vout[8]);
   TPaveText *pt1=new TPaveText(0.68,.265,1.5,0.441);
   pt1->SetBorderSize(1);
   pt1->SetFillColor(0);
   pt1->AddText(leg[0]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(1);
   pt1->AddText(leg[1]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(2);
   pt1->AddText(leg[2]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(3);
   pt1->AddText(leg[3]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(4);
   pt1->AddText(leg[4]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(5);
   pt1->AddText(leg[5]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(6);
   pt1->AddText(leg[6]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(7);
   pt1->AddText(leg[7]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(8);
   pt1->AddText(leg[8]);
   ((TText*)pt1->GetListOfLines()->Last())->SetTextColor(9);
   pt1->Draw();
   c1->SaveAs(Form("nonLinVSIout_ZK5555.png"));

   TCanvas *c2 = new TCanvas("c2","non-Linearity vs HV",0,0,700,500);
   TGraph *gr2 = new TGraph(points, myHV, my_Iout);
   gr2->SetTitle(Form("I_out vs HV for ZK5555 @ 0.15nALL; HV(V); Iout (#muA)"));
   TExec *ex2 = new TExec("ex2","HVoltage();");
   gr2->GetListOfFunctions()->Add(ex2);
   gr2->Draw("AP");
   gr2->GetXaxis()->CenterTitle();
   gr2->GetYaxis()->CenterTitle();
   gPad->Modified();
   TString leg2[points];
   leg2[0]=Form("Vadc=%1.2fV, 1.0M#Omega(MAIN)",Vout[0]);
   leg2[1]=Form("Vadc=%1.2fV, 2.0M#Omega(MAIN)",Vout[1]);
   leg2[2]=Form("Vadc=%1.2fV, 2.0M#Omega(MAIN)",Vout[2]);
   leg2[3]=Form("Vadc=%1.2fV, 4.0M#Omega(MAIN)",Vout[3]);
   leg2[4]=Form("Vadc=%1.2fV, 4.0M#Omega(MAIN)",Vout[4]);
   leg2[5]=Form("Vadc=%1.2fV, 10.0M#Omega(SNS)",Vout[5]);
   leg2[6]=Form("Vadc=%1.2fV, 10.0M#Omega(SNS)",Vout[6]);
   leg2[7]=Form("Vadc=%1.2fV, 25.0M#Omega(LUMI)",Vout[7]);
   leg2[8]=Form("Vadc=%1.2fV, 25.0M#Omega(LUMI)",Vout[8]);
   TPaveText *pt2=new TPaveText(-512,2.7,-612,3.75);
   pt2->SetBorderSize(1);
   pt2->SetFillColor(0);
   pt2->AddText(leg2[0]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(1);
   pt2->AddText(leg2[1]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(2);
   pt2->AddText(leg2[2]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(3);
   pt2->AddText(leg2[3]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(4);
   pt2->AddText(leg2[4]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(5);
   pt2->AddText(leg2[5]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(6);
   pt2->AddText(leg2[6]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(7);
   pt2->AddText(leg2[7]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(8);
   pt2->AddText(leg2[8]);
   ((TText*)pt2->GetListOfLines()->Last())->SetTextColor(9);
   pt2->Draw();
   c2->SaveAs(Form("IoutVSHV_ZK5555.png"));

  TCanvas *c = new TCanvas("c","nonLinearity vs HV",0,0,900,600);
  gPad->SetLogy(0);
  gPad->SetLogx(0);
  TH2F *hr;
  hr = new TH2F("hr","",100,-960,-530,100,0,4);
  hr->SetTitle(Form("I_out vs HV for ZK5555 @ 0.15nALL; HV(V); Iout (#muA)"));
  hr->GetXaxis()->CenterTitle();
  hr->GetYaxis()->CenterTitle();
  hr->Draw();
  TF1 *f = new TF1("f","pol1",-960,-750);
  TF1 *ff = new TF1("ff","pol1",-750,-530);
  TGraph *gr = new TGraph(points, myHV, my_Iout);
  gr->SetLineColor(1);
  gr->SetMarkerStyle(20);
  gr->SetMarkerColor(1);
  gr->SetMarkerSize(0.55);
  gr->Draw("P");
  f->SetLineWidth(2);
  f->SetLineColor(1);
  gr->Fit("f","R"); 
  ff->SetLineWidth(2);
  ff->SetLineColor(2);
  gr->Fit("ff","R+"); 
   c->SaveAs(Form("IoutVSHV_fit_ZK5555.png"));
   }

void HVoltage(){
   Int_t i,points,j;
   Double_t x,y;

   TGraph *g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
   points = g->GetN();
   TMarker *m;
   for (i=0; i<points; i++) {
   g->GetPoint(i,x,y);
   m = new TMarker(x,y,29);
   m->SetMarkerColor(i+1);	
   m->Paint();
   }
   }