#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 ranges from run 0867 to run 0902
void MSPLscan867902(long int first_run = 867, long int last_run = 902)
{

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

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

  tempeff = efficiency(867,902);
  tempn = nevents(867,902);

  // First runs (mspl = 4) are mixed:
  // run - lat - i
  //
  // 870 - 11  - 3
  // 871 - 12  - 4
  // 872 - 13  - 5
  // 867 - 14  - 0
  // 868 - 15  - 1
  // 869 - 16  - 2
  // 873 - 17  - 6
  // 874 - 18  - 7
  // 875 - 19  - 8

  mspl4[0] = tempeff[3];
  mspl4[1] = tempeff[4];
  mspl4[2] = tempeff[5];
  mspl4[3] = tempeff[0];
  mspl4[4] = tempeff[1];
  mspl4[5] = tempeff[2];
  mspl4[6] = tempeff[6];
  mspl4[7] = tempeff[7];
  mspl4[8] = tempeff[8];

  mspl4ey[0] = sqrt( tempeff[3] * (1 - tempeff[3]) / tempn[3] );
  mspl4ey[1] = sqrt( tempeff[4] * (1 - tempeff[4]) / tempn[4] );
  mspl4ey[2] = sqrt( tempeff[5] * (1 - tempeff[5]) / tempn[5] );
  mspl4ey[3] = sqrt( tempeff[0] * (1 - tempeff[0]) / tempn[0] );
  mspl4ey[4] = sqrt( tempeff[1] * (1 - tempeff[1]) / tempn[1] );
  mspl4ey[5] = sqrt( tempeff[2] * (1 - tempeff[2]) / tempn[2] );
  mspl4ey[6] = sqrt( tempeff[6] * (1 - tempeff[6]) / tempn[6] );
  mspl4ey[7] = sqrt( tempeff[7] * (1 - tempeff[7]) / tempn[7] );
  mspl4ey[8] = sqrt( tempeff[8] * (1 - tempeff[8]) / tempn[8] );

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

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

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

  TGraphErrors *monost1 = new TGraphErrors (lat,mspl1,ex,mspl1ey);
  monost1->SetLineColor(kMagenta);
  monost1->SetMarkerColor(kMagenta);
  monost1->SetLineWidth(2);
  monost1->SetTitle("MSPL = 1clk");
  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");
  TGraphErrors *monost4 = new TGraphErrors (lat,mspl4,ex,mspl4ey);
  monost4->SetLineColor(kBlue);
  monost4->SetMarkerColor(kBlue);
  monost4->SetLineWidth(2);
  monost4->SetTitle("MSPL = 4clk");

  TCanvas *c1 = new TCanvas("c1","MSPL Scan");

  TMultiGraph *plot = new TMultiGraph();
  plot->Add(monost1);
  plot->Add(monost2);
  plot->Add(monost3);
  plot->Add(monost4);

  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("Point P signal timing scan");
  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();

}
