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

// HV scan for Ar/CO2/CF4 gas mixture
// ranges from run 0857 to run 0869
TVectorD HVscanARCO2CF4(long int first_run = 857, long int last_run = 869)
{

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

  TVectorD efficiencies (size);
  TVectorD n_events (size);
  TVectorD Th60 (thsize);
  TVectorD Th60ey (thsize);
  TVectorD ex (thsize);
  TVectorD HV (thsize);

  HV[0] = 5.00;
  HV[1] = 5.10;
  HV[2] = 5.20;
  HV[3] = 5.25;
  HV[4] = 5.30;

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

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


  if (efficiencies[size-4] > efficiencies[size-5])
    {
      if (efficiencies[size-4] > efficiencies[size-6])
	{
	  Th60[count60] = efficiencies[size-4];
	  Th60ey[count60] = sqrt( efficiencies[size-4] * (1 - efficiencies[size-4]) / n_events[size-4] );
	}
      else
	{
	  Th60[count60] = efficiencies[size-6];
	  Th60ey[count60] = sqrt( efficiencies[size-6] * (1 - efficiencies[size-6]) / n_events[size-6] );
	};
    }
  else
    {
      Th60[count60] = efficiencies[size-5];
      Th60ey[count60] = sqrt( efficiencies[size-5] * (1 - efficiencies[size-5]) / n_events[size-5] );
    }
  count60++;

  if (efficiencies[size-1] > efficiencies[size-2])
    {
      if (efficiencies[size-1] > efficiencies[size-3])
	{
	  Th60[count60] = efficiencies[size-1];
	  Th60ey[count60] = sqrt( efficiencies[size-1] * (1 - efficiencies[size+1]) / n_events[size-1] );
	}
      else
	{
	  Th60[count60] = efficiencies[size-3];
	  Th60ey[count60] = sqrt( efficiencies[size-3] * (1 - efficiencies[size-3]) / n_events[size-3] );
	};
    }
  else
    {
      Th60[count60] = efficiencies[size-2];
      Th60ey[count60] = sqrt( efficiencies[size-2] * (1 - efficiencies[size-2]) / n_events[size-2] );
    }

  TCanvas* c1 = new TCanvas ("c1","high voltage scan");

  TGraphErrors *hvscan = new TGraphErrors(HV,Th60,ex,Th60ey);
  hvscan->SetTitle("Threshold -60 DAC steps; MSPL 4clk");
  hvscan->SetLineWidth(2);

  hvscan->Draw("ALP");
  hvscan->GetXaxis()->SetRangeUser(4.80,5.30);
  hvscan->GetYaxis()->SetRangeUser(0,1);
  hvscan->GetYaxis()->SetDecimals();
  hvscan->SetTitle("AR/CF4/CO2 60/20/20 HV scan");
  hvscan->GetXaxis()->SetTitle("Negative voltage on LG divider [kV]");
  hvscan->GetYaxis()->SetTitle("LG efficiency");
  c1->Update();
  c1->SetGridx();
  c1->SetGridy();
  c1->SetTickx();
  c1->SetTicky();
  c1->Update();
  return Th60;

}



// ********************************************************************************

// HV scan for Ar/CO2/CF4 gas mixture
// ranges from run 0857 to run 0869
TVectorD HVscanARCO2CF4ey(long int first_run = 857, long int last_run = 869)
{

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

  TVectorD efficiencies (size);
  TVectorD n_events (size);
  TVectorD Th60 (thsize);
  TVectorD Th60ey (thsize);
  TVectorD ex (thsize);
  TVectorD HV (thsize);

  HV[0] = 5.00;
  HV[1] = 5.10;
  HV[2] = 5.20;
  HV[3] = 5.25;
  HV[4] = 5.30;

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

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

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


  if (efficiencies[size-4] > efficiencies[size-5])
    {
      if (efficiencies[size-4] > efficiencies[size-6])
	{
	  Th60[count60] = efficiencies[size-4];
	  Th60ey[count60] = sqrt( efficiencies[size-4] * (1 - efficiencies[size-4]) / n_events[size-4] );
	}
      else
	{
	  Th60[count60] = efficiencies[size-6];
	  Th60ey[count60] = sqrt( efficiencies[size-6] * (1 - efficiencies[size-6]) / n_events[size-6] );
	};
    }
  else
    {
      Th60[count60] = efficiencies[size-5];
      Th60ey[count60] = sqrt( efficiencies[size-5] * (1 - efficiencies[size-5]) / n_events[size-5] );
    }
  count60++;

  if (efficiencies[size-1] > efficiencies[size-2])
    {
      if (efficiencies[size-1] > efficiencies[size-3])
	{
	  Th60[count60] = efficiencies[size-1];
	  Th60ey[count60] = sqrt( efficiencies[size-1] * (1 - efficiencies[size+1]) / n_events[size-1] );
	}
      else
	{
	  Th60[count60] = efficiencies[size-3];
	  Th60ey[count60] = sqrt( efficiencies[size-3] * (1 - efficiencies[size-3]) / n_events[size-3] );
	};
    }
  else
    {
      Th60[count60] = efficiencies[size-2];
      Th60ey[count60] = sqrt( efficiencies[size-2] * (1 - efficiencies[size-2]) / n_events[size-2] );
    }

  return Th60ey;

}
