Switching to and fro: multiple canvases

hello rooters,

how does one assign data to be plotted in several canvases. Say in this simple for loop

int filter = 20;
Double_t x,y;
TCanvas *c1 = new TCanvas();
TCanvas *c2 = new TCanvas();

for(int i = 0; i<someImax; i++) {
   x = 10.5*i;
   TGraph *g = new TGraph();
   TPolyLine3D *polyLine = new TPolyLine3D(filter);
   for (int j=0; j<someJmax; j++) {
       y =5.5*j;
       g->SetPoint(i, x,y)
       polyLine->SetPoint(i, i, x,y);
    }
   //then i want to draw g in c1 and polyLine in c2. Do i c1->Update() and c2->Update() each time? How do I switch from one canvas to another inside the loop?
   g->Draw("AL");
   polyLine->Draw();
  
}

Thanks

You need to “SomeCanvas->cd(SomePadNumber);” in order to draw something in a particular pad of a particular canvas (“SomePadNumber” = 0 if you don’t have any sub-pads, of course).

Thanks Wile. In this piece of code for instance, TGraph *g was drawn perfectly well in c1, TH3F *h3eff was drawn on c2, however with polyLine->Draw() in c2, nothing happened.

TCanvas *c1 = new TCanvas();
TCanvas *c2 = new TCanvas();
	
  TH3F *h3eff = new TH3F("heff", histName,size_goodChan, -0.5, size_goodChan+0.5, filter, 0., 5000000.,filter, 0., 1000000.);
  h3eff->SetStats(kFALSE);
  h3eff->SetFillColor(2);   
  c2->cd();
  h3eff->Draw();  
  
	M1:
	if(igoodChan>=0 && igoodChan< size_goodChan){
			
		TGraph *geff = new TGraph();
		TPolyLine3D *polyLine = new TPolyLine3D(filter);
		polyLine->SetLineColor(2);
				
		for (int ifilter =0; ifilter<filter; ifilter++) {			
			avPixCount = avCnt[ifilter][igoodChan];			
			iocData_elem = iocVector[ifilter];			
			geff->SetPoint(ifilter, iocData_elem, avPixCount);		
			polyLine -> SetPoint(ifilter, ichan, iocData_elem, avPixCount);			
		}	
		
		
		c1->cd();
		geff->Draw("ALP");		
		c1->Modified();		
		c1->Update();	
		
		c2->cd();		
		polyLine->Draw(); //problem here
		c2->Update();
						
		igoodChan++;			
		if (igoodChan<=size_goodChan)
			goto M1;
	}			

See, for example, the TPolyLine3D class description for an example how to draw it.
Note: the “axes-ranges” of your “polyLine” must be similar to the “axes-ranges” of your “h3eff” (which actually creates the “view” when drawn), otherwise you will not see your “polyLine” (it will be drawn “outside” of your canvas).
Note also: in your “M1: … goto M1;” loop you create “TGraph” and “TPolyLine3D” objects but you never delete them.

Try: [code]#include “TCanvas.h”
#include “TPad.h”
#include “TRandom.h”
#include “TH3.h”
#include “TPolyLine3D.h”

void trial(void) {
Int_t i, j;
Double_t x, y, z;

TCanvas *c1 = new TCanvas(“c1”);
c1 = c1; // get rid of the warning: unused variable ‘c1’

TH3F h3 = new TH3F(“h3”, “TH3 and TPolyLine3D”,
10, -2, 2, 10, -2, 2, 10, 0, 4);
for (i = 0; i < 10000; i++) {
gRandom->Rannor(x, y);
z = x
x + y*y;
h3->Fill(x, y, z);
} // for (i …

// c1->cd();
h3->Draw();
gPad->Modified(); gPad->Update();

Int_t n = 2;
TPolyLine3D *l = ((TPolyLine3D *)0);

for (j = 0; j < 100; j++) {
#if 0 /* 0 or 1 /
delete l; // delete the “previous” one
#endif /
0 or 1 */
l = new TPolyLine3D(n);
for (i = 0; i < n; i++) {
x = 2.0 * gRandom->Rndm();
y = 2.0 * gRandom->Rndm();
z = 2.0 * gRandom->Rndm();
l->SetPoint(i, x, y, z);
} // for (i …

// c1->cd();
l->Draw();
gPad->Modified(); gPad->Update();

} // for (j …
} // void trial(void) …[/code]

Hi Wile,
I present a similar inquiry that I have posted in the url below

https://root-forum.cern.ch/t/tgraph2d-setpoint-problems-inside-a-non-for-loop/15419/1

If I follow the example (for-loop inside a for loop) as you propose, i encounter busyflag error as far as my needs , well described in the link above…

for (j = 0; j < 100; j++) {
    l = new TPolyLine3D(n);
    for (i = 0; i < n; i++) {
      x = 2.0 * gRandom->Rndm();
      y = 2.0 * gRandom->Rndm();
      z = 2.0 * gRandom->Rndm();
      l->SetPoint(i, x, y, z);
    } 
    l->Draw();

So any suggestion is appreciated.