#include "TMHistogrammingGlobalPixelPhiDist.h"
#include "TMControlSPD.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH1F.h>
#include <TMath.h>

//_____________________________________________________________________
TMHistogrammingGlobalPixelPhiDist::TMHistogrammingGlobalPixelPhiDist() :
  TMHistogramming()
{ 

  fTotal = new TH1F("GlobalPixelPhiDistTotal","Global Phi Distribution (black=all, red=outer, blue=inner layer)",200,-3.15,3.15);
  fTotal->SetLineColor(kBlack);
  fTotal->GetXaxis()->SetTitle("Phi [rad]");
  fTotal->SetStats(0);

  fInner = new TH1F("GlobalPixelPhiDistInner","Global Phi Distribution (black=all, red=outer, blue=inner layer)",200,-3.15,3.15);
  fInner->SetLineColor(kBlue);
  fInner->GetXaxis()->SetTitle("Phi [rad]");
  fInner->SetStats(0);

  fOuter = new TH1F("GlobalPixelPhiDistOuter","Global Phi Distribution (black=all, red=outer, blue=inner layer)",200,-3.15,3.15);
  fOuter->SetLineColor(kRed);
  fOuter->GetXaxis()->SetTitle("Phi [rad]");
  fOuter->SetStats(0);
}
//_____________________________________________________________________
TMHistogrammingGlobalPixelPhiDist::~TMHistogrammingGlobalPixelPhiDist() {
  delete fTotal;
  delete fInner;
  delete fOuter;
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelPhiDist::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    UInt_t mod, colM, rowM;
    AliITSRawStreamSPD::OnlineToOffline(eq,hs,chip,col,row,mod,colM,rowM);
    TMControlSPD::Instance()->TransformClusterToGlobal(mod,colM,rowM);  
    Float_t x = TMControlSPD::Instance()->GetGlobalX();
    Float_t y = TMControlSPD::Instance()->GetGlobalY();
    //  Float_t z = TMControlSPD::Instance()->GetGlobalZ();
    
    //  Float_t r = sqrt(x*x + y*y + z*z);
    Float_t phi = atan2(y,x);
    fTotal->Fill(phi);
    if (hs<2) fInner->Fill(phi);
    else      fOuter->Fill(phi);
  }
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelPhiDist::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  fTotal->Draw();
  fInner->Draw("same");
  fOuter->Draw("same");
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelPhiDist::Reset() {
  fTotal->Reset();
  fInner->Reset();
  fOuter->Reset();
  TMHistogramming::Reset();
}


