Getting x value of maximum amplitude

Hi ROOT friends,

Relatively new to ROOT, and not sure what my problem is here:
I have a plot that measures amplitude vs. frequency, and have successfully found the maximum amplitude in a given range using:

double maxamp = G->GetHistogram()->GetMaximum();

However, I run into problems when trying to find the corresponding frequency (x-axis) of maximum amplitude. Searching through these forums, I found someone with a similar problem that used:

int binmax = G->GetHistogram()->GetMaximumBin();
double xMax = G->GetXaxis()->GetBinCenter(binmax);

But this doesn’t give the corresponding x-value for me. Instead, it appears to give the value of the lower limit of my x-axis.

For instance, if my limits of frequency are 5900Hz to 6100Hz, it outputs: 5900.0020278677 Hz
If my limits are 6000Hz to 6050Hz, it outputs: 5999.9987121388 Hz

Any ideas? Thanks!

What is your G? It looks like it is a TGraph and then G->GetHistogram() gets the histogram used to draw the axes. This histogram does not have any entries, so GetMaximumBin just returns the first bin.

Instead, all you can do is iterate through the points:

Double_t findMaxPosition(TGraph *G) {
    Double_t x, y;
    G->GetPoint(0, x, y);
    Double_t max_x = x, max_y = y;
    for(int i = 1; i < G->GetN(); i++)
        G->GetPoint(i, x, y);
        if(y > max_y) {
           max_x = x;
           max_y = y;
    return max_x;
root [19] Double_t x[] = {1, 2, 3, 4};
root [20] Double_t y[] = {1, 2, 3, 2};
root [21] g = new TGraph(4, x, y);
root [22] findMaxPosition(g)
(double) 3.000000

root [25] Double_t x2[] = {1, 2, 3, 4, 5, 6};
root [26] Double_t y2[] = {1, 2, 3, 2, 4, 1};
root [27] g = new TGraph(6, x2, y2);
root [28] findMaxPosition(g)
(double) 5.000000

Hi Andreas,

Thanks for the help!

Your code worked for me with slight modification. Unfortunately, my ‘max_y’ value fell outside of the range I was looking at so I simply needed to take that into account:

double xtest, ytest;
G->GetPoint(0, xtest, ytest);
double max_xtest = 0., max_ytest = 0.;
for(int i = 1; i < G->GetN(); i++)
    G->GetPoint(i, xtest, ytest);
    if(f1>xtest) {
    if(f1<xtest<f2) {
       if(ytest > max_ytest) {
       max_xtest = xtest;
       max_ytest = ytest;


where f1 and f2 are my limits.

Thanks again!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.