TTree::Draw and functions

I have found that in the interactive mode of root I can perform the following:


i.e somefunction() returns the difference of its two arguments and I have a TTree called MyTree which contains two branches called ‘x’ and ‘y’. By executing the TTree::Draw() command as I did above, a histogram will be produced of the difference (or whatever I write somefunction() to do) of x and y for each entry. What are the limitations of this feature? I tried doing something similar where I pass a pointer to an array of floats to somefunction() but I receive a segmentation fault. A clear indication of what is and is not possible would be greatly appreciated. Thanks in advance.

Max Atkin


The functions used by TTree::Draw have to take simple numerical arguments and return simple numerical arguments. I.e. only int, floats, double, etc. and no array or objects.

To use function works on arrays, you should use the new TTree Proxy mechanism released in ROOT 4.00/08 where you can run a function in context where the branchname can be used as variables.



But is it possible to use to MakeProxy mechanism in the selection variable of TTree::Draw() (as it was in PAW)? For example,
MyTree.Draw(“somefunc.C++”,“func1.C++<a && func2.C++>b”), where a and b are numbers.

If not is there any ROOT analog of such a mechanism in PAW which allows to use automatic histogram creating (like Draw(), in contrast with MakeClass) with user defined function of Ntuple variables (such as arrays) in varexp and selection?


You can use:MyTree.Draw("somefunc.C+","somesel.C+");(+ vs ++ to avoid unnecessary recompilation).

Currently you can not pass argument to the generated code from inside the Draw command itself. However an alternative is to use something like:[code]// file somefunc.C
double a;
double b;
double func1() {

double func2() {

void somefunc() {
if (func1()<a && func2()>b) {
double value = …;
and use with:MyTree->MakeProxy("sel","somefunc.C","","nohist"); gROOT->ProcessLine(".L sel.h+"); sel *mysel = new sel(); sel->a = val1; sel->b = val2; MyTree->Process(mysel);


Thank you very much.


Hi, I have a somewhat similar question. I’m working with a tree whose branches are collection.

I’m trying to do something like:
tree->Draw(“”, “f(Cone4H1TowerJets)>0”);

I get different root reactions, depending on the way f is defined:

  1. double f(const JetCollection *jets)
    Error in TTreeFormula::Compile: Bad numerical expression :

if I use @Cone4H1TowerJets in the selection argument of Draw:
Error in TTreeFormula::Compile: Bad numerical expression :

same thing without the const.

  1. double f(JetCollection jets)
    no errors, but inside f, jets.size() is always 0
    same thing with const.
    same thing with @

  2. double f( JetCollection &jets)
    segmentation fault
    same thing with const
    same thing with @

I was also trying to use MakeProxy, but I get punched in the face:

root [1] tree->MakeProxy(“proxytest01”, “somefunc.C”, “”, “nohist”);
Fatal in ROOT::TBranchProxyClassDescriptor:: strcmp(fInfo->GetName(), type)==0 violated at line 117 of `treeplayer/src/TBranchProxyClassDescriptor.cxx’
… … …

root version is 5.18/00a btw.


TTree::Draw can not pass object to a function, it can only pass numerical values. [I.e. all three of your example should have failed with a nice warnign message :frowning:]

root [1] tree->MakeProxy("proxytest01", "somefunc.C", "", "nohist"); Fatal in <ROOT::TBranchProxyClassDescriptor::>: strcmp(fInfo->GetName(), type)==0 violated at line 117 of `treeplayer/src/TBranchProxyClassDescriptor.cxx' Humm … this seems to indicate an issue with the ROOT type system, it is missing some information about your classes! Could you send me your ROOT file so that I can try to reproduce this problem?


Thanks Philippe.

I attached my - in which are the dpd’s I’m trying to use. On of the files there is from castor so I guess you can use it.

load.cxx - the file I use to load the tree

somefunc.C - the file I tried to use with MakeProxy

(everything is in /afs/

Thanks again,
somefunc.C (51 Bytes)
load.cxx (598 Bytes) (2.43 KB)