Home | News | Documentation | Download

Insert 3 histograms in 1 Canvas

Hi ROOTers :evergreen_tree:,

I tried to write a code that fill, in a random way, 3 histograms and in a second time I need to draw this objects in ONLY one canvas.

How can I do this?

I created, for the moment, 3 canvas, I don’t receive any error but I see 3 canvas empty, but the histograms are correctly write in the file. Someone can help me, please?

{
	TCanvas *c1 = new TCanvas ("c1", "Distribuzione random gaus", 700, 500);
	TCanvas *c2 = new TCanvas ("c2", "Distribuzione random lineare", 700, 500);
	TCanvas *c3 = new TCanvas ("c3", "Distribuzione random landau", 700, 500);

	TFile *f= new TFile ("esercizio_esame.root", "recreate");

	

	TH1D *h_1 = new TH1D ("Istogramma Gauss", "Distribuzione random Gaus", 100, -20, -20);

	gRandom = new TRandom1();

	for(Int_t i=0; i<10000; i++){

		h_1->Fill(gRandom->Gaus(5,3));

	}

	c1->cd();

	h_1->Draw();

	TF1 *g = h_1->GetFunction("gaus");

	h_1->Fit("gaus");

	h_1->Write();

	TH1D *h_2 = new TH1D ("Istogramma Lineare", "Distribuzione random Lineare", 200, 0, 200);

	gRandom = new TRandom2();

	for(Int_t i=0; i<100000; i++){

		h_2->Fill(gRandom->Integer(200));

	}

	c2->cd();

	h_2->Draw();

	h_2->Write();

	TH1D *h_3 = new TH1D ("Istogramma Landau", "Distribuzione random Lineare", 50, 0, 80);

	gRandom = new TRandom3();

	for(Int_t i=0; i<100000; i++){

		h_3->Fill(gRandom->Landau(2));

	}

	c3->cd();

	h_3->Draw();

	h_3->Write();

	f->Close();

}

Thanks for all.


ROOT Version: 6.18
Platform: Xubuntu 19.04
Compiler: gcc version 7.3.0


In your macro above, use “DrawCopy” instead of “Draw”.

For the rest, see: ROOT Primer -> Histograms

1 Like

@Wile_E_Coyote Now i see the histograms h_2 and h_3 but not h_1.

Move the drawing command to after “Fit”.

1 Like

I read this topic : “Merge more than one canvas in one”

and i modified my code:

{	
	TCanvas *C = new TCanvas("C", "Main Canvas", 960, 1100);

    C->Divide(1,3);

	TCanvas *c1 = new TCanvas ("c1", "Distribuzione random gaus", 700, 500);
	TCanvas *c2 = new TCanvas ("c2", "Distribuzione random lineare", 700, 500);
	TCanvas *c3 = new TCanvas ("c3", "Distribuzione random landau", 700, 500);

	TFile *f= new TFile ("esercizio_esame.root", "recreate");

	

	TH1D *h_1 = new TH1D ("Istogramma Gauss", "Distribuzione random Gaus", 100, -20, -20);

	gRandom = new TRandom1();

	for(Int_t i=0; i<10000; i++){

		h_1->Fill(gRandom->Gaus(5,3));

	}

	TF1 *g = h_1->GetFunction("gaus");

	h_1->Fit("gaus");

	c1->cd();

	h_1->DrawCopy();

	h_1->Write();

	C->cd(1);
    c1->DrawClonePad();

	TH1D *h_2 = new TH1D ("Istogramma Lineare", "Distribuzione random Lineare", 200, 0, 200);

	gRandom = new TRandom2();

	for(Int_t i=0; i<100000; i++){

		h_2->Fill(gRandom->Integer(200));

	}

	c2->cd();

	h_2->DrawCopy();

	h_2->Write();

	C->cd(2);
    c2->DrawClonePad();

	TH1D *h_3 = new TH1D ("Istogramma Landau", "Distribuzione random Lineare", 50, 0, 80);

	gRandom = new TRandom3();

	for(Int_t i=0; i<100000; i++){

		h_3->Fill(gRandom->Landau(2));

	}

	c3->cd();

	h_3->DrawCopy();

	h_3->Write();

    C->cd(3);
    c3->DrawClonePad();


	f->Close();

}

Now I see 4, the 3 canvas that i created before (How can I un-draw them?) and the main canvas, but it was empty…

One quick solution is to move these lines:

  TFile *f= new TFile ("esercizio_esame.root", "recreate");
  h_1->Write();
  h_2->Write();
  h_3->Write();

to the bottom, after c3->DrawClonePad(); (with this, you can also use Draw() instead of DrawCopy()).
There are probably other ways, though; you can read this: Object Ownership

Thank you so much! Now works but i see other 3 canvas apart of the main canvas, how can i delete them?

You are saving those 3 canvases to the file, that’s why I kept them. Do you still want that? If you want to save them but then close them automatically you can check this https://root-forum.cern.ch/t/solved-canvas-wont-close/4310
Or describe more precisely what you need. If you only want the 3 plots in one canvas (“C”), then you need to remove all the things with c1, c2 and c3 and just draw h_1, h_2 and h_3 directly to C (of course keeping C->cd(1), etc…).

Perfect, now works perfectly, thanks to all! The final code, for the future readers, is:

{	
	TCanvas *C = new TCanvas("C", "Distribuzioni Random", 960, 1100);

    C->Divide(1,3);

	TCanvas *c1 = new TCanvas ("c1", "Distribuzione random gaus", 700, 500);
	TCanvas *c2 = new TCanvas ("c2", "Distribuzione random lineare", 700, 500);
	TCanvas *c3 = new TCanvas ("c3", "Distribuzione random landau", 700, 500);
	

	TH1D *h_1 = new TH1D ("Istogramma Gauss", "Distribuzione random Gaus", 100, -20, -20);

	gRandom = new TRandom1();

	for(Int_t i=0; i<10000; i++){

		h_1->Fill(gRandom->Gaus(5,3));

	}

	TF1 *g = h_1->GetFunction("gaus");

	h_1->Fit("gaus");

	c1->cd();

	h_1->Draw();

	C->cd(1);

    c1->DrawClonePad();

	TH1D *h_2 = new TH1D ("Istogramma Lineare", "Distribuzione random Lineare", 200, 0, 200);

	gRandom = new TRandom2();

	for(Int_t i=0; i<100000; i++){

		h_2->Fill(gRandom->Integer(200));

	}

	c2->cd();

	h_2->Draw();

	C->cd(2);

    c2->DrawClonePad();

	TH1D *h_3 = new TH1D ("Istogramma Landau", "Distribuzione random Landau", 50, -5, 50);

	gRandom = new TRandom3();

	for(Int_t i=0; i<100000; i++){

		h_3->Fill(gRandom->Landau(2));

	}

	c3->cd();

	h_3->Draw();

	C->cd(3);
    c3->DrawClonePad();

    TFile *f= new TFile ("esercizio_esame.root", "recreate");

	h_3->Write();
	h_2->Write();
	h_1->Write();

 	c1->Close();
 	c2->Close();
 	c3->Close();

	f->Close();

}