//************************************************
// Author: Federica Lionetto
// Created on: 11/11/2014
//**********************************************

/*
CheckBaselineShift checks the baseline shift between data acquisitions in pedestal run mode and data acquisitions in laser run mode.
It shows the distribution of the observables dPed=ped(ped run 2)-ped(ped run 1), dMix=ped(las run 2)-ped(ped run 1), and dLas=ped(las run 2)-ped(las run 1) (TH1F *hdPedBeetle<n>, TH1F *hdMixBeetle<n>, and TH1F *hdLasBeetle<n>, respectively) and the 2D distribution of the observables ped(ped run 2) and ped(las run 2) (TH2F *h2pedLasBeetle<n>), where <n>={1,2}.
It shows the same distributions as above, but for Beetle channels connected to silicon and Beetle channels not connected to silicon, instead of Beetle 1 and Beetle 2 (TH1F *hdPedConn, TH1F *hdMixConn, TH1F *hdLasConn, TH2F *h2pedLasConn, TH1F *hdPedNotConn, TH1F *hdMixNotConn, TH1F *hdLasNotConn, and TH2F *h2pedLasNotConn).
It shows the dMix as a function of the Beetle channel (TH1F *hdMixvsstrip).

Compile with:

make

Run with:

./CheckBaselineShift [additional folder]

where:
- [additional folder] is the optional additional folder where the output will be saved.
*/

#include "../Tools/Lib.C"
#include "../Tools/lhcbStyle.C"
#include "../Tools/Style.C"

#include "../Tools/Par.C"

void CheckBaselineShift(char *externalPath=0);

int main(int argc, char *argv[])
{
  getLHCbStyle();
  PersonalStyle();
  
  if ((argc ==2) && (string(argv[1]) == "--info"))
  {
    cout << "**************************************************" << endl;
    
    cout << "Some comments." << endl;
    
    cout << "**************************************************" << endl;
    
    return 0;
  }
  else if (argc==1)
  {
    CheckBaselineShift();
    
    return 0;
  }
  else if (argc==2)
  {
    CheckBaselineShift(argv[1]);
    
    return 0;
  }
  else
  {
    cout << "Error! Too many arguments given..." << endl;
    
    return 0;
  }
}

