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

**URL:** <https://root-forum.cern.ch/t/how-to-call-user-function-from-ttree-draw/10948>\
**Category:** ROOT\
**Created:** [September 13, 2010, 12:33pm UTC](https://root-forum.cern.ch/t/how-to-call-user-function-from-ttree-draw/10948 "2010-09-13T12:33:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giraudpf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@giraudpf](https://root-forum.cern.ch/u/giraudpf)\
**Post date:** [September 13, 2010, 12:33pm UTC](https://root-forum.cern.ch/t/how-to-call-user-function-from-ttree-draw/10948/1 "2010-09-13T12:33:55Z")

</div>

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/./test_C.so
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?

Thanks,

Pierre-François  
[test.C](https://root-forum.cern.ch/uploads/default/original/2X/8/88b78c56cfbedbd4282db00eb331a4fdb033b54d.C) (634 Bytes)

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 14, 2010, 2:06pm UTC](https://root-forum.cern.ch/t/how-to-call-user-function-from-ttree-draw/10948/2 "2010-09-14T14:06:05Z")

</div>

Hi,

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 do`tree->Draw("drawor.C");`and get the effect you are looking for. You can also simply use:`// drawor.C 
double drawor()
{
    return ev->value() + 1.;
}`

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![giraudpf](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@giraudpf](https://root-forum.cern.ch/u/giraudpf)\
**Post date:** [September 14, 2010, 2:44pm UTC](https://root-forum.cern.ch/t/how-to-call-user-function-from-ttree-draw/10948/3 "2010-09-14T14:44:52Z")

</div>

Thanks,

this solves my problem.

Pierre-François
