#include "TMHistogrammingClustersPerHSC.h"
#include "TMControlSPD.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH2F.h>
#include <TLine.h>

//_____________________________________________________________________
TMHistogrammingClustersPerHSC::TMHistogrammingClustersPerHSC(TLine *tla[240]) :
  TMHistogrammingShowDet()
{ 

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

  for (UInt_t eq=0; eq<10; eq++) {
    for (UInt_t hs=0; hs<6; hs++) {
      fNumberClus[eq][hs]=0;
    }
  }
  fHist = new TH2F("ClustersPerHSC","Clusters Per HS Side C",40,-9,9,40,-9,9);
  fHist->SetXTitle("X [cm]");
  fHist->SetYTitle("Y [cm]");
  fHist->SetStats(0);

}
//_____________________________________________________________________
TMHistogrammingClustersPerHSC::~TMHistogrammingClustersPerHSC() {
  delete fHist;
}
//_____________________________________________________________________
void TMHistogrammingClustersPerHSC::ProcessCluster(Float_t x, Float_t y, Float_t z, UInt_t module, UInt_t size, UInt_t widz, UInt_t widphi, UInt_t type) {
  if (fEventAccept) {
    UInt_t eq = AliITSRawStreamSPD::GetOnlineEqIdFromOffline(module);
    if (eq>=10) {
      UInt_t hs = AliITSRawStreamSPD::GetOnlineHSFromOffline(module);
      fNumberClus[eq-10][hs]++;
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingClustersPerHSC::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    for (UInt_t eq=10; eq<20; eq++) {
      for (UInt_t hs=0; hs<6; hs++) {
      
	UInt_t mod, colM, rowM;
	AliITSRawStreamSPD::OnlineToOffline(eq,hs,0,0,128,mod,colM,rowM);
	TMControlSPD::Instance()->TransformClusterToGlobal(mod,colM,rowM);  
	Float_t x = TMControlSPD::Instance()->GetGlobalX();
	Float_t y = TMControlSPD::Instance()->GetGlobalY();
	fHist->Fill(x,y,fNumberClus[eq-10][hs]);
	fNumberClus[eq-10][hs]=0;

      }
    }

    for (UInt_t eq=0; eq<10; eq++) {
      for (UInt_t hs=0; hs<6; hs++) {
	UInt_t mod1 = AliITSRawStreamSPD::GetOfflineModuleFromOnline(eq,hs,0);
	UInt_t mod2 = AliITSRawStreamSPD::GetOfflineModuleFromOnline(eq,hs,5);
	UInt_t mod3 = AliITSRawStreamSPD::GetOfflineModuleFromOnline(eq+10,hs,0);
	UInt_t mod4 = AliITSRawStreamSPD::GetOfflineModuleFromOnline(eq+10,hs,5);
	if (str->IsActiveHS(eq+10,hs)) {
	  fDetLines[mod1]->SetLineColor(kBlack);
	  fDetLines[mod2]->SetLineColor(kBlack);
	  fDetLines[mod3]->SetLineColor(kBlack);
	  fDetLines[mod4]->SetLineColor(kBlack);
	}
	else {
	  fDetLines[mod1]->SetLineColor(gStyle->GetCanvasColor());
	  fDetLines[mod2]->SetLineColor(gStyle->GetCanvasColor());
	  fDetLines[mod3]->SetLineColor(gStyle->GetCanvasColor());
	  fDetLines[mod4]->SetLineColor(gStyle->GetCanvasColor());
	}
      }
    }
  }

  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingClustersPerHSC::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  fHist->Draw("colz");
}
//_____________________________________________________________________
void TMHistogrammingClustersPerHSC::Reset() {
  fHist->Reset();

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

