#include "TMHistogrammingClusterInnerVsOuter.h"
#include "HenrikClusterFinderSPD.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH2F.h>

//_____________________________________________________________________
TMHistogrammingClusterInnerVsOuter::TMHistogrammingClusterInnerVsOuter() :
  TMHistogramming(), fRangeX(50), fRangeY(50)
{
  CreateHisto();
}
//_____________________________________________________________________
TMHistogrammingClusterInnerVsOuter::~TMHistogrammingClusterInnerVsOuter() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingClusterInnerVsOuter::CreateHisto() {
  fTheHisto = new TH2F("ClusterInnerVsOuter", "Number of Clusters Per Event (Inner Layer vs Outer Layer)", 50, -0.5, 49.5, 50, -0.5, 49.5);
  fTheHisto->SetMinimum(0);
  fTheHisto->SetStats(0);
  fTheHisto->GetXaxis()->SetTitle("N Clusters Inner Layer");
  fTheHisto->GetYaxis()->SetTitle("N Clusters Outer Layer");
  fTheHisto->SetTitleOffset(1.2,"X");
  fTheHisto->SetTitleOffset(1.6,"Y");
  fTheHisto->SetBit(TH1::kCanRebin);
}
//_____________________________________________________________________
void TMHistogrammingClusterInnerVsOuter::ProcessClusters(HenrikClusterFinderSPD* clFinder) {
  if (fEventAccept) {
    UInt_t clusterCountInner = 0;
    UInt_t clusterCountOuter = 0;
    for (UInt_t mod=0; mod<80; mod++) {
      clusterCountInner+= clFinder->GetClusterCount(mod);
    }    
    for (UInt_t mod=80; mod<240; mod++) {
      clusterCountOuter+= clFinder->GetClusterCount(mod);
    }

    if (clusterCountInner >= fRangeX) {
      while (clusterCountInner >= fRangeX) {
	fRangeX*=2;
      }
      RebinHisto();
    }
    if (clusterCountOuter >= fRangeY) {
      while (clusterCountOuter >= fRangeY) {
	fRangeY*=2;
      }
      RebinHisto();
    }

    fTheHisto->Fill(clusterCountInner,clusterCountOuter);

  }
}
//_____________________________________________________________________
void TMHistogrammingClusterInnerVsOuter::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  fTheHisto->Draw("colz");

  fNeedRedraw = kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingClusterInnerVsOuter::Reset() {
  fRangeX=50;
  fRangeY=50;

  delete fTheHisto;

  CreateHisto();

  fNeedRedraw = kTRUE;

  TMHistogramming::Reset();
}
//_____________________________________________________________________
void TMHistogrammingClusterInnerVsOuter::RebinHisto() {
  fTheHisto->RebinAxis((Double_t)fRangeX,fTheHisto->GetXaxis());
  fTheHisto->RebinAxis((Double_t)fRangeY,fTheHisto->GetYaxis());
}
