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

//_____________________________________________________________________
TMHistogrammingFastorMissingEq::TMHistogrammingFastorMissingEq(UInt_t eq) :
  TMHistogramming(), fEq(eq)
{
  ResetCounters();
  ResetHitChip();

  TString name = Form("FastorMissingEq%d",fEq);
  TString title = Form("Ratio of Missing FastOR Signals Eq %d (Per Chip)",fEq);

  fTheHisto = new TH1F(name, title, 60, -0.5, 59.5);
  fTheHisto->SetMinimum(0);
  fTheHisto->SetStats(0);
  fTheHisto->SetFillColor(kBlue);
  fTheHisto->GetXaxis()->SetTitle("hs*10 + chip");
  fTheHisto->GetYaxis()->SetTitle("Missing Signals / Expected Signals");
  fTheHisto->SetTitleOffset(1.2,"X");
  fTheHisto->SetTitleOffset(1.6,"Y");
}
//_____________________________________________________________________
TMHistogrammingFastorMissingEq::~TMHistogrammingFastorMissingEq() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingEq::ResetHitChip() {
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      fHitChip[hs][chip] = kFALSE;
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingEq::ResetCounters() {
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      fExpected[hs][chip] = 0;
      fActual[hs][chip] = 0;
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingEq::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 TMHistogrammingFastorMissingEq::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]) {
	  fExpected[hs][chip]++;
	  if (str->GetFastOrSignal(fEq,hs,chip)) {
	    fActual[hs][chip]++;
	  }
	}

	if (fExpected[hs][chip]>0) {
	  fTheHisto->SetBinContent(hs*10+chip+1, (Float_t)(fExpected[hs][chip]-fActual[hs][chip]) / fExpected[hs][chip]);
	}
      }
    }

    ResetHitChip();
  }

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

  fTheHisto->Draw();
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingEq::Reset() {
  ResetCounters();
  fTheHisto->Reset();  

  TMHistogramming::Reset();
}


