#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>
#include <TH1.h>
#include "TLegend.h"


double effex2(TFile& file0);
TVectorD nevents(long int run_number, long int last_run);
TVectorD efficiency(long int run_number, long int last_run);

// TH scan for point A ranges from run 0554-bis = 0921 to run 0621
void THscanAmacro(long int first_run = 555, long int last_run = 621)
{

  Int_t size = last_run - first_run + 1 + 1; //Adding another +1 because of 554-bis
  Int_t thsize = 17;

  TVectorD tempefficiencies (size-1); // Still 554-bis stuff
  TVectorD tempn_events (size-1); // Still 554-bis stuff
  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;
  
  tempefficiencies = efficiency(first_run,last_run);
  tempn_events = nevents(first_run,last_run);
  TFile f("../RootData/Run0921_reco.root");
  efficiencies[0] = effex2(f);
  TH1F* heffbgch = dynamic_cast<TH1F*>(f.Get("heffbgch"));	
  n_events[0] = heffbgch->GetEntries();
  f.Close();

  for (Int_t i = 1; i < size; i++)
    {
      efficiencies[i] = tempefficiencies[i-1];
      n_events[i] = tempn_events[i-1];
    }

  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");

  TGraphErrors *thscan5p15 = new TGraphErrors (th,hv5p15,ex,hv5p15ey);
  thscan5p15->SetTitle("HV -5.15kV (859#muA)");
  thscan5p15->SetMarkerColor(kBlue);
  thscan5p15->SetLineColor(kBlue);
  
  TGraphErrors *thscan5p10 = new TGraphErrors (th,hv5p10,ex,hv5p10ey);
  thscan5p10->SetTitle("HV -5.10kV (851#muA)");
  thscan5p10->SetMarkerColor(kRed);
  thscan5p10->SetLineColor(kRed);
  
  TGraphErrors *thscan5p05 = new TGraphErrors (th,hv5p05,ex,hv5p05ey);
  thscan5p05->SetTitle("HV -5.05kV (841#muA)");
  thscan5p05->SetMarkerColor(kGreen);
  thscan5p05->SetLineColor(kGreen);
  
  TGraphErrors *thscan5p00 = new TGraphErrors (th,hv5p00,ex,hv5p00ey);
  thscan5p00->SetTitle("HV -5.00kV (834#muA)");
  thscan5p00->SetMarkerColor(15);
  thscan5p00->SetLineColor(15);


  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.6,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 A)}{[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();
  
}
