#include <string>
#include <TTree.h>
#include <TFile.h>
#include <cstdio>
#include <TGraph.h>
#include <TMultiGraph.h>
#include <TCanvas.h>
#include <TF1.h>
#include <stdio.h>
#include <TAxis.h>
#include "TotemMap.cpp"
#include <TVectorT.h>
#include "TLegend.h"


using namespace std;

// This file contains routines for calibration pulse scans

void CalPulseAnalizer(TString inputfile, TString outputfile, const Int_t nlines, Int_t vfatnumber);
double GetX(int channel);
double GetY(int channel);
double dist(double x0, double y0, double x1, double y1);

Double_t GetR(int channel)
{
  Double_t channelX = GetX(channel);
  Double_t channelY = GetY(channel);
  Double_t radius;
  radius = dist(channelX, channelY, 0, 0);
  return radius;
}


TVectorD CPScan(long int firstPad = 2, long int lastPad = 128, Int_t nFiles = 64, Int_t nVFAT = 0){
	
	// Defining vectors
	TVectorD nPad (nFiles);
	TVectorD nRadius (nFiles);
	TVectorD nSigma (nFiles);
	TVectorD nMean (nFiles);
	// Initializing values
	Double_t Sigma = 0;
	Double_t Mean = 0;
	// Defining filename strings
	string rawfile_name;
	string rootfile_name;
	
	// Cycling over the channels indicated
        long int count=0;
	for (long int i=firstPad;i<lastPad+1;i=i+2)
	{
       	      nRadius[count] = GetR(i);

	      // Converting i to a string to open the i-th file
	      string s;
	      char Pad_n[30];
	      sprintf(Pad_n,"%ld",i);     
	      s = string(Pad_n);
	      
	      if (i<10)
	      {
		rootfile_name = "CP-RootData/CPscan-00" + s + ".root";
		rawfile_name = "CP-RawData/00" + s + ".dat";
	      }	      
	      else 
	      {
	      	if (i>=10 && i<100)
	      	{
	      		rootfile_name = "CP-RootData/CPscan-0" + s + ".root";
	      		rawfile_name = "CP-RawData/0" + s + ".dat";
	      	}
	      	else
	      	{
	      		rootfile_name = "CP-RootData/CPscan-" + s + ".root";
	      		rawfile_name = "CP-RawData/" + s + ".dat";
	      	}
	      }
	      nPad[count] = i;

	      CalPulseAnalizer(rawfile_name,rootfile_name,151,nVFAT);

	      TFile *rootfile = new TFile(rootfile_name.c_str());
	      TGraph* tempG = dynamic_cast<TGraph*>(rootfile->Get("Graph"));
	      Sigma = tempG->GetFunction("sigmoid")->GetParameter(2);
	      nSigma[count] = Sigma;
	      Mean = tempG->GetFunction("sigmoid")->GetParameter(1);
	      nMean[count] = Mean;
	      count++;
	      delete tempG;
	      rootfile->Close();
	}

		return nSigma;
}


TVectorD CPScanRadius(long int firstPad = 2, long int lastPad = 128, Int_t nFiles = 64, Int_t nVFAT = 0){
	
  TVectorD nPad (nFiles);
  TVectorD nRadius (nFiles);
  TVectorD nSigma (nFiles);
  TVectorD nMean (nFiles);
	// Initializing values
	Double_t Sigma = 0;
	Double_t Mean = 0;
	// Defining filename strings
	string rawfile_name;
	string rootfile_name;
	
	// Cycling over the channels indicated
        long int count=0;
	for (long int i=firstPad;i<lastPad+1;i=i+2)
	{
       	      nRadius[count] = GetR(i);

	      // Converting i to a string to open the i-th file
	      string s;
	      char Pad_n[30];
	      sprintf(Pad_n,"%ld",i);     
	      s = string(Pad_n);
	      
	      if (i<10)
	      {
		rootfile_name = "CP-RootData/CPscan-00" + s + ".root";
		rawfile_name = "CP-RawData/00" + s + ".dat";
	      }	      
	      else 
	      {
	      	if (i>=10 && i<100)
	      	{
	      		rootfile_name = "CP-RootData/CPscan-0" + s + ".root";
	      		rawfile_name = "CP-RawData/0" + s + ".dat";
	      	}
	      	else
	      	{
	      		rootfile_name = "CP-RootData/CPscan-" + s + ".root";
	      		rawfile_name = "CP-RawData/" + s + ".dat";
	      	}
	      }
	      nPad[count] = i;

	      CalPulseAnalizer(rawfile_name,rootfile_name,151,nVFAT);

	      TFile *rootfile = new TFile(rootfile_name.c_str());
	      TGraph* tempG = dynamic_cast<TGraph*>(rootfile->Get("Graph"));
	      Sigma = tempG->GetFunction("sigmoid")->GetParameter(2);
	      nSigma[count] = Sigma;
	      Mean = tempG->GetFunction("sigmoid")->GetParameter(1);
	      nMean[count] = Mean;
	      count++;
	      delete tempG;
	      rootfile->Close();
	}

	return nRadius;
}


