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


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

// MSPL scan for point P ranges from run 0109 to run 0268
TVectorD MSPLscanPmacro(long int first_run = 109, long int last_run = 268)
{

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

  TVectorD efficiencies (size);
  TVectorD n_events (size);
  
  TVectorD Th40mspl4 (thsize); // Data arrays
  TVectorD Th60mspl4 (thsize);
  TVectorD Th80mspl4 (thsize);
  TVectorD Th100mspl4 (thsize);
  TVectorD Th40mspl4ey (thsize); // Errors arrays
  TVectorD Th60mspl4ey (thsize);
  TVectorD Th80mspl4ey (thsize);
  TVectorD Th100mspl4ey (thsize);
  TVectorD Th40mspl3 (thsize); // Data arrays
  TVectorD Th60mspl3 (thsize);
  TVectorD Th80mspl3 (thsize);
  TVectorD Th100mspl3 (thsize);
  TVectorD Th40mspl3ey (thsize); // Errors arrays
  TVectorD Th60mspl3ey (thsize);
  TVectorD Th80mspl3ey (thsize);
  TVectorD Th100mspl3ey (thsize);
  TVectorD Th40mspl2 (thsize); // Data arrays
  TVectorD Th60mspl2 (thsize);
  TVectorD Th80mspl2 (thsize);
  TVectorD Th100mspl2 (thsize);
  TVectorD Th40mspl2ey (thsize); // Errors arrays
  TVectorD Th60mspl2ey (thsize);
  TVectorD Th80mspl2ey (thsize);
  TVectorD Th100mspl2ey (thsize);
  TVectorD Th40mspl1 (thsize); // Data arrays
  TVectorD Th60mspl1 (thsize);
  TVectorD Th80mspl1 (thsize);
  TVectorD Th100mspl1 (thsize);
  TVectorD Th40mspl1ey (thsize); // Errors arrays
  TVectorD Th60mspl1ey (thsize);
  TVectorD Th80mspl1ey (thsize);
  TVectorD Th100mspl1ey (thsize);
  TVectorD ex (thsize);

  TVectorD lat (thsize); // X-axis contains latencies from 10 to 19
  for (Int_t i = 10; i < 20; i++)
    {
      lat[i-10] = i;
    }

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

  efficiencies = efficiency(first_run,last_run);
  n_events = nevents(first_run,last_run);
  
  // Filling efficiency arrays for different monostable pulse lenghts and thresholds
  
  for (Int_t i = 0; i < 10; i++)
    {
      Int_t count404 = i; // MSPL = 4clk
      Int_t count604 = i + 10;
      Int_t count804 = i + 20;
      Int_t count1004 = i + 30;
      Int_t count403 = i + 40; // MSPL = 3clk
      Int_t count603 = i + 50;
      Int_t count803 = i + 60;
      Int_t count1003 = i + 70;
      Int_t count402 = i + 80; // MSPL = 2clk
      Int_t count602 = i + 90;
      Int_t count802 = i + 100;
      Int_t count1002 = i + 110;
      Int_t count401 = i + 120; // MSPL = 1clk
      Int_t count601 = i + 130;
      Int_t count801 = i + 140;
      Int_t count1001 = i + 150;
	      
      Th40mspl4[i] = efficiencies[count404];
      Th40mspl4ey[i] = sqrt( efficiencies[count404] * (1 - efficiencies[count404]) / n_events[count404] );

      Th60mspl4[i] = efficiencies[count604];
      Th60mspl4ey[i] = sqrt( efficiencies[count604] * (1 - efficiencies[count604]) / n_events[count604] );

      Th80mspl4[i] = efficiencies[count804];
      Th80mspl4ey[i] = sqrt( efficiencies[count804] * (1 - efficiencies[count804]) / n_events[count804] );

      Th100mspl4[i] = efficiencies[count1004];
      Th100mspl4ey[i] = sqrt( efficiencies[count1004] * (1 - efficiencies[count1004]) / n_events[count1004] );


      Th40mspl3[i] = efficiencies[count403];
      Th40mspl3ey[i] = sqrt( efficiencies[count403] * (1 - efficiencies[count403]) / n_events[count403] );

      Th60mspl3[i] = efficiencies[count603];
      Th60mspl3ey[i] = sqrt( efficiencies[count603] * (1 - efficiencies[count603]) / n_events[count603] );

      Th80mspl3[i] = efficiencies[count803];
      Th80mspl3ey[i] = sqrt( efficiencies[count803] * (1 - efficiencies[count803]) / n_events[count803] );

      Th100mspl3[i] = efficiencies[count1003];
      Th100mspl3ey[i] = sqrt( efficiencies[count1003] * (1 - efficiencies[count1003]) / n_events[count1003] );


      Th40mspl2[i] = efficiencies[count402];
      Th40mspl2ey[i] = sqrt( efficiencies[count402] * (1 - efficiencies[count402]) / n_events[count402] );

      Th60mspl2[i] = efficiencies[count602];
      Th60mspl2ey[i] = sqrt( efficiencies[count602] * (1 - efficiencies[count602]) / n_events[count602] );

      Th80mspl2[i] = efficiencies[count802];
      Th80mspl2ey[i] = sqrt( efficiencies[count802] * (1 - efficiencies[count802]) / n_events[count802] );

      Th100mspl2[i] = efficiencies[count1002];
      Th100mspl2ey[i] = sqrt( efficiencies[count1002] * (1 - efficiencies[count1002]) / n_events[count1002] );


      Th40mspl1[i] = efficiencies[count401];
      Th40mspl1ey[i] = sqrt( efficiencies[count401] * (1 - efficiencies[count401]) / n_events[count401] );

      Th60mspl1[i] = efficiencies[count601];
      Th60mspl1ey[i] = sqrt( efficiencies[count601] * (1 - efficiencies[count601]) / n_events[count601] );

      Th80mspl1[i] = efficiencies[count801];
      Th80mspl1ey[i] = sqrt( efficiencies[count801] * (1 - efficiencies[count801]) / n_events[count801] );

      Th100mspl1[i] = efficiencies[count1001];
      Th100mspl1ey[i] = sqrt( efficiencies[count1001] * (1 - efficiencies[count1001]) / n_events[count1001] );


    }

  // Plotting MSPL scan
  TCanvas* c1 = new TCanvas ("c1","Point P MSPL/Latency scan");

  //MSPL = 4clk
  TGraphErrors *mspl4th100 = new TGraphErrors (lat,Th100mspl4,ex,Th100mspl4ey);
  mspl4th100->SetTitle("MSPL 4clk | Threshold -100 DAC steps");
  mspl4th100->SetMarkerColor(kBlue+3);
  mspl4th100->SetLineColor(kBlue+3);
  mspl4th100->SetLineWidth(2);
  
  TGraphErrors *mspl4th80 = new TGraphErrors (lat,Th80mspl4,ex,Th80mspl4ey);
  mspl4th80->SetTitle("MSPL 4clk | Threshold -80 DAC steps");
  mspl4th80->SetMarkerColor(kBlue);
  mspl4th80->SetLineColor(kBlue);
  mspl4th80->SetLineWidth(2);
  
  TGraphErrors *mspl4th60 = new TGraphErrors (lat,Th60mspl4,ex,Th60mspl4ey);
  mspl4th60->SetTitle("MSPL 4clk | Threshold -60 DAC steps");
  mspl4th60->SetMarkerColor(kBlue-5);
  mspl4th60->SetLineColor(kBlue-5);
  mspl4th60->SetLineWidth(2);
  
  TGraphErrors *mspl4th40 = new TGraphErrors (lat,Th40mspl4,ex,Th40mspl4ey);
  mspl4th40->SetTitle("MSPL 4clk | Threshold -40 DAC steps");
  mspl4th40->SetMarkerColor(kBlue-10);
  mspl4th40->SetLineColor(kBlue-10);
  mspl4th40->SetLineWidth(2);
  mspl4th40->SetLineStyle(7);

  //MSPL = 3clk
  TGraphErrors *mspl3th100 = new TGraphErrors (lat,Th100mspl3,ex,Th100mspl3ey);
  mspl3th100->SetTitle("MSPL 3clk | Threshold -100 DAC steps");
  mspl3th100->SetMarkerColor(kSpring-6);
  mspl3th100->SetLineColor(kSpring-6);
  mspl3th100->SetLineWidth(2);
  
  TGraphErrors *mspl3th80 = new TGraphErrors (lat,Th80mspl3,ex,Th80mspl3ey);
  mspl3th80->SetTitle("MSPL 3clk | Threshold -80 DAC steps");
  mspl3th80->SetMarkerColor(kSpring+4);
  mspl3th80->SetLineColor(kSpring+4);
  mspl3th80->SetLineWidth(2);
  
  TGraphErrors *mspl3th60 = new TGraphErrors (lat,Th60mspl3,ex,Th60mspl3ey);
  mspl3th60->SetTitle("MSPL 3clk | Threshold -60 DAC steps");
  mspl3th60->SetMarkerColor(kSpring-1);
  mspl3th60->SetLineColor(kSpring-1);
  mspl3th60->SetLineWidth(2);
  
  TGraphErrors *mspl3th40 = new TGraphErrors (lat,Th40mspl3,ex,Th40mspl3ey);
  mspl3th40->SetTitle("MSPL 3clk | Threshold -40 DAC steps");
  mspl3th40->SetMarkerColor(kSpring+7);
  mspl3th40->SetLineColor(kSpring+7);
  mspl3th40->SetLineWidth(2);
  mspl3th40->SetLineStyle(7);
  
  //MSPL = 2clk
  TGraphErrors *mspl2th100 = new TGraphErrors (lat,Th100mspl2,ex,Th100mspl2ey);
  mspl2th100->SetTitle("MSPL 2clk | Threshold -100 DAC steps");
  mspl2th100->SetMarkerColor(kOrange+9);
  mspl2th100->SetLineColor(kOrange+9);
  mspl2th100->SetLineWidth(2);
  
  TGraphErrors *mspl2th80 = new TGraphErrors (lat,Th80mspl2,ex,Th80mspl2ey);
  mspl2th80->SetTitle("MSPL 2clk | Threshold -80 DAC steps");
  mspl2th80->SetMarkerColor(kOrange+7);
  mspl2th80->SetLineColor(kOrange+7);
  mspl2th80->SetLineWidth(2);
  
  TGraphErrors *mspl2th60 = new TGraphErrors (lat,Th60mspl2,ex,Th60mspl2ey);
  mspl2th60->SetTitle("MSPL 2clk | Threshold -60 DAC steps");
  mspl2th60->SetMarkerColor(kOrange-3);
  mspl2th60->SetLineColor(kOrange-3);
  mspl2th60->SetLineWidth(2);
  
  TGraphErrors *mspl2th40 = new TGraphErrors (lat,Th40mspl2,ex,Th40mspl2ey);
  mspl2th40->SetTitle("MSPL 2clk | Threshold -40 DAC steps");
  mspl2th40->SetMarkerColor(kOrange-2);
  mspl2th40->SetLineColor(kOrange-2);
  mspl2th40->SetLineWidth(2);
  mspl2th40->SetLineStyle(7);
  
  //MSPL = 1clk
  TGraphErrors *mspl1th100 = new TGraphErrors (lat,Th100mspl1,ex,Th100mspl1ey);
  mspl1th100->SetTitle("MSPL 1clk | Threshold -100 DAC steps");
  mspl1th100->SetMarkerColor(kMagenta+2);
  mspl1th100->SetLineColor(kMagenta+2);
  mspl1th100->SetLineWidth(2);
  
  TGraphErrors *mspl1th80 = new TGraphErrors (lat,Th80mspl1,ex,Th80mspl1ey);
  mspl1th80->SetTitle("MSPL 1clk | Threshold -80 DAC steps");
  mspl1th80->SetMarkerColor(kMagenta);
  mspl1th80->SetLineColor(kMagenta);
  mspl1th80->SetLineWidth(2);
  
  TGraphErrors *mspl1th60 = new TGraphErrors (lat,Th60mspl1,ex,Th60mspl1ey);
  mspl1th60->SetTitle("MSPL 1clk | Threshold -60 DAC steps");
  mspl1th60->SetMarkerColor(kMagenta-7);
  mspl1th60->SetLineColor(kMagenta-7);
  mspl1th60->SetLineWidth(2);
  
  TGraphErrors *mspl1th40 = new TGraphErrors (lat,Th40mspl1,ex,Th40mspl1ey);
  mspl1th40->SetTitle("MSPL 1clk | Threshold -40 DAC steps");
  mspl1th40->SetMarkerColor(kMagenta-9);
  mspl1th40->SetLineColor(kMagenta-9);
  mspl1th40->SetLineWidth(2);
  mspl1th40->SetLineStyle(7);
  

  TMultiGraph *MSPLscan = new TMultiGraph();
  MSPLscan->Add(mspl4th100);
  MSPLscan->Add(mspl4th80);
  MSPLscan->Add(mspl4th60);
  MSPLscan->Add(mspl4th40);
  MSPLscan->Add(mspl3th100);
  MSPLscan->Add(mspl3th80);
  MSPLscan->Add(mspl3th60);
  MSPLscan->Add(mspl3th40);
  MSPLscan->Add(mspl2th100);
  MSPLscan->Add(mspl2th80);
  MSPLscan->Add(mspl2th60);
  MSPLscan->Add(mspl2th40);
  MSPLscan->Add(mspl1th100);
  MSPLscan->Add(mspl1th80);
  MSPLscan->Add(mspl1th60);
  MSPLscan->Add(mspl1th40);

  MSPLscan->Draw("ALP");
  c1->SetGridx();
  c1->SetGridy();
  c1->SetTickx();
  c1->SetTicky();
  MSPLscan->GetYaxis()->SetRangeUser(0,1);
  MSPLscan->GetXaxis()->SetRangeUser(10,19);
  MSPLscan->GetYaxis()->SetDecimals();
  MSPLscan->SetTitle("Point P signal timing scan");
  MSPLscan->GetXaxis()->SetTitle("Latency [clock cycles = 25ns steps]");
  MSPLscan->GetYaxis()->SetTitle("LG efficiency");


  c1->Update();
  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("#splitline{Latency scan @ Ar-CO2 70/30}{[I=859#muA, th=-60steps]}");
  legend->AddEntry(mspl4th40,mspl4th40->GetTitle(),"lp");
  legend->AddEntry(mspl4th60,mspl4th60->GetTitle(),"lp");
  legend->AddEntry(mspl4th80,mspl4th80->GetTitle(),"lp");
  legend->AddEntry(mspl4th100,mspl4th100->GetTitle(),"lp");
  legend->AddEntry(mspl3th40,mspl3th40->GetTitle(),"lp");
  legend->AddEntry(mspl3th60,mspl3th60->GetTitle(),"lp");
  legend->AddEntry(mspl3th80,mspl3th80->GetTitle(),"lp");
  legend->AddEntry(mspl3th100,mspl3th100->GetTitle(),"lp");
  legend->AddEntry(mspl2th40,mspl2th40->GetTitle(),"lp");
  legend->AddEntry(mspl2th60,mspl2th60->GetTitle(),"lp");
  legend->AddEntry(mspl2th80,mspl2th80->GetTitle(),"lp");
  legend->AddEntry(mspl2th100,mspl2th100->GetTitle(),"lp");
  legend->AddEntry(mspl1th40,mspl1th40->GetTitle(),"lp");
  legend->AddEntry(mspl1th60,mspl1th60->GetTitle(),"lp");
  legend->AddEntry(mspl1th80,mspl1th80->GetTitle(),"lp");
  legend->AddEntry(mspl1th100,mspl1th100->GetTitle(),"lp");
  legend->Draw();

  c1->Update();

  return Th60mspl2;
  
}


