Separate axis when dividing a canvas


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.24/06
Platform: Not Provided
Compiler: Not Provided


Hi everyone,

I am having trouble plotting histograms with a specified range. To give some context I am plotting the same histogram on a canvas divided in two parts. Left part where I want the histogram to be plotted within a set of bin lets call it range 1 and the right part the same histogram but in range 2.

I wrote this code

for(Int_t i = 1; i <= 3; i++){
		TCanvas *c = new TCanvas(TString::Format("h%d", i), TString::Format("h%d", i), 1400, 600);
		c->Divide(2, 1);
		c->cd(1);
		h[i]->GetXaxis()->SetRange(h[i]->FindFixBin(TETRA_thr_inf[i]), h[i]->FindFixBin(TETRA_thr_sup[i]));
		h[i]->Draw();
		
		c->cd(2);
		hbis[i]->GetXaxis()->SetRange(hbis[i]->FindFixBin(TETRA_thr_inf2[i]), hbis[i]->FindFixBin(TETRA_thr_sup[i]));
		hbis[i]->Draw();
		}

As you can see I already tried to have the same histograms in two different variables called h and hbis. Notice the different ranges (inf, sup) and (inf2, sup). When executing my code ROOT will show me only the last range called for both pads in my canvas.

How do I solve that ?

Thanks for reading me !
Marius

You should make a clone of your histogram.

1 Like

Thanks it works !

I share my code if someone have that problem

for(Int_t i = 1; i <= 3; i++){
		TCanvas *c = new TCanvas(TString::Format("h%d", i), TString::Format("h%d", i), 1400, 600);
		c->Divide(2, 1);
		c->cd(1);
		h[i]->GetXaxis()->SetRange(h[i]->FindFixBin(TETRA_thr_inf[i]), h[i]->FindFixBin(TETRA_thr_sup[i]));
		h[i]->Draw();
		h2[i]->Draw("SAME");
		h2[i]->SetLineColor(kRed);
		
		c->cd(2);
		hbis[i] = (TH1F*)h[i]->Clone();
		hbis[i]->GetXaxis()->SetRange(hbis[i]->FindFixBin(TETRA_thr_inf2[i]), hbis[i]->FindFixBin(TETRA_thr_sup[i]));
		hbis[i]->Draw();
		h2bis[i] = (TH1F*)h2[i]->Clone();
		h2bis[i]->Draw("SAME");
		h2bis[i]->SetLineColor(kRed);
		}