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

//_____________________________________________________________________
TMHistogrammingCErrorFractionVsEq::TMHistogrammingCErrorFractionVsEq() :
  TMHistogramming()
{
  fTheHisto = new TH1F("Consistency_ErrorFractions_Per_Eq", "Fraction of Events with Errors vs Eq", 20, -0.5, 19.5);
  fTheHisto->SetMinimum(0);
  fTheHisto->SetStats(0);
  fTheHisto->SetFillColor(kRed);
  fTheHisto->GetXaxis()->SetTitle("Eq ID");
  fTheHisto->GetYaxis()->SetTitle("Fraction of Events");
  fTheHisto->SetTitleOffset(1.2,"X");
  fTheHisto->SetTitleOffset(1.6,"Y");
  for (UInt_t eq=0; eq<20; eq++) {
    fTotalNumberErrorEvents[eq]=0;
  }
}
//_____________________________________________________________________
TMHistogrammingCErrorFractionVsEq::~TMHistogrammingCErrorFractionVsEq() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingCErrorFractionVsEq::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    AliITSRawStreamSPDErrorLog *advLogger = str->GetAdvancedErrorLog();
    for (UInt_t eq=0; eq<20; eq++) {
      if (advLogger->GetNrErrors(0,eq) > 0) fTotalNumberErrorEvents[eq]++;
      fTheHisto->SetBinContent(eq+1,((Float_t) fTotalNumberErrorEvents[eq])/(fNrEvents+1));
    }
  }
  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingCErrorFractionVsEq::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

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