Home | News | Documentation | Download

How to save histograms in a loop

Hi, I am running a code where I produce multiple histograms in loop, for different cuts of the same branch. However, only the last histogram gets displayed in TCanvas. How can I display and save all the histograms that I produce (preferably a single histogram in a single canvas)?
Here’s how my code looks like-

hist = new TH1D ("hist1","Plot", numBins,lowLim,highLim);
t = new TChain ("...");

branch = ("lifetimeformula>>hist1").c_str();

if (cut1) {
t->Draw(branch, cutsfor1)
}

if (cut2) {
t->Draw(branch, cutsfor2)
}

and so on.

ROOT Version: 6.24/02

You can create a new TCanvas for each histogram (e.g. inside each if (cut)... statement). Or create an array of canvases.
If you create the canvases before the if statements (e.g. as canvas[i]), you just have to cd to them (canvas[i]->cd();) before drawing each histogram [i] on them.