// MSPL scan for point p ranges from run 0109 to run 0268
TVectorD MSPLscanPmacroey(long int first_run = 109, long int last_run = 268)
{

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

  TVectorD efficiencies (size);
  TVectorD n_events (size);
  
  TVectorD Th40mspl4 (thsize); // Data arrays
  TVectorD Th60mspl4 (thsize);
  TVectorD Th80mspl4 (thsize);
  TVectorD Th100mspl4 (thsize);
  TVectorD Th40mspl4ey (thsize); // Errors arrays
  TVectorD Th60mspl4ey (thsize);
  TVectorD Th80mspl4ey (thsize);
  TVectorD Th100mspl4ey (thsize);
  TVectorD Th40mspl3 (thsize); // Data arrays
  TVectorD Th60mspl3 (thsize);
  TVectorD Th80mspl3 (thsize);
  TVectorD Th100mspl3 (thsize);
  TVectorD Th40mspl3ey (thsize); // Errors arrays
  TVectorD Th60mspl3ey (thsize);
  TVectorD Th80mspl3ey (thsize);
  TVectorD Th100mspl3ey (thsize);
  TVectorD Th40mspl2 (thsize); // Data arrays
  TVectorD Th60mspl2 (thsize);
  TVectorD Th80mspl2 (thsize);
  TVectorD Th100mspl2 (thsize);
  TVectorD Th40mspl2ey (thsize); // Errors arrays
  TVectorD Th60mspl2ey (thsize);
  TVectorD Th80mspl2ey (thsize);
  TVectorD Th100mspl2ey (thsize);
  TVectorD Th40mspl1 (thsize); // Data arrays
  TVectorD Th60mspl1 (thsize);
  TVectorD Th80mspl1 (thsize);
  TVectorD Th100mspl1 (thsize);
  TVectorD Th40mspl1ey (thsize); // Errors arrays
  TVectorD Th60mspl1ey (thsize);
  TVectorD Th80mspl1ey (thsize);
  TVectorD Th100mspl1ey (thsize);
  TVectorD ex (thsize);

  TVectorD lat (thsize); // X-axis contains latencies from 10 to 19
  for (Int_t i = 10; i < 20; i++)
    {
      lat[i-10] = i;
    }

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

  efficiencies = efficiency(first_run,last_run);
  n_events = nevents(first_run,last_run);
  
  // Filling efficiency arrays for different monostable pulse lenghts and thresholds
  
  for (Int_t i = 0; i < 10; i++)
    {
      Int_t count404 = i; // MSPL = 4clk
      Int_t count604 = i + 10;
      Int_t count804 = i + 20;
      Int_t count1004 = i + 30;
      Int_t count403 = i + 40; // MSPL = 3clk
      Int_t count603 = i + 50;
      Int_t count803 = i + 60;
      Int_t count1003 = i + 70;
      Int_t count402 = i + 80; // MSPL = 2clk
      Int_t count602 = i + 90;
      Int_t count802 = i + 100;
      Int_t count1002 = i + 110;
      Int_t count401 = i + 120; // MSPL = 1clk
      Int_t count601 = i + 130;
      Int_t count801 = i + 140;
      Int_t count1001 = i + 150;
	      
      Th40mspl4[i] = efficiencies[count404];
      Th40mspl4ey[i] = sqrt( efficiencies[count404] * (1 - efficiencies[count404]) / n_events[count404] );

      Th60mspl4[i] = efficiencies[count604];
      Th60mspl4ey[i] = sqrt( efficiencies[count604] * (1 - efficiencies[count604]) / n_events[count604] );

      Th80mspl4[i] = efficiencies[count804];
      Th80mspl4ey[i] = sqrt( efficiencies[count804] * (1 - efficiencies[count804]) / n_events[count804] );

      Th100mspl4[i] = efficiencies[count1004];
      Th100mspl4ey[i] = sqrt( efficiencies[count1004] * (1 - efficiencies[count1004]) / n_events[count1004] );


      Th40mspl3[i] = efficiencies[count403];
      Th40mspl3ey[i] = sqrt( efficiencies[count403] * (1 - efficiencies[count403]) / n_events[count403] );

      Th60mspl3[i] = efficiencies[count603];
      Th60mspl3ey[i] = sqrt( efficiencies[count603] * (1 - efficiencies[count603]) / n_events[count603] );

      Th80mspl3[i] = efficiencies[count803];
      Th80mspl3ey[i] = sqrt( efficiencies[count803] * (1 - efficiencies[count803]) / n_events[count803] );

      Th100mspl3[i] = efficiencies[count1003];
      Th100mspl3ey[i] = sqrt( efficiencies[count1003] * (1 - efficiencies[count1003]) / n_events[count1003] );


      Th40mspl2[i] = efficiencies[count402];
      Th40mspl2ey[i] = sqrt( efficiencies[count402] * (1 - efficiencies[count402]) / n_events[count402] );

      Th60mspl2[i] = efficiencies[count602];
      Th60mspl2ey[i] = sqrt( efficiencies[count602] * (1 - efficiencies[count602]) / n_events[count602] );

      Th80mspl2[i] = efficiencies[count802];
      Th80mspl2ey[i] = sqrt( efficiencies[count802] * (1 - efficiencies[count802]) / n_events[count802] );

      Th100mspl2[i] = efficiencies[count1002];
      Th100mspl2ey[i] = sqrt( efficiencies[count1002] * (1 - efficiencies[count1002]) / n_events[count1002] );


      Th40mspl1[i] = efficiencies[count401];
      Th40mspl1ey[i] = sqrt( efficiencies[count401] * (1 - efficiencies[count401]) / n_events[count401] );

      Th60mspl1[i] = efficiencies[count601];
      Th60mspl1ey[i] = sqrt( efficiencies[count601] * (1 - efficiencies[count601]) / n_events[count601] );

      Th80mspl1[i] = efficiencies[count801];
      Th80mspl1ey[i] = sqrt( efficiencies[count801] * (1 - efficiencies[count801]) / n_events[count801] );

      Th100mspl1[i] = efficiencies[count1001];
      Th100mspl1ey[i] = sqrt( efficiencies[count1001] * (1 - efficiencies[count1001]) / n_events[count1001] );


    }

  return Th60mspl2ey;
  
}
