#include "TMHistogrammingHitMapEq.h"
#include <TStyle.h>
#include <TPad.h>
#include <TH2F.h>
#include <TGaxis.h>

//_____________________________________________________________________
TMHistogrammingHitMapEq::TMHistogrammingHitMapEq(UInt_t eq) :
  TMHistogramming(), fEq(eq)
{ 
  
  TString histoName = Form("fHitMap_%d",fEq);
  TString histoTitle = Form("Eq %d",fEq);

  fHitMap = new TH2F(histoName.Data(), histoTitle.Data(), 
		     32*10, -0.5, 32*10-0.5,
		     256*6, -0.5, 256*6-0.5);
  fHitMap->SetMinimum(0);
  fHitMap->SetNdivisions(-10,"X");
  fHitMap->SetNdivisions(-006,"Y");
  fHitMap->SetTickLength(0,"X");
  fHitMap->SetTickLength(0,"Y");
  fHitMap->GetXaxis()->SetLabelColor(gStyle->GetCanvasColor());
  fHitMap->GetYaxis()->SetLabelColor(gStyle->GetCanvasColor());
  fHitMap->SetStats(0);

  fAxis1 = new TGaxis(-0.5,-120,32*10-0.5,-120,-0.5,9.5,510,"-");
  fAxis1->SetLineColor(gStyle->GetCanvasColor());
  fAxis1->SetName("axis1");

  fAxis2 = new TGaxis(-15,256*6-0.5,-15.0001,-0.5,-0.5,5.5,510,"-");
  fAxis2->SetLineColor(gStyle->GetCanvasColor());
  fAxis2->SetName("axis2");

//!!!  // create histogramming objs for all 60 chips:
//!!!  for (UInt_t hs=0; hs<6; hs++) {
//!!!    for (UInt_t chip=0; chip<10; chip++) {
//!!!      fHistChip[hs][chip] = new TMHistogrammingHitMapChip(eq,hs,chip);
//!!!    }
//!!!  }

}
//_____________________________________________________________________
TMHistogrammingHitMapEq::~TMHistogrammingHitMapEq() {
  delete fHitMap;
  delete fAxis1;
  delete fAxis2;
//!!!  for (UInt_t hs=0; hs<6; hs++) {
//!!!    for (UInt_t chip=0; chip<10; chip++) {
//!!!      delete fHistChip[hs][chip];
//!!!    }
//!!!  }
}
//_____________________________________________________________________
void TMHistogrammingHitMapEq::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
//!!!  if (fEventAccept) {
//!!!    for (UInt_t hs=0; hs<6; hs++) {
//!!!      for (UInt_t chip=0; chip<10; chip++) {
//!!!	fHistChip[hs][chip]->EndEvent(str, globalEventNumber);
//!!!      }
//!!!    }
//!!!  }

  TMHistogramming::EndEvent(str, globalEventNumber);
}
//_____________________________________________________________________
void TMHistogrammingHitMapEq::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
  if (fEventAccept) {
    if (eq==fEq) {
      fHitMap->Fill(chip*32+col,(5-hs)*256+row);
//!!!      fHistChip[hs][chip]->ProcessPixel(eq,hs,chip,col,row);
    }
  }
}
//_____________________________________________________________________
void TMHistogrammingHitMapEq::Draw(TPad *pad) {
  pad->cd();
  pad->SetRightMargin(0.15);
  pad->SetBorderSize(0);
  pad->SetBorderMode(0);
  pad->SetGridx(1);
  pad->SetGridy(1);

  fHitMap->Draw("colz");
  fAxis1->Draw();
  fAxis2->Draw();

  pad->SetEditable(kFALSE);
}
//_____________________________________________________________________
void TMHistogrammingHitMapEq::Reset() {
//!!!  for (UInt_t hs=0; hs<6; hs++) {
//!!!    for (UInt_t chip=0; chip<10; chip++) {
//!!!      fHistChip[hs][chip]->Reset();
//!!!    }
//!!!  }
  fHitMap->Reset();
  TMHistogramming::Reset();
}
//______________________________________________________________________
void TMHistogrammingHitMapEq::ResetAuto() {
//!!!  if (fEventAccept) {
//!!!    for (UInt_t hs=0; hs<6; hs++) {
//!!!      for (UInt_t chip=0; chip<10; chip++) {
//!!!	fHistChip[hs][chip]->ResetAuto();
//!!!      }
//!!!    }
//!!!  }

  TMHistogramming::ResetAuto();
}
