#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 "TGaxis.h"
#include <TFile.h>
#include "TF1.h"
#include "TLegend.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 a ranges from run 0337 to run 0554
TVectorD HVscanAmacro(long int first_run = 337, long int last_run = 554)
{
  Int_t size = last_run - first_run + 1;
  Int_t thsize = 25;
  Int_t datasize = 8;
  Int_t correctsize = size - 18; // Do not consider duplicate runs

  TVectorD dumbefficiencies (size); // With duplicate runs
  TVectorD efficiencies (correctsize);
  TVectorD dumbn_events (size);
  TVectorD n_events (correctsize);
  TVectorD mspl (correctsize);
  TVectorD th (correctsize);
  
  TVectorD dumbTh40mspl4 (thsize); // Data arrays
  TVectorD dumbTh60mspl4 (thsize);
  TVectorD dumbTh80mspl4 (thsize);
  TVectorD dumbTh100mspl4 (thsize);
  TVectorD Th40mspl4 (datasize); // Data arrays
  TVectorD Th60mspl4 (datasize);
  TVectorD Th80mspl4 (datasize);
  TVectorD Th100mspl4 (datasize);
  TVectorD Th40mspl4ey (thsize); // Errors arrays
  TVectorD Th60mspl4ey (thsize);
  TVectorD Th80mspl4ey (thsize);
  TVectorD Th100mspl4ey (thsize);
  TVectorD dumbTh40mspl3 (thsize); // Data arrays
  TVectorD dumbTh60mspl3 (thsize);
  TVectorD dumbTh80mspl3 (thsize);
  TVectorD dumbTh100mspl3 (thsize);
  TVectorD Th40mspl3 (datasize); // Data arrays
  TVectorD Th60mspl3 (datasize);
  TVectorD Th80mspl3 (datasize);
  TVectorD Th100mspl3 (datasize);
  TVectorD Th40mspl3ey (thsize); // Errors arrays
  TVectorD Th60mspl3ey (thsize);
  TVectorD Th80mspl3ey (thsize);
  TVectorD Th100mspl3ey (thsize);
  TVectorD ex (datasize);
  // Temporary
  TVectorD ey (datasize);

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

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

  dumbefficiencies = efficiency(first_run,last_run);
  dumbn_events = nevents(first_run,last_run);
  
  Int_t count = 0;
  for (Int_t i = 0; i < size; i++)
    {
      if (i + first_run < 351 || i + first_run > 368) // Not considering duplicate runs!
	{
	  if ( (count <= 7) || (count >= 32 && count <= 37) || (count >= 56 && count <= 61) || (count >= 80 && count <= 85) || (count >= 104 && count <= 109) || (count >= 128 && count <= 133) || (count >= 152 && count <= 157) || (count >= 176 && count <= 181) )
	    {
	      th[count] = 100;
	    }
	  else if ( (count >= 8 && count <= 15) || (count >= 38 && count <= 43) || (count >= 62 && count <= 67) || (count >= 86 && count <= 91) || (count >= 110 && count <= 115) || (count >= 134 && count <= 139) || (count >= 158 && count <= 163) || (count >= 182 && count <= 187) )
	    {
	      th[count] = 80;
	    }
	  else if ( (count >= 16 && count <= 23) || (count >= 44 && count <= 49) || (count >= 68 && count <= 73) || (count >= 92 && count <= 97) || (count >= 116 && count <= 121) || (count >= 140 && count <= 145) || (count >= 164 && count <= 169) || (count >= 188 && count <= 193) )
	    {
	      th[count] = 60;
	    }
	  else
	    {
	      th[count] = 40;
	    }
	    count++;
	}
      
    }


  // Filling efficiencies array without duplicate runs
  count = 0;
  for (Int_t i = 0; i < size; i++)
    {
      if (i + first_run < 351 || i + first_run > 368) // Not concidering duplicate runs!
	{
	  efficiencies[count] = dumbefficiencies[i];
	  count++;
	}
    }

  // Filling n_events array without duplicate runs
  count = 0;
  for (Int_t i = 0; i < size; i++)
    {
      if (i + first_run < 351 || i + first_run > 368) // Not concidering duplicate runs!
	{
	  n_events[count] = dumbn_events[i];
	  count++;
	}
    }

  // Filling monostables array
  count = 0;
  for (Int_t j = 0; j < 4; j++) // First scans are taken with 4 different latencies
    {
	  for (Int_t i = 0; i < 4; i++)
	    {
	      mspl[count] = 4;
	      count++;
	    }
	  for (Int_t i = 0; i < 4; i++)
	    {
	      mspl[count] = 3;
	      count++;
	    }
    }
  for (Int_t j = 0; j < 28; j++) // The other scans are taken with only 3 different latencies
    {
	  for (Int_t i = 0; i < 3; i++)
	    {
	      mspl[count] = 4;
	      count++;
	    }
	  for (Int_t i = 0; i < 3; i++)
	    {
	      mspl[count] = 3;
	      count++;
	    }
    }


  // Filling efficiency arrays for different thresholds
  Int_t count40 = 0;
  Int_t count60 = 0;
  Int_t count80 = 0;
  Int_t count100 = 0;
  Int_t count403 = 0;
  Int_t count603 = 0;
  Int_t count803 = 0;
  Int_t count1003 = 0;
  for (Int_t i = 0; i < correctsize; i++)
    {
      if (mspl[i] == 4) // MSPL 4clk
	{
	  if (th[i] == 100)
	    {
	      dumbTh100mspl4[count100] = efficiencies[i];
	      count100++;
	    }
	  else if (th[i] == 80)
	    {
	      dumbTh80mspl4[count80] = efficiencies[i];
	      count80++;
	    }
	  else if (th[i] == 60)
	    {
	      dumbTh60mspl4[count60] = efficiencies[i];
	      count60++;
	    }
	  else
	    {
	      dumbTh40mspl4[count40] = efficiencies[i];
	      count40++;
	    }
	}
      else // MSPL 3clk
	{
	  if (th[i] == 100)
	    {
	      dumbTh100mspl3[count1003] = efficiencies[i];
	      count1003++;
	    }
	  else if (th[i] == 80)
	    {
	      dumbTh80mspl3[count803] = efficiencies[i];
	      count803++;
	    }
	  else if (th[i] == 60)
	    {
	      dumbTh60mspl3[count603] = efficiencies[i];
	      count603++;
	    }
	  else
	    {
	      dumbTh40mspl3[count403] = efficiencies[i];
	      count403++;
	    }
	}
    }


//    Now i select the best efficiencies over the scanned latencies.
//    Data format is, for every dumbThXXmsplX array:
//    [0], [1], [2], [3] --> HV -5.25, lat = 16, 15, 14, 13
//    [4], [5], [6]      --> HV -5.20, lat = 15, 14, 13
//    [7], [8], [9]      --> HV -5.15, lat = 15, 14, 13
//    [10], [11], [12]   --> HV -5.10, lat = 15, 14, 13
//    [13], [14], [15]   --> HV -5.00, lat = 15, 14, 13
//    [16], [17], [18]   --> HV -4.90, lat = 15, 14, 13
//    [19], [20], [21]   --> HV -4.80, lat = 15, 14, 13
//    [22], [23], [24]   --> HV -4.60, lat = 15, 14, 13

// I compute separately the first element of each data array
// (4 latencies instead of 3)
    Th100mspl4[0] = max( max(dumbTh100mspl4[0], dumbTh100mspl4[1]), max(dumbTh100mspl4[2], dumbTh100mspl4[3]) );
    Th80mspl4[0] = max( max(dumbTh80mspl4[0], dumbTh80mspl4[1]), max(dumbTh80mspl4[2], dumbTh80mspl4[3]) );
    Th60mspl4[0] = max( max(dumbTh60mspl4[0], dumbTh60mspl4[1]), max(dumbTh60mspl4[2], dumbTh60mspl4[3]) );
    Th40mspl4[0] = max( max(dumbTh40mspl4[0], dumbTh40mspl4[1]), max(dumbTh40mspl4[2], dumbTh40mspl4[3]) );

    Th100mspl3[0] = max( max(dumbTh100mspl3[0], dumbTh100mspl3[1]), max(dumbTh100mspl3[2], dumbTh100mspl3[3]) );
    Th80mspl3[0] = max( max(dumbTh80mspl3[0], dumbTh80mspl3[1]), max(dumbTh80mspl3[2], dumbTh80mspl3[3]) );
    Th60mspl3[0] = max( max(dumbTh60mspl3[0], dumbTh60mspl3[1]), max(dumbTh60mspl3[2], dumbTh60mspl3[3]) );
    Th40mspl3[0] = max( max(dumbTh40mspl3[0], dumbTh40mspl3[1]), max(dumbTh40mspl3[2], dumbTh40mspl3[3]) );

    // I compute the other elements
    count100 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh100mspl4[i] > dumbTh100mspl4[i + 1])
	  {
	    if (dumbTh100mspl4[i] > dumbTh100mspl4[i + 2])
	      {
		Th100mspl4[count100] = dumbTh100mspl4[i];
	      }
	    else
	      {
		Th100mspl4[count100] = dumbTh100mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th100mspl4[count100] = dumbTh100mspl4[i + 1];
	  }
	count100++;
      }

    count80 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh80mspl4[i] > dumbTh80mspl4[i + 1])
	  {
	    if (dumbTh80mspl4[i] > dumbTh80mspl4[i + 2])
	      {
		Th80mspl4[count80] = dumbTh80mspl4[i];
	      }
	    else
	      {
		Th80mspl4[count80] = dumbTh80mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th80mspl4[count80] = dumbTh80mspl4[i + 1];
	  }
	count80++;
      }

    count60 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh60mspl4[i] > dumbTh60mspl4[i + 1])
	  {
	    if (dumbTh60mspl4[i] > dumbTh60mspl4[i + 2])
	      {
		Th60mspl4[count60] = dumbTh60mspl4[i];
	      }
	    else
	      {
		Th60mspl4[count60] = dumbTh60mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th60mspl4[count60] = dumbTh60mspl4[i + 1];
	  }
	count60++;
      }

    count40 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh40mspl4[i] > dumbTh40mspl4[i + 1])
	  {
	    if (dumbTh40mspl4[i] > dumbTh40mspl4[i + 2])
	      {
		Th40mspl4[count40] = dumbTh40mspl4[i];
	      }
	    else
	      {
		Th40mspl4[count40] = dumbTh40mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th40mspl4[count40] = dumbTh40mspl4[i + 1];
	  }
	count40++;
      }

    
    count1003 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh100mspl3[i] > dumbTh100mspl3[i + 1])
	  {
	    if (dumbTh100mspl3[i] > dumbTh100mspl3[i + 2])
	      {
		Th100mspl3[count1003] = dumbTh100mspl3[i];
	      }
	    else
	      {
		Th100mspl3[count1003] = dumbTh100mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th100mspl3[count1003] = dumbTh100mspl3[i + 1];
	  }
	count1003++;
      }

    count803 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh80mspl3[i] > dumbTh80mspl3[i + 1])
	  {
	    if (dumbTh80mspl3[i] > dumbTh80mspl3[i + 2])
	      {
		Th80mspl3[count803] = dumbTh80mspl3[i];
	      }
	    else
	      {
		Th80mspl3[count803] = dumbTh80mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th80mspl3[count803] = dumbTh80mspl3[i + 1];
	  }
	count803++;
      }

    count603 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh60mspl3[i] > dumbTh60mspl3[i + 1])
	  {
	    if (dumbTh60mspl3[i] > dumbTh60mspl3[i + 2])
	      {
		Th60mspl3[count603] = dumbTh60mspl3[i];
	      }
	    else
	      {
		Th60mspl3[count603] = dumbTh60mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th60mspl3[count603] = dumbTh60mspl3[i + 1];
	  }
	count603++;
      }

    count403 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh40mspl3[i] > dumbTh40mspl3[i + 1])
	  {
	    if (dumbTh40mspl3[i] > dumbTh40mspl3[i + 2])
	      {
		Th40mspl3[count403] = dumbTh40mspl3[i];
	      }
	    else
	      {
		Th40mspl3[count403] = dumbTh40mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th40mspl3[count403] = dumbTh40mspl3[i + 1];
	  }
	count403++;
      }
 

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

  TGraphErrors *HVScan100 = new TGraphErrors (HV,Th100mspl4,ex,ey);
  HVScan100->SetTitle("th -100 DAC steps | MSPL 4clk");
  HVScan100->SetMarkerColor(21);
  HVScan100->SetLineColor(21);
  TGraphErrors *HVScan80 = new TGraphErrors (HV,Th80mspl4,ex,ey);
  HVScan80->SetTitle("th -80 DAC steps   | MSPL 4clk");
  HVScan80->SetMarkerColor(2);
  HVScan80->SetLineColor(2);
  TGraphErrors *HVScan60 = new TGraphErrors (HV,Th60mspl4,ex,ey);
  HVScan60->SetTitle("th -60 DAC steps   | MSPL 4clk");
  HVScan60->SetMarkerColor(3);
  HVScan60->SetLineColor(3);
  TGraphErrors *HVScan40 = new TGraphErrors (HV,Th40mspl4,ex,ey);
  HVScan40->SetTitle("th -40 DAC steps   | MSPL 4clk");
  HVScan40->SetMarkerColor(4);
  HVScan40->SetLineColor(4);
  TGraphErrors *HVScan1003 = new TGraphErrors (HV,Th100mspl3,ex,ey);
  HVScan1003->SetTitle("th -100 DAC steps | MSPL 3clk");
  HVScan1003->SetMarkerColor(21);
  HVScan1003->SetLineColor(21);
  HVScan1003->SetLineStyle(2);
  TGraphErrors *HVScan803 = new TGraphErrors (HV,Th80mspl3,ex,ey);
  HVScan803->SetTitle("th -80 DAC steps   | MSPL 3clk");
  HVScan803->SetMarkerColor(2);
  HVScan803->SetLineColor(2);
  HVScan803->SetLineStyle(2);
  TGraphErrors *HVScan603 = new TGraphErrors (HV,Th60mspl3,ex,ey);
  HVScan603->SetTitle("th -60 DAC steps   | MSPL 3clk");
  HVScan603->SetMarkerColor(3);
  HVScan603->SetLineColor(3);
  HVScan603->SetLineStyle(2);
  TGraphErrors *HVScan403 = new TGraphErrors (HV,Th40mspl3,ex,ey);
  HVScan403->SetTitle("th -40 DAC steps   | MSPL 3clk");
  HVScan403->SetMarkerColor(4);
  HVScan403->SetLineColor(4);
  HVScan403->SetLineStyle(2);

  TMultiGraph *HVScan = new TMultiGraph();
  HVScan->Add(HVScan100);
  HVScan->Add(HVScan80);
  HVScan->Add(HVScan60);
  HVScan->Add(HVScan40);
  HVScan->Add(HVScan1003);
  HVScan->Add(HVScan803);
  HVScan->Add(HVScan603);
  HVScan->Add(HVScan403);

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

  HVScan->SetTitle("Point A high voltage scan");
  HVScan->GetXaxis()->SetTitle("Negative voltage on LG divider [kV]");
  HVScan->GetYaxis()->SetTitle("LG efficiency");
  c1->Update();

  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("High Voltage scan (point A)");
  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->AddEntry(HVScan1003,HVScan1003->GetTitle(),"lp");
  legend->AddEntry(HVScan803,HVScan803->GetTitle(),"lp");
  legend->AddEntry(HVScan603,HVScan603->GetTitle(),"lp");
  legend->AddEntry(HVScan403,HVScan403->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();

  return Th60mspl4;
  
}






