# TTree with Generic Object (nil return)

**URL:** <https://root-forum.cern.ch/t/ttree-with-generic-object-nil-return/37185>\
**Category:** Newbie\
**Created:** [December 13, 2019, 1:53am UTC](https://root-forum.cern.ch/t/ttree-with-generic-object-nil-return/37185 "2019-12-13T01:53:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![theyellowdog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/theyellowdog/32/20460_2.png) [@theyellowdog](https://root-forum.cern.ch/u/theyellowdog)\
**Post date:** [December 13, 2019, 1:53am UTC](https://root-forum.cern.ch/t/ttree-with-generic-object-nil-return/37185/1 "2019-12-13T01:53:20Z")

</div>

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

```auto
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

* * *

---

<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:** [December 13, 2019, 7:46pm UTC](https://root-forum.cern.ch/t/ttree-with-generic-object-nil-return/37185/2 "2019-12-13T19:46:02Z")

</div>

where/when do you get ‘nil’ objects?

---

<div class="post-metadata">

**Author:** ![theyellowdog](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/theyellowdog/32/20460_2.png) [@theyellowdog](https://root-forum.cern.ch/u/theyellowdog)\
**Post date:** [December 13, 2019, 10:01pm UTC](https://root-forum.cern.ch/t/ttree-with-generic-object-nil-return/37185/3 "2019-12-13T22:01:10Z")

</div>

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.
