Absolute value for a variable in TTree


I am trying to get the absolute value of pdg (particle ID) to insert it in the condition part when I draw Eta=-log(tan(TMath::Pi()/4-atan(pz/sqrt(px^2+py^2))/2)) , these variables are already in my TTree … how can i make my command

TTree name.Draw(“Eta”,“Abs(pdg)”,"")? is that correct? what command i need to type before this one???

Also, how can I make 2 conditions when I draw 2 dim histogram between two variables in that TTree (for each variable there is a condition)

Please Advice.


Hello Mai,
abs(pid) is correct. You can connect two conditions via &&. E.g. “abs(pid1)&&abs(pid2)”

Best wishes, Peter

To be more generic, in a TTree::Draw expression, for the formula or the condition parameter, you can use functions from and the functions in TMath. You can also use the conditional ( a ? b : c ) expression.

So to do absolute value (for example), you could use “abs(foo)”, “TMath::Abs(foo)”, or “foo > 0 ? foo : -1*foo”.

The biggest limitation is that you can’t use arbitrary C++ functions or TF1 functions, but with cmath, TMath, and the conditional, you can make pretty complex things.


I think that the statement about “” is not really correct.

In TTree::Draw “varexp” expressions, you can use functions which are implemented in the TFormula class and all TMath functions.

BTW. There’s one warning about using “standard” functions (e.g. “abs”) in C/C++ code … see the old “[url=https://root-forum.cern.ch/t/fabs-and-abs/14556/1 and abs[/url]” thread.

thanks all,

i tried it with TMath and it works…

but when i made the 2 condition command I found the canvas is empty (white), is that an error??

For Mr. Coyote:
You’re right, it’s not the stuff in that works, but a lot of it is named similarly.

Is the official list of supported non-TMath functions in a TTree varexp the one found here? http://root.cern.ch/root/html/TFormula.html#TFormula:Analyze

I wonder if it’d be possible for ROOT to eventually automatically support all the functions in . Like a code generator that parses the header and generates the relevant TFormula code.

For Mai:
When you combine selection criteria, it is very possible to create a combined selection that is not satisfied by any events. If 0 events pass the cut, then the TTree::Draw action doesn’t actually do anything to the canvas (as far as I know).

The TTree::Draw action has a return value, and this value is the number of entries that were used, so you can check if this number is 0 to see if your blank canvas is normal (i.e. the result of a selection that rejects all entries) or some other problem.