How to call user function from TTree::Draw()

Dear ROOT,

I would like to draw the the result of a function I write in a macro, and takes as arguments objects that are in a tree.

Attached is a small macro that demonstrates my problem. In this example, I would like to draw the result of testor(ev), where ev is the class that is saved in the tree. Here’s what happens:

$ root -l root [0] .L test.C+ Info in <TUnixSystem::ACLiC>: creating shared library /home/giraudpf/tmp/./ root [1] TTree* tree = create_tree(); root [2] tree->Draw("ev->value()") // Works OK <TCanvas::MakeDefCanvas>: created default TCanvas with name c1 root [3] tree->Draw("testor(ev)") // Fails Error in <TTreeFormula::Compile>: Bad numerical expression : "testor(ev)"

Do you know how to solve this problem?


test.C (634 Bytes)


The ‘formula’ version of TTree::Draw can only call (member) functions that take no arguments or take only numerical arguments. To call more complex function, you can switch to the script version of TTree::Draw (which leverage MakeProxy). For example with the files:// drawor.h class MyEvent; double testor(const MyEvent* event);and// drawor.C double drawor() { return testor(ev.obj.GetPtr()); }you can dotree->Draw("drawor.C");and get the effect you are looking for. You can also simply use:// drawor.C double drawor() { return ev->value() + 1.; }



this solves my problem.