Reading arrays with incremental names in a loop to plot

Hello,

I have 10 Arrays with data which are names as y1,y2 … and so on. I want to plot all these arrays in a single plot against the same x axis using a for loop. Is there an efficient way of doing that?
Here is what I have which is giving me tons of errors-

TGraph *g[9];

   char *y = new char;
   TMultiGraph *mg = new TMultiGraph();

   for (int i=1; i<10; i++) {
       string s = "y";
       i.Form(i,"%d");
       s = s+i;
       //sprintf(y,"y%d",i);
       g[i] = new TGraph(9, x, s);
       mg->Add(g[i]);
    }
   mg->Draw("AL");

Can anyone help? Thank you for your time!

ROOT Version: 6.18/04
Platform: C++
Compiler: Visual Studio Code

This cannot work. TGraph constructor does not accept character string as third parameter. If you have only 10 arrays the simplest be to not do a loop but duplicate 10 times:

       auto g1 = new TGraph(9, x, y1);
       mg->Add(g1);

ou do the same for all the graphs until g10. Otherwise the way you did it will require to have an array of arrays.

void graphs() {
   double x[3] = {1,2,3};
   double y[10][3] = {
          {1,2,3},
          {4,5,6},
          {7,8,9},
          {10,11,12},
          {13,14,15},
          {10,21,13},
          {14,15,21},
          {17,18,19},
          {10,1,2},
          {3,14,15}
   };

   auto mg = new TMultiGraph();
   TGraph *g;

   for(int i = 0; i<10; i++) {
      g = new TGraph (3,&x[0],&y[i][0]);
      mg->Add(g);
   }

   mg->Draw("AL*");
}

1 Like

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