Pyroot function to create a TTree with Raw data

I have created this function in pyroot turn my raw data into a ROOT Tree. ChanNames is a list with all the channels available in my detector. I dont know why but I am only saving the real data from the first event in the first channel. In the rest, the branches for each channel are filled with zeros or with a constant value. Not the real data. I don’t what could be wrong

def create_tree(filepath, chanNames, det,nevents):
    myreader = rawio.RawDataReader(filepath)
    events = myreader.read_events(output_format = 2,
                                  skip_empty = True,
                                  nb_events = nevents,
                                  #dump_nums=[1,2,3,4,5,6,7,8,9,10],
                                  trigger_types = [1],
                                  detector_nums = [det],
                                  channel_names = chanNames)

    sampling_interval = 1./625.

    t = TTree('content_events', '')
    baseline = {}
    std_bl = {}
    bcontent={}
    for chan in chanNames:

        # Defining the TTree branches using python arrays of length = 1
        bcontent[chan]=array('f', [0.0])
        baseline[chan] = array('f', [0.0])
        std_bl[chan] = array('f', [0.0])
        
        t.Branch('bcontent_'+chan, bcontent[chan],'bcontent_'+chan+'\F') 
    print('Start!')
    print('')
    for id in range(len(events)):
        if id%1000 == 0 and id > 0:
            print('Event', id)
        traces = events[id]['Z'+str(det)]

        for chan in chanNames:

            # Filling the "baseline" and "std_bl" lists
            bl = []
            time = np.arange(0, len(traces[chan])*sampling_interval, sampling_interval)
            i=0
            bcontent[chan][0]=0
            while time[i] <= 23:
                i+=1
                bl.append(traces[chan][i])   
            baseline[chan][0] = float(np.mean(bl))
            std_bl[chan][0] = float(np.std(bl))
            #print(chan+' baseline= ', baseline[chan][0])
            for j in range(len(time)):
                bcontent[chan][0]=float(traces[chan][j]-baseline[chan][0])
                t.Fill()

    f = TFile('content_events.root', 'recreate')
    t.Write()
    
            
    return f

may be @pcanal and @vpadulan can help ?

In the meantime, a couple of observations:
1 - Make sure that the values you are passing to the tree are what you expect, i.e., print bcontent[chan][0] just before doing t.Fill() and see if it looks ok.
2 - You should only do t.Fill() once per event, filling values for all branches (chan) at once. Now you are filling in a loop inside another loop over the branches, so if you have e.g. 5 branches, the tree has 5*(Nevents) entries, instead of Nevents (and since chan is not changing in the Fill loop, the branches that are not changing in that loop just keep filling with their last entry value).

Dear @RAFALPZN ,

Thanks for reaching out to the forum! Let me stres this point in particular by @dastudillo which is very important

Once you report back after fixing this we can analyse your situation in more detail.

Also, out of curiosity:

        bcontent[chan]=array('f', [0.0])
        baseline[chan] = array('f', [0.0])
        std_bl[chan] = array('f', [0.0])
        
        t.Branch('bcontent_'+chan, bcontent[chan],'bcontent_'+chan+'\F') 

Here it looks like you are preparing for three branches, but you only call t.Branch once, is this intentional?

Cheers,
Vincenzo