# Save Histograms with date/time appended to filename

**URL:** <https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [August 10, 2021, 3:23pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261 "2021-08-10T15:23:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan/32/24087_2.png) [@Taylor\_Carnahan](https://root-forum.cern.ch/u/Taylor_Carnahan)\
**Post date:** [August 10, 2021, 3:23pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/1 "2021-08-10T15:23:41Z")

</div>

Hello everyone;

This is probably more of a coding question than anything, but I’m attempting to save a histogram with the local date & time appended to the output filename. I’ve been trying to play with things like strftime with parameters ("%Y%m%d\_%H%M%S") or TDatime to no avail. Here’s a part of the code where I think the format of date/time should be inserted:

-----after filling histogram—

c1-\>Update();  
slope\_hist-\>Draw();  
c1-\>SaveAs(“slope” + (date/time string??)\*\* + “.png”);  
c1-\>Close()

\*\*is this legal to insert a string for the date/time here?

---

<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:** [August 10, 2021, 4:10pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/2 "2021-08-10T16:10:45Z")

</div>

> [@Taylor\_Carnahan](#):
>
> \*\*is this legal to insert a string for the date/time here?

It takes `const char *`. You can use for example an array of char that you fill before doing SaveAs, or use Form directly inside SaveAs (e.g. `SaveAs(Form("%s...%s","slope",...,".png"))`.

---

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan/32/24087_2.png) [@Taylor\_Carnahan](https://root-forum.cern.ch/u/Taylor_Carnahan)\
**Post date:** [August 10, 2021, 5:38pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/3 "2021-08-10T17:38:10Z")

</div>

Hey @dastudillo, thank you for the info! I’m running into some errors, and I’m sure it’s due to me being new to coding.

If you have a free sec, I just have a few questions: I’d like to actually use the Form() directly inside SaveAs. Upon attempting several things, I’m finding that the %s…%s is the designator that tells the function we’re printing a string using the following strings separated by commas (I hope this is correct–this is what it seems like to me at least). The difficult part for me is figuring out exactly what to do in order to place a time character string inside this guy. This is what I’ve attempted, and the output is directly beneath:

Code:

time\_t t;  
struct tm \*tmp;  
char MY\_TIME[20];  
time( &t );  
tmp = localtime( &t );

strftime(MY\_TIME, 20, “%Y%m%d\_%H%M%S”, tmp);

c1-\>SaveAs(Form("%s %s",“slope\_v\_endcap\_”, MY\_TIME , “.png”));  
c1-\>Close();

Output:

slope\_v\_endcap\_20210810\_131643 (lacking .png)!

The warning I get is this:

warning: data argument not used by format string [-Wformat-extra-args] (i.e. pointing to the ‘’ before .png)

---

<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 10, 2021, 6:26pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/4 "2021-08-10T18:26:15Z")

</div>

`"%s%s%s"`

---

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan/32/24087_2.png) [@Taylor\_Carnahan](https://root-forum.cern.ch/u/Taylor_Carnahan)\
**Post date:** [August 10, 2021, 6:30pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/5 "2021-08-10T18:30:48Z")

</div>

I tried that but also got an error, @Wile_E_Coyote! I did however figure it out! Thank you guys!

I ended up doing this:

c1-\>SaveAs(Form("%s%s.png",“slope\_v\_endcap\_”, MY\_TIME));

Worked a treat!

---

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan/32/24087_2.png) [@Taylor\_Carnahan](https://root-forum.cern.ch/u/Taylor_Carnahan)\
**Post date:** [August 10, 2021, 6:33pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/6 "2021-08-10T18:33:33Z")

</div>

I have an extracurricular Q though, if you guys wouldn’t mind–is there a way to keep all of this formatting, but just introduce a directory header. In other words, saving this output file name exactly as it is but ensuring it makes it into a directory (or makes a directory if the directory doesn’t yet exist)?

---

<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 10, 2021, 6:42pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/7 "2021-08-10T18:42:49Z")

</div>

```cpp
const char *MY_DIR = "some/sub/directory";
gSystem->mkdir(MY_DIR, kTRUE);
c1->SaveAs(Form("%s/slope_v_endcap_%s.png", MY_DIR, MY_TIME));

```

---

<div class="post-metadata">

**Author:** ![Taylor\_Carnahan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/taylor_carnahan/32/24087_2.png) [@Taylor\_Carnahan](https://root-forum.cern.ch/u/Taylor_Carnahan)\
**Post date:** [August 10, 2021, 7:00pm UTC](https://root-forum.cern.ch/t/save-histograms-with-date-time-appended-to-filename/46261/8 "2021-08-10T19:00:39Z")

</div>

Worked perfectly–thank you so much; you guys have been super helpful!
