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

//_____________________________________________________________________
TMHistogrammingPixelMultiplicity::TMHistogrammingPixelMultiplicity() :
  TMHistogrammingCheckButton(),
  fTotalHits(0), fInnerHits(0), fOuterHits(0),
  fInternalEventCounter(0),
  fRangeX1(0), fRangeX2(25), fRangeY2(25),
  fTimeStamp(0)
{ 

  fTheHisto=NULL;
  fNeedRedraw = kTRUE;
  CreateHistos();
}
//_____________________________________________________________________
TMHistogrammingPixelMultiplicity::~TMHistogrammingPixelMultiplicity() {
  if (fTheHisto!=NULL) delete fTheHisto;
  delete fTotal;
  delete fInner;
  delete fOuter;
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicity::CreateHistos() {
  fTotal = new TGraph();
  fTotal->SetLineColor(kBlack);
  fTotal->SetMarkerColor(kBlack);
  fTotal->SetMarkerStyle(8);
  fTotal->SetMarkerSize(0.5);

  fInner = new TGraph();
  fInner->SetLineColor(kBlue);
  fInner->SetMarkerColor(kBlue);
  fInner->SetMarkerStyle(8);
  fInner->SetMarkerSize(0.5);

  fOuter = new TGraph();
  fOuter->SetLineColor(kRed);
  fOuter->SetMarkerColor(kRed);
  fOuter->SetMarkerStyle(8);
  fOuter->SetMarkerSize(0.5);
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicity::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    fTotalHits++;
    if (hs<2) fInnerHits++;
    else      fOuterHits++;
  }
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicity::ProcessEvent(eventHeaderStruct *event) {
  fTimeStamp = event->eventTimestamp;
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicity::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {

    time_t stamp = (time_t) fTimeStamp;
    TString stampMsg = Form("%s",ctime(&stamp));
    stampMsg.ReplaceAll('\n',"");

    if (fChecked) {
      printf("%s : event %d , Num Pixels: %d\n",stampMsg.Data(),globalEventNumber,fTotalHits);
    }

    if (fInternalEventCounter==0) {
      fRangeX1 = globalEventNumber - 2;
      if (fRangeX1<0) fRangeX1 = 0;
      fNeedRedraw = kTRUE;
    }
    if ((Int_t)globalEventNumber > fRangeX2) {
      fRangeX2 = (UInt_t) (globalEventNumber*1.1);
      fNeedRedraw = kTRUE;
    }
    if ((Int_t)fTotalHits > fRangeY2) {
      fRangeY2 += (UInt_t) (fTotalHits*1.1);
      fNeedRedraw = kTRUE;
    }
    
    fTotal->SetPoint(fInternalEventCounter,globalEventNumber,fTotalHits);
    fInner->SetPoint(fInternalEventCounter,globalEventNumber,fInnerHits);
    fOuter->SetPoint(fInternalEventCounter,globalEventNumber,fOuterHits);
    
    fTotalHits=0;
    fInnerHits=0;
    fOuterHits=0;
    
    fInternalEventCounter++;
    
  }

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

  if (fTheHisto!=NULL) delete fTheHisto;

  fTheHisto = new TH2F("PixelMultiplicity","Pixel Multiplicity (black=all, red=outer, blue=inner layer)",
		       201,fRangeX1-0.5,fRangeX2+0.5,200,0,fRangeY2);
  fTheHisto->SetXTitle("Event Number");
  fTheHisto->SetYTitle("Pixel Hits");
  fTheHisto->SetTitleOffset(1.8,"Y");
  fTheHisto->SetStats(0);
  fTheHisto->Draw();

  fTotal->Draw("pL");
  fInner->Draw("pL");
  fOuter->Draw("pL");

  fNeedRedraw = kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingPixelMultiplicity::Reset() {
  if (fTheHisto!=NULL) fTheHisto->Reset();
  delete fTotal;
  delete fInner;
  delete fOuter;

  CreateHistos();

  fInternalEventCounter=0;
  fRangeX1=0;
  fRangeX2=25;
  fRangeY2=25;
  fNeedRedraw=kTRUE;

  TMHistogramming::Reset();
}


