How can I save a TMemFile to disk


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


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

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:

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();
1 Like

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.