void MakeSPlot(Int_t nFiles = 64){

  TVectorD nSigma0 (nFiles);
  TVectorD nSigma1 (nFiles);
  TVectorD nSigma2 (nFiles);
  TVectorD nSigma3 (nFiles);
  TVectorD nSigma4 (nFiles);
  TVectorD nSigma5 (nFiles);
  TVectorD nSigma6 (nFiles);
  TVectorD nSigma7 (nFiles);
  TVectorD nRadius (nFiles);

  nSigma0 = CPScan(2,128,64,0);
  nSigma1 = CPScan(2,128,64,1);
  nSigma2 = CPScan(2,128,64,2);
  nSigma3 = CPScan(2,128,64,3);
  nSigma4 = CPScan(2,128,64,4);
  nSigma5 = CPScan(2,128,64,5);
  nSigma6 = CPScan(2,128,64,6);
  nSigma7 = CPScan(2,128,64,7);

  nRadius = CPScanRadius(2,128,64,0);

  TCanvas* c1 = new TCanvas ("c1","S-Curve sigma for different readout pads");

  TGraph *plot0 = new TGraph(nRadius,nSigma0);
  plot0->SetMarkerColor(42);
  plot0->SetTitle("VFAT0");
  plot0->SetMarkerStyle(3);

  TGraph *plot1 = new TGraph(nRadius,nSigma1);
  plot1->SetMarkerColor(43);
  plot1->SetTitle("VFAT1");
  plot1->SetMarkerStyle(4);

  TGraph *plot2 = new TGraph(nRadius,nSigma2);
  plot2->SetMarkerColor(44);
  plot2->SetTitle("VFAT2");
  plot2->SetMarkerStyle(25);

  TGraph *plot3 = new TGraph(nRadius,nSigma3);
  plot3->SetMarkerColor(45);
  plot3->SetTitle("VFAT3");
  plot3->SetMarkerStyle(26);

  TGraph *plot4 = new TGraph(nRadius,nSigma4);
  plot4->SetMarkerColor(46);
  plot4->SetTitle("VFAT4");
  plot4->SetMarkerStyle(27);

  TGraph *plot5 = new TGraph(nRadius,nSigma5);
  plot5->SetMarkerColor(47);
  plot5->SetTitle("VFAT5");
  plot5->SetMarkerStyle(28);

  TGraph *plot6 = new TGraph(nRadius,nSigma6);
  plot6->SetMarkerColor(48);
  plot6->SetTitle("VFAT6");
  plot6->SetMarkerStyle(30);

  TGraph *plot7 = new TGraph(nRadius,nSigma7);
  plot7->SetLineColor(12);
  plot7->SetMarkerColor(12);
  plot7->SetTitle("VFAT7 channels [DISCONNECTED]");

  TMultiGraph *multiplot = new TMultiGraph;
  multiplot->Add(plot0);
  multiplot->Add(plot1);
  multiplot->Add(plot2);
  multiplot->Add(plot3);
  multiplot->Add(plot4);
  multiplot->Add(plot5);
  multiplot->Add(plot6);

  multiplot->Draw("AP");
  multiplot->GetXaxis()->SetRangeUser(155,725);
  multiplot->GetXaxis()->SetTitle("Pad distance from chamber's vertex [mm]");
  multiplot->GetYaxis()->SetTitle("Noise amplitude [VFAT2 DAC steps]");
  c1->SetGridx();
  c1->SetGridy();
  c1->SetTickx();
  c1->SetTicky();
  c1->Update();

  TLegend *legend = new TLegend (0.1,0.4,0.4,0.9);
  legend->SetHeader("S-Curve Sigma");
  legend->AddEntry(plot0,plot0->GetTitle(),"p");
  legend->AddEntry(plot1,plot1->GetTitle(),"p");
  legend->AddEntry(plot2,plot2->GetTitle(),"p");
  legend->AddEntry(plot3,plot3->GetTitle(),"p");
  legend->AddEntry(plot4,plot4->GetTitle(),"p");
  legend->AddEntry(plot5,plot5->GetTitle(),"p");
  legend->AddEntry(plot6,plot6->GetTitle(),"p");
  legend->Draw();

  c1->Update();

}
