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

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

  fTheHisto->Draw();
}
//_____________________________________________________________________
void TMHistogrammingCErrorFractionVsEC::Reset() {
  fTheHisto->Reset();
  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::kNrErrorCodes; err++) {
    fTotalNumberErrorEvents[err]=0;
  }
  TMHistogramming::Reset();
}
