#include "TMHistogrammingPixelMultiplicityHS.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH1F.h>

//_____________________________________________________________________
TMHistogrammingPixelMultiplicityHS::TMHistogrammingPixelMultiplicityHS() :
  TMHistogramming(), fRangeX(25)
{ 

  for (UInt_t eq=0; eq<20; eq++) {
    for (UInt_t hs=0; hs<6; hs++) {
      fTotalHits[eq][hs]=0;
    }
  }
  
  CreateHistos();

}
//_____________________________________________________________________
TMHistogrammingPixelMultiplicityHS::~TMHistogrammingPixelMultiplicityHS() {
  delete fHistTotal;
  delete fHistInner;
  delete fHistOuter;
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityHS::CreateHistos() {

  fHistTotal = new TH1F("PixelMultiplicityHStot","Pixel Multiplicity Per HS (black=all, red=outer, blue=inner layer)",
		       200,-0.5,fRangeX-0.5);
  fHistTotal->SetXTitle("Pixel Hits");
  fHistTotal->SetStats(0);
  fHistTotal->SetLineColor(kBlack);
  fHistTotal->SetBit(TH1::kCanRebin);


  fHistInner = new TH1F("PixelMultiplicityHSinn","Pixel Multiplicity Per HS (black=all, red=outer, blue=inner layer)",
		       200,-0.5,fRangeX-0.5);
  fHistInner->SetXTitle("Pixel Hits");
  fHistInner->SetStats(0);
  fHistInner->SetLineColor(kBlue);
  fHistInner->SetBit(TH1::kCanRebin);

  fHistOuter = new TH1F("PixelMultiplicityHSout","Pixel Multiplicity Per HS (black=all, red=outer, blue=inner layer)",
		       200,-0.5,fRangeX-0.5);
  fHistOuter->SetXTitle("Pixel Hits");
  fHistOuter->SetStats(0);
  fHistOuter->SetLineColor(kRed);
  fHistOuter->SetBit(TH1::kCanRebin);

}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityHS::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    fTotalHits[eq][hs]++;
  }
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityHS::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    for (UInt_t eq=0; eq<20; eq++) {
      for (UInt_t hs=0; hs<6; hs++) {
	
	if ((Int_t)fTotalHits[eq][hs] >= fRangeX) {
	  //	UInt_t oldRange=fRangeX;
	  while ((Int_t)fTotalHits[eq][hs] >= fRangeX) {
	    fRangeX*=2;
	  }
	  //	printf("%d,%d: %d (%d,%d)\n",eq,hs,fTotalHits[eq][hs],oldRange,fRangeX);
	  RebinHistos();
	}
	
	fHistTotal->Fill(fTotalHits[eq][hs]);
	if (hs<2) {
	  fHistInner->Fill(fTotalHits[eq][hs]);
	}
	else {
	  fHistOuter->Fill(fTotalHits[eq][hs]);
	}
	fTotalHits[eq][hs]=0;
      }
    }
  }

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

  fHistTotal->Draw();
  fHistInner->Draw("same");
  fHistOuter->Draw("same");

  fNeedRedraw = kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityHS::Reset() {
  fRangeX=25;

  delete fHistTotal;
  delete fHistInner;
  delete fHistOuter;

  CreateHistos();

  fNeedRedraw = kTRUE;

  TMHistogramming::Reset();
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityHS::RebinHistos() {
  fHistTotal->RebinAxis((Double_t)fRangeX,fHistTotal->GetXaxis());
  fHistInner->RebinAxis((Double_t)fRangeX,fHistInner->GetXaxis());
  fHistOuter->RebinAxis((Double_t)fRangeX,fHistOuter->GetXaxis());
}



