scikit-hep / uproot3-methods

Pythonic behaviors for non-I/O related ROOT classes.
BSD 3-Clause "New" or "Revised" License
21 stars 28 forks source link

Fix rotate_axis and similar methods in TLorentzVector #74

Closed dntaylor closed 4 years ago

dntaylor commented 4 years ago

This fixes an issue with the current version of rotate_axis (and similar methods) of TLorentzVector. Currently the master will throw an error with the conversion of the tuple returned by TVector3 into the appropriate TLorentzVector type. I've also added some unit tests for rotation.

You can test the results with

import uproot
import uproot_methods
import numpy
import numpy as np
import awkward

p4 = uproot_methods.TLorentzVector(0,1,0,1)
print(p4)
rot = p4.rotatex(np.pi)
print(rot)

px = [1,0,0]
py = [0,1,0]
pz = [0,0,1]
en = [1,1,1]

p4 = uproot_methods.TLorentzVectorArray(px,py,pz,en)
print(p4)
rot = p4.rotatex(np.pi)
print(rot)

px = awkward.fromiter([[1,0],[0]])
py = awkward.fromiter([[0,1],[0]])
pz = awkward.fromiter([[0,0],[1]])
en = awkward.fromiter([[1,1],[1]])

p4 = uproot_methods.TLorentzVectorArray.from_cartesian(px,py,pz,en)
print(p4)
rot = p4.rotatex(np.pi)
print(rot)

The output of which should be

TLorentzVector(x=0, y=1, z=0, t=1)
TLorentzVector(x=0, y=-1, z=1.2246e-16, t=1)
[TLorentzVector(x=1, y=0, z=0, t=1) TLorentzVector(x=0, y=1, z=0, t=1) TLorentzVector(x=0, y=0, z=1, t=1)]
[TLorentzVector(x=1, y=0, z=0, t=1) TLorentzVector(x=0, y=-1, z=1.2246e-16, t=1) TLorentzVector(x=0, y=-1.2246e-16, z=-1, t=1)]
[[TLorentzVector(x=1, y=0, z=0, t=1) TLorentzVector(x=0, y=1, z=0, t=1)] [TLorentzVector(x=0, y=0, z=1, t=1)]]
[[TLorentzVector(x=1, y=0, z=0, t=1) TLorentzVector(x=0, y=-1, z=1.2246e-16, t=1)] [TLorentzVector(x=0, y=-1.2246e-16, z=-1, t=1)]]
jpivarski commented 4 years ago

It looks like these were bugs that got through due to insufficient testing.

It surprises me that p3 is a tuple—I would have thought it was a TVector3. I'm going to take a closer look to be sure it's always a tuple.

Thanks for adding these tests and correcting it!

jpivarski commented 4 years ago

Oh, yeah, it is. The name p3 is used in other contexts as a TVector3, but the output of the rotation methods are tuples.