//  
//  trackMonitor.C
//  macro to plot track information (p, pt, chi2, ghost prob; also as a function of the distance from the
//  beam pipe)
//
//  Created by Christian Elsasser on 31.05.13.
//  University of Zurich, elsasser@cern.ch
//  Copyright only for commercial use
//

//  General include
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <stdlib.h>

#include <time.h>


//  ROOT include
#include "incBasicROOT.h"
#include "incDrawROOT.h"
#include "incHistROOT.h"
#include "incIOROOT.h"


#include "basicROOTTools.h"
#include "basicRooFitTools.h"


#include "lhcbstyle.C"








int trackMonitor(int argc, char* argv[]);


int main(int argc, char* argv[]){
  TStyle* style = lhcbStyle();
  int argc_copy = argc;
  
  TApplication theApp("Analysis", &argc, argv);
  argc = theApp.Argc();
  argv = theApp.Argv();

  style->SetPadLeftMargin(0.12);
  
  
  int exit = trackMonitor(argc,argv);
  
  Printf("End");
  theApp.Run(kTRUE); 
  
  return exit;
}


//  Executable method
int trackMonitor(int argc, char* argv[]){
  
  
  gStyle->SetPadLeftMargin(0.15);
  gStyle->SetTitleOffset(1.2,"Y");
  gStyle->SetTitleOffset(1.0,"X");

  const char* diskVar = std::getenv ("DISK");
  
  double uT2010 = 1262304000.0;
  
  TString dn_track(Form("%s/data/ST/Aging_Tuples",diskVar));
  TString fn_track;
  TString tn_track;
  
  if (strcmp(argv[1],"TT")==0) {
    fn_track = TString(Form("%s/TT_gen.root ",dn_track.Data()));
    tn_track = TString("STADCTrackMonitor/HitInfo/TTaU");
  }else{
    fn_track = TString(Form("%s/IT_gen.root ",dn_track.Data()));
    tn_track = TString("STADCTrackMonitor/HitInfo/T3X2");
  }
  
  
  
  
  TFile* f_track = TFile::Open(fn_track.Data());
  if (!f_track) {
    Error("trackMonitor","File %s not found!",fn_track.Data());
    return 1;
  }
  
  TTree* t_track = (TTree*)f_track->Get(tn_track.Data());
  if (!t_track) {
    Error("trackMonitor","Tree %s not found!",tn_track.Data());
    return 1;
  }
  
  
  TH2F* h_pr    = new TH2F("h_pr","Histo momentum - radius",
                           100,0.0,200.0,
                           100,0.0,100.0);
  h_pr->Sumw2();
  h_pr->SetXTitle("#it{p} [GeV/#it{c}]");
  h_pr->SetYTitle("Radius [cm]");
  h_pr->SetLineColor(kRed);
  h_pr->SetMarkerColor(kRed);
  h_pr->SetFillColor(kRed-7);
  h_pr->SetMarkerStyle(0);
  
  TH1F* h_pt    = new TH1F("h_pt","Histo pT",
                           100,0.0,40.0);
  h_pt->Sumw2();
  h_pt->SetXTitle("#it{p}_{T} [GeV/#it{c}]");
  h_pt->SetYTitle(Form("Fraction / (%4.2f GeV/#it{c})",h_pt->GetBinWidth(1)));
  h_pt->SetLineColor(kRed);
  h_pt->SetMarkerColor(kRed);
  h_pt->SetFillColor(kRed-7);
  h_pt->SetMarkerStyle(0);
  
  TH1F* h_trchi = new TH1F("h_trchi","Histo track chi2",
                           50,0.0,5.0);
  h_trchi->Sumw2();
  h_trchi->SetXTitle("#chi^{2}_{track}/ndf");
  h_trchi->SetYTitle(Form("Fraction / (%4.2f)",h_trchi->GetBinWidth(1)));
  h_trchi->SetLineColor(kRed);
  h_trchi->SetMarkerColor(kRed);
  h_trchi->SetFillColor(kRed-7);
  h_trchi->SetMarkerStyle(0);
  
  TH1F* h_ghPro = new TH1F("h_ghPro","Histo ghost Prob",
                           50,0.0,1.0);
  h_ghPro->Sumw2();
  h_ghPro->SetXTitle("Ghost prob.");
  h_ghPro->SetYTitle(Form("Fraction / (%4.2f)",h_ghPro->GetBinWidth(1)));
  h_ghPro->SetLineColor(kRed);
  h_ghPro->SetMarkerColor(kRed);
  h_ghPro->SetFillColor(kRed-7);
  h_ghPro->SetMarkerStyle(0);
  
  
  TH2F* h_hit   = new TH2F("h_hit","Histo Hit map",
                           120,-60.0,60.0,
                           120,-60.0,60.0);
  h_hit->Sumw2();
  h_hit->SetXTitle("#it{x} [cm]");
  h_hit->SetYTitle("#it{y} [cm]");
  
  t_track->Draw("TMath::Sqrt(xHit*xHit+yHit*yHit)/10.0:p/1000>>h_pr","","goff");
  t_track->Draw("pt/1000>>h_pt","","goff");
  t_track->Draw("TrChi2/TrNDoF>>h_trchi","","goff");
  t_track->Draw("GhostP>>h_ghPro","","goff");
  t_track->Draw("yHit/10.0:xHit/10.0>>h_hit","","goff");
  
  
  TH1D* h_p = h_pr->ProjectionX("h_p");
  h_p->SetYTitle(Form("Fraction / (%4.2f GeV/#it{c})",h_p->GetBinWidth(1)));
  TProfile* h_r = h_pr->ProfileY("h_r");
  h_r->SetYTitle("<#it{p}> (GeV/#it{c})");
  
  h_p->Scale(1.0/h_p->Integral());
  h_pt->Scale(1.0/h_pt->Integral());
  h_trchi->Scale(1.0/h_trchi->Integral());
  h_ghPro->Scale(1.0/h_ghPro->Integral());

  TCanvas* c_p = new TCanvas("c_p","Canvas p",800,600);
  c_p->SetLogy();
  h_p->Draw();
  h_p->Draw("Same E2");
  h_p->Draw("Same ][");
  
  TCanvas* c_r = new TCanvas("c_r","Canvas r",800,600);
  h_r->Draw();
  h_r->Draw("Same E2");
  h_r->Draw("Same ][");

  TCanvas* c_pt = new TCanvas("c_pt","Canvas pT",800,600);
  c_pt->SetLogy();
  h_pt->Draw();
  h_pt->Draw("Same E2");
  h_pt->Draw("Same ][");
  
  TCanvas* c_trchi = new TCanvas("c_trchi","Canvas track chi2",800,600);
  h_trchi->Draw();
  h_trchi->Draw("Same E2");
  h_trchi->Draw("Same ][");
  
  TCanvas* c_ghPro = new TCanvas("c_ghPro","Canvas ghostProb",800,600);
  h_ghPro->Draw();
  h_ghPro->Draw("Same E2");
  h_ghPro->Draw("Same ][");
  
  gStyle->SetPadLeftMargin(0.20);
  gStyle->SetPadRightMargin(0.25);
  gStyle->SetPadBottomMargin(0.15);
  gStyle->SetPadTopMargin(0.05);
  
  h_hit->Scale(1.0/h_hit->Integral());
  h_hit->SetMinimum(1e-8);
  h_hit->SetMaximum(1e-2);
  
  h_hit->SetZTitle("Hit fraction [cm^{-2}]");
  
  TCanvas* c_hit = new TCanvas("c_hit","Hit map",1200,800);
  c_hit->SetLogz();
  h_hit->Draw("colz");
  
  
  
  return 0;
}


