Pyroot fix

exmple .dat file:
1 1 3 1.9717 181 189 2 179 2 187 0.0000
2212 0.000 0.000 -99.9956 0.940 -2.47 -7.71 -0.20 0.20
2112 0.000 0.000 99.9956 0.940 7.71 -3.14 0.20 0.20
2212 0.000 0.000 -99.9956 0.940 -6.52 2.13 -0.20 0.20

I want to create all the branches in a single tree. But there are two loop (loop under loop). for that i am not able to fill the branches separately. How to do it. please fix it. (M.Sc student, INDIA)
code: given link
pyroot version (latest/stable v6-30-04)

CODE:

import ROOT
import numpy as np
no_e=100
with open(‘ampt.dat’, ‘r’) as ff, ROOT.TFile.Open(“need.root”, “RECREATE”) as outFile:
# Creating TTree, branches in root file
tree = ROOT.TTree(“tree”, “tree”)

# C structure to hold event and particle variables
# cmd = "struct tree_str {float event; float test_no; int multiplicity; float Impact_p; float Z_posi; float Z_neg; float proj_elastic_n; float proj_Inelastic_n; float tar_elastic_n; float tar_Inelastic_n; float R_p;};"
cmd = "struct tree_str {float event; float test_no; float multiplicity; float Impact_p; float Z_posi; float Z_neg; float proj_elastic_n; float proj_Inelastic_n; float tar_elastic_n; float tar_Inelastic_n; float R_p; float p_id; float px; float py; float pz; float mass; float x; float y; float z; float t;};"
ROOT.gROOT.ProcessLine(cmd)
tree_out = ROOT.tree_str()


# EVENT
tree.Branch("event", ROOT.addressof(tree_out, "event"), "event/F")
tree.Branch("test_no", ROOT.addressof(tree_out, "test_no"), "test_no/F")
tree.Branch("multiplicity", ROOT.addressof(tree_out, "multiplicity"), "multiplicity/I")
tree.Branch("Impact_p", ROOT.addressof(tree_out, "Impact_p"), "Impact_p/F")
tree.Branch("Z_posi", ROOT.addressof(tree_out, "Z_posi"), "Z_posi/F")
tree.Branch("Z_neg", ROOT.addressof(tree_out, "Z_neg"), "Z_neg/F")
tree.Branch("proj_elastic_n", ROOT.addressof(tree_out, "proj_elastic_n"), "proj_elastic_n/F")
tree.Branch("proj_Inelastic_n", ROOT.addressof(tree_out, "proj_Inelastic_n"), "proj_Inelastic_n/F")
tree.Branch("tar_elastic_n", ROOT.addressof(tree_out, "tar_elastic_n"), "tar_elastic_n/F")
tree.Branch("tar_Inelastic_n", ROOT.addressof(tree_out, "tar_Inelastic_n"), "tar_Inelastic_n/F")
tree.Branch("R_p", ROOT.addressof(tree_out, "R_p"), "R_p/F")


# Dynamically allocate memory for arrays based on multiplicity
tree.Branch("p_id", ROOT.addressof(tree_out, "p_id"), "p_id/F")
tree.Branch("px", ROOT.addressof(tree_out, "px"), "px/F")
tree.Branch("py", ROOT.addressof(tree_out, "py"), "py/F")
tree.Branch("pz", ROOT.addressof(tree_out, "pz"), "pz/F")
tree.Branch("mass", ROOT.addressof(tree_out, "mass"), "mass/F")
tree.Branch("x", ROOT.addressof(tree_out, "x"), "x/F")
tree.Branch("y", ROOT.addressof(tree_out, "y"), "y/F")
tree.Branch("z", ROOT.addressof(tree_out, "z"), "z/F")
tree.Branch("t", ROOT.addressof(tree_out, "t"), "t/F")
    

for i in np.arange(no_e):
    line = ff.readline()
    if not line:
        break  # Exit the loop if there are no more lines
    data = line.split()

    # Event data
    tree_out.event = float(data[0])
    tree_out.test_no = float(data[1])
    tree_out.multiplicity = int(data[2])
    tree_out.Impact_p = float(data[3])
    tree_out.Z_posi = float(data[4])
    tree_out.Z_neg = float(data[5])
    tree_out.proj_elastic_n = float(data[6])
    tree_out.proj_Inelastic_n = float(data[7])
    tree_out.tar_elastic_n = float(data[8])
    tree_out.tar_Inelastic_n = float(data[9])
    tree_out.R_p = float(data[10])
    n = tree_out.multiplicity
    print(tree_out.event, "___", tree_out.multiplicity)
    tree.Fill()

    # Particle data
    for j in np.arange(n):
        line = ff.readline()
        if not line:
            break  # Exit the loop if there are no more lines
        particle_data = line.split()

        tree_out.p_id = float(particle_data[0])
        tree_out.px = float(particle_data[1])
        tree_out.py = float(particle_data[2])
        tree_out.pz = float(particle_data[3])
        tree_out.mass = float(particle_data[4])
        tree_out.x = float(particle_data[5])
        tree_out.y = float(particle_data[6])
        tree_out.z = float(particle_data[7])
        tree_out.t = float(particle_data[8])
        tree.Fill()
        
tree.Write()

print(“--------------”)
print(“–COMPLETED–”)
print(“--------------”)

Hi,

Welcome to the ROOT community.
I would propose a few links which can be of help:

Those should be enough to help you carry out your task!
I hope this helps.

Cheers,
Danilo