Basic question about creating multiple objects


i have a quite basic question:

i want to open 8 files and create several graphs. the problem is, that i don’t know how to create multiple objects. my code within the for-loop:


TString filename = "wic_";
filename +=i;
filename +=".tab";
cout << filename; data...

TString cname = "c";
cname += i;
TCanvas *cname = new TCanvas("c"i, "Canvas",1);

...creating more objects...

for the filenames, it works, so i can actually open all files and read data and so on. obviously, the creating of the canvi does not work. so how do i get the “i” in my canvasname?


ps: i know, this is a basic programming question, but i don’t know what to ask google for… c++ “creating multiple objects” returns only useless links


Try this:

TString cname; [...] cname.Form("c%d",i); TCanvas *cname = new TCanvas(cname.Data(), "Canvas", 600, 400);

Thank you Bertrand!

This works for canvi, pads, etc., but how do I get multiple graphs?

TCanvas *c[files]; c[i] = new TCanvas(Form("c%d",i), Form("Canvas %d", i), 1);

TGraph *gr[files]; gr[i] = new TGraph(n, Form("x%d[n]",i), Form("y%d[n]", i))
does not work


EDIT: maybe i sould include my complete code. without the for-loop, it works.
multifile.txt (2.53 KB)
multifile.C (2.53 KB)


Please take a look in $ROOTSYS/tutorials/graphs to see how to use TGraph…


it seems, that i don’t have the tutorials on my PC. locate can’t find either tutorials or graphs. i obtained root 5.17 from this page:

you don’t talk about do you?

There are many examples in the tutorials directory… I would advise to take a look at them, and at the documentation too:

Cheers, Bertrand.

I looked through a lot documents and tutorials, but my problem is, that i don’t know where to look. The examples all deal with single graphs (or manually create more than one). I know how to use them, my code works for one. I just can’t get my indexed arrays xi[n] in my i Graphs gr[i].

I don’t think this is a specific TGraph-Problem but a general problem in my understanding of c++. I think I am missing something really simple here.

ah ok, I did have a syntax-error in my graph.
now i use TGraph(n, Form(“x%d”,i), Form(“x%d”,i)), but the error is still the same

Error: Can’t call TGraph::TGraph(n,Form(“x%d”,i),Form(“y%d”,i)) in current scope multifile.C:61:

if i use “TGraph(n,x1,y1)” for testing, i get a graph in each canvas, but its just a big “X” with a weird scale…hum


You can use something like this:

[code]Double_t x[files][lines], y[files][lines], z[files][lines];


chaos[i] = new TGraph(n, x[i], y[i]);
Cheers, Bertrand.

I just reworked my script and yep, it works! Thanks you VERY much! :slight_smile:

one little problem is left:
i draw to graphs with different scaling and want the right y-axis to be completely red. with “axis->SetLabelColor(kRed)” and “SetTitleColor” this works for the text and numbers, but not for the lines. when i try


(like in the twoscale example) i get:

*** Interpreter error recovered ***

is this a new feature in ROOT (i use version 5.17) or what’s my problem here?
multifile.C (2.37 KB)

I cannot reproduce the problem… could you post your macro, please?
– Bertrand.

my macro is attached above (i edited right after posting, you’re really quick :wink:), here is also one of the data files for reading (txt instead of tab due to upload restrictions)

if i make the line “rechtsY[i]->SetLineColor(kRed);” a comment, no error occurs
wic_1.txt (58.8 KB)

Hi wing,

Please use: rechtsY[i]->SetAxisColor(kRed);
TGraph::GetYaxis() returns a TAxis, not a TGAxis :wink:


ah, ok.
now my output looks exactly like i want it to, thanks!

You’re welcome! :slight_smile: