#include <string>
#include <iostream>
#include <sstream>
#include <TTree.h>
#include <TFile.h>
#include <TH1F.h>
#include <TCut.h>
#include <cstdio>
#include "TMultiGraph.h"
#include "TAxis.h"
#include "TGraph.h"
#include "TLegend.h"
#include "TVectorD.h"
#include "TCanvas.h"


TVectorD EffRadius(long int run_number, double xoffsetmm, double yoffsetmm, long int minrad, Int_t nsteps);

void EffradNoise(double offsetX, double offsetY){

  // I make a multigraph with all the efficiency VS radius graphs for the points around the LG.
  // Change offsets to move around the LG
  Int_t steps = 30;
  long int begin = 0;
  TCanvas* c1;
  long int run1 = 550;
  long int run2 = 380;
  long int run3 = 371;

  // Declaring efficiency arrays
  // 2 data sets should be enough
  TVectorD run550 (steps+1); // HV -4.60 th 40
  TVectorD run380 (steps+1); // HV -5.25 th 40
  TVectorD run371 (steps+1); // HV -5.25 th 60

  // Declaring and initializing list of efficiency radiuses
  TVectorD rads (steps+1);
  for (int i = 0; i<=steps; i++) {rads[i]=2*i;};

  // Filling efficiency arrays
  run550 = EffRadius(run1,offsetX,offsetY,begin,steps);
  run380 = EffRadius(run2,offsetX,offsetY,begin,steps);
  run371 = EffRadius(run3,offsetX,offsetY,begin,steps);

  TGraph *graph550 = new TGraph (rads,run550);
  graph550->SetTitle("HV -4.60kV; TH -40ds");
  graph550->SetMarkerColor(kBlack);
  graph550->SetMarkerStyle(24);
  graph550->SetLineColor(kBlack);
  TGraph *graph380 = new TGraph (rads,run380);
  graph380->SetTitle("HV -5.25kV; TH -40ds");
  graph380->SetMarkerColor(kBlack);
  graph380->SetMarkerStyle(25);
  graph380->SetLineColor(kBlack);
  TGraph *graph371 = new TGraph (rads,run371);
  graph371->SetTitle("HV -5.25kV; TH -60ds");
  graph371->SetMarkerColor(kBlack);
  graph371->SetMarkerStyle(26);
  graph371->SetLineColor(kBlack);

  TMultiGraph *multigraph = new TMultiGraph();
  multigraph->Add(graph550);
  multigraph->Add(graph380);
  multigraph->Add(graph371);

  multigraph->Draw("ACP");

  multigraph->GetXaxis()->SetTitle("Efficiency radius [mm]");
  multigraph->GetYaxis()->SetTitle("LG Efficiency (wrong offsets)");
  multigraph->GetYaxis()->SetDecimals();
  multigraph->GetXaxis()->SetRangeUser(0,60);
  multigraph->SetTitle("LG out-beam efficiency versus efficiency radius");

  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("Noise contribution");
  legend->AddEntry(graph550,graph550->GetTitle(),"lp");
  legend->AddEntry(graph380,graph380->GetTitle(),"lp");
  legend->AddEntry(graph371,graph371->GetTitle(),"lp");
  legend->Draw();
  c1->Update();
  c1->SetGridx();
  c1->SetGridy();

  c1->Update();

}
