# Large TFile containing only small TTree

**URL:** <https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398>\
**Category:** ROOT\
**Created:** [July 18, 2013, 1:08pm UTC](https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398 "2013-07-18T13:08:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arekfu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arekfu/32/27537_2.png) [@arekfu](https://root-forum.cern.ch/u/arekfu)\
**Post date:** [July 18, 2013, 1:08pm UTC](https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398/1 "2013-07-18T13:08:07Z")

</div>

Hi all,

I use two TTree objects to write a selection of events to a TFile as follows, in an attempt to save disk space. The procedure below follows my previous question at [url][Applying a cut when filling a TTree](https://root-forum.cern.ch/t/applying-a-cut-when-filling-a-ttree/15865/1)

First I create a `tree' and a`bufferTree’:

`TTree *tree = new TTree("tree","tree name");
// definition of tree branches here
TTree *bufferTree = tree->CloneTree();`I add rows to `bufferTree’:

`bufferTree->Fill();`From time to time, I copy events satisfying some condition over to `tree' and I reset`bufferTree’:

`TTree *tempTree = bufferTree->CopyTree(condition);
tree->CopyEntries(tempTree);
delete tempTree;
bufferTree->Reset();`Finally, I periodically AutoSave `tree’ to a TFile.

This works, in the sense that the ROOT file contains only one tree (`tree’) with the events that I have selected. However, the size of the ROOT file is practically the same as what I would get if I just wrote all my events to the file directly. That’s unfortunate, because I’m jumping through all these hoops precisely because I would like to save some disk space. How can I get a normal TFile size?

Regards,  
Davide

---

<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:** [July 18, 2013, 3:04pm UTC](https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398/2 "2013-07-18T15:04:01Z")

</div>

Hi Davide,

Well, I supose it depends on your condition and the way you save the tree… I just tried with making a selection on a random tree, and it works. Selecting half the events (see snapshot below) produce a half size file…

` t = gRandom->Gaus(10,5);
   bufferTree->Fill();
   [...]
   TTree *tempTree = bufferTree->CopyTree("t>10.0");
`

Cheers, bertrand.

---

<div class="post-metadata">

**Author:** ![arekfu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arekfu/32/27537_2.png) [@arekfu](https://root-forum.cern.ch/u/arekfu)\
**Post date:** [July 18, 2013, 4:03pm UTC](https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398/3 "2013-07-18T16:03:21Z")

</div>

Hi Bertrand,

Thank you for looking at this. I have come up with a complete minimal example that illustrates my problem. On my machine (running ROOT 5.34 as packaged in Debian) the program results in a 1.9-MB ROOT file containing a tree with 127 events. If I separately generate a 127-event tree, the file size is only ~8 kB. Can you please try it?

Cheers,  
Davide  
[test.cc](https://root-forum.cern.ch/uploads/default/original/2X/0/0d5d2cf98aa6b32549736488641e15dc72561994.cc) (519 Bytes)

---

<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:** [July 19, 2013, 7:28am UTC](https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398/4 "2013-07-19T07:28:18Z")

</div>

Hi,

Simply add `t->SetDirectory(0);` to “disconnect” the original tree from the file, or you can also do it this way (at the end of your loop): ` TFile *f = new TFile("test.root","RECREATE");
  f->WriteObject(tc, "tc");
  f->Close();
`  
And BTW, Use either tc-\>Write() or f-\>Write() (not both)

Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![arekfu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/arekfu/32/27537_2.png) [@arekfu](https://root-forum.cern.ch/u/arekfu)\
**Post date:** [July 19, 2013, 8:55am UTC](https://root-forum.cern.ch/t/large-tfile-containing-only-small-ttree/16398/5 "2013-07-19T08:55:25Z")

</div>

Hi Bertrand,

Great, that works! Thank you!

Cheers,  
Davide
