#include "TMHistogrammingGlobalPixelZDistRef.h"
#include "TMControlSPD.h"
#include "AliITSRawStreamSPD.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH1F.h>
#include <TMath.h>

//_____________________________________________________________________
TMHistogrammingGlobalPixelZDistRef::TMHistogrammingGlobalPixelZDistRef() :
  TMHistogramming()
{ 

  fTotal = new TH1F("GlobalPixelZDistRefTotal","Global Z Distribution Ref (red=outer, blue=inner layer)",150,-15,15);
  fTotal->SetLineColor(kBlack);
  fTotal->GetXaxis()->SetTitle("Z [cm]");
  fTotal->SetStats(0);

  fInner = new TH1F("GlobalPixelZDistRefInner","Global Z Distribution Ref (red=outer, blue=inner layer)",150,-15,15);
  fInner->SetLineColor(kBlue);
  fInner->GetXaxis()->SetTitle("Z [cm]");
  fInner->SetStats(0);

  fOuter = new TH1F("GlobalPixelZDistRefOuter","Global Z Distribution Ref (red=outer, blue=inner layer)",150,-15,15);
  fOuter->SetLineColor(kRed);
  fOuter->GetXaxis()->SetTitle("Z [cm]");
  fOuter->SetStats(0);
}
//_____________________________________________________________________
TMHistogrammingGlobalPixelZDistRef::~TMHistogrammingGlobalPixelZDistRef() {
  delete fInner;
  delete fOuter;
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelZDistRef::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  if (fEventAccept) {
    if (fNrEvents==0) {
      UInt_t mod, colM, rowM;
      for (UInt_t eq=0; eq<20; eq++) {
	for (UInt_t hs=0; hs<6; hs++) {
	  for (UInt_t chip=0; chip<10; chip++) {
	    if (str->IsActiveChip(eq,hs,chip)) {
	      for (UInt_t col=0; col<32; col++) {
		UInt_t row=0;
		AliITSRawStreamSPD::OnlineToOffline(eq,hs,chip,col,row,mod,colM,rowM);
		TMControlSPD::Instance()->TransformClusterToGlobal(mod,colM,rowM);  
		Float_t z = TMControlSPD::Instance()->GetGlobalZ();
		fTotal->Fill(z,256);
		if (hs<2) fInner->Fill(z,256);
		else      fOuter->Fill(z,256);
	      }
	    }
	  }
	}
      }
    }
  }

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

  fTotal->Draw();
  fInner->Draw("same");
  fOuter->Draw("same");
}
//_____________________________________________________________________
void TMHistogrammingGlobalPixelZDistRef::Reset() {
  fTotal->Reset();
  fInner->Reset();
  fOuter->Reset();
  TMHistogramming::Reset();
}


