//************************************************
// Author: Federica Lionetto
// Created on: 21/07/2014
//************************************************

/*
FindStrip reads the file called <inputFilename> and returns the following information:
- Beetle channel corresponding to the strip hit by the laser;
- direction for finding the cluster of four adjacent strips (not Beetle channels!).
It also plots the hit map (pedestal subtracted ADC counts averaged over events as a function of the Beetle channel, for all Beetle channels and for Beetle channels connected to silicon.
*/

// Header guard.
#ifndef __FINDSTRIP_C_INCLUDED__
#define __FINDSTRIP_C_INCLUDED__

#include "Lib.C"
#include "lhcbStyle.C"
#include "Style.C"
#include "Par.C"

void FindStrip(string inputFilename, string outputFilename, TString path_to_figures, int *strip, string *direction);

void FindStrip(string inputFilename, string outputFilename, TString path_to_figures, int *strip, string *direction) {
  cout << "**************************************************" << endl;
  cout << "Finding the Beetle channel corresponding to the strip hit by the laser..." << endl;
  cout << "**************************************************" << endl;

  cout << "Input file: " << inputFilename << endl;
  cout << "Output file: " << outputFilename << endl;

  int found;

  // Check that the input filename provided corresponds to a ROOT file.
  found = inputFilename.find(".root");
  if (found==string::npos)  {
    cout << "Error! The filename provided is not associated to a ROOT file." << endl;
    return;
  }

 // Check that the output filename provided corresponds to a ROOT file.
  found = outputFilename.find(".root");
  if (found==string::npos)  {
    cout << "Error! The filename provided is not associated to a ROOT file." << endl;
    return;
  }

  // Check that the input filename provided corresponds to an existing ROOT file.
  /*
  if (!(boost::filesystem::exists(inputFilename.c_str()))) {
    cout << "Error! The filename provided is not associated to an existing ROOT file." << endl;
    return;
  }
  */

  cout << "Open input ROOT file " << inputFilename << endl;
  TFile *input = TFile::Open(TString(inputFilename));

  TTree *EventInfo = (TTree *)input->Get("EventInfo");
  int EventsEventInfo = EventInfo->GetEntries();

  std::vector<double> *ADCProcessed = 0;
  TBranch *b_ADCProcessed = 0;
  EventInfo->SetBranchAddress("ADCProcessed",&ADCProcessed,&b_ADCProcessed);

  cout << "Open output ROOT file " << outputFilename << endl;
  TFile *output = TFile::Open(TString(outputFilename),"RECREATE");

  //==================================================================
  //
  // Canvases.
  //
  
  TCanvas *cmap = new TCanvas("cmap","",400,300);
  TCanvas *cmapConnected = new TCanvas("cmapConnected","",400,300);

  //==================================================================
  //
  // Histograms.
  //

  TH1F *hmap = new TH1F("hmap","",N,0,N);
  TH1F *hmapConnected = new TH1F("hmapConnected","",N/4,0,N);

  InitHist(hmap,"Hit map - all Beetle channels","Beetle channel","");
  InitHist(hmapConnected,"Hit map - Beetle channels connected to Si","Beetle channel","");

  //==================================================================
  //
  // Fill histograms.
  //

  for (int iEvent=0;iEvent<EventsEventInfo;iEvent++)
  {
    EventInfo->GetEntry(iEvent);
    for(int iChannel=0;iChannel<N;++iChannel)
    {
      hmap->Fill(iChannel,ADCProcessed->at(iChannel));
      if (iChannel%NSkip==0)
      {
	hmapConnected->Fill(iChannel,ADCProcessed->at(iChannel));
      }
    }
  }

  hmap->Scale(1./EventsEventInfo);
  hmapConnected->Scale(1./EventsEventInfo);

  *strip = hmap->GetMaximumBin()-1;
  cout << "Beetle channel with the maximum ADC count: " << *strip << endl;
  int max = hmap->GetMaximum();
  cout << "Maximum ADC count: " << max << endl;
  int stripLeft = *strip-NSkip;
  int left = hmap->GetBinContent(stripLeft+1); // The +1 is necessary to take the number scheme of the histogram into account.
  int stripRight = *strip+NSkip;
  int right = hmap->GetBinContent(stripRight+1); // The +1 is necessary to take the number scheme of the histogram into account.
  cout << "Adjacent Beetle channel on the left: " << stripLeft << ", " << left << endl;
  cout << "Adjacent Beetle channel on the right: " << stripRight << ", " << right << endl;
  if (left <= right)
    *direction = "right";
  else
    *direction = "left";
  cout << "More signal on the " << *direction << endl;

  //==================================================================
  //
  // Style.
  //

  //==================================================================
  //
  // Draw histograms, stats, legends, ... and write output pdf files.
  //

  DrawHist(cmap,hmap,"",path_to_figures);
  DrawHist(cmapConnected,hmapConnected,"",path_to_figures);

  //==================================================================
  //
  //  Write output ROOT files.
  //

  output->Write();

  //==================================================================
  //
  // Close input and output files.
  //

  input->Close();
  output->Close();
  
  return;
}

#endif
