Hello, I am searching for a command to find coordinates to the a max value of a 2D histogram.
h_gaus = ROOT.TH2D("h_gaus", "", 8, 0, 8, 8, 0, 8)
I am filling this histogram with Gaussian distribution.
So now I want a way to find the the x and y values which correspond to the maximum value/bin in the histogram.
Dose anyone know?
Thanks for the reply. But I don’t quiet get how to do it. As I have understood it TH1::GetMaximumBin should do what I want, but I cant quite make sense on what it returns. Here is an example:
h_gaus = ROOT.TH2D("h_gaus", "", 48, 0, 48, 48, 0, 48)
GetMaximumBin returns “1266” which dose not all corresponds to parameter  (15) or parameter  (25). How do I get it to return (~15, ~25)? Which should be the coordinates for the location of the maximum.
Thanks for response, So I am a bit of a beginner here. GetBinXYZ is suppose to get GetMaximumBin as input and then store the result in x,y,z?
How dose it work if I just have two dimensions? I am unable to get this to work in PyROOT.
yes. There is no speciality if the histo has 2 dimensions only.
Here is how to use the two method Danilo and I mentioned. I made a .C macro with your script (I am easier with C++…). The macro is:
TH2D *h_gaus = new TH2D("h_gaus", "", 48, 0, 48, 48, 0, 48);
TF2 *TwoDGaus = new TF2("TwoDGaus","*TMath::Gaus(x,,)*TMath::Gaus(y,,)",0,10,0,10);
Int_t MaxBin = h_gaus->GetMaximumBin();
h_gaus->GetBinXYZ(MaxBin, x, y, z);
printf("The bin having the maximum value is (%d,%d)\n",x,y);
root  .x radio.C
The bin having the maximum value is (15,26)
Alright that explains it, thanks for the example. Now I know how it is suppose to work in C++. Just need to figure out how it should work in Python.
The same … simply add in your code the 2 calls retrieving bins info.
Ok finishing touch. By pre-define my variables with ROOT.Long for storing the coordinates I am able to use the GetBinXYZ.
xx, yy, zz = ROOT.Long(0), ROOT.Long(0), ROOT.Long(0)
h_gaus.GetBinXYZ(maxbin, xx, yy, zz)
This prints 16 , 25 , 0