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

//_____________________________________________________________________
TMHistogrammingEventsWithHitsEq::TMHistogrammingEventsWithHitsEq(UInt_t eq) :
  TMHistogramming(), fEq(eq)
{ 
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      fTotalNumberHitEvents[hs][chip] = 0;
    }
  }

  TString histoName = Form("EventsWithHitsEq%d",eq);
  TString histoTitle = Form("Ratio of Events with Hits (Per Chip)  Eq %d",eq);

  fTheHisto = new TH1F(histoName.Data(), histoTitle.Data(), 60, -0.5, 59.5);
  fTheHisto->SetMinimum(0);
  fTheHisto->SetStats(0);
  fTheHisto->SetFillColor(kBlue);
  fTheHisto->GetXaxis()->SetTitle("hs*10 + chip");
  fTheHisto->GetYaxis()->SetTitle("Hit Events / Total Number of Events");
  fTheHisto->SetTitleOffset(1.2,"X");
  fTheHisto->SetTitleOffset(1.6,"Y");
}
//_____________________________________________________________________
TMHistogrammingEventsWithHitsEq::~TMHistogrammingEventsWithHitsEq() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingEventsWithHitsEq::StartEvent(eventHeaderStruct *event, UInt_t globalEventNumber) {
  TMHistogramming::StartEvent(event,globalEventNumber);
  if (fEventAccept) {
    for (UInt_t hs=0; hs<6; hs++) {
      for (UInt_t chip=0; chip<10; chip++) {
	fHitChip[hs][chip] = kFALSE;
      }
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingEventsWithHitsEq::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    if (eq==fEq) {
      fHitChip[hs][chip] = kTRUE;
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingEventsWithHitsEq::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    for (UInt_t hs=0; hs<6; hs++) {
      for (UInt_t chip=0; chip<10; chip++) {
	if (fHitChip[hs][chip]) {
	  fTotalNumberHitEvents[hs][chip]++;
	  fTheHisto->SetBinContent(hs*10+chip+1,((Float_t) fTotalNumberHitEvents[hs][chip])/(fNrEvents+1));
	}
      }
    }
  }
  TMHistogramming::EndEvent(str,globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingEventsWithHitsEq::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  fTheHisto->Draw();
}
//_____________________________________________________________________
void TMHistogrammingEventsWithHitsEq::Reset() {
  fTheHisto->Reset();
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      fTotalNumberHitEvents[hs][chip] = 0;
    }
  }
  TMHistogramming::Reset();
}