TVectorD HVscanAmacroey(long int first_run = 337, long int last_run = 554)
{
  Int_t size = last_run - first_run + 1;
  Int_t thsize = 25;
  Int_t datasize = 8;
  Int_t correctsize = size - 18; // Do not consider duplicate runs

  TVectorD dumbefficiencies (size); // With duplicate runs
  TVectorD efficiencies (correctsize);
  TVectorD dumbn_events (size);
  TVectorD n_events (correctsize);
  TVectorD mspl (correctsize);
  TVectorD th (correctsize);
  
  TVectorD dumbTh40mspl4 (thsize); // Data arrays
  TVectorD dumbTh60mspl4 (thsize);
  TVectorD dumbTh80mspl4 (thsize);
  TVectorD dumbTh100mspl4 (thsize);
  TVectorD Th40mspl4 (datasize); // Data arrays
  TVectorD Th60mspl4 (datasize);
  TVectorD Th80mspl4 (datasize);
  TVectorD Th100mspl4 (datasize);
  TVectorD Th40mspl4ey (thsize); // Errors arrays
  TVectorD Th60mspl4ey (thsize);
  TVectorD Th80mspl4ey (thsize);
  TVectorD Th100mspl4ey (thsize);
  TVectorD dumbTh40mspl3 (thsize); // Data arrays
  TVectorD dumbTh60mspl3 (thsize);
  TVectorD dumbTh80mspl3 (thsize);
  TVectorD dumbTh100mspl3 (thsize);
  TVectorD Th40mspl3 (datasize); // Data arrays
  TVectorD Th60mspl3 (datasize);
  TVectorD Th80mspl3 (datasize);
  TVectorD Th100mspl3 (datasize);
  TVectorD Th40mspl3ey (thsize); // Errors arrays
  TVectorD Th60mspl3ey (thsize);
  TVectorD Th80mspl3ey (thsize);
  TVectorD Th100mspl3ey (thsize);
  TVectorD ex (datasize);
  // Temporary
  TVectorD ey (datasize);

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

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

  dumbefficiencies = efficiency(first_run,last_run);
  dumbn_events = nevents(first_run,last_run);
  
  Int_t count = 0;
  for (Int_t i = 0; i < size; i++)
    {
      if (i + first_run < 351 || i + first_run > 368) // Not considering duplicate runs!
	{
	  if ( (count <= 7) || (count >= 32 && count <= 37) || (count >= 56 && count <= 61) || (count >= 80 && count <= 85) || (count >= 104 && count <= 109) || (count >= 128 && count <= 133) || (count >= 152 && count <= 157) || (count >= 176 && count <= 181) )
	    {
	      th[count] = 100;
	    }
	  else if ( (count >= 8 && count <= 15) || (count >= 38 && count <= 43) || (count >= 62 && count <= 67) || (count >= 86 && count <= 91) || (count >= 110 && count <= 115) || (count >= 134 && count <= 139) || (count >= 158 && count <= 163) || (count >= 182 && count <= 187) )
	    {
	      th[count] = 80;
	    }
	  else if ( (count >= 16 && count <= 23) || (count >= 44 && count <= 49) || (count >= 68 && count <= 73) || (count >= 92 && count <= 97) || (count >= 116 && count <= 121) || (count >= 140 && count <= 145) || (count >= 164 && count <= 169) || (count >= 188 && count <= 193) )
	    {
	      th[count] = 60;
	    }
	  else
	    {
	      th[count] = 40;
	    }
	    count++;
	}
      
    }


  // Filling efficiencies array without duplicate runs
  count = 0;
  for (Int_t i = 0; i < size; i++)
    {
      if (i + first_run < 351 || i + first_run > 368) // Not considering duplicate runs!
	{
	  efficiencies[count] = dumbefficiencies[i];
	  count++;
	}
    }

  // Filling n_events array without duplicate runs
  count = 0;
  for (Int_t i = 0; i < size; i++)
    {
      if (i + first_run < 351 || i + first_run > 368) // Not considering duplicate runs!
	{
	  n_events[count] = dumbn_events[i];
	  count++;
	}
    }

  // Filling monostables array
  count = 0;
  for (Int_t j = 0; j < 4; j++) // First scans are taken with 4 different latencies
    {
	  for (Int_t i = 0; i < 4; i++)
	    {
	      mspl[count] = 4;
	      count++;
	    }
	  for (Int_t i = 0; i < 4; i++)
	    {
	      mspl[count] = 3;
	      count++;
	    }
    }
  for (Int_t j = 0; j < 28; j++) // The other scans are taken with only 3 different latencies
    {
	  for (Int_t i = 0; i < 3; i++)
	    {
	      mspl[count] = 4;
	      count++;
	    }
	  for (Int_t i = 0; i < 3; i++)
	    {
	      mspl[count] = 3;
	      count++;
	    }
    }

  // Filling efficiency arrays for different thresholds
  Int_t count40 = 0;
  Int_t count60 = 0;
  Int_t count80 = 0;
  Int_t count100 = 0;
  Int_t count403 = 0;
  Int_t count603 = 0;
  Int_t count803 = 0;
  Int_t count1003 = 0;
  for (Int_t i = 0; i < correctsize; i++)
    {
      if (mspl[i] == 4)
	{
	  if (th[i] == 100)
	    {
	      dumbTh100mspl4[count100] = efficiencies[i];
	      count100++;
	    }
	  else if (th[i] == 80)
	    {
	      dumbTh80mspl4[count80] = efficiencies[i];
	      count80++;
	    }
	  else if (th[i] == 60)
	    {
	      dumbTh60mspl4[count60] = efficiencies[i];
	      count60++;
	    }
	  else
	    {
	      dumbTh40mspl4[count40] = efficiencies[i];
	      count40++;
	    }
	}
      else
	{
	  if (th[i] == 100)
	    {
	      dumbTh100mspl3[count1003] = efficiencies[i];
	      count1003++;
	    }
	  else if (th[i] == 80)
	    {
	      dumbTh80mspl3[count803] = efficiencies[i];
	      count803++;
	    }
	  else if (th[i] == 60)
	    {
	      dumbTh60mspl3[count603] = efficiencies[i];
	      count603++;
	    }
	  else
	    {
	      dumbTh40mspl3[count403] = efficiencies[i];
	      count403++;
	    }
	}
    }


// I compute separately the first element of each data array
// (4 latencies instead of 3)
    Th100mspl4[0] = max( max(dumbTh100mspl4[0], dumbTh100mspl4[1]), max(dumbTh100mspl4[2], dumbTh100mspl4[3]) );
    Th80mspl4[0] = max( max(dumbTh80mspl4[0], dumbTh80mspl4[1]), max(dumbTh80mspl4[2], dumbTh80mspl4[3]) );
    Th60mspl4[0] = max( max(dumbTh60mspl4[0], dumbTh60mspl4[1]), max(dumbTh60mspl4[2], dumbTh60mspl4[3]) );
    Th40mspl4[0] = max( max(dumbTh40mspl4[0], dumbTh40mspl4[1]), max(dumbTh40mspl4[2], dumbTh40mspl4[3]) );

    Th100mspl3[0] = max( max(dumbTh100mspl3[0], dumbTh100mspl3[1]), max(dumbTh100mspl3[2], dumbTh100mspl3[3]) );
    Th80mspl3[0] = max( max(dumbTh80mspl3[0], dumbTh80mspl3[1]), max(dumbTh80mspl3[2], dumbTh80mspl3[3]) );
    Th60mspl3[0] = max( max(dumbTh60mspl3[0], dumbTh60mspl3[1]), max(dumbTh60mspl3[2], dumbTh60mspl3[3]) );
    Th40mspl3[0] = max( max(dumbTh40mspl3[0], dumbTh40mspl3[1]), max(dumbTh40mspl3[2], dumbTh40mspl3[3]) );

    // I compute the other elements
    count100 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh100mspl4[i] > dumbTh100mspl4[i + 1])
	  {
	    if (dumbTh100mspl4[i] > dumbTh100mspl4[i + 2])
	      {
		Th100mspl4[count100] = dumbTh100mspl4[i];
	      }
	    else
	      {
		Th100mspl4[count100] = dumbTh100mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th100mspl4[count100] = dumbTh100mspl4[i + 1];
	  }
	count100++;
      }

    count80 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh80mspl4[i] > dumbTh80mspl4[i + 1])
	  {
	    if (dumbTh80mspl4[i] > dumbTh80mspl4[i + 2])
	      {
		Th80mspl4[count80] = dumbTh80mspl4[i];
	      }
	    else
	      {
		Th80mspl4[count80] = dumbTh80mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th80mspl4[count80] = dumbTh80mspl4[i + 1];
	  }
	count80++;
      }

    count60 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh60mspl4[i] > dumbTh60mspl4[i + 1])
	  {
	    if (dumbTh60mspl4[i] > dumbTh60mspl4[i + 2])
	      {
		Th60mspl4[count60] = dumbTh60mspl4[i];
	      }
	    else
	      {
		Th60mspl4[count60] = dumbTh60mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th60mspl4[count60] = dumbTh60mspl4[i + 1];
	  }
	count60++;
      }

    count40 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh40mspl4[i] > dumbTh40mspl4[i + 1])
	  {
	    if (dumbTh40mspl4[i] > dumbTh40mspl4[i + 2])
	      {
		Th40mspl4[count40] = dumbTh40mspl4[i];
	      }
	    else
	      {
		Th40mspl4[count40] = dumbTh40mspl4[i + 2];
	      }
	  }
	else
	  {
	    Th40mspl4[count40] = dumbTh40mspl4[i + 1];
	  }
	count40++;
      }

    
    count1003 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh100mspl3[i] > dumbTh100mspl3[i + 1])
	  {
	    if (dumbTh100mspl3[i] > dumbTh100mspl3[i + 2])
	      {
		Th100mspl3[count1003] = dumbTh100mspl3[i];
	      }
	    else
	      {
		Th100mspl3[count1003] = dumbTh100mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th100mspl3[count1003] = dumbTh100mspl3[i + 1];
	  }
	count1003++;
      }

    count803 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh80mspl3[i] > dumbTh80mspl3[i + 1])
	  {
	    if (dumbTh80mspl3[i] > dumbTh80mspl3[i + 2])
	      {
		Th80mspl3[count803] = dumbTh80mspl3[i];
	      }
	    else
	      {
		Th80mspl3[count803] = dumbTh80mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th80mspl3[count803] = dumbTh80mspl3[i + 1];
	  }
	count803++;
      }

    count603 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh60mspl3[i] > dumbTh60mspl3[i + 1])
	  {
	    if (dumbTh60mspl3[i] > dumbTh60mspl3[i + 2])
	      {
		Th60mspl3[count603] = dumbTh60mspl3[i];
	      }
	    else
	      {
		Th60mspl3[count603] = dumbTh60mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th60mspl3[count603] = dumbTh60mspl3[i + 1];
	  }
	count603++;
      }

    count403 = 1;
    for (Int_t i = 4; i < thsize; i = i + 3)
      {
	if (dumbTh40mspl3[i] > dumbTh40mspl3[i + 1])
	  {
	    if (dumbTh40mspl3[i] > dumbTh40mspl3[i + 2])
	      {
		Th40mspl3[count403] = dumbTh40mspl3[i];
	      }
	    else
	      {
		Th40mspl3[count403] = dumbTh40mspl3[i + 2];
	      }
	  }
	else
	  {
	    Th40mspl3[count403] = dumbTh40mspl3[i + 1];
	  }
	count403++;
      }

    // WARNING: preliminarly over-existimating errors!
    for (Int_t i = 0; i < datasize; i++)
      {
	ey[i] = sqrt( Th40mspl3[i] * (1 - Th40mspl3[i]) / 5000 );
      }

  return ey;
  
}
