Home | News | Documentation | Download

Using a 2D array to plot with TGraph

I am trying to plot a TGraph (Root cern) using data stored in a 2D array. I have defined my 2D array like this

double**meansig = new double*[NPIXAX];
for(int i = 0; i < NPIXAX; i++){
meansig[i] = new double[NPIXAY];}

There are 2 more arrays defined the same way. I want to plot the graph using TGraph and I tried to do it like this

TGraph* g = new TGraph(pixn,pixid,meansig);

but that didn’t work so I tried this

TGraph* g = new TGraph(pixn,*pixid,*meansig);

and this

TGraph* g = new TGraph(pixn,**pixid,**meansig);

but they didn’t work either, I would appreciate any advice

Thanks

TGraph needs simple one dimensional arrays, so try:
double *meansig = new double[((NPIXAX) * (NPIXAY))];

I need the 2D array as the data is extracted from a 2D histogram from a root file, so unfortunately having a 2D array is important

What is the “x” and “y” of the points?

Maybe you want a TGraph2D for 2D data.

in this case x is the ID of the pixel and y is the mean of the signal

TGraph *g = new TGraph(((NPIXAX) * (NPIXAY)));
for (int x = 0; x < (NPIXAX); x++)
  for (int y = 0; y < (NPIXAY); y++)
    g->SetPoint(((NPIXAY) * x + y), pixid[x][y], meansig[x][y]);

Thanks, that did work