#include "TMHistogrammingGlobalPixelXYZ.h"
#include "TMControlSPD.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH3F.h>
#include <TPolyLine3D.h>

//_____________________________________________________________________
TMHistogrammingGlobalPixelXYZ::TMHistogrammingGlobalPixelXYZ(TPolyLine3D *tla[240]) :
  TMHistogrammingShowDet()
{

  for (UInt_t mod=0; mod<240; mod++) {
    fDetLines[mod] = tla[mod];
  }

  fTheHisto = new TH3F("fGlobalXYZHisto","Global Pixel XYZ",100,-15,15,100,-9,9,100,-9,9);
  //  fTheHisto->SetMarkerStyle(8);
  //  fTheHisto->SetMarkerSize(0.4);
  fTheHisto->SetMarkerColor(kBlue);
  fTheHisto->GetXaxis()->SetTitle("Z [cm]");
  fTheHisto->GetYaxis()->SetTitle("X [cm]");
  fTheHisto->GetZaxis()->SetTitle("Y [cm]");
  fTheHisto->SetStats(0);

}
//_____________________________________________________________________
TMHistogrammingGlobalPixelXYZ::~TMHistogrammingGlobalPixelXYZ() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelXYZ::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();
    fTheHisto->Fill(z,x,y);
  }
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelXYZ::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  fTheHisto->Draw();

}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelXYZ::Reset() {
  fTheHisto->Reset();
  TMHistogramming::Reset();
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelXYZ::DrawDetector(TPad *pad) {
  pad->cd();
  for (UInt_t mod=0; mod<240; mod++) {
    fDetLines[mod]->Draw();
  }
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelXYZ::HideDetector(TPad *pad) {
  pad->Clear();
  Draw(pad);
}

