#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 <algorithm>


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

// HV scan for point p ranges from run 0021 to run 0108
void HVscanPmacro(long int first_run = 21, long int last_run = 108)
{

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

  TVectorD efficiencies (size);
  TVectorD n_events (size);
  
  TVectorD Th40 (thsize); // Data arrays
  TVectorD Th60 (thsize);
  TVectorD Th80 (thsize);
  TVectorD Th100 (thsize);
  TVectorD Th40ey (thsize); // Error arrays
  TVectorD Th60ey (thsize);
  TVectorD Th80ey (thsize);
  TVectorD Th100ey (thsize);
  TVectorD ex (thsize);

  TVectorD HV (thsize);
  HV[0] = 4.60;
  HV[1] = 4.80;
  HV[2] = 5.00;
  HV[3] = 5.10;
  HV[4] = 5.15;
  HV[5] = 5.20;
  HV[6] = 5.25;

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

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

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

  // Filling efficiency arrays for different thresholds
  Int_t count40 = 0;
  Int_t count60 = 0;
  Int_t count80 = 0;
  Int_t count100 = 0;

  for (Int_t i = 0; i < size - 4; i = i + 12)
    {
      Th100[count100] = max( max(efficiencies[i],efficiencies[i+1]) , efficiencies[i+2] );
      Int_t j;
      if (efficiencies[i] > efficiencies[i+1]) {j = i;}
      else {j = i + 1;};
      if (efficiencies[i+2] > efficiencies[j]) {j = i + 2;};
      Th100ey[count100] = sqrt( Th100[count100] * (1 - Th100[count100]) / n_events[j] );
      count100++;
    }

  for (Int_t i = 3; i < size - 4; i = i + 12)
    {
      Th80[count80] = max( max(efficiencies[i],efficiencies[i+1]) , efficiencies[i+2] );
      Int_t j;
      if (efficiencies[i] > efficiencies[i+1]) {j = i;}
      else {j = i + 1;};
      if (efficiencies[i+2] > efficiencies[j]) {j = i + 2;};
      Th80ey[count80] = sqrt( Th80[count80] * (1 - Th80[count80]) / n_events[j] );
      count80++;
    }

  for (Int_t i = 6; i < size - 4; i = i + 12)
    {
      Th60[count60] = max( max(efficiencies[i],efficiencies[i+1]) , efficiencies[i+2] );
      Int_t j;
      if (efficiencies[i] > efficiencies[i+1]) {j = i;}
      else {j = i + 1;};
      if (efficiencies[i+2] > efficiencies[j]) {j = i + 2;};
      Th60ey[count60] = sqrt( Th60[count60] * (1 - Th60[count60]) / n_events[j] );
      count60++;
    }

  for (Int_t i = 9; i < size - 4; i = i + 12)
    {
      Th40[count40] = max( max(efficiencies[i],efficiencies[i+1]) , efficiencies[i+2] );
      Int_t j;
      if (efficiencies[i] > efficiencies[i+1]) {j = i;}
      else {j = i + 1;};
      if (efficiencies[i+2] > efficiencies[j]) {j = i + 2;};
      Th40ey[count40] = sqrt( Th40[count40] * (1 - Th40[count40]) / n_events[j] );
      count40++;
    }

  Int_t j = 0;

  Th100[count100 - 1] = max( efficiencies[size - 4], Th100[count100 - 1]);
  if (efficiencies[size - 4] > Th100[count100 - 1]) {j = size - 4;}
  else {j = (count100 - 1) * 12;};
  Th100ey[count100 - 1] = sqrt( Th100[count100 - 1] * (1 - Th100[count100 - 1]) / n_events[j] );
  j = 0;

  Th80[count80 - 1] = max( efficiencies[size - 3], Th80[count80 - 1]);
  if (efficiencies[size - 3] > Th80[count80 - 1]) {j = size - 3;}
  else {j = (count80 - 1) * 12 + 3;};
  Th80ey[count80 - 1] = sqrt( Th80[count80 - 1] * (1 - Th80[count100 - 1]) / n_events[j] );
  j = 0;

  Th60[count60 - 1] = max( efficiencies[size - 2], Th60[count60 - 1]);
  if (efficiencies[size - 2] > Th60[count60 - 1]) {j = size - 2;}
  else {j = (count60 - 1) * 12 + 6;};
  Th60ey[count60 - 1] = sqrt( Th60[count60 - 1] * (1 - Th60[count60 - 1]) / n_events[j] );
  j = 0;

  Th40[count40 - 1] = max( efficiencies[size - 1], Th40[count40 - 1]);
  if (efficiencies[size - 1] > Th40[count40 - 1]) {j = size - 1;}
  else {j = (count40 - 1) * 12 + 9;};
  Th40ey[count40 - 1] = sqrt( Th40[count40 - 1] * (1 - Th40[count40 - 1]) / n_events[j] );
  j = 0;

  // Plotting HV scan
  TCanvas* c1 = new TCanvas ("c1","Point P high voltage scan");

  TGraphErrors *HVScan100 = new TGraphErrors (HV,Th100,ex,Th100ey);
  HVScan100->SetTitle("Threshold -100 DAC steps");
  HVScan100->SetMarkerColor(21);
  HVScan100->SetLineColor(21);
  TGraphErrors *HVScan80 = new TGraphErrors (HV,Th80,ex,Th80ey);
  HVScan80->SetTitle("Threshold -80 DAC steps");
  HVScan80->SetMarkerColor(2);
  HVScan80->SetLineColor(2);
  TGraphErrors *HVScan60 = new TGraphErrors (HV,Th60,ex,Th60ey);
  HVScan60->SetTitle("Threshold -60 DAC steps");
  HVScan60->SetMarkerColor(3);
  HVScan60->SetLineColor(3);
  TGraphErrors *HVScan40 = new TGraphErrors (HV,Th40,ex,Th40ey);
  HVScan40->SetTitle("Threshold -40 DAC steps");
  HVScan40->SetMarkerColor(4);
  HVScan40->SetLineColor(4);

  TMultiGraph *HVScan = new TMultiGraph();
  HVScan->Add(HVScan100);
  HVScan->Add(HVScan80);
  HVScan->Add(HVScan60);
  HVScan->Add(HVScan40);

  HVScan->Draw("ALP");
  c1->SetGridx();
  c1->SetGridy();
  c1->SetTickx();
  c1->SetTicky();
  HVScan->GetYaxis()->SetRangeUser(0,1);
  HVScan->GetXaxis()->SetRangeUser(4.6,5.25);
  HVScan->GetYaxis()->SetDecimals();

  HVScan->SetTitle("Point P high voltage scan");
  HVScan->GetXaxis()->SetTitle("Divider HV [kV]");
  HVScan->GetYaxis()->SetTitle("LG efficiency");

  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("High Voltage scan (point P)");
  legend->AddEntry(HVScan100,HVScan100->GetTitle(),"lp");
  legend->AddEntry(HVScan80,HVScan80->GetTitle(),"lp");
  legend->AddEntry(HVScan60,HVScan60->GetTitle(),"lp");
  legend->AddEntry(HVScan40,HVScan40->GetTitle(),"lp");
  legend->Draw();

  TF1 *f1 = new TF1 ("f1","x",767,875);
  f1->Update();

  TGaxis *current = new TGaxis (4.6, 1, 5.25, 1, "f1", 1020, "-");
  current->SetLabelOffset(0.005);
  current->SetLabelSize(0.04);
  current->SetTickSize(0.03);
  current->SetGridLength(0);
  current->SetTitleOffset(1.1);
  current->SetTitleSize(0.04);
  current->SetTitleColor(1);
  current->SetTitleFont(62);
  current->SetTitle("Divider current [uA]");
  current->Draw();
  c1->Update();

}
