#include "TMHistogrammingFastorProblemAll.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH1F.h>

//_____________________________________________________________________
TMHistogrammingFastorProblemAll::TMHistogrammingFastorProblemAll() :
  TMHistogramming(), fTotalNumberOfProblems(0)
{
  ResetHitChip();

  fTheHisto = new TH1F("FastorProblemAll", "Events with Chip Hits w/o FastOR (Per Eq)", 20, -0.5, 19.5);
  fTheHisto->SetMinimum(0);
  fTheHisto->SetStats(0);
  fTheHisto->SetFillColor(kBlue);
  fTheHisto->GetXaxis()->SetTitle("Eq");
  fTheHisto->GetYaxis()->SetTitle("Number of Events");
  fTheHisto->SetTitleOffset(1.2,"X");
  fTheHisto->SetTitleOffset(1.6,"Y");
}
//_____________________________________________________________________
TMHistogrammingFastorProblemAll::~TMHistogrammingFastorProblemAll() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingFastorProblemAll::ResetHitChip() {
  for (UInt_t eq=0; eq<20; eq++) {
    for (UInt_t hs=0; hs<6; hs++) {
      for (UInt_t chip=0; chip<10; chip++) {
	fHitChip[eq][hs][chip] = kFALSE;
      }
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingFastorProblemAll::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    fHitChip[eq][hs][chip]=kTRUE;
  }
}
//_____________________________________________________________________
void TMHistogrammingFastorProblemAll::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    UInt_t totalProblems = 0;
    for (UInt_t eq=0; eq<20; eq++) {
      UInt_t problems=0;
      for (UInt_t hs=0; hs<6; hs++) {
	for (UInt_t chip=0; chip<10; chip++) {
	  if (fHitChip[eq][hs][chip]) {
	    if (!str->GetFastOrSignal(eq,hs,chip)) {
	      problems++;
	      totalProblems++;
	    }
	  }
	}
      }
      if (problems>0) {
	fTheHisto->Fill(eq);
      }
    }

    ResetHitChip();

    fTotalNumberOfProblems += (totalProblems>0);
  }


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

  fTheHisto->Draw();
}
//_____________________________________________________________________
void TMHistogrammingFastorProblemAll::Reset() {
  fTheHisto->Reset();
  fTotalNumberOfProblems = 0;

  TMHistogramming::Reset();
}


