#include "TMHistogrammingCErrorVsEvent.h"
#include "AliITSRawStreamSPD.h"
#include "event.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH2F.h>
#include <TGraph.h>

//_____________________________________________________________________
TMHistogrammingCErrorVsEvent::TMHistogrammingCErrorVsEvent() :
  TMHistogrammingCErrorEqChoice(),
  fCurrentErr(0), fCurrentEq(20)
{
  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::GetNrErrorCodes(); err++) {
    for (UInt_t eq=0; eq<=20; eq++) {
      fInternalEventCounter[err][eq] = 0;
      fRangeX1[err][eq] = 0;
      fRangeX2[err][eq] = 25;
      fRangeY2[err][eq] = 25;
    }
  }      

  fTheHisto=NULL;
  fNeedRedraw = kTRUE;

  CreateHistos();
}
//_____________________________________________________________________
TMHistogrammingCErrorVsEvent::~TMHistogrammingCErrorVsEvent() {
  if (fTheHisto!=NULL) delete fTheHisto;
  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::GetNrErrorCodes(); err++) {
    for (UInt_t eq=0; eq<=20; eq++) {
      delete fTheGraph[err][eq];
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingCErrorVsEvent::CreateHistos() {
  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::GetNrErrorCodes(); err++) {
    for (UInt_t eq=0; eq<=20; eq++) {
      fTheGraph[err][eq] = new TGraph();
      fTheGraph[err][eq]->SetLineColor(kRed);
      fTheGraph[err][eq]->SetMarkerColor(kRed);
      fTheGraph[err][eq]->SetMarkerStyle(8);
      fTheGraph[err][eq]->SetMarkerSize(0.5);
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingCErrorVsEvent::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {

    AliITSRawStreamSPDErrorLog *errLog = str->GetAdvancedErrorLog();

    for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::GetNrErrorCodes(); err++) {
      UInt_t nrErrors;
      for (UInt_t eq=0; eq<20; eq++) {
	nrErrors = errLog->GetNrErrors(err,eq);
	if (fInternalEventCounter[err][eq]==0) {
	  fRangeX1[err][eq] = globalEventNumber - 2;
	  if (fRangeX1[err][eq]<0) fRangeX1[err][eq] = 0;
	}
	if ((Int_t)globalEventNumber > fRangeX2[err][eq]) {
	  fNeedRedraw = kTRUE;
	  fRangeX2[err][eq] = (UInt_t) (globalEventNumber*1.1);
	}
	if ((Int_t)nrErrors > fRangeY2[err][eq]) {
	  fNeedRedraw = kTRUE;
	  fRangeY2[err][eq] += (UInt_t) (nrErrors*1.1);
	}
	if (nrErrors>0) {
	  fTheGraph[err][eq]->SetPoint(fInternalEventCounter[err][eq],globalEventNumber,nrErrors);
	  fInternalEventCounter[err][eq]++;
	}
      }
      nrErrors = errLog->GetNrErrorsAllEq(err);
      if (fInternalEventCounter[err][20]==0) {
	fRangeX1[err][20] = globalEventNumber - 2;
	if (fRangeX1[err][20]<0) fRangeX1[err][20] = 0;
      }
      if ((Int_t)globalEventNumber > fRangeX2[err][20]) {
	fNeedRedraw = kTRUE;
	fRangeX2[err][20] = (UInt_t) (globalEventNumber*1.1);
      }
      if ((Int_t)nrErrors > fRangeY2[err][20]) {
	fNeedRedraw = kTRUE;
	fRangeY2[err][20] += (UInt_t) (nrErrors*1.1);
      }
      if (nrErrors>0) {
	fTheGraph[err][20]->SetPoint(fInternalEventCounter[err][20],globalEventNumber,nrErrors);
	fInternalEventCounter[err][20]++;
      }
    }

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

  if (fTheHisto!=NULL) delete fTheHisto;
  TString title;
  if (fCurrentEq<20) {
    title = Form("Number of Errors Vs Event Number , EC %d , Eq %d",
		 fCurrentErr,fCurrentEq);
  }
  else {
    title = Form("Number of Errors Vs Event Number , EC %d , All Eq",
		 fCurrentErr);
  }

  fTheHisto = new TH2F("CErrorVsEvent",title.Data(),
		       201,fRangeX1[fCurrentErr][fCurrentEq]-0.5,fRangeX2[fCurrentErr][fCurrentEq]+0.5,200,0,fRangeY2[fCurrentErr][fCurrentEq]);
  fTheHisto->SetXTitle("Event Number");
  fTheHisto->SetYTitle("Number of Errors");
  fTheHisto->SetTitleOffset(1.8,"Y");
  fTheHisto->SetStats(0);
  fTheHisto->SetFillColor(4);
  fTheHisto->Draw();

  fTheGraph[fCurrentErr][fCurrentEq]->Draw("p");

  fNeedRedraw = kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingCErrorVsEvent::Reset() {
  if (fTheHisto!=NULL) fTheHisto->Reset();

  for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::GetNrErrorCodes(); err++) {
    for (UInt_t eq=0; eq<=20; eq++) {
      delete fTheGraph[err][eq];
      fInternalEventCounter[err][eq]=0;
      fRangeX1[err][eq]=0;
      fRangeX2[err][eq]=25;
      fRangeY2[err][eq]=25;
    }
  }

  CreateHistos();

  fNeedRedraw=kTRUE;

  TMHistogramming::Reset();
}
//_____________________________________________________________________
void TMHistogrammingCErrorVsEvent::SetCurrentErr(UInt_t err) {
  if (err>=AliITSRawStreamSPDErrorLog::GetNrErrorCodes()) {
    printf("ERROR: TMHistogrammingCErrorVsEvent::SetCurrentErr (%d out of range)\n",err);
  }
  else {
    fCurrentErr = err;
  }
}
//_____________________________________________________________________
void TMHistogrammingCErrorVsEvent::SetCurrentEq(UInt_t eq) {
  if (eq>20) {
    printf("ERROR: TMHistogrammingCErrorVsEvent::SetCurrentEq (%d out of range)\n",eq);
  }
  else {
    fCurrentEq = eq;
  }
}
