# How to save histograms in a loop

**URL:** <https://root-forum.cern.ch/t/how-to-save-histograms-in-a-loop/47210>\
**Category:** Newbie\
**Created:** [October 12, 2021, 9:44pm UTC](https://root-forum.cern.ch/t/how-to-save-histograms-in-a-loop/47210 "2021-10-12T21:44:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulan](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/p/b77776/32.png) [@paulan](https://root-forum.cern.ch/u/paulan)\
**Post date:** [October 12, 2021, 9:44pm UTC](https://root-forum.cern.ch/t/how-to-save-histograms-in-a-loop/47210/1 "2021-10-12T21:44:47Z")

</div>

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-

```auto
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

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [October 13, 2021, 3:55am UTC](https://root-forum.cern.ch/t/how-to-save-histograms-in-a-loop/47210/2 "2021-10-13T03:55:34Z")

</div>

You can create a new [TCanvas](https://root.cern/doc/master/classTCanvas.html) 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.
