How to detect bad TTreeFormula?

Hello,

I am looking for a way to detect that a TTreeFormula contains a mistake. If I construct a TTreeFormula and it cannot be compiled properly, ROOT prints an Error, but there doesn’t seem to be a way for me to programmatically check that error. I had tried using

TTreeFormula tf("tf","BadFormula",tree);
int isBad = tf.Compile("BadFormula");

which would return “1” if the formula was bad, but that seemed to put the TTreeFormula in a broken state (it wouldn’t evaluate properly).
So I was hoping there was a better way to detect the problem.

Thanks
Will

Hi Will,

If the formula compilation failed (during the construction) then:tf.GetNdim();will return zero.

Cheers,
Philippe.

Hi Phillipe,

Indeed, I was using that (sorry I should have said), but I’ve found cases where that isn’t true. In particular:

TTree t;
std::vector<float> b;
t.Branch("b",&b);
TTreeFormula f("f","b[c]",&t);

That’s a bad formula, but f.GetNdim returns 1.

Indeed, the error is not propagate from the inner formula (“c”) to the outer formula which thinks it is valid :frowning:
(For example see the result of t.Scan(“b[c]”) … [For this to show something, you will need to add a name to the TTree, add data to the input and Fill the tree]:

[code]root [0] TTree t(“T”,“T”);
root [1] std::vector b;
root [2] t.Branch(“b”,&b);
root [3] b.push_back(1);
root [4] b.push_back(2);
root [5] b.push_back(0);
root [6] t.Fill()
(Int_t) 22
root [7] t.Fill()
(Int_t) 22
root [8] t.Fill()
(Int_t) 22
root [9] t.Scan(“b”)
Reading symbols for shared libraries ++++++ done


  • Row * Instance * b *

  •    0 *        0 *         1 *
    
  •    0 *        1 *         2 *
    
  •    0 *        2 *         0 *
    
  •    1 *        0 *         1 *
    
  •    1 *        1 *         2 *
    
  •    1 *        2 *         0 *
    
  •    2 *        0 *         1 *
    
  •    2 *        1 *         2 *
    
  •    2 *        2 *         0 *
    

(Long64_t) 9
root [10] t.Scan(“b[0]”)


  • Row * b[0] *

  •    0 *         1 *
    
  •    1 *         1 *
    
  •    2 *         1 *
    

(Long64_t) 3
root [11] t.Scan(“b[c]”)
Error in TTreeFormula::Compile: Bad numerical expression : “c”


  • Row * b[c] *

  •    0 *         1 *
    
  •    1 *         1 *
    
  •    2 *         1 *
    

(Long64_t) 3
root [12] t.Scan(“b2[c]”)
Error in TTreeFormula::Compile: Bad numerical expression : “b2[c]”


  • Row * b2[c] *

  •    0 *           *
    
  •    1 *           *
    
  •    2 *           *
    

(Long64_t) 3
[/code].

i.e. this is a bug and the information is somewhat ‘lost’. We would need to update TTreeFormula to make it work correctly.

Cheers,
Philippe.

I’ve opened a ticket at sft.its.cern.ch/jira/browse/ROOT-8218

Until it’s fixed, I found this workaround:

TTreeFormula* t = new TTreeFormula("t","BadFormula",tree);
if(t->Compile()) {
	delete t; //was a bad formula
} else {
	delete t;
	t = new TTreeFormula("t","BadFormula",tree);
}

It’s weird, I found I had to remake the TTreeFormula because after calling Compile, the formula stopped working correctly for me.

The problem in now solved in the master, v5.34 and v6.06 patch branches.

Cheers,
Philippe.

Thank you!