Segfault when using TTreeFormula on arrays

Dear experts,

I’m facing a segfault when trying to evaluate a TTreeFormula on an array variable.

Here’s a test script to create two files, each with a TTree containing a few entries with two array branches of 5 elements (I’ve tried both arrays and std.vectors, hence the commented lines, the conclusion is the same):

[code]import ROOT as R
from array import array

for iFile in range(2):
f = R.TFile(“testWrite_{}.root”.format(iFile), “recreate”)
t = R.TTree(“t”, “t”)

var1 = array("i", [0]*5)
t.Branch("var1", var1, "var1[5]/I")
var2 = array("i", [0]*5)
t.Branch("var2", var2, "var1[5]/I")

#var1 = R.std.vector(float)()
#t.Branch("var1", var1)
#var2 = R.std.vector(float)()
#t.Branch("var2", var2)

for i in xrange(20):
    for j in xrange(5):
        var1[j] = j
        var2[j] = 2*j
        #var1.push_back(j)
        #var2.push_back(2*j)
    t.Fill()
    #var1.clear()
    #var2.clear()

t.Write()
f.Close()[/code]

Up to now, no problems, the TTree looks as it should. Now, I try to access those variables using TTreeFormulas:

[code]import ROOT as R

t = R.TChain(“t”)

t.Add(“testWrite_0.root”)
t.Add(“testWrite_1.root”)

form1 = R.TTreeFormula(“form1”, “var1”, t)
t.SetNotify(form1)

form2 = R.TTreeFormula(“form2”, “var2”, t)
t.SetNotify(form2)

for i in xrange(t.GetEntries()):
t.GetEntry(i)

print "## Variable 1"
print "Length: {}".format(len(t.var1))
print "Ndata: {}".format(form1.GetNdata())
print "var1[1]: {}".format(t.var1[1])
print "var1[1] from formula: {}".format(form1.EvalInstance(1))

print "## Variable 2"
print "Length: {}".format(len(t.var2))
print "Ndata: {}".format(form2.GetNdata())
print "var2[1]: {}".format(t.var2[1])
print "var2[1] from formula: {}".format(form2.EvalInstance(1))[/code]

And I get a segfault when EvalInstance() is called for the first time. The weird thing is, the segfault appears only when at least two files in the TChain and two formulas are used (e.g. one file and two formulas is fine, so is two files and one formula). You can check it easily by commenting out the corresponding lines.

I’ve tried the same in C++ using the code below. And still the weird behaviour “2 files && 2 formulas”.

[code]#include
#include “TChain.h”
#include “TTreeFormula.h”

int main(void){
TChain t(“t”);

t.Add(“testWrite_0.root”);
t.Add(“testWrite_1.root”);

TTreeFormula form1(“form1”, “var1”, &t);
t.SetNotify(&form1);
TTreeFormula form2(“form2”, “var2”, &t);
t.SetNotify(&form2);

for(int i=0; i<t.GetEntries(); ++i){
t.GetEntry(i);

std::cout << std::endl << "Entry " << i << std::endl;

std::cout << "## Variable 1" << std::endl;
std::cout << "form1 NData: " << form1.GetNdata() << std::endl;
std::cout << "form1 var1[1]: " << form1.EvalInstance(1) << std::endl;

std::cout << "## Variable 2" << std::endl;
std::cout << "form2 NData: " << form2.GetNdata() << std::endl;
std::cout << "form2 var2[1]: " << form2.EvalInstance(1) << std::endl;

}

return 0;
}[/code]

Am I doing something wrong or is this a real bug? I’ve tried it on ROOT 6.04.00, 6.02.05 and 5.34.09.

Cheers,
Sebastien

PS: using the examples above the segfault is clearly caused by EvalInstance(), and GetNdata() always runs fine, but in some other code of mine it’s GetNdata() that seems to cause the problem in the “2 files && 2 formulas” situation. I don’t understand what makes it different from the example here, if I figure it out I’ll post it here…

Bump… No one?