TMultiGraph add graphs from directories

Hi,

i have transferred the bin values of histograms into graphes , now i’m trying to merge those graphes.
Is it possible to add graphes to a multigraph from different root files?

for example: i have 3 root files like output_bla_1.root … output_bla_3.root . in every of these root files is a graph i’d like to add to my multigraph.

Thanks

K.

...
   TFile *f1 = new TFile ("f1.root");
   TGraph *g1 = (TGraph*)f1->Get("my_g1");

   TFile *f2 = new TFile ("f2.root");
   TGraph *g2 = (TGraph*)f2->Get("my_g2");

 ....

  TMultiGraph *mg = new TMultiGraph();
  mg->Add(g1);
  mg->Add(g2);

.....

thank you:)

now lets say i have like 1000 files in one directory with names i cannot use for a loop. Is it possible to extract the graph of all files inside a directory?

Thanks
K.

You will have to loop over the files.
Do the files have some naming convention ?

ok the looping over all files works but i always get an error in

TMultiGraph::Add(TGraph*, char const*)

[code]int multi(){

TMultiGraph *mg = new TMultiGraph();
mg->SetName(“mg”);
mg->SetTitle(“multi”);

for(int i=1;i<20;i++){
char output[20];
char fname[6];
char gname[6];
sprintf(output,“output_[%d]0_[%d]9.root”,i);
sprintf(fname ,“f[%d]”,i);
sprintf(gname,“g[%d]”,i);

 TFile*fname = new TFile(output);
 TGraph *gname = (TGraph*)fname->Get("gr");
 mg->Add(gname,"graph");

}

 mg->Draw();

mg->Write();

}[/code]

You define “fname” and “gname” twice. Once as a “char fname[6];” / “char gname[6];” and then as a “TFile *fname” / “TGraph *gname” (and there are no such drawing options like “graph”, so you cannot use them in the “mg->Add(…);” call).

Try to replace the lines: TFile*fname = new TFile(output); TGraph *gname = (TGraph*)fname->Get("gr"); mg->Add(gname,"graph"); with: TFile *f = new TFile(output); TGraph *g; f->GetObject("gr", g); if (g) { g->SetName(gname); mg->Add(g); } else printf("Warning: graph %d not found.", i); delete f;
In general, always try to pre-compile your macros with ACLiC, so that the c++ compiler checks your source code.

Thanks, now it works:)