GetBinContent

hi. i would like to ask a stupid question.

I have a 2D histogram (TH2F). I would like to extract Count, given i and j. For instance

TH1F *getAve(TH2F *h2, int ch_mi, int ch_ma) {
  Int_t i, j;
  Double_t Count = 0.;
  Double_t sumCount=0.;
  Double_t meanCounts;

  Double_t mi = h2->GetYaxis()->GetXmin();
  Double_t ma = h2->GetYaxis()->GetXmax();
  Int_t nsteps =  h2->GetYaxis()->GetNbins();
  Double_t chan_diff = (Double_t) ch_ma - (Double_t) ch_mi;
  
  TH1F* h1=new TH1F("h1", "Average:Channels",nsteps,mi,ma);

  for (j=mi; j<ma; j++) {
    sumCount = 0.;
    Count = 0.;
    meanCounts=0.;
   
    for (i=ch_mi; i<ch_ma+1;i++) {
      Count += h2->GetBinContent(i,j);
      meanCounts=Count/chan_diff;
    }
    h1->SetBinContent(j,meanCounts);
  }
  
 return h1;
}

GetBinContent(i,j) provides me zeroz while i am expecting non-zero Count values. Am I using the right function the values in the 3rd dimension of my plot?, or am I using the function correctly?
Thanks.
f.

for (j = 0; j <= (nsteps + 1); j++) {

hi Pepe,

GetBinContent is not reading the Z value of the corresponding X and Y in my histogram…I am not really interested to read from Y=0 or X = 0 but at some defined Ymax and Xmax. What did I probably miss?

F.

for (j = h2->GetYAxis()->FindBin(ymin); j <= h2->GetYAxis()->FindBin(ymax); j++) {
// …
for (i = h2->GetXAxis()->FindBin(xmin); i <= h2->GetXAxis()->FindBin(xmax); i++) {

See: http://root.cern.ch/root/html/TAxis.html#TAxis:FindBin and http://root.cern.ch/root/html/TH1.html

thank you