Create new TTree but get unexpected values

I create two TTree in one root file and assign some branches for trees.

myfile = ROOT.TFile.Open("3j3b.root","RECREATE")
mytree_sig = ROOT.TTree("sig","Signal")
mytree_bkg = ROOT.TTree("bkg","Background")

topFs = array.array('i',[0])
topFb = array.array('i',[0])
mytree_sig.Branch("jet_GBHInit_topHadronOriginFlag",topFs,"jet_GBHInit_topHadronOriginFlag_sig/I")
mytree_bkg.Branch("jet_GBHInit_topHadronOriginFlag",topFb,"jet_GBHInit_topHadronOriginFlag_bkg/I")
# I skipped codes of other branches that work well

for k in range(10000):
    topFb[0] = origin[k]
    topFs[0] = origin[k]
    # Here origin is a list of integers
    mytree_sig.Fill()
    mytree_bkg.Fill()
mytree_sig.SetDirectory(0)
mytree_bkg.SetDirectory(0)

myfile.cd()
mytree_sig.Write()
mytree_bkg.Write()
myfile.Close()

However, I finally got four TTree (two repeated trees) and weird values (should be integers) for the branch.


)
Does anyone know the reasons for them? Thank you in advance!

Happy Holiday!

If in your code above I replace origin[k] to just k I get the expected:

root [2] sig->Scan("jet_GBHInit_topHadronOriginFlag_sig")
************************
*    Row   * jet_GBHIn *
************************
*        0 *         0 *
*        1 *         1 *
*        2 *         2 *
*        3 *         3 *
*        4 *         4 *
*        5 *         5 *
*        6 *         6 *
*        7 *         7 *
*        8 *         8 *
*        9 *         9 *
*       10 *        10 *
*       11 *        11 *
*       12 *        12 *
*       13 *        13 *
*       14 *        14 *
*       15 *        15 *
*       16 *        16 *
*       17 *        17 *
*       18 *        18 *
*       19 *        19 *
*       20 *        20 *

So, check your values in origin[k]; if they are ok, try running only the lines in your example above and if those run well, then the problem should be in the lines you didn’t show above.

You’d better use the python array type code 'l' to define the ROOT TTree fundamental type "/I" (Int_t).

I print origin[k] just before writing them and nothing looks wrong.

Using ‘l’ instead keeps most values as integers, although some still are recorded as float values (but a different value!). It meets requirements of program anyway, so thank you!

Attach the output of: sig->Print(); bkg->Print();





Here I should have some ‘-99’ instead of ‘1.844e19’.
I noticed that ‘jet_GBHInit_topHadronOriginFlag’ has different basket number from other branches. Could it be a problem?

You have plenty "/F" (a 32 bit floating point Float_t) branches, one "/l" (a 64 bit unsigned integer ULong64_t) branch, and no any "/I" (a 32 bit signed integer Int_t) branches.

For all your branches, you need to match the ROOT TTree fundamental type and the python array type code.

The first 15 branches are expected to be filled by float values, so I use "/F" for them. Only the last branch is expected to be filled by integers. I used "/I" initially and it returned weird output. Didn’t you suggest to use "/l" instead of "/I" ?

Either:
b = array.array('l', [0]); t.Branch("b", b, "b/I")
or:
b = array.array('Q', [0]); t.Branch("b", b, "b/l")

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