Problem when filling tree from Pythia

I am trying to fill a ntuple from Pythia data. The problem is that the generated data does not correspond with the data saved in the ntuple. When I read the data in the tree, some strange numbers appears close to zeros like 10E-320. Pythia data types are <class ‘float’>, and I am filling the tree with this code:

# Creating file
file = TFile.Open("FileName, "recreate")

# Tree
tree = TTree("tree", "tree")
i      = 0

for i in range(0, len(pythia.event.size())):

    # Data from Pythia
    phi_i = np.array(pythia.event[i].phi())

    # Creating branch in the first iteration
    if i ==0: tree.Branch("phi", phi_i, "phi/F")

    # Filling
    tree.Fill()
    
    i+=1


# Writing tree
tree.Write()

# Writing file
file.Write()

file.Close()

I have checked the phi values in the loop and they are fine, the problem arises when filling the tree.

Someone know which is the problem?

Cheers,
Imanol


Please read tips for efficient and successful posting and posting code

_ROOT Version: 6.26/00
Platform: Not Provided
Compiler: Not Provided


If phi_i is an array of fixed size N, you need to save it also as array in the tree, e.g.

tree.Branch("phi", phi_i, "phi[N]/F")

As far as I know, it is a float, as I pointed in the descripcion of the error. When I print the type of the variable it shows this: <class ‘float’>

you have

If it is a simple number, why not do simply phi_i = pythia.event[i].phi()?

Oh, yeah. If I don’t do that it gives an error:

TypeError: Template method resolution failed:
  none of the 8 overloaded methods succeeded. Full details:
  int TTree::Branch(TList* list, int bufsize = 32000, int splitlevel = 99) =>
    TypeError: could not convert argument 1
  TBranch* TTree::Branch(const char* name, char* address, const char* leaflist, int bufsize = 32000) =>
    TypeError: could not convert argument 2 (could not convert argument to buffer or nullptr)
  TBranch* TTree::Branch(const char* name, int address, const char* leaflist, int bufsize = 32000) =>
    TypeError: could not convert argument 2 (int/long conversion expects an integer object)
  int TTree::Branch(TCollection* list, int bufsize = 32000, int splitlevel = 99, const char* name = "") =>
    TypeError: could not convert argument 1
  int TTree::Branch(const char* folder, int bufsize = 32000, int splitlevel = 99) =>
    TypeError: could not convert argument 2 (int/long conversion expects an integer object)
  TBranch* TTree::Branch(const char* name, long address, const char* leaflist, int bufsize = 32000) =>
    TypeError: could not convert argument 2 (int/long conversion expects an integer object)
  TBranch* TTree::Branch(const char* name, void* address, const char* leaflist, int bufsize = 32000) =>
    TypeError: could not convert argument 2
  TBranch* TTree::Branch(const char* name, const char* classname, void* addobj, int bufsize = 32000, int splitlevel = 99) =>
    TypeError: could not convert argument 2 (bad argument type for built-in operation)
  Failed to instantiate "Branch(std::string,double,std::string)"
´´´

Maybe I am not using well the method ? In fact in the https://root.cern.ch/doc/master/classTTree.html#addcolumnoffundamentaltypes, the array syntax is the recomended one

Indeed, the second argument in Branch has to point to the variable; in C++, you’d do &Phi_i or simply the name of an array, which is the pointer, but in Python you use an array of 1 element; the example you link to says

# Basic type branch (float) - use array of length 1
n = array('f', [ 1.5 ])
t.Branch('floatb', n, 'floatb/F')

So, if pythia.event[i].phi() is a float, try

phi_i = np.array( 'f', [pythia.event[i].phi()] )

keeping tree.Branch("phi", phi_i, "phi/F")

I have tried

phi_i = array('f', [1.5])
tree.Branch('phi', phi_i, 'phi/F')

And the result is this:

Some good values but a lot of non-sense ones

import ROOT
from array import array

# Creating file
file = ROOT.TFile.Open("aa.root", "recreate")

# Tree
tree = ROOT.TTree("tree", "tree")
phi_i = array('f', [0])
tree.Branch("phi", phi_i, "phi/F")

for i in range(0, len(pythia.event.size())):
    phi_i[0] = pythia.event[i].phi()
    # Filling
    tree.Fill()    

# Writing file
file.Write()

file.Close()

Yeah, now It seems to work.
So the trick is to predefine the variable with a random value at the beggining and redefine it later? I don’t understand why.

You were creating a new array at every iteration of the loop, but only the address of the first one was associated with the branch.

ok, i see. So to avoid this you redefine the value of the array linked to the tree at every iteration instead of creating a new array right?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.