# TFile::Write() vs. Flush() -- which makes file more usable?

**URL:** <https://root-forum.cern.ch/t/tfile-write-vs-flush-which-makes-file-more-usable/35356>\
**Category:** ROOT\
**Tags:** io\
**Created:** [July 25, 2019, 11:19pm UTC](https://root-forum.cern.ch/t/tfile-write-vs-flush-which-makes-file-more-usable/35356 "2019-07-25T23:19:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Kelsey](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/michael_kelsey/32/16014_2.png) [@Michael\_Kelsey](https://root-forum.cern.ch/u/Michael_Kelsey)\
**Post date:** [July 25, 2019, 11:19pm UTC](https://root-forum.cern.ch/t/tfile-write-vs-flush-which-makes-file-more-usable/35356/1 "2019-07-25T23:19:10Z")

</div>

We write out fairly large ROOT files from simulation jobs. If the jobs end successfully, there’s no problem (obviously), but if a job crashes or is aborted early, quite often the ROOT file is unusable.

I thought that I could use TFile::Flush() at the end of each event to ensure that the the file on disk was as close to usable as possible. But if I end a job early, the resulting ROOT file is as empty as before I introduced the Flush() call.

Should I be using TFile::Write() for this purpose? If I do this on every event (essentially short-circuiting the buffers), should I call it with the kOverwrite or kWriteDelete option?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 26, 2019, 2:12pm UTC](https://root-forum.cern.ch/t/tfile-write-vs-flush-which-makes-file-more-usable/35356/2 "2019-07-26T14:12:52Z")

</div>

Hi @Michael_Kelsey,  
Yes, `TFile::Write()` takes care of writing your object to a file. If you don’t care about keeping different versions of the objects, but just want to rewrite it up to the current state in the loop, you could use the `kOverwrite` option. If what you are trying to write to the file is a `TTree`, I suggest you take a look also at [`TTree::SetAutoSave`](https://root.cern.ch/doc/master/classTTree.html#a583aa74b14c7ac7147a7bb4efbd5fbc5). This [section](https://root.cern.ch/root/htmldoc/guides/users-guide/InputOutput.html#the-logical-root-file-tfile-and-tkey) of the user guide will give you a broader context on the subject as well.  
Hope this helps,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 9, 2019, 2:18pm UTC](https://root-forum.cern.ch/t/tfile-write-vs-flush-which-makes-file-more-usable/35356/3 "2019-08-09T14:18:19Z")

</div>

Closed on August 9, 2019, 2:18pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
