/**
 * TriggerScan
 * Author  Nagni
 * Modified by G. De Rosa
 * Version 1.2
 * Date 27 Apr 2006
 *
 * Description:
 * Describe the performance of the Trigger.
 *
 * Parameters:
 * 	TString base            - the path to the root directory for the specific Pamela unpack session 
 * 	TString outDirectory    - the path where to save the output image (Default = base) 
 *      TString format          - the format which will be used for save the produced images (Default = "ps") 
 */

#include <TString.h>
#include <TObjString.h>
#include <TTree.h>
#include <TBranch.h>
#include <TGraph.h>
#include <TStyle.h>
#include <TH2S.h>
#include <TPaveText.h>
#include <TCanvas.h>
#include <TLatex.h>
#include <TGraph.h>
#include <physics/trigger/TriggerEvent.h>
#include <EventHeader.h>
#include <PscuHeader.h>
#include <iostream>

using namespace std;

char bit(int decimal, char pos)
{
  return( (decimal>>pos)%2 );
}

void TriggerScan(TString base, TString outDirectory = "", TString format = ""){
  TLatex *t=new TLatex();  
    
  std::stringstream sst;
  if (outDirectory == "") outDirectory = base.Data();
  TString filename = ((TObjString*)base.Tokenize('/')->Last())->GetString();
  
  TFile *file  =new TFile(base.Data()) ;
  if (!file){
    printf("file not Found \n");
    return;
  }
  
  TTree *PhysicsTr = (TTree*)file->Get("Physics");
  TBranch *TrigBr = PhysicsTr->GetBranch("Trigger");
  TBranch *HeadBr = PhysicsTr->GetBranch("Header");

  pamela::EventHeader *eh = 0;
  pamela::PscuHeader  *ph = 0;
  
  pamela::trigger::TriggerEvent *triggerEvent = 0;
  
  PhysicsTr->SetBranchAddress("Trigger", &triggerEvent);
  PhysicsTr->SetBranchAddress("Header", &eh);
  
  Long64_t nevents    = TrigBr->GetEntries();
  if (nevents <= 0) {
    printf("nevents = %llu \n", nevents);
    file->Close();
    return;
  }
   
  /* Histo DEF */
  
  /*
   * PTM Plane Counters histograms
   */
  TCanvas *PMTCanvas = new TCanvas("PMTCanvas","PMTCanvas", 1280, 1024);
  PMTCanvas->Divide(1,3);
  //----------
  sst.str("");
  sst << "Histogram of PMT Plane Counters (S11 AND S12) based on " << base.Data();
  TH1S *pmt1Histo      = new TH1S("PMT1Histo", sst.str().c_str(), 20, 0., 300.);
  //----------
  sst.str("");
  sst << "Histogram of PMT Plane Counters (S21 AND S22)  based on " << base.Data();
  TH1S *pmt2Histo      = new TH1S("PMT2Histo", sst.str().c_str(), 20, 0., 300.);
  //----------
  sst.str("");
  sst << "Histogram of PMT Plane Counters (S31 AND S32) based on " << base.Data();
  TH1S *pmt3Histo      = new TH1S("PMT3Histo", sst.str().c_str(), 20, 0., 300.);
  
  /*
   * Trigger Rate Counters histograms
   */
  TCanvas *TrigRate1Canvas = new TCanvas("TrigRate1Canvas","TrigRate1Canvas", 1280, 1024);
  TrigRate1Canvas->Divide(1,3);
  
  TCanvas *TrigRate2Canvas = new TCanvas("TrigRate2Canvas","TrigRate2Canvas", 1280, 1024);
  TrigRate2Canvas->Divide(1,3);
  //----------
  sst.str("");
  sst << "Histogram of Trigger Rate Counters (S11 OR S12) AND (S21 OR S22) AND (S31 OR S32)  based on " << base.Data();
  TH1S *trRate1Histo      = new TH1S("trRate1Histo", sst.str().c_str(), 20, 0., 5.);
  //----------
  sst.str("");
  sst << "Histogram of Trigger Rate Counters (S11 AND S12) AND (S21 AND S22) AND (S31 AND S32) based on " << base.Data();
  TH1S *trRate2Histo      = new TH1S("trRate2Histo", sst.str().c_str(), 20, 0., 5.);
  //---------
  sst.str("");
  sst << "Histogram of Trigger Rate Counters  (S21 OR S22) AND (S31 OR S32)  based on " << base.Data();
  TH1S *trRate3Histo      = new TH1S("trRate3Histo", sst.str().c_str(), 20, 0., 5.);
  //---------
  sst.str("");
  sst << "Histogram of Trigger Rate Counters (S21 AND S22) AND (S31 AND S32) based on " << base.Data();
  TH1S *trRate4Histo      = new TH1S("trRate4Histo", sst.str().c_str(), 20, 0., 5.);
  //---------
  sst.str("");
  sst << "Histogram of Trigger Rate Counters S12 AND (S21 OR S22)  based on " << base.Data();
  TH1S *trRate5Histo      = new TH1S("trRate5Histo", sst.str().c_str(), 20, 0., 5.);
  //---------
  sst.str("");
  sst << "Histogram of Trigger Rate Counters (S11 OR S12) AND (S31 OR S32) based on " << base.Data();
  TH1S *trRate6Histo      = new TH1S("trRate6Histo", sst.str().c_str(), 20, 0., 5.);
  
  
  /*
   * S4 and Calorimeter Counters histograms
   */
  TCanvas *S4CalCanvas = new TCanvas("S4CalCanvas","S4CalCanvas", 1280, 1024);
  S4CalCanvas->Divide(1,2);
  //----------
  sst.str("");
  sst << "S4 Counters based on " << base.Data();
  TH1S *S4Cal1Histo      = new TH1S("S4Cal1Histo", sst.str().c_str(), 20, 0, 20);
  //----------
  sst.str("");
  sst << "Calorimeter Counters based on " << base.Data();
  TH1S *S4Cal2Histo      = new TH1S("S4Cal2Histo", sst.str().c_str(), 20, 0, 20);
  
  /*
   * Pattern Trigger Rate histograms
   */
  
  TCanvas *PatternTrig0 = new TCanvas("PatternTrig0","PatternTrig0", 1280, 1024);
  //----------
  sst.str("");
  sst << "Histogram of Calo pattern trigger " << base.Data();
  TH1S *PatternTrig0Histo      = new TH1S("PatternTrig0Histo", sst.str().c_str(), 4, 0, 4);
  
  TCanvas *PatternTrig1 = new TCanvas("PatternTrig1","PatternTrig1", 1280, 1024);
  //----------
  sst.str("");
  sst << "Histogram of S4 pattern trigger " << base.Data();
  TH1S *PatternTrig1Histo      = new TH1S("PatternTrig1Histo", sst.str().c_str(), 3, 0, 3);
  
  
  TCanvas *PatternTrig2 = new TCanvas("PatternTrig2","PatternTrig2", 1280, 1024);
  //----------
  sst.str("");
  sst << "Histogram of S3 pattern trigger " << base.Data();
  TH1S *PatternTrig2Histo      = new TH1S("PatternTrig2Histo", sst.str().c_str(), 16, 0, 16);
  
  
  TCanvas *PatternTrig3 = new TCanvas("PatternTrig3","PatternTrig3", 1280, 1024);
  //----------
  sst.str("");
  sst << "Histogram of S2 pattern trigger " << base.Data();
  TH1S *PatternTrig3Histo      = new TH1S("PatternTrig3Histo", sst.str().c_str(), 16, 0, 16);
  
  
  TCanvas *PatternTrig4 = new TCanvas("PatternTrig4","PatternTrig4", 1280, 1024);
  //----------
  sst.str("");
  sst << "Histogram of S12 pattern trigger " << base.Data();
  TH1S *PatternTrig4Histo      = new TH1S("PatternTrig4Histo", sst.str().c_str(), 16, 0, 16);
  
  
  TCanvas *PatternTrig5 = new TCanvas("PatternTrig5","PatternTrig5", 1280, 1024);
  //----------
  sst.str("");
  sst << "Histogram of S11 pattern trigger " << base.Data();
  TH1S *PatternTrig5Histo      = new TH1S("PatternTrig5Histo", sst.str().c_str(), 16, 0, 16);
  
  
 
  int i  = 0;
  Int_t size  = nevents;

  Double_t *time = new Double_t[size];
  Double_t *evCounter = new Double_t[size];
  Double_t *deadTime  = new Double_t[size];
  Double_t *liveTime  = new Double_t[size];
  
  while(i < nevents){
    HeadBr->GetEntry(i);
    TrigBr->GetEntry(i);
    
    /* time vector fill*/
    ph           = eh->GetPscuHeader();
    time[i]      = ph->GetOrbitalTime();
    	  
    /* eventCounter vector fill*/
    evCounter[i] = triggerEvent->evcount;
    
    /* live time vector fill*/
    liveTime[i] = 0.16 * triggerEvent->dltime[0];
    
    /* dead time vector fill*/
    deadTime[i] = 0.01 * triggerEvent->dltime[1];
    
    gStyle->SetStatY(0.85);
    
    /* PMT Plane Counters fill*/
    
    pmt1Histo->Fill((1/0.06)*triggerEvent->pmtpl[0]);
    pmt2Histo->Fill((1/0.06)*triggerEvent->pmtpl[1]);
    pmt3Histo->Fill((1/0.06)*triggerEvent->pmtpl[2]);
    
    /* Trigger Rate Counters fill*/
    trRate1Histo->Fill((1/4.)*triggerEvent->trigrate[0]);
    trRate2Histo->Fill((1/4.)*triggerEvent->trigrate[1]);
    trRate3Histo->Fill((1/4.)*triggerEvent->trigrate[2]);
    trRate4Histo->Fill((1/4.)*triggerEvent->trigrate[3]);
    trRate5Histo->Fill((1/4.)*triggerEvent->trigrate[4]);
    trRate6Histo->Fill((1/4.)*triggerEvent->trigrate[5]);
    
    /* S4 and Calorimeter Counters fill*/
    S4Cal1Histo->Fill(triggerEvent->s4calcount[0]);
    S4Cal2Histo->Fill(triggerEvent->s4calcount[1]);
    
    int trte0=(int)triggerEvent->patterntrig[0];
    
    PatternTrig0Histo->AddBinContent(1,bit(trte0,0));
    PatternTrig0Histo->AddBinContent(2,bit(trte0,1));
    PatternTrig0Histo->AddBinContent(3,bit(trte0,2));
    PatternTrig0Histo->AddBinContent(4,bit(trte0,3));	  	  
    
    int trte1=(int)triggerEvent->patterntrig[1];
    
    PatternTrig1Histo->AddBinContent(1,bit(trte1,0));
    PatternTrig1Histo->AddBinContent(2,bit(trte1,1));
    PatternTrig1Histo->AddBinContent(3,bit(trte1,2));
    
    
    int trte2=(int)triggerEvent->patterntrig[2];	  
    
    PatternTrig2Histo->AddBinContent(1,bit(trte2,0));
    PatternTrig2Histo->AddBinContent(2,bit(trte2,1));
    PatternTrig2Histo->AddBinContent(3,bit(trte2,2));
    PatternTrig2Histo->AddBinContent(4,bit(trte2,3));
    PatternTrig2Histo->AddBinContent(5,bit(trte2,4));
    PatternTrig2Histo->AddBinContent(6,bit(trte2,5));
    PatternTrig2Histo->AddBinContent(7,bit(trte2,6));
    PatternTrig2Histo->AddBinContent(8,bit(trte2,7));
    PatternTrig2Histo->AddBinContent(9,bit(trte2,8));
    PatternTrig2Histo->AddBinContent(10,bit(trte2,9));
    PatternTrig2Histo->AddBinContent(11,bit(trte2,10));
    PatternTrig2Histo->AddBinContent(12,bit(trte2,11));
    PatternTrig2Histo->AddBinContent(13,bit(trte2,12));
    PatternTrig2Histo->AddBinContent(14,bit(trte2,13));
    PatternTrig2Histo->AddBinContent(15,bit(trte2,14));
    PatternTrig2Histo->AddBinContent(16,bit(trte2,15));
    
    int trte3=(int)triggerEvent->patterntrig[3];
    
    
    PatternTrig3Histo->AddBinContent(1,bit(trte3,0));
    PatternTrig3Histo->AddBinContent(2,bit(trte3,1));
    PatternTrig3Histo->AddBinContent(3,bit(trte3,2));
    PatternTrig3Histo->AddBinContent(4,bit(trte3,3));
    PatternTrig3Histo->AddBinContent(5,bit(trte3,4));
    PatternTrig3Histo->AddBinContent(6,bit(trte3,5));
    PatternTrig3Histo->AddBinContent(7,bit(trte3,6));
    PatternTrig3Histo->AddBinContent(8,bit(trte3,7));
    PatternTrig3Histo->AddBinContent(9,bit(trte3,8));
    PatternTrig3Histo->AddBinContent(10,bit(trte3,9));
    PatternTrig3Histo->AddBinContent(11,bit(trte3,10));
    PatternTrig3Histo->AddBinContent(12,bit(trte3,11));
    PatternTrig3Histo->AddBinContent(13,bit(trte3,12));
    PatternTrig3Histo->AddBinContent(14,bit(trte3,13));
    PatternTrig3Histo->AddBinContent(15,bit(trte3,14));
    PatternTrig3Histo->AddBinContent(16,bit(trte3,15));
    
    int trte4=(int)triggerEvent->patterntrig[4];
    
    
    PatternTrig4Histo->AddBinContent(1,bit(trte4,0));
    PatternTrig4Histo->AddBinContent(2,bit(trte4,1));
    PatternTrig4Histo->AddBinContent(3,bit(trte4,2));
    PatternTrig4Histo->AddBinContent(4,bit(trte4,3));
    PatternTrig4Histo->AddBinContent(5,bit(trte4,4));
    PatternTrig4Histo->AddBinContent(6,bit(trte4,5));
    PatternTrig4Histo->AddBinContent(7,bit(trte4,6));
    PatternTrig4Histo->AddBinContent(8,bit(trte4,7));
    PatternTrig4Histo->AddBinContent(9,bit(trte4,8));
    PatternTrig4Histo->AddBinContent(10,bit(trte4,9));
    PatternTrig4Histo->AddBinContent(11,bit(trte4,10));
    PatternTrig4Histo->AddBinContent(12,bit(trte4,11));
    PatternTrig4Histo->AddBinContent(13,bit(trte4,12));
    PatternTrig4Histo->AddBinContent(14,bit(trte4,13));
    PatternTrig4Histo->AddBinContent(15,bit(trte4,14));
    PatternTrig4Histo->AddBinContent(16,bit(trte4,15));
	  
    int trte5=(int)triggerEvent->patterntrig[5];
	  
	  
    PatternTrig5Histo->AddBinContent(1,bit(trte5,0));
    PatternTrig5Histo->AddBinContent(2,bit(trte5,1));
    PatternTrig5Histo->AddBinContent(3,bit(trte5,2));
    PatternTrig5Histo->AddBinContent(4,bit(trte5,3));
    PatternTrig5Histo->AddBinContent(5,bit(trte5,4));
    PatternTrig5Histo->AddBinContent(6,bit(trte5,5));
    PatternTrig5Histo->AddBinContent(7,bit(trte5,6));
    PatternTrig5Histo->AddBinContent(8,bit(trte5,7));
    PatternTrig5Histo->AddBinContent(9,bit(trte5,8));
    PatternTrig5Histo->AddBinContent(10,bit(trte5,9));
    PatternTrig5Histo->AddBinContent(11,bit(trte5,10));
    PatternTrig5Histo->AddBinContent(12,bit(trte5,11));
    PatternTrig5Histo->AddBinContent(13,bit(trte5,12));
    PatternTrig5Histo->AddBinContent(14,bit(trte5,13));
    PatternTrig5Histo->AddBinContent(15,bit(trte5,14));
    PatternTrig5Histo->AddBinContent(16,bit(trte5,15));
	  
    i++;
  }
	
	
  gStyle->SetOptStat(111111);
  gStyle->SetStatH(0.15);
  gStyle->SetStatW(0.15);


  /*
   * Event Counter graph
   */
  TCanvas *EvCounterCanvas = new TCanvas("EvCounterCanvas","EvCounterCanvas", 1280, 1024);
  //----------
  sst.str("");	
  sst << "Event Counter";
  TGraph *evCounterGraph   = new TGraph(nevents, time, evCounter);
  evCounterGraph->SetTitle(sst.str().c_str());
  evCounterGraph->Draw("APL");

  /*
   * Dead and Live time graphs
   */
  TCanvas *DeadLiveCanvas = new TCanvas("DeadLiveCanvas","DeadLiveCanvas", 1280, 1024);
  DeadLiveCanvas->Divide(1,2);
  //----------
  sst.str("");
  sst << "Graph of Dead Time vs OBT based on " << base.Data();
  TGraph *DeadTimeGraph   = new TGraph(nevents, time, deadTime);
  DeadLiveCanvas->cd(1);
  sst.str("");
  sst << "Dead Time";
  DeadTimeGraph->SetTitle(sst.str().c_str());
  DeadTimeGraph->SetFillColor(2);
  DeadTimeGraph->GetXaxis()->SetTitle("OnBoardTime");
  DeadTimeGraph->GetXaxis()->CenterTitle();
  DeadTimeGraph->GetYaxis()->SetTitle("DeadTime(ms)");
  DeadTimeGraph->GetYaxis()->CenterTitle();
  DeadTimeGraph->SetMaximum(100);
  DeadTimeGraph->Draw("APL");
  //----------
  sst.str("");
  sst << "Graph of Live Time vs OBT based on " << base.Data();
  TGraph *LiveTimeGraph   = new TGraph(nevents, (const Double_t*)time, (const Double_t*)liveTime);
  DeadLiveCanvas->cd(2);
  sst.str("");
  sst << "Live Time";
  LiveTimeGraph->SetTitle(sst.str().c_str());
  LiveTimeGraph->SetFillColor(2);
  LiveTimeGraph->GetXaxis()->SetTitle("OnBoardTime");
  LiveTimeGraph->GetXaxis()->CenterTitle();
  LiveTimeGraph->GetYaxis()->SetTitle("LiveTime(ms)");
  LiveTimeGraph->GetYaxis()->CenterTitle();
  LiveTimeGraph->Draw("APL");
	
  /* PMT Plane Counters draw*/
	
  PMTCanvas->cd(1);
  pmt1Histo->GetXaxis()->SetTitle("Hz");
  pmt1Histo->Draw();
  PMTCanvas->cd(2);
  pmt2Histo->GetXaxis()->SetTitle("Hz");
  pmt2Histo->Draw();
  PMTCanvas->cd(3);
  pmt3Histo->GetXaxis()->SetTitle("Hz");
  pmt3Histo->Draw();
	
  /* Trigger Rate Counters draw*/
  TrigRate1Canvas->cd(1);
  trRate1Histo->GetXaxis()->SetTitle("Hz");
  trRate1Histo->Draw();
  TrigRate1Canvas->cd(2);
  trRate2Histo->GetXaxis()->SetTitle("Hz");
  trRate2Histo->Draw();
  TrigRate1Canvas->cd(3);
  trRate3Histo->GetXaxis()->SetTitle("Hz");
  trRate3Histo->Draw();
  TrigRate2Canvas->cd(1);
  trRate4Histo->GetXaxis()->SetTitle("Hz");
  trRate4Histo->Draw();
  TrigRate2Canvas->cd(2);
  trRate5Histo->GetXaxis()->SetTitle("Hz");
  trRate5Histo->Draw();
  TrigRate2Canvas->cd(3);
  trRate6Histo->GetXaxis()->SetTitle("Hz");
  trRate6Histo->Draw();
	    
  /* S4 and Calorimeter Counters draw*/
  S4CalCanvas->cd(1);
  S4Cal1Histo->GetXaxis()->SetTitle("Hz");
  S4Cal1Histo->Draw();
  S4CalCanvas->cd(2);
  S4Cal2Histo->GetXaxis()->SetTitle("Hz");
  S4Cal2Histo->Draw();

  /* Pattern Trigger draw*/

  gStyle->SetOptStat(1101);

  PatternTrig5->cd();
  PatternTrig5Histo->Draw("");

  PatternTrig4->cd();
  PatternTrig4Histo->Draw("");
		    
  PatternTrig3->cd();
  PatternTrig3Histo->Draw("");
	
  PatternTrig2->cd();
  PatternTrig2Histo->Draw("");
	    
  PatternTrig0->cd();
  PatternTrig0Histo->Draw("");
	
  PatternTrig1->cd();
  PatternTrig1Histo->Draw("");
	    
  gStyle->SetOptStat(1111);
  gStyle->SetStatH(0.3);
  gStyle->SetStatW(0.3);

  TCanvas *RatePMT1 = new TCanvas("RatePMT1", "Rate PMT S11A", 1);
  RatePMT1->Divide (3,3);
  //----------
  RatePMT1->cd(1);
  PhysicsTr->Draw("pmtcount1[0]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S111A");
  //----------
  RatePMT1->cd (2);
  PhysicsTr->Draw("pmtcount1[1]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S112A");
  //----------
  RatePMT1->cd (3);
  PhysicsTr->Draw("pmtcount1[2]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S113A");	
  //----------
  RatePMT1->cd(4);
  PhysicsTr->Draw("pmtcount1[3]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S114A");
  //----------
  RatePMT1->cd(5);	
  PhysicsTr->Draw("pmtcount1[4]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S115A");
  //----------
  RatePMT1->cd(6);
  PhysicsTr->Draw("pmtcount1[5]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S116A");
  //----------
  RatePMT1->cd(7);
  PhysicsTr->Draw("pmtcount1[6]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S117A");
  //----------
  RatePMT1->cd(8);
  PhysicsTr->Draw("pmtcount1[7]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S118A");
	    
  TCanvas *RatePMT2 = new TCanvas("RatePMT2", "Rate PMT S11B", 1);
  RatePMT2->Divide (3,3);
  //----------
  RatePMT2->cd(1);
  PhysicsTr-> Draw("pmtcount1[8]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S111B");
  //----------
  RatePMT2->cd(2);
  PhysicsTr->Draw("pmtcount1[9]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S112B");
  //----------
  RatePMT2->cd(3);
  PhysicsTr->Draw("pmtcount1[10]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S113B");
  //----------
  RatePMT2->cd(4);
  PhysicsTr->Draw("pmtcount1[11]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S114B");
  //----------
  RatePMT2->cd(5);
  PhysicsTr-> Draw("pmtcount1[12]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S115B");
  //----------
  RatePMT2->cd(6);
  PhysicsTr-> Draw("pmtcount1[13]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S116B");
  //----------
  RatePMT2->cd(7);
  PhysicsTr-> Draw("pmtcount1[14]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S117B");
  //----------
  RatePMT2->cd(8);
  PhysicsTr->Draw("pmtcount1[15]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S118B");
	    
  TCanvas *RatePMT3 = new TCanvas("RatePMT3", "Rate PMT S12", 1);
  RatePMT3->Divide (3,4);
  //----------
  RatePMT3->cd(1);
  PhysicsTr-> Draw("pmtcount1[16]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S121A");
  //----------
  RatePMT3->cd(2);
  PhysicsTr-> Draw("pmtcount1[17]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S122A");
  //----------
  RatePMT3->cd (3);	
  PhysicsTr-> Draw("pmtcount1[18]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S123A");
  //----------
  RatePMT3->cd(4);
  PhysicsTr->Draw("pmtcount1[19]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S124A");
  //----------
  RatePMT3->cd(5);
  PhysicsTr-> Draw("pmtcount1[20]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S125A");
  //----------
  RatePMT3->cd(6);
  PhysicsTr-> Draw("pmtcount1[21]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S126A");
  //----------
  RatePMT3->cd(7);
  PhysicsTr->Draw("pmtcount1[22]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S121B");
  //----------
  RatePMT3->cd(8);
  PhysicsTr-> Draw("pmtcount1[23]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S122B");
  //----------
  RatePMT3->cd (9);
  PhysicsTr->Draw("pmtcount2[0]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S123B");
  //----------
  RatePMT3->cd (10);
  PhysicsTr->Draw("pmtcount2[1]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S124B");
  //----------
  RatePMT3->cd (11);
  PhysicsTr->Draw("pmtcount2[2]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S125B");
  //----------
  RatePMT3->cd (12);
  PhysicsTr->Draw("pmtcount2[3]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S126B");
	    
  TCanvas *RatePMT4 = new TCanvas("RatePMT4", "Rate PMT S2", 1);
  RatePMT4->Divide (3,3);
  //----------
  RatePMT4->cd (1);
  PhysicsTr-> Draw("pmtcount2[4]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S211A");
  //----------
  RatePMT4->cd (2);
  PhysicsTr-> Draw("pmtcount2[5]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S212A");
  //----------
  RatePMT4->cd (3);
  PhysicsTr-> Draw("pmtcount2[6]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S211B");
  //----------
  RatePMT4->cd (4);
  PhysicsTr-> Draw("pmtcount2[7]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S212B");
  //----------
  RatePMT4->cd (5);
  PhysicsTr-> Draw("pmtcount2[8]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S221A");
  //----------
  RatePMT4->cd (6);
  PhysicsTr-> Draw("pmtcount2[9]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S222A");
  //----------
  RatePMT4->cd (7);	
  PhysicsTr-> Draw("pmtcount2[10]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S221B");
  //----------
  RatePMT4->cd (8);
  PhysicsTr->Draw("pmtcount2[11]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S222B");
	
  TCanvas *RatePMT5 = new TCanvas("RatePMT5", "Rate PMT S3", 1);
  RatePMT5->Divide (3,4);
  //----------
  RatePMT5->cd (1);
  PhysicsTr-> Draw("pmtcount2[12]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S311A");
  //----------
  RatePMT5->cd (2);
  PhysicsTr-> Draw("pmtcount2[13]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S312A");
  //----------
  RatePMT5->cd (3);
  PhysicsTr-> Draw("pmtcount2[14]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S313A");
  //----------
  RatePMT5->cd (4);
  PhysicsTr-> Draw("pmtcount2[15]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S311B");
  //----------
  RatePMT5->cd (5);
  PhysicsTr-> Draw("pmtcount2[16]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S312B");
  //----------
  RatePMT5->cd (6);
  PhysicsTr-> Draw("pmtcount2[17]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S313B");
  //----------
  RatePMT5->cd (7);
  PhysicsTr-> Draw("pmtcount2[18]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S321A");
  //----------
  RatePMT5->cd (8);
  PhysicsTr-> Draw("pmtcount2[19]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S322A");
  //----------
  RatePMT5->cd (9);
  PhysicsTr-> Draw("pmtcount2[20]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S323A");
  //----------
  RatePMT5->cd (10);
  PhysicsTr-> Draw("pmtcount2[21]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S321B");
  //----------
  RatePMT5->cd (11);
  PhysicsTr-> Draw("pmtcount2[22]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S322B");
  //----------
  RatePMT5->cd (12);
  PhysicsTr-> Draw("pmtcount2[23]");
  t=new TLatex();
  t->SetTextFont(32);
  t->SetTextColor(1);
  t->SetTextAlign(12);
  t->SetTextSize(0.12);
  t->DrawLatex(5.,20.,"S323B");

  // print the output
  const char *saveas = format;
  if ( !strcmp(saveas,"ps") ) {
    // ------print the ps
    
    gStyle->SetOptStat(111111);

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan." << saveas  << "(";
    EvCounterCanvas->Print(sst.str().c_str());
    DeadLiveCanvas->Print(sst.str().c_str());
    PMTCanvas->Print(sst.str().c_str());
    TrigRate1Canvas->Print(sst.str().c_str());
    TrigRate2Canvas->Print(sst.str().c_str());
    S4CalCanvas->Print(sst.str().c_str());

    gStyle->SetOptStat(1101);

    PatternTrig5->Print(sst.str().c_str());
    PatternTrig4->Print(sst.str().c_str());
    PatternTrig3->Print(sst.str().c_str());    
    PatternTrig2->Print(sst.str().c_str());
    PatternTrig0->Print(sst.str().c_str());
    PatternTrig1->Print(sst.str().c_str());
    RatePMT1->Print(sst.str().c_str());
    RatePMT2->Print(sst.str().c_str());
    RatePMT3->Print(sst.str().c_str());
    RatePMT4->Print(sst.str().c_str());
    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan." << saveas << ")";
    RatePMT5->Print(sst.str().c_str());

  } else {

    gStyle->SetOptStat(111111);

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan1." << saveas;
    EvCounterCanvas->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan2." << saveas;
    DeadLiveCanvas->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan3." << saveas;
    PMTCanvas->SaveAs(sst.str().c_str());
    
    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan4." << saveas;
    TrigRate1Canvas->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan5." << saveas;
    TrigRate2Canvas->SaveAs(sst.str().c_str());
    
    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan6." << saveas;
    S4CalCanvas->SaveAs(sst.str().c_str());

    gStyle->SetOptStat(1101);

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan7." << saveas;
    PatternTrig5->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan8." << saveas;
    PatternTrig4->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan9." << saveas;
    PatternTrig3->SaveAs(sst.str().c_str());  

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan10." << saveas;
    PatternTrig2->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan11." << saveas;
    PatternTrig0->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan12." << saveas;
    PatternTrig1->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan13." << saveas;
    RatePMT1->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan14." << saveas;
    RatePMT2->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan15." << saveas;
    RatePMT3->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan16." << saveas;
    RatePMT4->SaveAs(sst.str().c_str());

    sst.str("");
    sst << outDirectory.Data() << filename.Data() << "TriggerScan17." << saveas;
    RatePMT5->SaveAs(sst.str().c_str());
    
  }

}


