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

//_____________________________________________________________________
TMHistogrammingCErrorPos::TMHistogrammingCErrorPos() :
  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();
}
//_____________________________________________________________________
TMHistogrammingCErrorPos::~TMHistogrammingCErrorPos() {
  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 TMHistogrammingCErrorPos::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 TMHistogrammingCErrorPos::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {

    AliITSRawStreamSPDErrorLog *errLog = str->GetAdvancedErrorLog();

    for (UInt_t err=0; err<AliITSRawStreamSPDErrorLog::GetNrErrorCodes(); err++) {
      for (UInt_t eq=0; eq<20; eq++) {

	UInt_t nErrPosCounter = errLog->GetEventErrPosCounter(err,eq);
	for (UInt_t nErr=0; nErr<nErrPosCounter; nErr++) {
	  UInt_t bytesRead = errLog->GetEventErrPos(nErr,err,eq);
	  if (fInternalEventCounter[err][eq]==0) {
	    fRangeX1[err][eq] = globalEventNumber - 2;
	    if (fRangeX1[err][eq]<0) fRangeX1[err][eq] = 0;
	  }
	  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][eq]) {
	    fNeedRedraw = kTRUE;
	    fRangeX2[err][eq] = (UInt_t) (globalEventNumber*1.1);
	  }
	  if ((Int_t)globalEventNumber > fRangeX2[err][20]) {
	    fNeedRedraw = kTRUE;
	    fRangeX2[err][20] = (UInt_t) (globalEventNumber*1.1);
	  }
	  if ((Int_t)bytesRead > fRangeY2[err][eq]) {
	    fNeedRedraw = kTRUE;
	    fRangeY2[err][eq] += (UInt_t) (bytesRead*1.1);
	  }
	  if ((Int_t)bytesRead > fRangeY2[err][20]) {
	    fNeedRedraw = kTRUE;
	    fRangeY2[err][20] += (UInt_t) (bytesRead*1.1);
	  }
	  
	  fTheGraph[err][eq]->SetPoint(fInternalEventCounter[err][eq],globalEventNumber,bytesRead);
	  fTheGraph[err][20]->SetPoint(fInternalEventCounter[err][20],globalEventNumber,bytesRead);
	
	  fInternalEventCounter[err][eq]++;
	  fInternalEventCounter[err][20]++;
	}

      }
    }


  }
  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingCErrorPos::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("Bytes Read (when error) Vs Event Number , EC %d , Eq %d",
		 fCurrentErr,fCurrentEq);
  }
  else {
    title = Form("Bytes Read (when error) Vs Event Number , EC %d , All Eq",
		 fCurrentErr);
  }

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

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

  fNeedRedraw = kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingCErrorPos::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 TMHistogrammingCErrorPos::SetCurrentErr(UInt_t err) {
  if (err>=AliITSRawStreamSPDErrorLog::GetNrErrorCodes()) {
    printf("ERROR: TMHistogrammingCErrorPos::SetCurrentErr (%d out of range)\n",err);
  }
  else {
    fCurrentErr = err;
  }
}
//_____________________________________________________________________
void TMHistogrammingCErrorPos::SetCurrentEq(UInt_t eq) {
  if (eq>20) {
    printf("ERROR: TMHistogrammingCErrorPos::SetCurrentEq (%d out of range)\n",eq);
  }
  else {
    fCurrentEq = eq;
  }
}
