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

//_____________________________________________________________________
TMHistogrammingFastorMissingAll::TMHistogrammingFastorMissingAll() :
  TMHistogramming(), fTotalExpected(0), fTotalActual(0)
{
  ResetCounters();
  ResetHitChip();

  fTheHisto = new TH1F("FastorMissingAll", "Ratio of Missing FastOR Signals (Per Eq)", 20, -0.5, 19.5);
  fTheHisto->SetMinimum(0);
  fTheHisto->SetStats(0);
  fTheHisto->SetFillColor(kBlue);
  fTheHisto->GetXaxis()->SetTitle("Eq");
  fTheHisto->GetYaxis()->SetTitle("Missing Signals / Expected Signals");
  fTheHisto->SetTitleOffset(1.2,"X");
  fTheHisto->SetTitleOffset(1.6,"Y");
}
//_____________________________________________________________________
TMHistogrammingFastorMissingAll::~TMHistogrammingFastorMissingAll() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingAll::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 TMHistogrammingFastorMissingAll::ResetCounters() {
  fTotalExpected = 0;
  fTotalActual = 0;
  for (UInt_t eq=0; eq<20; eq++) {
    fExpected[eq]=0;
    fActual[eq]=0;
  }
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingAll::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    fHitChip[eq][hs][chip]=kTRUE;
  }
}
//_____________________________________________________________________
void TMHistogrammingFastorMissingAll::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 (fHitChip[eq][hs][chip]) {
	    fTotalExpected++;
	    fExpected[eq]++;
	    if (str->GetFastOrSignal(eq,hs,chip)) {
	      fTotalActual++;
	      fActual[eq]++;
	    }
	  }
	}
      }
      
      if (fExpected[eq]>0) {
	fTheHisto->SetBinContent(eq+1, (Float_t)(fExpected[eq]-fActual[eq]) / fExpected[eq]);
      }

    }

    ResetHitChip();

  }

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

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

  TMHistogramming::Reset();
}


