#include <TGraph.h>
#include <TGraphErrors.h>
#include <TCanvas.h>
#include <TAxis.h>
#include <stdio.h>
#include <cstdio>
#include "TVectorT.h"
#include <stdlib.h>
#include <iostream>
#include "TMultiGraph.h"
#include "TAxis.h"
#include <TFile.h>


TVectorD nevents(long int run_number, long int last_run);
TVectorD efficiency(long int run_number, long int last_run);

void THscanPmacro(long int first_run = 269, long int last_run = 336)
{

  Int_t size = last_run - first_run + 1;
  Int_t thsize = 17;

  TVectorD efficiencies (size);
  TVectorD n_events (size);
  
  TVectorD hv5p15 (thsize); // Data arrays
  TVectorD hv5p10 (thsize);
  TVectorD hv5p05 (thsize);
  TVectorD hv5p00 (thsize);
  TVectorD hv5p15ey (thsize); // Data arrays
  TVectorD hv5p10ey (thsize);
  TVectorD hv5p05ey (thsize);
  TVectorD hv5p00ey (thsize);
  TVectorD ex (thsize);

  TVectorD th (thsize);
  th[0] = 10;
  th[1] = 20;
  th[2] = 30;
  th[3] = 40;
  th[4] = 50;
  th[5] = 60;
  th[6] = 70;
  th[7] = 80;
  th[8] = 90;
  th[9] = 100;
  th[10] = 120;
  th[11] = 140;
  th[12] = 160;
  th[13] = 180;
  th[14] = 200;
  th[15] = 225;
  th[16] = 250;

  for (Int_t i = 0; i < thsize; i++) // No X errors
    {
      ex[i] = 0;
    }

  std::cout << "Number of runs: " << size << std::endl;

  efficiencies = efficiency(first_run,last_run);
  n_events = nevents(first_run,last_run);


  for (Int_t i = 0; i < thsize; i++)
    {
      Int_t hv515 = i;
      Int_t hv510 = i + 17;
      Int_t hv505 = i + 34;
      Int_t hv500 = i + 51;
	      
      hv5p15[i] = efficiencies[hv515];
      hv5p15ey[i] = sqrt( efficiencies[hv515] * (1 - efficiencies[hv515]) / n_events[hv515] );

      hv5p10[i] = efficiencies[hv510];
      hv5p10ey[i] = sqrt( efficiencies[hv510] * (1 - efficiencies[hv510]) / n_events[hv510] );

      hv5p05[i] = efficiencies[hv505];
      hv5p05ey[i] = sqrt( efficiencies[hv505] * (1 - efficiencies[hv505]) / n_events[hv505] );

      hv5p00[i] = efficiencies[hv500];
      hv5p00ey[i] = sqrt( efficiencies[hv500] * (1 - efficiencies[hv500]) / n_events[hv500] );
    }

  // Plotting MSPL scan
  TCanvas* c1 = new TCanvas ("c1","Point P MSPL/Latency scan");

  //MSPL = 4clk
  TGraphErrors *thscan5p15 = new TGraphErrors (th,hv5p15,ex,hv5p15ey);
  thscan5p15->SetTitle("HV -5.15kV (859uA)");
  thscan5p15->SetMarkerColor(kBlue);
  thscan5p15->SetLineColor(kBlue);
  thscan5p15->SetLineWidth(2);
  
  TGraphErrors *thscan5p10 = new TGraphErrors (th,hv5p10,ex,hv5p10ey);
  thscan5p10->SetTitle("HV -5.10kV (851uA)");
  thscan5p10->SetMarkerColor(kSpring);
  thscan5p10->SetLineColor(kSpring);
  thscan5p10->SetLineWidth(2);
  
  TGraphErrors *thscan5p05 = new TGraphErrors (th,hv5p05,ex,hv5p05ey);
  thscan5p05->SetTitle("HV -5.05kV (841uA)");
  thscan5p05->SetMarkerColor(kOrange);
  thscan5p05->SetLineColor(kOrange);
  thscan5p05->SetLineWidth(2);
  
  TGraphErrors *thscan5p00 = new TGraphErrors (th,hv5p00,ex,hv5p00ey);
  thscan5p00->SetTitle("HV -5.00kV (834uA)");
  thscan5p00->SetMarkerColor(kMagenta+2);
  thscan5p00->SetLineColor(kMagenta+2);
  thscan5p00->SetLineWidth(2);


  TMultiGraph *THScanPlot = new TMultiGraph();
  THScanPlot->Add(thscan5p15);
  THScanPlot->Add(thscan5p10);
  THScanPlot->Add(thscan5p05);
  THScanPlot->Add(thscan5p00);

  THScanPlot->Draw("ALP");
  THScanPlot->GetXaxis()->SetRangeUser(10,250);
  THScanPlot->GetYaxis()->SetRangeUser(0,1);
  THScanPlot->GetYaxis()->SetDecimals();
  THScanPlot->GetXaxis()->SetTitle("Negative threshold [VFAT2 DAC steps = 3.3mV = 0.045fC]");
  THScanPlot->GetYaxis()->SetTitle("LG efficiency");
  c1->Update();
  c1->SetGridx();
  c1->SetGridy();
  c1->SetTickx();
  c1->SetTicky();
  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("#splitline{Threshold scan (point P)}{[MSPL 4clk, lat 14clk]}");
  legend->AddEntry(thscan5p15,thscan5p15->GetTitle(),"lp");
  legend->AddEntry(thscan5p10,thscan5p10->GetTitle(),"lp");
  legend->AddEntry(thscan5p05,thscan5p05->GetTitle(),"lp");
  legend->AddEntry(thscan5p00,thscan5p00->GetTitle(),"lp");
  legend->Draw();
  c1->Update();
  
}
