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

//_____________________________________________________________________
TMHistogrammingClusterSizeInner::TMHistogrammingClusterSizeInner() :
  TMHistogramming(), fDivX(10), fRangeX(10)
{
  fHist = new TH1F("ClusterSizeInner","Cluster Size Inner Layer",fDivX,0.5,fRangeX+0.5);
  fHist->SetMinimum(0);
  fHist->SetStats(0);
  fHist->SetFillColor(kYellow);
  fHist->GetXaxis()->SetTitle("Cluster Size");
  fHist->GetYaxis()->SetTitle("Number of Clusters");
  fHist->SetTitleOffset(1.2,"X");
  fHist->SetTitleOffset(1.6,"Y");
}
//_____________________________________________________________________
TMHistogrammingClusterSizeInner::~TMHistogrammingClusterSizeInner() {
  delete fHist;
}
//_____________________________________________________________________
void TMHistogrammingClusterSizeInner::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) {
    if (module<80) {
      if ((Int_t)size>fRangeX) {
	RebinHistos(size);
      }
      fHist->Fill(size);
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingClusterSizeInner::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  fHist->Draw();
}
//_____________________________________________________________________
void TMHistogrammingClusterSizeInner::Reset() {
  fHist->Reset();
  RebinHistos(fDivX);
  fNeedRedraw = kTRUE;
  TMHistogramming::Reset();
}
//_____________________________________________________________________
void TMHistogrammingClusterSizeInner::RebinHistos(UInt_t includeVal) {
  Int_t nrBinsNew = 0;
  while ((Int_t)includeVal>nrBinsNew) {
    nrBinsNew+=fDivX;
  }

  fRangeX = nrBinsNew;
  if (nrBinsNew>200) {
    nrBinsNew = 200;
  }

  TH1F *fHistNew;
  fHistNew = new TH1F("ClusterSizeInnerNew","Cluster Size Inner Layer",nrBinsNew,0.5,fRangeX+0.5);
  fHistNew->SetMinimum(fHist->GetMinimum());
  fHistNew->SetStats(0);
  fHistNew->SetFillColor(kYellow);
  fHistNew->GetXaxis()->SetTitle("Cluster Size");
  fHistNew->GetYaxis()->SetTitle("Number of Clusters");
  fHistNew->SetTitleOffset(1.2,"X");
  fHistNew->SetTitleOffset(1.6,"Y");

  Int_t nrBins = fHist->GetNbinsX();
  for (Int_t bin=1; bin<=nrBins; bin++) {
    Double_t xVal = fHist->GetBinCenter(bin);
    fHistNew->Fill(xVal,fHist->GetBinContent(bin));
  }

  delete fHist;
  fHist = fHistNew;
  fHist->SetName("ClusterSizeInner");

  fNeedRedraw = kTRUE;
}
