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

//_____________________________________________________________________
TMHistogrammingClusterMultiplicity::TMHistogrammingClusterMultiplicity() :
  TMHistogramming(),
  fClusterCountInner(0), fClusterCountOuter(0), fClusterCountTotal(0),
  fInternalEventCounter(0),
  fRangeX1(0), fRangeX2(25), fRangeY2(25),
  fTimeStamp(0)
{
  fTheHisto=NULL;
  fNeedRedraw = kTRUE;
  CreateHistos();
}
//_____________________________________________________________________
TMHistogrammingClusterMultiplicity::~TMHistogrammingClusterMultiplicity() {
  if (fTheHisto!=NULL) delete fTheHisto;
  delete fTotal;
  delete fInner;
  delete fOuter;
}
//_____________________________________________________________________
void TMHistogrammingClusterMultiplicity::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 TMHistogrammingClusterMultiplicity::ProcessClusters(HenrikClusterFinderSPD* clFinder) {
  if (fEventAccept) {
    for (UInt_t mod=0; mod<80; mod++) {
      fClusterCountInner+= clFinder->GetClusterCount(mod);
      fClusterCountTotal+= clFinder->GetClusterCount(mod);
    }
    
    for (UInt_t mod=80; mod<240; mod++) {
      fClusterCountOuter+= clFinder->GetClusterCount(mod);
      fClusterCountTotal+= clFinder->GetClusterCount(mod);
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingClusterMultiplicity::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {

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

    //    printf("%s : event %d: %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)fClusterCountTotal > fRangeY2) {
      fRangeY2 += (UInt_t) (fClusterCountTotal*1.1);
      fNeedRedraw = kTRUE;
    }
    
    fTotal->SetPoint(fInternalEventCounter,globalEventNumber,fClusterCountTotal);
    fInner->SetPoint(fInternalEventCounter,globalEventNumber,fClusterCountInner);
    fOuter->SetPoint(fInternalEventCounter,globalEventNumber,fClusterCountOuter);
    
    fClusterCountTotal=0;
    fClusterCountInner=0;
    fClusterCountOuter=0;
   
    

    fInternalEventCounter++;


    
  }

  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingClusterMultiplicity::Draw(TPad *pad) {
  //  printf("draw intcount=%d , x1=%d , x2=%d , y2=%d\n",fInternalEventCounter,fRangeX1,fRangeX2,fRangeY2);
  pad->cd();
  pad->SetLeftMargin(0.15);
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);

  if (fTheHisto!=NULL) delete fTheHisto;

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

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

  fNeedRedraw = kFALSE;
}
//_____________________________________________________________________
void TMHistogrammingClusterMultiplicity::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();
}


