How to find 2D Histogram min and max value


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hello Dear experts,
I having a problem with 2D histogram. I am trying to find max and min value of just Y axis. I know the Y axis min value but I want to code it so I can use that value to calculate something.

TGraph *gr= new TGraph("5Mv_CH3.txt");
    TH2D* h=new TH2D("h","5Mv_CH3.txt",273, -20, 200,273, 0,-150);
    auto nPoints = gr->GetN(); 
    for(int i=0; i < nPoints; ++i) {
        double x,y;
        gr->GetPoint(i, x, y);
        h->Fill(x,y);
    }
double y1 = h->GetYaxis()->GetXmin();
cout << y1 << endl; 
´´´ 
5Mv_CH3.txt is my data.
Y axis goes like (-5, -5.1 ,-6.2 ............ -33, -35, -36.5 ....... -5)
The minimum value of Y axis is -36.5 but instead my code show 3.

Cheers

You create a 2-D histogram with automatic bins on the Y axis (you set “ymin = 0” > “ymax = -150”), so ROOT tries to automatically compute reasonable limits (which are usually not exactly equal to what you have in your data).

1 Like

Thank your reply. I made a simple mistake in setting the boundaries. I fixed it but now it says ymin -50 ymax 0 just like the limits I set .

TGraph *gr= new TGraph("5Mv_CH3.txt");
    TH2D* h=new TH2D("h","5Mv_CH3.txt",273, -20, 200,273, -50, 0);
    auto nPoints = gr->GetN(); 
    for(int i=0; i < nPoints; ++i) {
        double x,y;
        gr->GetPoint(i, x, y);
        h->Fill(x,y);
    }
double y1 = h->GetYaxis()->GetXmin();
cout << y1 << endl; 
std::cout << h->GetXaxis()->GetBinLowEdge(h->FindFirstBinAbove(0., 1)) << "\n";
std::cout << h->GetXaxis()->GetBinUpEdge(h->FindLastBinAbove(0., 1)) << "\n";
std::cout << h->GetYaxis()->GetBinLowEdge(h->FindFirstBinAbove(0., 2)) << "\n";
std::cout << h->GetYaxis()->GetBinUpEdge(h->FindLastBinAbove(0., 2)) << "\n";

or:

std::cout << h->GetXaxis()->GetBinCenter(h->FindFirstBinAbove(0., 1)) << "\n";
std::cout << h->GetXaxis()->GetBinCenter(h->FindLastBinAbove(0., 1)) << "\n";
std::cout << h->GetYaxis()->GetBinCenter(h->FindFirstBinAbove(0., 2)) << "\n";
std::cout << h->GetYaxis()->GetBinCenter(h->FindLastBinAbove(0., 2)) << "\n";
1 Like

Yes that’s worked. Thank you.