//************************************************
// Author: Federica Lionetto
// Created on: 05/03/2015
//************************************************

/*
CCEDelayScan reads all the ROOT files of a given CCE delay data taking and shows how the mu and sigma of the charge sharing coefficient eta vary as a function of the delay.

The CCE delay data taking is identified by the following information:
- <sensor>, that is, the type of sensor (Hans410, ...);
- <date>, that is, the date when the measurement was taken;
- <z>, that is, the position along z;
- <firstd>, that is, the first value of the delay;
- <lastd>, that is, the last value of the delay;
- <stepd>, that is, the step between two subsequent data acquisitions.

Compile with:

make

Run with:

./CCEDelayScan [sensor] [date] [z] [firstd] [lastd] [stepd] [additional folder]

For example:

./CCEDelayScan Hans410 20141121 176 67 75 1

A folder named AnalysisResults will be created in a fixed location and a ROOT file will be saved in there. A folder named Figures will be created inside the folder named AnalysisResults, with some monitoring plots.

If needed, an additional folder can be specified, that will be created inside the folder named AnalysisResults.
*/

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

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

void CCEDelayScan(char *sensor, char *date, char *z, const Float_t firstd, const Float_t lastd, const Float_t stepd, char *externalPath=0);

int main(int argc, char *argv[])
{
  getLHCbStyle();
  PersonalStyle();

  if ((argc == 2) && (string(argv[1]) == "--info"))
  {
    cout << "**************************************************" << endl;

    cout << "CCEDelayScan reads all the ROOT files of a given CCE delay data taking and shows how the mu and sigma of the charge sharing coefficient eta vary as a function of the delay." << endl;

    cout << "The CCE delay data taking is identified by the following information:" << endl;
    cout << "- <sensor>, that is, the type of sensor (Hans410, ...);" << endl;
    cout << "- <date>, that is, the date when the measurement was taken;" << endl;
    cout << "- <z>, that is, the position along z;" << endl;
    cout << "- <firstd>, that is, the first value of the delay;" << endl;
    cout << "- <lastd>, that is, the last value of the delay;" << endl;
    cout << "- <stepd>, that is, the step between two subsequent data acquisitions." << endl;

    cout << "Compile with:" << endl;

    cout << "make" << endl;

    cout << "Run with:" << endl;

    cout << "./CCEDelayScan [sensor] [date] [z] [firstd] [lastd] [stepd] [additional folder]" << endl;

    cout << "For example:" << endl;

    cout << "./CCEDelayScan Hans410 20141121 176 67 75 1" << endl;

    cout << "A folder named AnalysisResults will be created in a fixed location and a ROOT file will be saved in there. A folder named Figures will be created inside the folder named AnalysisResults, with some monitoring plots." << endl;

    cout << "If needed, an additional folder can be specified, that will be created inside the folder named AnalysisResults." << endl;
    
    cout << "**************************************************" << endl;

    return 0;
  }
  else if (argc < 7)
  {
    cout << "**************************************************" << endl;

    cout << "Error! Arguments missing..." << endl;
    cout << "Please use the following format:" << endl;
    cout << "./CCEDelayScan [1] [2] [3] [4] [5] [6]" << endl;
    cout << "with:" << endl;
    cout << "[1] = Type of sensor (Hans410, ...);" << endl;
    cout << "[2] = Date;" << endl;
    cout << "[3] = Position along z;" << endl;
    cout << "[4] = First value of the delay;" << endl;
    cout << "[5] = Last value of the delay" << endl;
    cout << "[6] = Step between two subsequent data acquisitions;" << endl;
    cout << "[7] = Additional folder, optional." << endl;
    cout << "Type ./CCEDelayScan --info for more information." << endl;
    
    cout << "**************************************************" << endl;

    return 0;
  }
  else
  {
    cout << "Type of sensor: " << argv[1] << endl;
    cout << "Date: " << argv[2] << endl;
    cout << "Position along z: " << argv[3] << endl;
    cout << "First value of the delay: " << argv[4] << endl;
    cout << "Last value of the delay: " << argv[5] << endl;
    cout << "Step between two subsequent data acquisitions: " << argv[6] << endl;
    if (argc == 7)
      CCEDelayScan(argv[1],argv[2],argv[3],atoi(argv[4]),atoi(argv[5]),atoi(argv[6]));
    else if (argc == 8)
      CCEDelayScan(argv[1],argv[2],argv[3],atoi(argv[4]),atoi(argv[5]),atoi(argv[6]),argv[7]);
    else
    {
      cout << "Error! Too many arguments given..." << endl;
      
      return 0;
    }
    
    return 0;  
  }
}

