# Saving TTree as class member leads to I/O error

**URL:** <https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590>\
**Category:** ROOT\
**Tags:** tree, io\
**Created:** [October 3, 2023, 4:39pm UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590 "2023-10-03T16:39:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![werthm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/8c91f0/32.png) [@werthm](https://root-forum.cern.ch/u/werthm)\
**Post date:** [October 3, 2023, 4:39pm UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/1 "2023-10-03T16:39:45Z")

</div>

Hi there

I’m getting I/O errors and invalid data when I try saving the clone of a tree as a class member to a new ROOT file.

I have a DAQ system where I fill a tree stored in a temporary file (don’t want to store it in memory and want to enable online analysis, therefore using ‘tree-\>AutoSave(“SaveSelf”)’ from time to time). After the acquisition, I would like to save the tree in the final file, embedded in a storage class. Later on, when I read the tree via the storage class from the final file, I get I/O errors.

The attached minimal example inspired from [TTree as member variable](https://root-forum.cern.ch/t/ttree-as-member-variable/18147) shows the error on my ROOT v6-28-04 installation on RHEL 7:

```auto
.L breakingExample.C++
breakingExampleWrite();
breakingExampleRead();

```

[breakingExample.C](https://root-forum.cern.ch/uploads/short-url/ubIsQDDBGkOaT1YP3UPQ7mcowms.C) (1.6 KB)

Any ideas? Many thanks for your help!

Cheers,  
Dominik

---

<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:** [October 4, 2023, 8:44am UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/2 "2023-10-04T08:44:01Z")

</div>

What kind of errors do you get? Maybe @pcanal can help?

---

<div class="post-metadata">

**Author:** ![werthm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/8c91f0/32.png) [@werthm](https://root-forum.cern.ch/u/werthm)\
**Post date:** [October 4, 2023, 10:06am UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/3 "2023-10-04T10:06:15Z")

</div>

```auto
******************************************************************************
*Tree :tree : tree *
*Entries : 10000000 : Total = 160004375 bytes File Size = 59685844 *
* : : Tree compression factor = 2.07 *
******************************************************************************
*Branch :Time *
*Entries : 10000000 : BranchElement (see below) *
*............................................................................*
*Br 0 :fSec : Int_t *
*Entries : 10000000 : Total Size= 40001093 bytes File Size = 150123 *
*Baskets : 6 : Basket Size= 20556843 bytes Compression= 205.40 *
*............................................................................*
*Br 1 :fNanoSec : Int_t *
*Entries : 10000000 : Total Size= 40001141 bytes File Size = 13449913 *
*Baskets : 6 : Basket Size= 20556843 bytes Compression= 2.29 *
*............................................................................*
*Br 2 :Value : Value/D *
*Entries : 10000000 : Total Size= 80001185 bytes File Size = 46085808 *
*Baskets : 7 : Basket Size= 20556843 bytes Compression= 1.34 *
*............................................................................*
Error in <TTree::Show()>: Cannot read entry 22 (I/O error)
************************************************
* Row * Time.fSec * Time.fNan * Value.Val *
************************************************
* 0 * 1.696e+09 * 5699000 * 0 *
* 1 * 1.696e+09 * 5699000 * 0 *
* 2 * 1.696e+09 * 5699000 * 0 *
* 3 * 1.696e+09 * 5699000 * 0 *
* 4 * 1.696e+09 * 5699000 * 0 *
* 5 * 1.696e+09 * 5699000 * 0 *
* 6 * 1.696e+09 * 5699000 * 0 *
* 7 * 1.696e+09 * 5699000 * 0 *
* 8 * 1.696e+09 * 5699000 * 0 *
* 9 * 1.696e+09 * 5699000 * 0 *

```

etc.

---

<div class="post-metadata">

**Author:** ![werthm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/8c91f0/32.png) [@werthm](https://root-forum.cern.ch/u/werthm)\
**Post date:** [October 16, 2023, 8:45am UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/4 "2023-10-16T08:45:22Z")

</div>

Hi @pcanal, would be great if you could have a look at this! Many thanks!

---

<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:** [October 16, 2023, 4:18pm UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/5 "2023-10-16T16:18:49Z")

</div>

When reading a `TTree` indirectly from a file (your case), you need to reconnect the `TTree` to the file explicitly:

```auto
    auto stor = (storage*)file->Get("storage");
    stor->tree->SetDirectory(file);

```

You can also automate this behavior by extending the `storage` class by adding the function

```auto
  void DirectoryAutoAdd(TDirectory *dir) {
     if (tree)
       tree->SetDirectory(dir);
  }

```

which will be automatically called by `TFile` when reading object of the `storage` type.

---

<div class="post-metadata">

**Author:** ![werthm](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/8c91f0/32.png) [@werthm](https://root-forum.cern.ch/u/werthm)\
**Post date:** [October 18, 2023, 9:11am UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/6 "2023-10-18T09:11:44Z")

</div>

Great, it’s working, thanks a lot!

---

<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:** [November 1, 2023, 9:11am UTC](https://root-forum.cern.ch/t/saving-ttree-as-class-member-leads-to-i-o-error/56590/7 "2023-11-01T09:11:47Z")

</div>

Closed on November 1, 2023, 9:11am UTC

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