# Using a variable from a loop when saving a histogram

**URL:** <https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286>\
**Category:** Newbie\
**Created:** [August 23, 2022, 9:03am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286 "2022-08-23T09:03:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/71e660/32.png) [@Ramin](https://root-forum.cern.ch/u/Ramin)\
**Post date:** [August 23, 2022, 9:03am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/1 "2022-08-23T09:03:04Z")

</div>

Hello, everyone.

I have the following situation:

A for loop where I vary a certain variable which contains another loop where I fill the histogram. Then I would like to store the histogram (by means of SaveAs). As I will have many of these histograms due to a variation of the parameter in my first loop, I wanted to add the value of the this variable in the name to be saved. How does one do this?

```auto
for (Int_t a = 0; a <11; a ++ ){
for (){
//Here the histogram is being filled.
}
//Saving the histogram containing the current value of a.
}

```

\_ROOT Version: 6.26/02  
\_Platform: Ubuntu 22.04.1 LTS  
\_Compiler: 11.2.0

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 23, 2022, 9:12am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/2 "2022-08-23T09:12:42Z")

</div>

“SaveAs” what (e.g., a “.root” file, a “.C++” macro file)?  
“name to be saved” of what (the histogram itself or the file)?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [August 23, 2022, 9:26am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/3 "2022-08-23T09:26:15Z")

</div>

You need to define the name (of whatever) using `Form()`.  
There are many examples on this forum.

---

<div class="post-metadata">

**Author:** ![Ramin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/71e660/32.png) [@Ramin](https://root-forum.cern.ch/u/Ramin)\
**Post date:** [August 23, 2022, 9:48am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/4 "2022-08-23T09:48:12Z")

</div>

Hello, Wile\_E\_Coyte.

I would like to save my histogram as png, so something of the form:

```auto
c1->SaveAs("InvMass.png");

```

With kind regards,  
Ramin

---

<div class="post-metadata">

**Author:** ![Ramin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/71e660/32.png) [@Ramin](https://root-forum.cern.ch/u/Ramin)\
**Post date:** [August 23, 2022, 9:49am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/5 "2022-08-23T09:49:54Z")

</div>

Hello,

Thank you very much. I will take a look.

With kind regards,  
Ramin

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [August 23, 2022, 9:51am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/6 "2022-08-23T09:51:14Z")

</div>

`c1->SaveAs(Form("InvMass.%d.png", a));`

---

<div class="post-metadata">

**Author:** ![Ramin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/71e660/32.png) [@Ramin](https://root-forum.cern.ch/u/Ramin)\
**Post date:** [August 23, 2022, 9:59am UTC](https://root-forum.cern.ch/t/using-a-variable-from-a-loop-when-saving-a-histogram/51286/7 "2022-08-23T09:59:00Z")

</div>

Hello, Wile\_E\_Coyte.

That worked like a charm. Thank you very much.

With kind regards,  
Ramin
