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

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:

from ROOT import *
import sys

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

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

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
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')
    i += 1
    if i % 100 == 0: print(i)
    if i == 1000: break


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:

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)

#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

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




will lead to a more efficient file organization.

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

Note that the pattern:

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).

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

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

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?

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

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

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

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