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

//_____________________________________________________________________
TMHistogrammingEventsWithHitsAll::TMHistogrammingEventsWithHitsAll() :
  TMHistogramming(), fTotalNumberHitEvents(0)
{ 
  for (UInt_t eq=0; eq<20; eq++) {
    fTotalNumberHitEventsEq[eq] = 0;
  }

  TString histoName = Form("EventsWithHitsAll");
  TString histoTitle = Form("Ratio of Events with Hits (Per Eq)");

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

  fTheHisto->Draw();
}
//_____________________________________________________________________
void TMHistogrammingEventsWithHitsAll::Reset() {
  fTheHisto->Reset();
  fTotalNumberHitEvents = 0;
  for (UInt_t eq=0; eq<20; eq++) {
    fTotalNumberHitEventsEq[eq] = 0;
  }
  TMHistogramming::Reset();
}


