_ROOT Version: Binary distribution ROOT 6.12/06
_Platform: MacOS
I try to make TTreeformula work with TChain, and from https://root-forum.cern.ch/t/ttreeformula-and-tchain/15155 I got the information that this feature has already been implanted in root from v6.10. But I still don’t know how to make it work correctly.
Here is my codes for test:
void formula_test(){
TChain* chain = new TChain("tree");
chain->Add("tuple2.root");
chain->Add("tuple1.root");
//chain->Add("tuple_all.root/tree");
int nentries = chain->GetEntries();
//int nentries = 1500;
cout << "total entries: " << nentries <<endl;
TTreeFormula* formula = new TTreeFormula("test", "random", chain);
chain->SetNotify(formula);
for(int jentry=0; jentry<nentries; jentry++){
chain->LoadTree(jentry);
chain->GetEntry(jentry);
float value = formula->EvalInstance(0);
//float value = formula->EvalInstance();
if(jentry%100==0)
cout << jentry << ": "<< value << endl;
}
}
where I add two trees to a chain, and want to use TTreeformula to get value of specific branch in every entry.
If I don’t add “chain->SetNotify(formula);” after the definition of formula , the scripts will collapse at:
a). The first time to process “formula->EvalInstance(0)” , if using “int nentries = chain->GetEntries()”;
b). The first entry of the second tree to process “formula->EvalInstance(0)”, if not using “int nentries = chain->GetEntries()”.
Then, I add “chain->SetNotify(formula)”, no error occur, but I find the value gotten from the first tree is 0, which is definitely wrong.
By the way, I also tried the TTreeFormulaGroup method mentioned in https://root-forum.cern.ch/t/ttreeformula-and-tchain/15155, but the value from the first tree is 0, too!
Did I do something wrong? Or what can I do to make TTreeformula works well with TChain?