Angle wrap in TTree.Draw

To calculate an angle difference within a range of -pi and pi, one can use C++ expression fmod in TTree.Draw and TTree.Scan, but it has an odd “truncated” definition, which breaks the group symmetry. When calculating angle between two objects as it ranges from -2pi to 2pi, and fmod(dphi,2*pi) is essentially identity (x=y) in this range.

To properly map the answer to a range between -pi and pi, one can do
fmod(phi1-phi2+3*pi,2*pi)-pi, in which the 3*pi serves to move the input out of the discontinuity and centers the range at 0.

However, as more angles are involved, the shift will have to increase. And it would be good if there is a simpler method.

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

I guess a general version would be (phi+pi<0)*2*pi+fmod(phi+pi,2*pi)-pi

Hi @sciencecw ,
TTree::Draw and TTree::Scan and the TTreeFormula expression syntax are not being evolved anymore, I think what you mention is the best solution to the problem.

@Axel or @pcanal might have further insight.


Does TVector2::Phi_mpi_pi(phi1-phi2) address this?

Yes it does, it uses a while loop to shift the number until to gets into desired range, circumventing the C++ problem

But I don’t believe it can be used in TTree::Draw

Totally! Useless example, just to demonstrate it works:

$ root tutorials/hsimple.root
root [0] ntuple->Draw("TVector2::Phi_mpi_pi(px-py)")

Thanks for the help. That is the right function. I thought nothing other than TMath are loaded into TTree::Draw syntax. Is this information found anywhere in the ROOT reference?

I think the doc on TTree::Draw() is quite clear - you might have been able to find that yourself :slight_smile:

The expressions can use all the operations and built-in functions supported by TFormula (see TFormula::Analyze()), including free functions taking numerical arguments (e.g. TMath::Bessel()). In addition, you can call member functions taking numerical arguments. For example, these are two valid expressions:


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.