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.
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')
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.