TTree with Generic Object (nil return)

I have root file with already tree object and want to add another one.

filename_tree = "ttree_example.root"

# Update the file with a new tree but without destroy previous data
ftree2=ROOT.TFile(filename_tree,"UPDATE")

# Here create the new TTree
tree2=ROOT.TTree("tree_vector","Tree with complex objects")

# Declare the variables to be assigned to the TTree: a 4-dimensional 
# vector mimicking tracks
pt_vec= ROOT.std.vector(float)()
eta_vec= ROOT.std.vector(float)()
phi_vec= ROOT.std.vector(float)()
mass_vec= ROOT.std.vector(float)()

# Declare a helper list
vec_list= [ pt_vec, zhi_vec, eta_vec, mass_vec ]

# Assign those variables to the TTree branches, remember use TTree.Branch method
tree2.Branch('pt', pt_vec)
tree2.Branch('phi',phi_vec)
tree2.Branch('eta', eta_vec)
tree2.Branch('mass', mass_vec)

N=128    
# Create N-random pions 
mass=0.13957
pt=np.random.exponential(10,N)
eta=np.random.uniform(-3,3,N)
phi=np.random.uniform(0,2.0*np.pi,N)
# How many pions per event (20 in average)
npart = np.random.poisson(20,N)


# Fill the TTree
for i in xrange(N):
    # clear the vectors and prepare them (use reserve) for the next iteration
    # IMPORTANT!! Clear the vectors, otherwise you are storing all the accumulated info
    pt_vec.clear()
    eta_vec.clear()
    phi_vec.clear()
    mass_vec.clear()
    pt_vec.reserve(npart[i])
    eta_vec.reserve(npart[i])
    phi_vec.reserve(npart[i])
    mass_vec.reserve(npart[i])
    # Store each particle in the vector
    for j in xrange(npart[i]):
        pt_vec.push_back(pt[i])
        eta_vec.push_back(eta[i])
        phi_vec.push_back(phi[i])
        mass_vec.push_back(mass)
    # fill the tree
    tree2.Fill()

# Here write the ntuple on the file and close the file
tree2.Write()
ftree2.Close()

But this tree doesn’t store in my component I get “nil” object. Can any tell me where I had made a mistake?


ROOT Version: Not Provided
Platform: Python
Compiler: Not Provided


where/when do you get ‘nil’ objects?

1 Like

Sorry my bad, the code i wrote is working fine . I made a mistake during t=f.Get() and that’s why I get a nil return.