# Write TTree out to file seems to corrupt it

**URL:** <https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553>\
**Category:** Newbie\
**Created:** [June 25, 2021, 9:18am UTC](https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553 "2021-06-25T09:18:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ACalcaterra](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ACalcaterra](https://root-forum.cern.ch/u/ACalcaterra)\
**Post date:** [June 25, 2021, 9:18am UTC](https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553/1 "2021-06-25T09:18:43Z")

</div>

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ 5.34/36  
_Platform:_ cygwin  
_Compiler:_ 10.2.0

* * *

Hi, this is slightly embarrassing because I seem to hit something really basic that I should know, but clearly I do not. This is my example:

```auto
void treeProblem() {

  // define my tree with 2 ints
  TTree *evnt = new TTree("eventData","event data");
  int DAQdms, SELdms;
  evnt->Branch("DAQdms",&DAQdms,"DAQdms/I");
  evnt->Branch("SELdms",&SELdms,"SELdms/I");

  DAQdms = 0;
  SELdms = 1;
  evnt->Fill(); // there is one entry in evnt

  TFile rootFile("pass1.root","recreate");
  evnt->Write(); // write this out on pass1.root
  rootFile.Close();

  DAQdms = 2;
  SELdms = 3;
  evnt->Fill(); // evnt has 2 entries now

  TFile rootFile("pass2.root","recreate");
  evnt->Write(); // write this out on pass2.root
  rootFile.Close();

}

```

The macro runs no problem and no errors. The data in pass1.root, when read out later, are correct. The data in pass2.root are corrupt.

If I comment all lines after the 1st evnt-\>Write(), and fire up a TBrowser, double clicking the leaves already shows junk.

The sample might appear stupid, but what I am trying to do is run on events for a long time, saving the Ttree in a different file after every (say) 10000 events, and have the complete stat in the last file.

I could (I think) accumulate 10000 events, save them, delete the TTree, start fresh a new one and so on, using TChain to read back all the files, but this appears silly.

What am I doing wrong? Can I keep my TTree accumulating correctly after each Write()?  
Thank you!

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 28, 2021, 7:46am UTC](https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553/2 "2021-06-28T07:46:55Z")

</div>

Hello,

The problem might be related to the registering of the tree in the files, since you are trying to use the same tree to write to two different files.

@pcanal will know better what is happening here and how this could be done.

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [June 28, 2021, 8:57am UTC](https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553/3 "2021-06-28T08:57:45Z")

</div>

Also the first `Close` should have deleted the tree if the first file owns the tree, so the tree shouldn’t be used afterwards. Perhaps you can do this with two trees, one for each file, or just write one file per execution of the application and parameterize the name of the file and what should be written in it.

---

<div class="post-metadata">

**Author:** ![ACalcaterra](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/7ea924/32.png) [@ACalcaterra](https://root-forum.cern.ch/u/ACalcaterra)\
**Post date:** [June 28, 2021, 9:13am UTC](https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553/4 "2021-06-28T09:13:45Z")

</div>

Thank you for looking into this. The first rootFile.Close() does not seem to harm the tree, commenting that line does not alter the result.  
In the meantime, I have given up with the OneTree idea, now every 10000 events I just close the prev file, trash the prev tree, plant a new one with a similar name, grow its leaves and start again.  
It looks anyway cleaner since I can very easily TChain later all files when reading. And last file is not growing forever.  
Than you anyhow! Sandro

---

<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 28, 2021, 8:28pm UTC](https://root-forum.cern.ch/t/write-ttree-out-to-file-seems-to-corrupt-it/45553/5 "2021-06-28T20:28:03Z")

</div>

You could try using `TTree::ChangeFile` or the pattern:

```auto
TFile rootFile("pass1.root","recreate");
TTree *evnt = new TTree("eventData","event data");
...
loop
    evnt->Fill();
...
rootFile.Write();
evnt->SetDirectory(nullptr);
evnt->Reset();
rootFile.Close();
....
TFile rootFile2("pass2.root","recreate");
evnt->SetDirectory(&rootFile2);
...
loop
    evnt->Fill();
...
rootFile2.Write();
evnt->SetDirectory(nullptr);
evnt->Reset();
rootFile2.Close();

```
