Order of adding files to TChain affects result

Dear all,

today I stumbled about this most confusing bug in my code. The order in which I add files to the TChain apparently affects the result I am going to get.

First of all, I have files with 3 trees each: A, B and C
To read in the files I use the following construct:

[code]template
class VariableReader {
public:
VariableReader() :
input(), variable(0), variableName("") {

}

VariableReader(TChainPointer chain, TString varName) :
    input(chain), variable(0), variableName(varName) {

}

~VariableReader() {
}

const variableType& getVariable() {
    return variable;
}

void initialise() {
    if (doesVariableExist()) {
        enableVariable();
        readVariableFromInput();
    } else
        throw VariableNotFoundException("Variable '" + variableName + "' was not found.");
}

bool doesVariableExist() {
    return input->GetBranch(variableName) != NULL;
}

protected:
TChainPointer input;
variableType variable;
TString variableName;

void readVariableFromInput() {
    input->SetBranchAddress(variableName, &variable);
}

void enableVariable() {
    input->SetBranchStatus(variableName, true);
}

};[/code]

This checks if the variable I am looking for exists, enabled the TBranch in the tree and sets the address of the branch to the private variable.

The way the TChains are initialised:

chainA = new TChain(A); chainB = new TChain(B); chainC = new TChain(C); VariableReader<double> reader = new VariableReader<double>(chainB, "myVar"); chainA->AddFriend(chainB); chainA->AddFriend(chainC);

Add the files:

numberOfFiles += chainA->Add(fileName); chainB->Add(fileName); chainC->Add(fileName);

Finally I loop over all entries:

while(chainA->LoadTree(currentEventEntry) >= 0){ chainA->GetEntry(currentEventEntry); //read variable }

I am then counting how many times the variable is >0 (I initialise each variable with 0 in the VariableReader constructor). File1 gives me a result of 463 of 485.
If I now add another file (file2) I can have these results for file1 only (I can display results for each file):
file1, file2 => 463 of 485 for file1
file2, file1 => 485 of 485 for file1

Which means that only when file1 is first I get the right results. I’ve checked the file directly in the TBrowser, and indeed 463 is the right result.
For some reason all values from file 1 become > 0;

I have a feeling that I am doing something very wrong, but I have no idea where to continue to look.

Any hint would be appreciated.

EDIT: I tried ROOT versions 5.26.00b and 5.27.04

I’ve tried now to access the variable directly without using the VariableReader. Same result.

While trying I discovered another interesting fact:
if I call

before or after SetBranchAddress/Status, all the values of this variable are set to 0.
Other variables are not affected!

However, since most other variables are from chainA and not from chainB, I’ve tried

as well - no change.

Obviously something goes wrong with the read of the variable

solved
Once the ‘friendship’ between the TChains was resolved and “GetEntry” used for all the TChains separately the right number appeared.

It seems TChains don’t like to be friends…