# I need to write a new branch in already existing root file using pyroot

**URL:** <https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033>\
**Category:** Newbie\
**Tags:** tree, python, root\
**Created:** [January 9, 2023, 11:35am UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033 "2023-01-09T11:35:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anton.c](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anton.c](https://root-forum.cern.ch/u/anton.c)\
**Post date:** [January 9, 2023, 11:35am UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/1 "2023-01-09T11:35:04Z")

</div>

Hi, this is the first time i write on this forum, so i’m sorry if what i’m asking here could sound trivial for someone.  
In order to analyze a certain data that was acquired as root file, I need to make histograms of each data set and save the histogram sum in a new branch of the existing file. So i wrote this program using pyroot:

```auto
from ROOT import *
import sys

f = TFile(sys.argv[1], "UPDATE")

tree = f.Get("Data_R")
c = TCanvas()

tree.GetEntry(1)
h = TH1F( "h", "h", tree.Samples.fN, 0, tree.Samples.fN )

#add new branch to tree, connect histogram to this branch
#if hist.Maximum > 1750 then write to new branch
i=0
for event in tree:
    for j in range( event.Samples.fN ):
        h.SetBinContent(j+1, 2**(14) - event.Samples.fArray[j])
        if h.GetMaximum() > 1800:
           branch = tree.Branch( "Data_int", h, 'TH1F')
           branch.Fill()
    i += 1
    if i % 100 == 0: print(i)
    if i == 1000: break

branch.Write()

```

I’ve made a stop at 1000 entry just because i’m testing if the program work properly. In order to do so i’ve also wrote a program which should read the information i wrote on the root file and display the final histogram, but all i’ve got is a blank canvas. This is the second program:

```auto
from ROOT import *
import sys

f = TFile.Open( sys.argv[1], "READ")
hData = f.Get("Data_int")

entries = hData.GetEntries()

print("entries", entries)
c = TCanvas()

hData = TH1F("hData", "hData", 1, 0, 248)
hData.Draw()

#print( "Data", hData )

input("Press enter")

```

My question is: is it wrong the way I store the data in the root file or the way i’m tryin to visualize what i’ve wrote? (or both?)

_ROOT Version:_ 6.26.10  
_Platform:_ Not Linux (KDE Plasma)  
_Compiler:_ Konsole

* * *

---

<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:** [January 9, 2023, 6:01pm UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/2 "2023-01-09T18:01:32Z")

</div>

> [@anton.c](#):
>
> `branch = tree.Branch( "Data_int", h, 'TH1F')`

This must be called exactly once. Otherwise you are creating many branch with the same name.

```auto
branch.Fill()

```

using

```auto
branch.BackFill()

```

will lead to a more efficient file organization.

---

<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:** [January 9, 2023, 6:03pm UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/3 "2023-01-09T18:03:00Z")

</div>

> [@anton.c](#):
>
> ```auto
> hData = TH1F("hData", "hData", 1, 0, 248)
> hData.Draw()
> 
> ```

Create an empty histogram that is in no way connected to the content.

---

<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:** [January 9, 2023, 6:06pm UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/4 "2023-01-09T18:06:13Z")

</div>

Note that the pattern:

> [@anton.c](#):
>
> ```auto
> for event in tree:
> if h.GetMaximum() > 1800:
> branch.BackFill()
> 
> ```

is unlikely to be what you mean. Because of the conditional, this means that the entries in the rest of the `TTree` and this branch are unaligned. For example if the first time the 1800 is reached is for entry 10, when you call `GetEntry(0)` on the resulting file/tree, you will get entry 0 for all the other branch and the histogram corresponding to entry 10 (i.e. the first one filled).

---

<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:** [January 9, 2023, 6:07pm UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/5 "2023-01-09T18:07:41Z")

</div>

So the main higher level question is, why do you need :

> [@anton.c](#):
>
> save the histogram sum in a new branch of the existing file.

i.e. why does it need to be in a branch?

---

<div class="post-metadata">

**Author:** ![anton.c](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anton.c](https://root-forum.cern.ch/u/anton.c)\
**Post date:** [January 10, 2023, 9:07am UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/6 "2023-01-10T09:07:41Z")

</div>

Thank you for the reply and the accuracy. There’s another a thing I would like to ask you.  
I’ve yet tryied drawing the Histograms with this condition and it worked well (it only draws histograms with a maximum higher than 1800). Why with the Fill function it doesn’t work?

---

<div class="post-metadata">

**Author:** ![anton.c](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anton.c](https://root-forum.cern.ch/u/anton.c)\
**Post date:** [January 10, 2023, 9:09am UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/7 "2023-01-10T09:09:47Z")

</div>

I need to recall that histogrm later to make a PSD, because we are analyzing the decay of different sources.

---

<div class="post-metadata">

**Author:** ![anton.c](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anton.c](https://root-forum.cern.ch/u/anton.c)\
**Post date:** [January 10, 2023, 9:12am UTC](https://root-forum.cern.ch/t/i-need-to-write-a-new-branch-in-already-existing-root-file-using-pyroot/53033/8 "2023-01-10T09:12:41Z")

</div>

You’re right, I’m gonna remove the definition from the loop. Thank you!

> [@pcanal](#):
>
> will lead to a more efficient file organization.

I will also prove to use this way of filling the branch.

> [@pcanal](#):
>
> Create an empty histogram that is in no way connected to the content.

You’re right, I got to do things differently. Thank you for the patience.
