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

//_____________________________________________________________________
TMHistogrammingCErrorFractionVsECperEq::TMHistogrammingCErrorFractionVsECperEq(UInt_t eq) :
  TMHistogramming(), fEq(eq)
{
  TString title = Form("Fraction of Events with Errors vs Error Code (Eq %d)",fEq);
  fTheHisto = new TH1F(title.Data(),title.Data(), 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;
  }
}
//_____________________________________________________________________
TMHistogrammingCErrorFractionVsECperEq::~TMHistogrammingCErrorFractionVsECperEq() {
  delete fTheHisto;
}
//_____________________________________________________________________
void TMHistogrammingCErrorFractionVsECperEq::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    AliITSRawStreamSPDErrorLog *advLogger = str->GetAdvancedErrorLog();
    for (UInt_t err=1; err<AliITSRawStreamSPDErrorLog::kNrErrorCodes; err++) {
      if (advLogger->GetNrErrors(err,fEq) > 0) fTotalNumberErrorEvents[err]++;
      fTheHisto->SetBinContent(err,((Float_t) fTotalNumberErrorEvents[err])/(fNrEvents+1));
    }
  }
  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingCErrorFractionVsECperEq::Draw(TPad *pad) {
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

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