TCanvas not dividing

I am using root 5.34/38 (this is what is returned when I punch in gROOT->GetVersion() )

I have an array of 12 TGraphs called g and an array of 6 TMultiGraphs called mg. The goal is to plot two of the TGraphs in each of the 6 TMultiGraphs and plot all 6 multigraphs on a single page. I initialize the canvas with:

TMultiGraph* mg[6];
TGraph* g[12];
TCanvas *c1 = new TCanvas("c1","PSA Parameters",1800,2400);
c1->Divide(2,3);

Later, I make the TGraphs using methods that are not found in native ROOT (we have some custom software). The TGraphs are made just fine and are successfully stored in the array g.

To make all the multigraphs, I have:

for(int j = 0; j < 6; j++){
    c1->cd(j+1);
    mg[j] = new TMultiGraph();
    mg[j]->Add(g[j]);
    mg[j]->Add(g[j+6]);
    mg[j]->Draw("AP");
    mg[j]->SetTitle(Form("This is a dummy Title"));
}

gPad->BuildLegend();
gPad->SetGrid();
c1->SaveAs(Form("%s/Channel_%03d.pdf",strOutPath.Data(),ch));

However, when I plot this, I get a single TGraph in my pdf file, not 6. What am I missing?

As it si written your macro will save only the last multigraph in the pdf file: mg[5]

See if you do not “Draw” something in another place (note also that all “gPad” calls should go inside of the “for” loop).
Before “c1->SaveAs(...);” add “c1->cd(0);

Yes, I see that, but can you tell me why please?

I draw the individual TGraphs. That should be okay though, right?

It depends where you draw them. Maybe you “overwrite” your canvas (it may “delete” all subpads).

1 Like

you should produce the pdf files in the loop. A SaveAs after each Draw.

I dont want a separate PDF for each instance of the loop. I want to save a single file with 6 different plots on it

Make a multigraph containing the 6 plots draw it and save it as a pdf

That is also not what I want to do. I want 6 multigraphs, each of which contains 2 plots, displayed as a 3x2 grid on the canvas

Are you even reading my original post? that’s in line 4 of the code I posted. This is exactly why I’m having a problem.

Can you send a reproducer macro we can run ?

No I cannot because the plots I am making are using experiment-specific software that you will not be able to run. Please read my original post.

@couet What is the problem that you see in the original code in the first post?

Ok give me some time I will make a reproducer

Thank you very much.

Before each "g[i]->Draw("ap"); add “c1->cd(i + 1);” (otherwise you automatically “delete” all subpads).

Do not use “gPad = c1->cd(j+1);”, use simply “c1->cd(j + 1);” (“gPad” will automatically be updated).

1 Like

OHHHHHHHH that makes a lot of sense I shall try that now

I managed to make this work by simply removing the g[j]->Draw(“ap”) lines. Thank you both for helping me with this. My sincerest gratitude to both of you.

void kvetter()
{
   TMultiGraph* mg[6];
   TGraph* g[12];
   TCanvas *c1 = new TCanvas("c1","PSA Parameters",1800,2400);
   c1->Divide(2,3);

   for (int i = 0; i <12; i++) {
      g[i] = new TGraph();
      g[i]->SetPoint(0,i,i*i);
      g[i]->SetPoint(1,i,i*i+1);
      g[i]->SetMarkerStyle(20);
      g[i]->SetName(Form("Graph #%d",i));
   }

   for(int j = 0; j < 6; j++){
      c1->cd(j+1);
      mg[j] = new TMultiGraph();
      mg[j]->Add(g[j]);
      mg[j]->Add(g[j+6]);
      mg[j]->SetTitle(Form("This is a dummy Title #%d",j));
      mg[j]->Draw("AP");
      gPad->Update();
      gPad->SetGrid();
      gPad->BuildLegend();
   }

   c1->SaveAs("kvetter.pdf");
}