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

// MSPL scan for AR/CO2/CF4 @ -5.35kV ranges from run 0902 to run 0920
TVectorD MSPLscan903920(long int first_run = 903, long int last_run = 920)
{

  Int_t size = last_run - first_run + 1;
  Int_t nlat = 9;
  TVectorD tempeff (size);
  TVectorD tempn (size);
  TVectorD lat (nlat);
  TVectorD mspl3 (nlat);
  TVectorD mspl2 (nlat);
  TVectorD mspl3ey (nlat);
  TVectorD mspl2ey (nlat);
  TVectorD ex (nlat);

  for (Int_t i = 0; i < nlat; i++)
    {
      ex[i] = 0;
      lat[i] = i + 11;
    }

  tempeff = efficiency(903,920);
  tempn = nevents(903,920);

  // MSPL = 3
  for (Int_t i = 0; i < nlat; i++)
    {
      mspl3[i] = tempeff[i];
      mspl3ey[i] = sqrt( tempeff[i] * (1 - tempeff[i]) / tempn[i] );
    }

  // MSPL = 2
  for (Int_t i = 0; i < nlat; i++)
    {
      mspl2[i] = tempeff[i + nlat];
      mspl2ey[i] = sqrt( tempeff[i + nlat] * (1 - tempeff[i + nlat]) / tempn[i + nlat] );
    }

 //  UNCOMMENT THE FOLLOWING TO PLOT THE SCAN
 //
 //  TGraphErrors *monost2 = new TGraphErrors (lat,mspl2,ex,mspl2ey);
 //  monost2->SetLineColor(kOrange);
 //  monost2->SetMarkerColor(kOrange);
 //  monost2->SetLineWidth(2);
 //  monost2->SetTitle("MSPL = 2clk");
 //  TGraphErrors *monost3 = new TGraphErrors (lat,mspl3,ex,mspl3ey);
 //  monost3->SetLineColor(kSpring);
 //  monost3->SetMarkerColor(kSpring);
 //  monost3->SetLineWidth(2);
 //  monost3->SetTitle("MSPL = 3clk");
 //
 //  TCanvas *c1 = new TCanvas("c1","MSPL Scan");
 //
 //  TMultiGraph *plot = new TMultiGraph();
 //  plot->Add(monost2);
 //  plot->Add(monost3);
 //
 //  plot->Draw("ALP");
 //  plot->GetYaxis()->SetRangeUser(0,1);
 //  // These commands have to be put after the Draw command
 //  // or it will cause segmentation violation
 //  plot->GetXaxis()->SetRangeUser(11,19);
 //  plot->GetYaxis()->SetDecimals();
 //  plot->SetTitle("AR/CF4/CO2 signal timing");
 //  plot->GetXaxis()->SetTitle("Latency [clock cycles = 25ns steps]");
 //  plot->GetYaxis()->SetTitle("LG efficiency");
 //
 //  c1->Update();
 //  c1->SetGridx();
 //  c1->SetGridy();
 //  c1->SetTickx();
 //  c1->SetTicky();
 //  c1->Update();

  return mspl2;

}


// MSPL scan for AR/CO2/CF4 @ -5.35kV ranges from run 0902 to run 0920
TVectorD MSPLscan903920ey(long int first_run = 903, long int last_run = 920)
{

  Int_t size = last_run - first_run + 1;
  Int_t nlat = 9;
  TVectorD tempeff (size);
  TVectorD tempn (size);
  TVectorD lat (nlat);
  TVectorD mspl3 (nlat);
  TVectorD mspl2 (nlat);
  TVectorD mspl3ey (nlat);
  TVectorD mspl2ey (nlat);
  TVectorD ex (nlat);

  for (Int_t i = 0; i < nlat; i++)
    {
      ex[i] = 0;
      lat[i] = i + 11;
    }

  tempeff = efficiency(903,920);
  tempn = nevents(903,920);

  // MSPL = 3
  for (Int_t i = 0; i < nlat; i++)
    {
      mspl3[i] = tempeff[i];
      mspl3ey[i] = sqrt( tempeff[i] * (1 - tempeff[i]) / tempn[i] );
    }

  // MSPL = 2
  for (Int_t i = 0; i < nlat; i++)
    {
      mspl2[i] = tempeff[i + nlat];
      mspl2ey[i] = sqrt( tempeff[i + nlat] * (1 - tempeff[i + nlat]) / tempn[i + nlat] );
    }

  return mspl2ey;

}