int main(int argc, char* argv[]){
  TString path;
  TString outDir        ="./";
  TString format        ="ps";
  
  if (argc < 2){
    printf("You have to insert at least the file to analyze \n");
    printf("Try '--help' for more information. \n");
    exit(1);
  }  
  
  if (!strcmp(argv[1], "--help")){
    printf( "Usage: TriggerScan FILE [OPTION] \n");
    printf( "\t --help                  Print this help and exit \n");	
    printf( "\t -outDir[path]           Path where to put the output [default ./] \n");
    printf( "\t -format[ps]             Format for output files [default 'ps'] \n");
    exit(1);
  }
  
  
  path=argv[1];
  
  for (int i = 2; i < argc; i++){
    
    if (!strcmp(argv[i], "-outDir")){
      if (++i >= argc){
	printf( "-outDir needs arguments. \n");
	printf( "Try '--help' for more information. \n");
	exit(1);
      } 
      else{
	outDir = argv[i];
	continue;
      }
    }
    
    
    
    if (!strcmp(argv[i], "-format")){
      if (++i >= argc){
	printf( "-format needs arguments. \n");
	printf( "Try '--help' for more information. \n");
	exit(1);
      } 
      else{
	format = argv[i];
	continue;
      }
    }
  }

  TriggerScan(argv[1], outDir, format);

}
