# Error Writing ROOT File?

**URL:** <https://root-forum.cern.ch/t/error-writing-root-file/30118>\
**Category:** ROOT\
**Created:** [August 4, 2018, 4:00pm UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118 "2018-08-04T16:00:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![RootGuy305](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootguy305/32/20779_2.png) [@RootGuy305](https://root-forum.cern.ch/u/RootGuy305)\
**Post date:** [August 4, 2018, 4:00pm UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/1 "2018-08-04T16:00:19Z")

</div>

* * *

_ROOT Version:_ 6.13/02  
_Platform:_ Ubuntu 17.10  
_Compiler:_ Not Provided

* * *

I am having trouble writing a ROOT file. This problem only just appeared, and I have written ROOT files before in this manner. A small test macro is attached. When I write the file, it produces a file with two branches/trees of different number of entries, even though I am not trying to write more than one branch/tree.

void ClCut ()  
{  
// Define Cuts //  
TCut timing = “abs(delvt)\<1.002&&abs(stVp-vtime)\<1.002&&abs(stVp2-vtime)\<1.002”;  
TCut targetFiducial = “sqrt(x_x+y_y)\<2.0&&z\>-110&&z\<-70”;  
TCut knockoutsAndFiducial = “fid1p1&&fid1p2&&tofkop1==1&&tofkop2==1”;  
TCut mehCut = timing&&knockoutsAndFiducial&&targetFiducial;

```
// Import root files as trees //
TFile *f = new TFile("~/root/datastuff/newData/WillsData0817.root","READ"); // Original data
TTree* bb = (TTree*)f->Get("pp;1");
TFile *ff = new TFile("~/root/datastuff/preRecon.root","RECREATE"); // Output file: pre-reconstruction file
TTree* cc = bb->CopyTree(mehCut); // Copies old data tree with target, timing, and fiducial cuts applied.

cc->Write("pp");
ff->Close();
f->Close();

```

} ![Screenshot%20from%202018-08-04%2012-01-45](https://root-forum.cern.ch/uploads/default/original/3X/d/a/daf27156400d89971929f89295901058659ee87b.png)

 ![Screenshot%20from%202018-08-04%2012-03-48](https://root-forum.cern.ch/uploads/default/original/3X/a/2/a21c2a1030be3f224a12386dab5ecd3c811dcb81.png)

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [August 4, 2018, 9:39pm UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/2 "2018-08-04T21:39:14Z")

</div>

Two suspicious things in your code:  
a) no error checking at all! Avoid Get, use GetObject instead and **check the result**.  
b) the “pp;1”: are you really really really sure you want the first cycle? Otherwise, just omit the “;1” and you will get the full tree.

Unrelated to your problem: create a new File with the static function `TFile::Open(...)` and free the object using `delete`. `Close` does not delete the object, but `delete` automatically closes the file.

---

<div class="post-metadata">

**Author:** ![RootGuy305](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootguy305/32/20779_2.png) [@RootGuy305](https://root-forum.cern.ch/u/RootGuy305)\
**Post date:** [August 5, 2018, 12:09am UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/3 "2018-08-05T00:09:25Z")

</div>

I used GetObject instead of Get, and I omitted the ;1 to get the entire tree. However, I am still getting the same error message. The generated/overwritten file preRecon.root still contains the two /cycles/ (not “branches”), and the two cycles do not have the same number of entries. Any reason two cycles would be generated with different numbers of entries?

 ![Screenshot%20from%202018-08-04%2020-08-56](https://root-forum.cern.ch/uploads/default/original/3X/a/3/a3ed2e6fbc3565b35b86f6291dc2b29c0cfedcbb.png)

---

<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:** [August 5, 2018, 4:08am UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/4 "2018-08-05T04:08:18Z")

</div>

> [@Aux;1 aux;2 aux3;](https://root-forum.cern.ch/t/aux-1-aux-2-aux3/13345/2):
>
> Search for “cycle” and/or “namecycle”, for example in: In a Nutshell: [Input and Output](https://root.cern.ch/d/input-and-output) . ROOT User’s Guide: [Input/Output](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#inputoutput) : [Retrieving Objects from Disk](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#retrieving-objects-from-disk) , [Subdirectories and Navigation](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#subdirectories-and-navigation) . Just for reference: [some additional notes about GetObject, Get, FindObjectAny, FindKeyAny, ReadObj, ReadObjAny](https://root-forum.cern.ch/t/broken-tdirectoryfile-findobjectany-and-tdirectoryfile-findkeyany/20943/16) .

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [August 5, 2018, 7:39am UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/5 "2018-08-05T07:39:30Z")

</div>

The error message suggests something is wrong with memory management, it can for example be caused by `free`ing an object more than once. So my next best guess is your WillsData0817.root. Could that already be corrupt? Can you share it?

---

<div class="post-metadata">

**Author:** ![RootGuy305](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootguy305/32/20779_2.png) [@RootGuy305](https://root-forum.cern.ch/u/RootGuy305)\
**Post date:** [August 5, 2018, 3:16pm UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/6 "2018-08-05T15:16:00Z")

</div>

The data file is 1.5 GB large, so it would take quite a while to upload. How can I check if it’s corrupt? Perhaps there is an issue with CopyTree?

---

<div class="post-metadata">

**Author:** ![RootGuy305](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rootguy305/32/20779_2.png) [@RootGuy305](https://root-forum.cern.ch/u/RootGuy305)\
**Post date:** [August 9, 2018, 12:05am UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/7 "2018-08-09T00:05:04Z")

</div>

The error does not come up if I use CopyTree("") i.e. no cuts. I confirmed the tree is the same as from WillsData0817. Is there an alternate to CopyTree?

UPDATE: I saved the new data file, preRecon.root, with the same tree as from WillsData0817. Then made preRecon2.root and saved the tree from preRecon.root with the cuts I was trying to apply. Oddly enough it worked with no issues. It seems I only get the error when trying to use CopyTree to copy selections of WillsData0817 to a new file.

---

<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 23, 2018, 12:05am UTC](https://root-forum.cern.ch/t/error-writing-root-file/30118/8 "2018-08-23T00:05:08Z")

</div>

Closed on August 23, 2018, 12:05am UTC

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