void CheckBaselineShift(char *externalPath)
{
  cout << "**************************************************" << endl;
  cout << "Checking baseline shift between pedestal run mode and laser run mode..." << endl;
  cout << "**************************************************" << endl;

  // Do some fanciness to get the directory right.
  string analysis = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift";
  if (externalPath!=0)
    analysis = string(analysis+"/"+externalPath);
  string figures = analysis+"/Figures";

  // Create the <analysis> folder. Do not worry, nothing bad is going to happen if the folder already exists.
  string path_to_make = "mkdir -p "+analysis;
  system(path_to_make.c_str());
  cout << "Setting output to: " << path_to_make << endl;

  // Create a folder named Figures inside the folder named <analysis>.
  string path_to_make_figures = "mkdir "+figures;
  system(path_to_make_figures.c_str());
  cout << "Setting figures to: " << path_to_make_figures << endl;  

  TString path_to_figures = (string(path_to_make_figures)).substr((string(path_to_make_figures)).find_last_of(' ')+1);

  cout << path_to_figures << endl;

  // Input pedestal text files.
  /*
    string inputPed1Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141110-ped.dat";
    string inputPed2Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141110-2-ped.dat";
    string inputLas1Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141112-laserOffSensor-las.dat";
    string inputLas2Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141112-2-laserOffSensor-las.dat";
  */

  string inputPed1Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141113-ped.dat";
  string inputPed2Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141113-2-ped.dat";
  string inputLas1Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141113-laserOffSensor-las.dat";
  string inputLas2Filename = "/home/hep/flionett/TestStand/AnalysisResults/Hans410/CheckBaselineShift/ComputePedestals-20141113-2-laserOffSensor-las.dat";

  cout << "First pedestal run: " << inputPed1Filename << endl;
  cout << "Second pedestal run: " << inputPed2Filename << endl;

  cout << "First laser run: " << inputLas1Filename << endl;
  cout << "Second laser run: " << inputLas2Filename << endl;

  // Open input pedestal text files.
  FILE *inputPed1 = fopen(TString(inputPed1Filename),"r");
  FILE *inputPed2 = fopen(TString(inputPed2Filename),"r");
  FILE *inputLas1 = fopen(TString(inputLas1Filename),"r");
  FILE *inputLas2 = fopen(TString(inputLas2Filename),"r");

  // Read pedestals.
  float ped1[N];
  float ped2[N];
  float las1[N];
  float las2[N];

  for (int iChannel=0; iChannel<N; ++iChannel) {
    fscanf(inputPed1,"%*d%*c%f%*c%*f%*c",&(ped1[iChannel]));
    fscanf(inputPed2,"%*d%*c%f%*c%*f%*c",&(ped2[iChannel]));
    fscanf(inputLas1,"%*d%*c%f%*c%*f%*c",&(las1[iChannel]));
    fscanf(inputLas2,"%*d%*c%f%*c%*f%*c",&(las2[iChannel]));
    // cout << iChannel << ", " << ped1[iChannel] << endl;
  }
  
  // Close input pedestal text files.
  fclose(inputPed1);
  fclose(inputPed2);
  fclose(inputLas1);
  fclose(inputLas2);

  // Open output data ROOT file.
  TFile *output = TFile::Open(TString(analysis+"/CheckBaselineShift.root"),"RECREATE");

  // Beetle 1.

  TH1F *hdPedBeetle1 = new TH1F("hdPedBeetle1","",60,-30,30);
  TH1F *hdMixBeetle1 = new TH1F("hdMixBeetle1","",60,-30,30);
  TH1F *hdLasBeetle1 = new TH1F("hdLasBeetle1","",60,-30,30);
  TH2F *h2pedLasBeetle1 = new TH2F("h2pedLasBeetle1","",150,450,600,150,450,600);

  InitHist(hdPedBeetle1,"Shift in pedestals - Beetle 1","ADC counts","");
  InitHist(hdMixBeetle1,"Shift in pedestals - Beetle 1","ADC counts",""); 
  InitHist(hdLasBeetle1,"Shift in pedestals - Beetle 1","ADC counts","");
  InitHist2(h2pedLasBeetle1,"Pedestals - Beetle 1","pedestal run (ADC counts)","laser run (ADC counts)","");

  TLegend *legd = CreateLegend3(hdPedBeetle1,"pedestal runs",hdMixBeetle1,"mixed runs",hdLasBeetle1,"laser runs");

  TCanvas *cdBeetle1 = new TCanvas("cdBeetle1","",400,300);
  TCanvas *cpedLasBeetle1 = new TCanvas("cpedLasBeetle1","",400,300);
  
  for (int iChannel=0;iChannel<NBeetle;++iChannel) {
    hdPedBeetle1->Fill(ped2[iChannel]-ped1[iChannel]);
    hdMixBeetle1->Fill(las2[iChannel]-ped1[iChannel]);
    hdLasBeetle1->Fill(las2[iChannel]-las1[iChannel]);
    h2pedLasBeetle1->Fill(ped2[iChannel],las2[iChannel]);
  }

  DrawHistCompare3(cdBeetle1,hdPedBeetle1,hdMixBeetle1,hdLasBeetle1,legd,path_to_figures);
  DrawHist2(cpedLasBeetle1,h2pedLasBeetle1,"colz",path_to_figures);



  // Beetle 2.

  TH1F *hdPedBeetle2 = new TH1F("hdPedBeetle2","",60,-30,30);
  TH1F *hdMixBeetle2 = new TH1F("hdMixBeetle2","",60,-30,30);
  TH1F *hdLasBeetle2 = new TH1F("hdLasBeetle2","",60,-30,30);
  TH2F *h2pedLasBeetle2 = new TH2F("h2pedLasBeetle2","",150,450,600,150,450,600);

  InitHist(hdPedBeetle2, "Shift in pedestals - Beetle 2","ADC counts","");
  InitHist(hdMixBeetle2,"Shift in pedestals - Beetle 2","ADC counts",""); 
  InitHist(hdLasBeetle2,"Shift in pedestals - Beetle 2","ADC counts","");
  InitHist2(h2pedLasBeetle2,"Pedestals - Beetle 2","pedestal run (ADC counts)","laser run (ADC counts)","");

  TCanvas *cdBeetle2 = new TCanvas("cdBeetle2","",400,300);
  TCanvas *cpedLasBeetle2 = new TCanvas("cpedLasBeetle2","",400,300);
  
  for (int iChannel=NBeetle;iChannel<N;++iChannel) {
    hdPedBeetle2->Fill(ped2[iChannel]-ped1[iChannel]);
    hdMixBeetle2->Fill(las2[iChannel]-ped1[iChannel]);
    hdLasBeetle2->Fill(las2[iChannel]-las1[iChannel]);
    h2pedLasBeetle2->Fill(ped2[iChannel],las2[iChannel]);
  }

  DrawHistCompare3(cdBeetle2,hdPedBeetle2,hdMixBeetle2,hdLasBeetle2,legd,path_to_figures);
  DrawHist2(cpedLasBeetle2,h2pedLasBeetle2,"colz",path_to_figures);



  // Strips connected to the silicon.

  TH1F *hdPedConn = new TH1F("hdPedConn","",60,-30,30);
  TH1F *hdMixConn = new TH1F("hdMixConn","",60,-30,30);
  TH1F *hdLasConn = new TH1F("hdLasConn","",60,-30,30);
  TH2F *h2pedLasConn = new TH2F("h2pedLasConn","",150,450,600,150,450,600);

  InitHist(hdPedConn,"Shift in pedestals - connected to Si","ADC counts","");
  InitHist(hdMixConn,"Shift in pedestals - connected to Si","ADC counts","");
  InitHist(hdLasConn,"Shift in pedestals - connected to Si","ADC counts","");
  InitHist2(h2pedLasConn,"Pedestals - connected to Si","pedestal run (ADC counts)","laser run (ADC counts)","");

  TCanvas *cdConn = new TCanvas("cdConn","",400,300);
  TCanvas *cpedLasConn = new TCanvas("cpedLasConn","",400,300);
  
  for (int iChannel=0;iChannel<N;++iChannel) {
    if (iChannel%NSkip==0) {
      hdPedConn->Fill(ped2[iChannel]-ped1[iChannel]);
      hdMixConn->Fill(las2[iChannel]-ped1[iChannel]);
      hdLasConn->Fill(las2[iChannel]-las1[iChannel]);
      h2pedLasConn->Fill(ped2[iChannel],las2[iChannel]);
    }
  }

  DrawHistCompare3(cdConn,hdPedConn,hdMixConn,hdLasConn,legd,path_to_figures);
  DrawHist2(cpedLasConn,h2pedLasConn,"colz",path_to_figures);



  // Strips not connected to the silicon.

  TH1F *hdPedNotConn = new TH1F("hdPedNotConn","",60,-30,30);
  TH1F *hdMixNotConn = new TH1F("hdMixNotConn","",60,-30,30);
  TH1F *hdLasNotConn = new TH1F("hdLasNotConn","",60,-30,30);
  TH2F *h2pedLasNotConn = new TH2F("h2pedLasNotConn","",150,450,600,150,450,600);

  InitHist(hdPedNotConn,"Shift in pedestals - not connected to Si","ADC counts","");
  InitHist(hdMixNotConn,"Shift in pedestals - not connected to Si","ADC counts","");
  InitHist(hdLasNotConn,"Shift in pedestals - not connected to Si","ADC counts","");
  InitHist2(h2pedLasNotConn,"Pedestals - not connected to Si","pedestal run (ADC counts)","laser run (ADC counts)","");

  TCanvas *cdNotConn = new TCanvas("cdNotConn","",400,300);
  TCanvas *cpedLasNotConn = new TCanvas("cpedLasNotConn","",400,300);
  
  for (int iChannel=0;iChannel<N;++iChannel) {
    if (iChannel%NSkip!=0) {
      hdPedNotConn->Fill(ped2[iChannel]-ped1[iChannel]);
      hdMixNotConn->Fill(las2[iChannel]-ped1[iChannel]);
      hdLasNotConn->Fill(las2[iChannel]-las1[iChannel]);
      h2pedLasNotConn->Fill(ped2[iChannel],las2[iChannel]);
    }
  }

  DrawHistCompare3(cdNotConn,hdPedNotConn,hdMixNotConn,hdLasNotConn,legd,path_to_figures);
  DrawHist2(cpedLasNotConn,h2pedLasNotConn,"colz",path_to_figures);



  // dMix as a function of the Beetle channel (TH1F *hdMixvsstrip).
  float strip[N];
  for (int iChannel=0;iChannel<N;++iChannel)
    strip[iChannel] = (float)iChannel; 
  TH1F *hdMixvsstrip = new TH1F("hdMixvsstrip","",256,0,256);
  InitHist(hdMixvsstrip,"Shift in pedestals","Beetle channel","ADC counts");
  for (int iChannel=0;iChannel<N;++iChannel) 
  {
    hdMixvsstrip->Fill(iChannel,las2[iChannel]-ped2[iChannel]);
  }
  hdMixvsstrip->SetMinimum(-30);
  hdMixvsstrip->SetMaximum(30);
  TCanvas *cdMixvsstrip = new TCanvas("cdMixvsstrip","",400,300);
  DrawHist(cdMixvsstrip,hdMixvsstrip,"",path_to_figures);

  // Write output ROOT file.
  output->Write();

  // Close output ROOT files.
  output->Close();

  return;
}
