# Saving a TTree to a TFile every 100 ms

**URL:** <https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [June 15, 2021, 12:48pm UTC](https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407 "2021-06-15T12:48:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anno](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ed655f/32.png) [@Anno](https://root-forum.cern.ch/u/Anno)\
**Post date:** [June 15, 2021, 12:48pm UTC](https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407/1 "2021-06-15T12:48:37Z")

</div>

For our experiment multiple devices are read out every 100 ms, their data is transferred to some buffers and these are then filled into a TTree, at the moment a couple of thousand Double\_t per event. This TTree is then saved to a TFile, which is read in different instances for online and offline analysis.

The following simplified semi-code seems to work:

```auto
// For creating the tree
TFile* treeFile = new TFile(treeFilename, "RECREATE");
TTree* tree = new TTree("tree", "data tree");

// Adding some branches with the addresses of the relevant buffers

// Every time a new event comes in and all buffers have been updated

tree->Fill();
tree->Write(0, TObject::kWriteDelete);
treeFile->Flush();

```

Now when I open the file in another session:

```auto
TFile* dataTreeFile = new TFile(dataTreeFilename, "READ"); // Read the new tree file
dataTreeFile->ls();

if (dataTreeFile->GetListOfKeys()->Contains("tree")){ // If the treeFile contains a tree, and is not a zombie
TTree* tree = (TTree*)dataTreeFile->Get("tree"); // And get the new tree from the file
}

```

I get the warning that the tree had to be recovered as the file was not closed properly.

```auto
Warning in <TFile::Init>: file [filename] probably not closed, trying to recover
Info in <TFile::Recover>: [filename], recovered key TTree:tree at address [address]
Warning in <TFile::Init>: successfully recovered 1 keys
TFile** [filename]
 TFile* [filename]
  KEY: TTree tree;6133 data tree

```

This gives me the correct TTree, however the warning worries me a bit. I understand that it has to do with the not correctly closing the TFile, I have tried this before by adding

```auto
treeFile->Close();

```

But this slows down the system drastically up to the point here it can not keep up with the datastream, as it has to be reopened so often. And from being able to recover the TTree it seems to be that we are not limited by read or write speeds. What would be the proper way to do this?

Thanks in advance, Anno

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 15, 2021, 1:34pm UTC](https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407/2 "2021-06-15T13:34:24Z")

</div>

Hi Anno,  
@pcanal can correct me if I’m wrong, but I don’t think reading a TTree in a process while it’s being written in another process is supported. You might be able to make it work, but by design some information is written in the TTree footer when writing is done, and that will be missing if you read a TTree that’s still being written out. That’s basically what the warning you see is about.

I remember there was another thread on the forum about this usecase but I cannot find it back, we need Philippe.  
Cheers,  
Enrico

---

<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:** [June 15, 2021, 1:44pm UTC](https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407/3 "2021-06-15T13:44:54Z")

</div>

> [@Errors in remote client reading of TTree from TFile via xrootd while simultaneously updating TTree on server](https://root-forum.cern.ch/t/errors-in-remote-client-reading-of-ttree-from-tfile-via-xrootd-while-simultaneously-updating-ttree-on-server/43479/6):
>
> Reading from a TFile is that is being written to is supported to some extent. //////////////////////////////////////////////////////////////////////////////// /// Read the linked list of keys. /// /// Every directory has a linked list (fKeys). This linked list has been /// written on the file via WriteKeys as a single data record. /// /// It is interesting to call this function in the following situation. /// Assume another process1 is connecting this directory in Update mode /// - Process1 i…

> [@Read histogram from TFile while actively writing to it (with example)](https://root-forum.cern.ch/t/read-histogram-from-tfile-while-actively-writing-to-it-with-example/28273/12):
>
> @pcanal : I determined the cause of the segfault was calling Draw() on the TH1I pointer that was still NULL. I realize now my issue with delete file-\>FindObject(name); file-\>ReadKeys(); These commands only work if you have loaded the ROOT file that is actively being written. This does not work if you have opened an old file of the same name. When the file is RECREATEd, I now have two working methods of reading a file from disk while a process is actively writing to it. Below…

> [@TTree and Thread compatibility](https://root-forum.cern.ch/t/ttree-and-thread-compatibility/6050/2):
>
> See an example in the two files fw.C and fr.C in attachment. Although not using threads you should be able to pick the idea. Rene [fr.C](https://root-forum.cern.ch/uploads/default/original/2X/d/d6abdc54cdef2162a1ec24fad82ef8af9bafa16f.C) (845 Bytes) [fw.C](https://root-forum.cern.ch/uploads/default/original/2X/f/f8f16e73c9c140f6fed4ae5e4a6af6f80c93a3d7.C) (1.83 KB)

---

<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:** [June 15, 2021, 1:51pm UTC](https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407/4 "2021-06-15T13:51:17Z")

</div>

> [@eguiraud](#):
>
> but I don’t think reading a TTree in a process while it’s being written in another process is supported.

It is. See the thread Wile’s forward for example.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [June 15, 2021, 1:54pm UTC](https://root-forum.cern.ch/t/saving-a-ttree-to-a-tfile-every-100-ms/45407/5 "2021-06-15T13:54:33Z")

</div>

Ok yes it’s possible but you might run in a bunch of weird effects, especially if you don’t lock writing while reading 👍
