C++ how to obtain the x value of graph corresponding to a certain y value

  • Context: Comp Sci 
  • Thread starter Thread starter lavster
  • Start date Start date
  • Tags Tags
    C++ Graph Value
Click For Summary

Discussion Overview

The discussion revolves around a programming challenge in C++ related to obtaining the x-values corresponding to specific y-values (90% and 20% of the maximum) from a graph created using ROOT. Participants are exploring how to implement this functionality programmatically rather than manually clicking on the graph.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant describes their approach to finding the maximum value in an array and expresses difficulty in obtaining corresponding x-values for specific y-values.
  • Another participant suggests calculating the 90% and 20% levels of the maximum value and looping through the array to find values that straddle these levels, proposing methods for determining the corresponding x-values.
  • A participant shares their implementation attempt but encounters errors, expressing uncertainty about the source of the errors and whether they are compiler or runtime errors.
  • Another participant points out a potential issue with the if statement syntax in the participant's code, suggesting a correction to the conditional expression.

Areas of Agreement / Disagreement

Participants generally agree on the approach to find the x-values corresponding to the specified y-values, but there is uncertainty regarding the implementation details and the source of errors in the code.

Contextual Notes

There are unresolved issues regarding the handling of multiple potential x-values for the peak graph, as well as concerns about the correctness of the code due to error messages encountered during processing.

lavster
Messages
213
Reaction score
0

Homework Statement



i have drawn a graph in C++/root which is a bunch of points (in an array) joined together ie it is not a function. it looks kind of like a gauss, but isn't ie its a peak. i want to find out the x distance between the points of 90% and 20% of the maximum. i am having difficulty finding how from knowing the value of y to obtain the corresponding x value of eg the 90% level. I can do it from clicking on the graph but i want to write some code to find it and print it on screen using the cout thingy

Homework Equations


The Attempt at a Solution



my array is called Es[]. to find the maximum value i have made a for loop;
Code:
 float max = 0;
  max=Es[0];
  for(int n=0; n<40; n++)
    {
      if(Es[n]>max)
	max=Es[n];
    }
 
  cout << "max value of energy deposited =" << max << "MeV" <<endl;

so i know what the y value is. I have no idea how to get the corresponding x value.

by graph is defined as so:

Code:
 TGraph *g =new TGraph(h->GetNbinsX());//defining a graph
 g->SetTitle("hi");

 cout << "hello" << endl;
 float graphPointNumber=0;//where we are
  cout << "salut" << endl;
  for(int m=1;  m<=h->GetNbinsX();m++)
   {
      cout << "hi" << endl;
      cout << "m="<< m  << endl;

     g->SetPoint(graphPointNumber,h->GetBinCenter(m),Es[m]); 
     graphPointNumber++;
 cout << "value =" << Es[m] << "MeV" << endl;

   }

can anyone tell me how to do this please?

thanks
 
Physics news on Phys.org
You have already found the largest value in the Es array - call it maxEs.
Calculate the two values .2 * maxEs and .9 * maxEs.
Loop through your Es array looking at successive pairs of Es and Es[i + 1] to find the values that straddle .2 * maxEs. (It's not likely that an array value will be exactly equal to .2*maxEs.)
For the left x-value you could use i or i + 1, or the average of the two, which is (2i + 1)/2. Note that this is not an actual x value - it's just the index of the array of Es values. In your previous work you mapped x values to interval numbers, so you will need to do the opposite to map array indexes back to x values.

For the right x-value, do pretty much the same thing, except this time you're looking for successive values in your array that straddle .9 * maxEs.
 
thanks! i think i pretty much followed what you said. however it doesn't work so i think I am doing something stupid... Here is what i wrote:

Code:
float max = 0;
  max=Es[0];
  for(int n=0; n<40; n++)
    {
      if(Es[n]>max)
        max=Es[n];
    }
 
  cout << "max value of energy deposited =" << max << "MeV" <<endl;

  float m90;
  m90=0.9*max;

cout << "90% level =" << m90 << "MeV" <<endl;

 float m20;
 m20=0.2*max;

cout << "20% level =" << m20 << "MeV" <<endl;


 float ind1;
 for(int n=0;n<40;n++)
   {
    
     if(m20>=Es[n]) && (m20<Es[n+1]))
   {
     ind1 =(2*n+1)/2;
   }
 
}

float a;
a=Phantom_xpre[ind1];
                                       
                                        cout << "index" << ind1 << "\t x position =" << a << "mm" <<endl;



 float ind2;
 for(int n=0;n<40;n++)
   {
    
     if(m90>=Es[n]) && (m90<Es[n+1]))
   {
     ind2 =(2*n+1)/2;
   }
 
}

float b;
b=Phantom_xpre[ind2];
                       
        cout << "index" << ind2 << "\t x position =" << b << "mm" <<endl;              

float c=b-a;

        cout << "penumbra =" << c << "mm" <<endl;

and the error message i receive is:

Error: reference tyoe $ with no initialization
Error: Incorrect referencing of $ graphy.C

I don't know what this means.

Can you see where i am going wrong?

thanks
 
The errors don't seem to have anything to do with the code you show. At least I don't see what they're referring to.

Are these compiler errors or runtime errors? Are there line numbers and filenames given with the error information?

The only thing I see, and I don't think it's related to the errors you're seeing, is where you have float c = b - a; down near the bottom. I don't see a mentioned anywhere else, so if a doesn't have a known value, you're going to get garbage for c.
 
Mark44 said:
The errors don't seem to have anything to do with the code you show. At least I don't see what they're referring to.

Are these compiler errors or runtime errors? Are there line numbers and filenames given with the error information?

The only thing I see, and I don't think it's related to the errors you're seeing, is where you have float c = b - a; down near the bottom. I don't see a mentioned anywhere else, so if a doesn't have a known value, you're going to get garbage for c.

a is defined, before b but after ind1.

it is when i process it. so i don't no if that's a compiler error or runtime error. yes it gives line number to be the line where i count the 20% level. but it gives prints the 20% max level to the screen suggesting that it isn't this line that's the problem. but it doesn't do the for loop straight after. and its in my emacs file I am writing in. "graphy.C"

i thought that since my graph is a peak it will have two 90% max points and 20% max points instead of just the one so this may cause problems. but the peak is symmetric so it doesn't really matter which pair i choose... do you think this is the problem?

thanks
 
Your if statements for m20 and m90 are malformed. Instead of this:
if(m20>=Es[n]) && (m20<Es[n+1]))

do this:
if( (m20>=Es[n]) && (m20<Es[n+1]) )

The syntax for the if statement is
1. if (expression) stmt;
or
2.
Code:
if (expression)
{
    stmt1;
    stmt2;
    etc.
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
9
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K