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
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
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…).