# How can I save a TMemFile to disk

**URL:** <https://root-forum.cern.ch/t/how-can-i-save-a-tmemfile-to-disk/62648>\
**Category:** Newbie\
**Created:** [January 13, 2025, 12:53pm UTC](https://root-forum.cern.ch/t/how-can-i-save-a-tmemfile-to-disk/62648 "2025-01-13T12:53:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/x/bbce88/32.png) [@Xin](https://root-forum.cern.ch/u/Xin)\
**Post date:** [January 13, 2025, 12:53pm UTC](https://root-forum.cern.ch/t/how-can-i-save-a-tmemfile-to-disk/62648/1 "2025-01-13T12:53:27Z")

</div>

* * *

How can I save a TMemFile to disk, what is the best way to do it

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 13, 2025, 1:50pm UTC](https://root-forum.cern.ch/t/how-can-i-save-a-tmemfile-to-disk/62648/2 "2025-01-13T13:50:02Z")

</div>

I think you can’t, but I might be wrong. Maybe @pcanal can tell if there is a way

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [January 13, 2025, 8:12pm UTC](https://root-forum.cern.ch/t/how-can-i-save-a-tmemfile-to-disk/62648/3 "2025-01-13T20:12:42Z")

</div>

What is the context? What feature lead you to choose using a `TMemFile` and then writing it to disk rather than using directly a `TFIle`? Are you merging the content from multiple `TMemFile` into a single file? (In the later case, the answer is to use the `TFileMerger` (or better yet to use the `TBufferMerger`).

The direct answer to the question is a bit convoluted since it was not envisioned as a use case:

```auto
TMemFile file(somename, "RECREATE");
... add stuff to the TMemFile and then Write and maybe Close it.
std::vector<std::byte> buffer( file.GetSize() );
file.CopyTo( buffer.data(), buffer.size() );

std::ofstream diskfile("somename.root", std::ios::out | std::ios::binary); // saving file
diskfile.write( buffer.data(), buffer.size() );
diskfile.close();

```

---

<div class="post-metadata">

**Author:** ![Xin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/x/bbce88/32.png) [@Xin](https://root-forum.cern.ch/u/Xin)\
**Post date:** [January 15, 2025, 5:58pm UTC](https://root-forum.cern.ch/t/how-can-i-save-a-tmemfile-to-disk/62648/4 "2025-01-15T17:58:03Z")

</div>

Thank you very much, I want to use jsroot + THttpServer to do the data monitor, the THttpServer example use TMemFile to construct histograms or graphs and refresh at a very high frequency. I also want to save it as normal root file for analysis at a low frequency.  
Thank you for the solution, seems intuitive, I will try it.
