# Different Values on cout and TH2

Hello,

I’m writing a program which calculates some possible solutions for an optical system.

I’m having some awkward results when printing the results and comparing them to the ones filled on a TH2D histogram.

I created a Function[][] which is then filled by 1 or 0 when I put some constraints. The strange thing is when I draw the histogram: the values go from 0 to 4, but the printed values on the screen are just 0 or 1, the values I defined.

I attached the macro file, and I would thank very much for any help.

Leonardo.
Macro.C (1.41 KB)

You are filling many bins multiple times with 1 (up to 4 times, I believe).
if (Function[i][j]) std::cout << Hist_Function->FindBin(d_System[i], f1[j]) << std::endl;
and e.g. search for bins 455, 458, 866, 1478 and so on.

I added your piece of code on my macro, putting a constraint > 1.:
if (Function[i][j] > 1.) std::cout << Hist_Function->FindBin(d_System[i], f1[j]) << std::endl;

But this way I have no output…which would mean that there are no values greater than 1.

Right after the “Hist_Function->Fill(…);” line, try to add: ```if (Function[i][j]) { Int_t bin = Hist_Function->FindBin(d_System[i], f1[j]); std::cout << bin << " " << Hist_Function->GetBinContent(bin) << std::endl; }```

Thank you Pepe Le Pew.

I inserted this:

if(Function[i][j]
std::cout << i << " " << d_System[i] << " " << j << " " << f1[j] << " "
<< Hist_Function->FindBin(d_System[i], f1[j])
<< " "
<< Hist_Function->GetBinContent(Hist_Function->FindBin(d_System[i], f1[j]))
<< std::endl;

And then my output is:

22 7.2 0 10 75 1
23 7.3 0 10 77 1
23 7.3 1 10.1 77 2
24 7.4 0 10 77 3
24 7.4 1 10.1 77 4
24 7.4 2 10.2 130 1
24 7.4 3 10.3 236 1
25 7.5 0 10 78 1
25 7.5 1 10.1 78 2
25 7.5 2 10.2 131 1
25 7.5 3 10.3 237 1
25 7.5 4 10.4 290 1

The bin 77, for example is from [i,j] = [23,0]…But the program sees the [23,1], [24,0] and [24,1] as the same bin.

Apparently that’s how you defined your histogram.

On second thought … you should get rid of floating-point rounding problems. Try with (in your case, even adding something like 1e-11 instead of 0.5 should be sufficient): ```d_System[i] = dmin + step * (i + 0.5); // (i + 0.5) for "x" bin center f1[j] = f1_min + step * (j + 0.5); // (j + 0.5) for "y" bin center```

Thank you Pepe Le Pew,

After adding 1e-11 to the steps, I got the numbers from 0 to 1, but now my result gets bit weird.

My solutions now are continuous (PNG’s attached). What I expected was discrete solutions, as it was on original macro.

Thanks a lot.

Well, you may also consider shifting histogram bins’ edges by “- step / 2”, so that the original (unmodified) “d_System[i]” and “f1[j]” values are equal to histogram bins’ centres. Try with (in your case, if you just want to get rid of floating-point rounding problems, even shifting by something like -1e-11 instead of -0.5 should be sufficient): ```TH2D *Hist_Function = new TH2D("Sol", "Possible Solutions;d;f1", n_points, dmin - 0.5 * step, dmax - 0.5 * step, n_points, f1_min - 0.5 * step, f1_max - 0.5 * step);```

Thank you Pepe Le Pew, for all your help. 