scikit-hep / root_numpy

The interface between ROOT and NumPy

Home Page:http://scikit-hep.org/root_numpy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Warning in TTree::SetEntries when exporting large array with array2root

tempse opened this issue · comments

Dear developers,

Executing the code

import numpy as np
import root_numpy

a = np.random.random(100000000)
a = np.array(a.astype(np.float64), dtype=[('rand', np.float64)])
root_numpy.array2root(a, 'root_numpy_test.root', 'tree', 'recreate')

returns the following warning: Warning in <TTree::SetEntries>: Tree branches have different numbers of entries, with 100000000 maximum.
However, if I create a numpy array of smaller size (e.g., a = np.random.random(99999999)), I don't get any warnings.

I am using Ubuntu 16.04 LTS, Python 3.4.5, numpy 1.11.3, ROOT 6.04/03, and root-numpy 4.4.0.

Can you explain this behavior? At the first glance my resulting tree looks fine, but I need to export more than 100M entries for my real physics analysis and I am wondering whether I should worry about this warning now.

Thank you!

Hi @tempse - this actually looks like a ROOT warning, rather than a root_numpy problem. In particular, when I look through https://root.cern.ch/root/html512/src/TTree.cxx.html, it appears

// if n >= 0 Set number of entries in the Tree = n.
//
// if (n < 0) Set number of entries in the Tree to match the
// number of entries in each branch. (default for n is -1)
// This function should be called only when one fills each branch
// independently via TBranch::Fill without calling TTree::Fill
// Calling TTree::SetEntries() make sense only if the number of entries
// in each branch is identical. A Warning is issued otherwise.
// The function returns the number of entries.

and this indicates that one of the branches has less entries than the others, but you don't get this unless you try to get all of them.... @ndawe I suspect it's from the call at this line here:

# Need to update the number of entries in the tree to match
# the number in the branches since each branch is filled separately.
tree.SetEntries(-1)

I've seen that warning before and I'm aware that's from the call to SetEntries. I'd suggest ignoring the warning. You of course don't have branches with different lengths because you only have one branch.
It's a harmless ROOT bug that should probably be fixed. Looking at the ROOT source:

https://github.com/root-project/root/blob/4cac5a12f98eebc39e9b9888ab6b11b40cddf09d/tree/tree/src/TTree.cxx#L8491

   // case 2; compute the number of entries from the number of entries in the branches
   TBranch * b;
   Long64_t nMin = 99999999;
   Long64_t nMax = 0;
   TIter next(GetListOfBranches());
   while((b = (TBranch*)next())){
      Long64_t n = b->GetEntries();
      if (n < nMin) nMin = n;
      if (n > nMax) nMax = n;
   }
   if (nMin != nMax) {
      Warning("SetEntries","Tree branches have different numbers of entries, with %lld maximum.",nMax);
   }
   fEntries = nMax;
   return fEntries;

It's because of the line Long64_t nMin = 99999999;. They shouldn't be computing the min and max this way. This should be reported to the ROOT team.

The output TTree you created should be fine and this warning looks harmless.

I'll close this issue once the problem is reported on ROOT's JIRA

Thank you for your quick response, @kratsg and @ndawe! The min/max calculation implementation on the side of ROOT seems to explain the problem. As suggested, I will ignore the warning for now.

I opened a JIRA ticket, which you can find here: https://sft.its.cern.ch/jira/browse/ROOT-8824

Thanks @tempse!