Plotting multiple data.asc files using TGraph class in one single plot

Hello all, I want to plot 9 graphs using TMultiGraph class on one canvas-as single plot, but I would like to make code so that every graph is filled with data from files automaticly and not by me. Here is what I tried but was getting errors.
(And this same code but for histograms is working perfectly by using the TH1F class for creating histograms and THStack for drawing them all on a single plot ).
I’m new to root and apologize in advance for any major mistakes, thank you for your time.

TCanvas *c1 = new TCanvas();

 int graph_ang_counts_all() {

 ifstream ang_file[9]; 
 ang_file[0].open("theta-fe54-e335.asc");
 ang_file[1].open("theta-fe54-e340.asc");
 ang_file[2].open("theta-fe54-e346.asc");
 ang_file[3].open("theta-fe54-e351.asc");
 ang_file[4].open("theta-fe54-e354.asc");
 ang_file[5].open("theta-fe54-e358.asc");
 ang_file[6].open("theta-fe54-e362.asc");
 ang_file[7].open("theta-fe54-e365.asc"); 
 ang_file[8].open("theta-fe54-e370.asc");

 double ang, counts;

 TMultiGraph *mgr = new TMultiGraph(); 

 TGraph *gr[9];

 for(int i=0;  i<9; i++){

  gr[i] = new TGraph(i, ang, counts); 
  gr[i]->SetMarkerStyle(20);
  gr[i]->SetMarkerColor(i+2);
   
  while (ang_file[i] >> ang >> counts )       
 { 

  gr[i]->SetPoint(i, ang, counts);    	
  mgr->Add(gr[i]);
 }
   
}
mgr->Draw("ALP");

return 0;        
                              
}

error: no matching constructor for initialization of ‘TGraph’
gr[i] = new TGraph(i, ang, counts);
^ ~~~~~~~~~~~~~~
/snap/root-framework/753/usr/local/include/TGraph.h:82:4: note: candidate constructor not viable: no known conversion from ‘double’ to ‘const Double_t *’ (aka ‘const double *’) for 2nd argument; take the address of the argument with &
TGraph(Int_t n, const Double_t *x, const Double_t *y);
^
/snap/root-framework/753/usr/local/include/TGraph.h:80:4: note: candidate constructor not viable: no known conversion from ‘double’ to ‘const Int_t *’ (aka ‘const int *’) for 2nd argument
TGraph(Int_t n, const Int_t *x, const Int_t *y);
^
/snap/root-framework/753/usr/local/include/TGraph.h:81:4: note: candidate constructor not viable: no known conversion from ‘double’ to ‘const Float_t *’ (aka ‘const float *’) for 2nd argument
TGraph(Int_t n, const Float_t *x, const Float_t *y);
^
/snap/root-framework/753/usr/local/include/TGraph.h:89:4: note: candidate constructor not viable: no known conversion from ‘int’ to ‘const char *’ for 1st argument
TGraph(const char *filename, const char *format=“%lg %lg”, Option_t *option=“”);
^
/snap/root-framework/753/usr/local/include/TGraph.h:85:4: note: candidate constructor not viable: requires 2 arguments, but 3 were provided
TGraph(const TVectorF &vx, const TVectorF &vy);
^
/snap/root-framework/753/usr/local/include/TGraph.h:86:4: note: candidate constructor not viable: requires 2 arguments, but 3 were provided
TGraph(const TVectorD &vx, const TVectorD &vy);
^
/snap/root-framework/753/usr/local/include/TGraph.h:88:4: note: candidate constructor not viable: requires at most 2 arguments, but 3 were provided
TGraph(const TF1 *f, Option_t *option=“”);
^
/snap/root-framework/753/usr/local/include/TGraph.h:79:4: note: candidate constructor not viable: requires single argument ‘n’, but 3 arguments were provided
TGraph(Int_t n);
^
/snap/root-framework/753/usr/local/include/TGraph.h:83:4: note: candidate constructor not viable: requires single argument ‘gr’, but 3 arguments were provided
TGraph(const TGraph &gr);
^
/snap/root-framework/753/usr/local/include/TGraph.h:87:4: note: candidate constructor not viable: requires single argument ‘h’, but 3 arguments were provided
TGraph(const TH1 *h);
^
/snap/root-framework/753/usr/local/include/TGraph.h:78:4: note: candidate constructor not viable: requires 0 arguments, but 3 were provided
TGraph();
^

Welcome to the ROOT forum.

I do not have your data files so I cannot run your macro, but looking at your code I spotted several mistakes in the logic. The following version of your code should fix them. Note I did not run it so some errors might still be present.

void graph_ang_counts_all() {
   ifstream ang_file[9];
   ang_file[0].open("theta-fe54-e335.asc");
   ang_file[1].open("theta-fe54-e340.asc");
   ang_file[2].open("theta-fe54-e346.asc");
   ang_file[3].open("theta-fe54-e351.asc");
   ang_file[4].open("theta-fe54-e354.asc");
   ang_file[5].open("theta-fe54-e358.asc");
   ang_file[6].open("theta-fe54-e362.asc");
   ang_file[7].open("theta-fe54-e365.asc");
   ang_file[8].open("theta-fe54-e370.asc");

   double ang, counts;
   auto mgr = new TMultiGraph();
   TGraph *gr[9];

   for (int i=0;  i<9; i++) {
      gr[i] = new TGraph();
      gr[i]->SetMarkerStyle(20);
      gr[i]->SetMarkerColor(i+2);
      while (ang_file[i] >> ang >> counts ) {
         gr[i]->AddPoint(ang, counts);
      }
      mgr->Add(gr[I]);
   }
   mgr->Draw("ALP");
}
1 Like

This is plain, straightforward code with nothing unique, yet I was still able to write it incorrectly. Thank you so much for fixing it; it is now running flawlessly. Please accept my sincere apologies for my shoddy C++.
(But I did manage to make code for a histogram for the same purpose that worked, so if anyone would need it, I would gladly post it.)

1 Like

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