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

TVectorD HVscanAmacro(long int first_run = 337, long int last_run = 554);
TVectorD HVscanAmacroey(long int first_run = 337, long int last_run = 554);
TVectorD HVscanARCO2CF4(long int first_run = 857, long int last_run = 869);
TVectorD HVscanARCO2CF4ey(long int first_run = 857, long int last_run = 869);

void GasComparisonHV()
{
  Int_t ARCO2size;
  Int_t ARCO2CF4size;
  ARCO2size = 8;
  ARCO2CF4size = 5;

  TVectorD ARCO2 (ARCO2size);
  TVectorD ARCO2CF4 (ARCO2CF4size);
  TVectorD ARCO2ey (ARCO2size);
  TVectorD ARCO2CF4ey (ARCO2CF4size);
  TVectorD ARCO2ex (ARCO2size);
  TVectorD ARCO2CF4ex (ARCO2CF4size);
  TVectorD ARCO2hv (ARCO2size);
  TVectorD ARCO2CF4hv (ARCO2CF4size);

  ARCO2 = HVscanAmacro();
  ARCO2ey = HVscanAmacroey();
  ARCO2CF4 = HVscanARCO2CF4();
  ARCO2CF4ey = HVscanARCO2CF4ey();

  for (Int_t i = 0; i < ARCO2size; i++)
    {
      ARCO2ex[i] = 0;
    }
  for (Int_t i = 0; i < ARCO2CF4size; i++)
    {
      ARCO2CF4ex[i] = 0;
    }

  ARCO2hv[0] = 5.25;
  ARCO2hv[1] = 5.20;
  ARCO2hv[2] = 5.15;
  ARCO2hv[3] = 5.10;
  ARCO2hv[4] = 5.00;
  ARCO2hv[5] = 4.90;
  ARCO2hv[6] = 4.80;
  ARCO2hv[7] = 4.60;

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

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

  TGraphErrors *firstgas = new TGraphErrors(ARCO2hv,ARCO2,ARCO2ex,ARCO2ey);
  firstgas->SetMarkerColor(kBlue);
  firstgas->SetLineColor(kBlue);
  firstgas->SetTitle("Ar-CO2 70/30");
  firstgas->SetLineWidth(2);

  TGraphErrors *secondgas = new TGraphErrors(ARCO2CF4hv,ARCO2CF4,ARCO2CF4ex,ARCO2CF4ey);
  secondgas->SetMarkerColor(kRed);
  secondgas->SetLineColor(kRed);
  secondgas->SetTitle("Ar-CO2-CF4 60/20/20");
  secondgas->SetLineWidth(2);

  TMultiGraph *GasComp = new TMultiGraph();
  GasComp->Add(firstgas);
  GasComp->Add(secondgas);

  GasComp->Draw("ACP");
  GasComp->GetYaxis()->SetRangeUser(0,1);
  // These commands have to be put after the Draw command,
  // or it will cause segmentation violation
  GasComp->GetXaxis()->SetRangeUser(4.6,5.3);
  GasComp->GetYaxis()->SetDecimals();
  GasComp->GetXaxis()->SetDecimals();
  GasComp->GetXaxis()->SetTitle("Divider HV [kV]");
  GasComp->GetYaxis()->SetTitle("LG efficiency");

  c1->Update();
  c1->SetGridx();
  c1->SetGridy();
  c1->SetTicky();
  c1->Update();

  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("Gas Mixture comparison");
  legend->AddEntry(firstgas,firstgas->GetTitle(),"lp");
  legend->AddEntry(secondgas,secondgas->GetTitle(),"lp");
  legend->Draw();

  TF1 *f1 = new TF1 ("f1","x",767,883);
  f1->Update();

  TGaxis *current = new TGaxis (4.6, 1, 5.3, 1, "f1", 1020, "-");
  current->SetTitle("Divider current [uA]");
  current->SetTitleOffset(1.1);
  current->Draw();
  c1->Update();

}
