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

//_____________________________________________________________________
TMHistogrammingPixelMultiplicityChip::TMHistogrammingPixelMultiplicityChip() :
  TMHistogramming(), fRangeX(10)
{ 

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

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


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

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

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

  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityChip::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");

}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicityChip::Reset() {
  fRangeX=10;

  delete fHistTotal;
  delete fHistInner;
  delete fHistOuter;

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


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

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

  fNeedRedraw = kTRUE;

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