void CCEDelayScan(char *sensor, char *date, char *z, const Float_t firstd, const Float_t lastd, const Float_t stepd, char *externalPath)
{
  cout << "**************************************************" << endl;
  cout << "Delay scan of charge sharing coefficient..." << endl;
  cout << "**************************************************" << endl;

  // Do not comment this line.
  gROOT->ProcessLine("#include <vector>");

  // Do some fanciness to get the directory right.
  string inputDirectory = "/disk/groups/hep/flionett/TestStand/AnalysisResults/"+string(sensor)+"/CCEDelayScan";
  string outputDirectory = "/disk/groups/hep/flionett/TestStand/AnalysisResults/"+string(sensor)+"/CCEDelayScan";
  if (externalPath!=0)
    outputDirectory = string(outputDirectory+"/"+externalPath);
  cout << "The input directory is: " << inputDirectory << endl;
  cout << "The output directory is: " << outputDirectory << endl;

  // Create the outputDirectory directory if it does not exist.
  string path_to_make = "mkdir -p "+outputDirectory;
  system(path_to_make.c_str());

  cout << "Figures stored in: " << outputDirectory+"/Figures/"+"CCE-ProcessRawData-"+date+"-216-"+z+"z" << endl;

  // Create a directory named Figures inside the directory named outputDirectory if it does not exist.
  string path_to_make_figures = "mkdir -p "+outputDirectory+"/Figures/"+"CCE-ProcessRawData-"+date+"-216-"+z+"z";
  system(path_to_make_figures.c_str());

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

  char char_input_ROOT[200];
  string input_ROOT;
  string output_ROOT;

  const Int_t steps = (Int_t)((lastd-firstd)/stepd+1);
  Float_t d[steps];
  Float_t ud[steps];

  Float_t muEtaSignalOverNoise[steps];
  Float_t sigmaEtaSignalOverNoise[steps];

  Float_t umuEtaSignalOverNoise[steps];
  Float_t usigmaEtaSignalOverNoise[steps];

  // Get information from tree.
  Float_t muEtaSignalOverNoise1;
  Float_t sigmaEtaSignalOverNoise1;

  Float_t umuEtaSignalOverNoise1;
  Float_t usigmaEtaSignalOverNoise1;

  TBranch *b_muEtaSignalOverNoise1 = 0;
  TBranch *b_sigmaEtaSignalOverNoise1 = 0;

  TBranch *b_umuEtaSignalOverNoise1 = 0;
  TBranch *b_usigmaEtaSignalOverNoise1 = 0;

  // Open output ROOT file.
  output_ROOT = outputDirectory+"/CCEDelayScan-"+date+"-216-"+z+"z"+".root";
  TFile *output = TFile::Open(TString(output_ROOT),"RECREATE");

  for (Int_t step=0;step<steps;step++) {
    d[step] = firstd+step*stepd;
    ud[step] = 0.;

    // Open input data ROOT files.
    // I did not find a better way to create a string containing the bias voltage.
    sprintf(char_input_ROOT,"%s/CCE-ProcessRawData-%s-%dns-216-%sz.root",inputDirectory.c_str(),date,(int)d[step],z);
    input_ROOT = string(char_input_ROOT);
    // Check that the filename provided corresponds to a ROOT file.
    int found = input_ROOT.find(".root");
    if (found==string::npos)  {
      cout << "Error! The filename provided is not associated to a ROOT file." << endl;
      return;
    }

    cout << "Open ROOT file #" << step+1 << ": " << input_ROOT << endl;
    TFile *input = TFile::Open(TString(input_ROOT));

    // Get information from tree.
    TTree *tFitResults = (TTree *)input->Get("tFitResults");
    int Events = tFitResults->GetEntries();
    if (Events != 1)
    {
      cout << "Error! The ROOT file has been corrupted..." << endl;
      
      return ;
    }
    else 
    {
      tFitResults->SetBranchAddress("muEtaSignalOverNoise",&muEtaSignalOverNoise1,&b_muEtaSignalOverNoise1);
      tFitResults->SetBranchAddress("sigmaEtaSignalOverNoise",&sigmaEtaSignalOverNoise1,&b_sigmaEtaSignalOverNoise1);
      tFitResults->SetBranchAddress("umuEtaSignalOverNoise",&umuEtaSignalOverNoise1,&b_umuEtaSignalOverNoise1);
      tFitResults->SetBranchAddress("usigmaEtaSignalOverNoise",&usigmaEtaSignalOverNoise1,&b_usigmaEtaSignalOverNoise1);

      tFitResults->GetEntry();
    }

    muEtaSignalOverNoise[step] = muEtaSignalOverNoise1;
    sigmaEtaSignalOverNoise[step] = sigmaEtaSignalOverNoise1;
    umuEtaSignalOverNoise[step] = umuEtaSignalOverNoise1;
    usigmaEtaSignalOverNoise[step] = usigmaEtaSignalOverNoise1;

    // Close input data ROOT files.
    input->Close();
  }

  output->cd();

  // Plot mu.
  TGraphErrors *gMuEtaSignalOverNoiseVSd = new TGraphErrors(steps,d,muEtaSignalOverNoise,ud,umuEtaSignalOverNoise);
  InitGraphErrors(gMuEtaSignalOverNoiseVSd,"Charge sharing coefficient - #mu","delay (ns)","#mu (#mum)");

  TCanvas *cMuEtaSignalOverNoiseVSd = new TCanvas(Form("cMuEtaSignalOverNoiseVSd-%s",date),"",550,300);
  DrawGraphErrors(cMuEtaSignalOverNoiseVSd,gMuEtaSignalOverNoiseVSd,"AP",path_to_figures);

  // Plot sigma.
  TGraphErrors *gSigmaEtaSignalOverNoiseVSd = new TGraphErrors(steps,d,sigmaEtaSignalOverNoise,ud,usigmaEtaSignalOverNoise);
  InitGraphErrors(gSigmaEtaSignalOverNoiseVSd,"Charge sharing coefficient - #sigma","delay (ns)","#sigma (#mum)");

  TCanvas *cSigmaEtaSignalOverNoiseVSd = new TCanvas(Form("cSigmaEtaSignalOverNoiseVSd-%s",date),"",550,300);
  DrawGraphErrors(cSigmaEtaSignalOverNoiseVSd,gSigmaEtaSignalOverNoiseVSd,"AP",path_to_figures);

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

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

  return;
}
