TGraphErrors::GetPoints(i,x,y)

    **TASK**
   I need the values of the Y axis and it's corresponding 
   X axis as plotted on my TGraphgErrors.
   
   I am trying to store the values of x and y in two separate arrays then 
   compare their values to a user-defined boundary.
    
    **PROBLEM**
    When I write the values of x and y to a file, 
    I get the desired output, but when I try to access the array, 
    I do not get a valid result.
    
    **HINT**
     /*Get x and y values for point number i. 
 The function returns -1 in case of an invalid request or the point number otherwise
      */
    Int_t TGraph::GetPoint(Int_t i, Double_t &x, Double_t &y) const
 
    
    I just learned that C++ doesn't permit storing a reference into an array 
    but how else can I store the values of x and y for further comparison?
    
    
    _**//code is below**_
    void pulse
   {
       int n;
       Double_t x,y;
       Double_t xPoints[71];
       Double_t yPoints[71];
       TGraph *g = new TGraph();
       |
       .
       .
       .
       .
        //get number of points on the TGraph *g
      m = g->GetN()

       for(n = 0; n < m; n++)
       {
            g->GetPoint(n,x,y);
              
              //assign values to the array
              xPoints[n] = x;
              yPoints[n] = y;

      f << "xPoints[" << n << "] = " << xPoints[n] << "\t" << "yPoints["<< n << "]=" << yPoints[n] << endl;
         }
    }

EXPECTED OUTPUT
xPoints[0] = -4 yPoints[0]=-0.00644252
xPoints[1] = -3.8 yPoints[1]=0.00168338
xPoints[2] = -3.6 yPoints[2]=-0.00267442
xPoints[3] = -3.4 yPoints[3]=-4.06603e-05
xPoints[4] = -3.2 yPoints[4]=-0.00406858
xPoints[5] = -3 yPoints[5]=0.00133091
xPoints[6] = -2.8 yPoints[6]=0.00035244
xPoints[7] = -2.6 yPoints[7]=-0.000301726

ACTUAL OUTPUT
xPoints[0] = 7.06416e-304 yPoints[0]=7.06327e-304
xPoints[1] = 7.06416e-304 yPoints[1]=7.06327e-304
xPoints[2] = 7.06416e-304 yPoints[2]=7.06327e-304
xPoints[3] = 7.06416e-304 yPoints[3]=7.06327e-304
xPoints[4] = 7.06416e-304 yPoints[4]=7.06327e-304
xPoints[5] = 7.06416e-304 yPoints[5]=7.06327e-304
xPoints[6] = 7.06416e-304 yPoints[6]=7.06327e-304
xPoints[7] = 7.06416e-304 yPoints[7]=7.06327e-304

What do you obtain then? And how do you write to a file? What I see in your code snippet seems OK

You can use either a std::reference_wrapper or a pointer instead. However, I don’t see any connection between your comment about storing references and the problem. If you want to sore the values, why bother with references at all?

Are you sure you want to hardcode “71”? Avoid magic numbers!

i just updated the code

I am assuming the problem lies here… i am finding it difficult to store the values of x and y in two seperate arrays.

fixed typo while editing

#include "TGraph.h"
#include <iostream>

void pulse(const TGraph *g) {
  if (!g) return; // just a precaution
  for(Int_t i = 0; i < g->GetN(); i++) {
    std::cout << "X[" << i << "] = " << (g->GetX())[i] << "\t"
              << "Y[" << i << "] = " << (g->GetY())[i]
              << std::endl;
  }
}

your code only prints out the desired output, it doesn’t store them…

I am trying to store these outputs in an array for further use.

i need help solving this misery pls

#include "TGraph.h"
#include <algorithm>
#include <iostream>

void pulse(const TGraph *g) {
  if ((!g) || (!(g->GetN()))) return; // just a precaution
  const Int_t N = g->GetN();
  Double_t *X = new Double_t[N];
  Double_t *Y = new Double_t[N];
  std::copy(g->GetX(), g->GetX() + N, X);
  std::copy(g->GetY(), g->GetY() + N, Y);
  for(Int_t i = 0; i < N; i++) {
    std::cout << "X[" << i << "] = " << X[i] << "\t"
              << "Y[" << i << "] = " << Y[i]
              << std::endl;
  }
  delete [] Y;
  delete [] X;
}

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