# File corruption when adding new branch

**URL:** <https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843>\
**Category:** Newbie\
**Tags:** tree\
**Created:** [March 5, 2021, 1:10pm UTC](https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843 "2021-03-05T13:10:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatthieuD](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@MatthieuD](https://root-forum.cern.ch/u/MatthieuD)\
**Post date:** [March 5, 2021, 1:10pm UTC](https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843/1 "2021-03-05T13:10:07Z")

</div>

Hello everyone,

I’m new to root and I get an error when I try to add a Branch to my existing TTree.  
Everything works fine, but the first Entries seem to be corrupted while writing.  
I can’t figure out why.

Here is the very simple test code (based on ROOT Manual - Functional parts/Trees):

```auto
void TestFunc() {
    TFile f("ntuples.root", "update");
    Float_t new_v;
    auto data = f.Get<TTree>("data");
    auto newBranch = data->Branch("new_v", &new_v, "new_v/F");
    Int_t entries = data->GetEntries();
    for (int i = 0; i < entries; i++)
    {
    	new_v = gRandom->Gaus(0, 1);
        newBranch->Fill();
    }
    data->Write("", TObject::kOverwrite);
}

```

Here is the error when I perform `data->TTree::Scan();`

```auto
************************************************************************************************************
* Row * particleI * Ein.Ein * posX.posX * posY.posY * posZ.posZ * dirTheta. * dirPhi.di * s0.s0 *
************************************************************************************************************
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:0, badread=0, nerrors=1, basketnumber=0
* 0 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:1, badread=0, nerrors=2, basketnumber=0
* 1 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:2, badread=0, nerrors=3, basketnumber=0
* 2 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:3, badread=0, nerrors=4, basketnumber=0
* 3 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:4, badread=0, nerrors=5, basketnumber=0
* 4 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:5, badread=0, nerrors=6, basketnumber=0
* 5 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:6, badread=0, nerrors=7, basketnumber=0
* 6 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:7, badread=0, nerrors=8, basketnumber=0
* 7 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:8, badread=0, nerrors=9, basketnumber=0
* 8 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
 file probably overwritten: stopping reporting error messages
Error in <TBranch::GetBasket>: File: ntuples.root at byte:0, branch:particleID, entry:9, badread=0, nerrors=10, basketnumber=0
* 9 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 10 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 11 * 0 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 12 * 16 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 13 * 17 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 14 * 36 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 15 * 37 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 16 * 72 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 17 * 73 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 18 * 84 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 19 * 85 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 20 * 4 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 21 * 5 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 22 * 32 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 23 * 33 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
* 24 * 54 * 2000 * 0 * 0 * -0.965 * 0.1684121 * 0 * 0 *
Type <CR> to continue or q to quit ==>

```

I don’t understand how to solve the problem. I understand there was a problem when writing the file, but I don’t know how to fix it.

Many thanks for your help,

Matthieu

_ROOT Version:_ 6.22/06  
_Platform:_ Linux/Ubuntu

---

<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:** [March 5, 2021, 1:14pm UTC](https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843/2 "2021-03-05T13:14:59Z")

</div>

What software was used to create the original “ntuples.root” file?

---

<div class="post-metadata">

**Author:** ![MatthieuD](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@MatthieuD](https://root-forum.cern.ch/u/MatthieuD)\
**Post date:** [March 5, 2021, 1:19pm UTC](https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843/3 "2021-03-05T13:19:47Z")

</div>

Hi !

Geant4 v10.06.p02.

Here is the non-corrupted file:

[ntuples.root](https://root-forum.cern.ch/uploads/short-url/nOYcsmirYc2H7It6DolezavjyBd.root) (137.2 KB)

Edit: with MT mode ON.

---

<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:** [March 5, 2021, 1:21pm UTC](https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843/4 "2021-03-05T13:21:20Z")

</div>

[Complain to Geant4 developers.](https://root-forum.cern.ch/t/opening-a-file-in-update-mode-causes-error-in-tbasket-streamer-the-value-of-fnbytes-is-incorrect/43340/15)

---

<div class="post-metadata">

**Author:** ![MatthieuD](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@MatthieuD](https://root-forum.cern.ch/u/MatthieuD)\
**Post date:** [March 5, 2021, 1:24pm UTC](https://root-forum.cern.ch/t/file-corruption-when-adding-new-branch/43843/5 "2021-03-05T13:24:01Z")

</div>

I see!

Thanks a lot for the quick answer!
