Draw a function of a variable in a compiled code

Dear Rooters,
I would like to draw a function of a variable defined in a tree.
I see here [1] that it should be possible.
The following code [2] is a dummy example.
If I do it in an interactive way it works, but if I try to compile it it fails (in execution time):

Error in <TTreeFormula::Compile>:  Bad numerical expression : "func(a)"

Do I miss something?

Thanks,
Andrea

[1] TTree::Draw, weight with a function?

[2]
// c++ root-config --cflags --glibs test.cpp -o test.exe

#include "TTree.h"

double func(double x){
 return 10 * x;
}

int main (){
 TTree tree("tree","tree");
 double a;
 tree.Branch("a",&a,"a/D");
 
 for (int i=0; i<1000; i++) {
  a = i;
  tree.Fill();
 }

 tree.Draw("a * func(a)");
}

Hi,

For TTree::Draw to know about your function (func) you must generate a dictionary for it.

Cheers,
Philippe.

Thank you!

I have another question related to this topic.

I have a class with a method that, given an input, returns a value (for example “double processValue(double X)”).

I would like to draw a variable of my tree, processed through the method “processValue”, that is

myClass a;
tree->Draw(“a.processValue(variable_in_tree)”)

Is it possible?

I read chapter 15 of ROOT manual, and followed “Adding a Class with a Shared Library”,
but maybe I missed something because I still get
Error in TTreeFormula::Compile: Bad numerical expression

Could you link me to some tutorial/example about this topic?

Thanks,
Andrea

[quote]tree->Draw(“a.processValue(variable_in_tree)”)[/quote]This would work if ‘a’ was the name of a branch.

[quote]tree->Draw(“myClass::processValue(variable_in_tree)”)[/quote]would work if processValue was a static function. If neither of those 2 options is true, then this wont work directly. Instead you will need to either use the TTree::Draw syntax that take a script as input or use directly TTree::MakeProxy or TTree::MakeSelector.

Cheers,
Philippe.

Perfect!
Thank you for clarification!

Thanks,
Andrea

Hi Philippe,

This may seem stupid but: how do you do that for this simple example ? I have the same problem, but I do not understand which part of the documentation I should be looking at for this (apparently working) solution ?

Thanks in advance,
Olivier

Hi Olivier,

See the ROOT FAQ on how to generate a dictionary: root.cern.ch/drupal/faq#n676
If you are still stuck after using this information (and re-reading the User’s Guide chapter on adding your own class), please send us what you try and how it failed.

Cheers,
Philippe.

Thanks for the pointers, it helped a lot !

Best,
Olivier