How to store TLorentzVector in a list of pyroot

Hello pyroot users,

I tried to store multiple TLorentzVector objects in a list, let’s say mass_bjet.

dRel = ROOT.TLorentzVector ()
mass_bjet = []
for k in range(10):
        lepton0 = ROOT.TLorentzVector ()

However, the error said
TypeError: TLorentzVector TLorentzVector::operator+(const TLorentzVector&) =>
TypeError: could not convert argument 1

The error is gone when I used (lepton0+dRel) instead, so I guess List is not compatible with TLorentzVector. What is the approach to store TLorentzVector objects in a list/array in python? Thank you so much!

Welcome to the ROOT forum.
I think @vpadulan or @moneta can help you.

Dear @Enki ,
Thanks for reaching out! Please, refrain from using TLorentzVector in your code. It is a legacy class, as stated in the docs. The suggested alternative is ROOT::Math::LorentzVector or one of its specialisations. They can naturally be used in Python lists, for example:

l = []
(l[0] + ROOT.Math.XYZTVector()).M() # prints 0.0

Dear vpadulan,

Thank you for reply! My coordinate system use (Pt, Eta, Phi, E) so I define the vector as

l[0] = ROOT.Math.PtEtaPhiEVector()
l[0].SetPtEtaPhiE(num1, num2, num3, num4)

However the attribute ‘SetPtEtaPhiE’ is not found. I also tried ‘SetCoordinates’ but it only accepts one parameter. How to set coordinate values for the 4-vector? Thank you!

Indeed, I don’t see any SetPtEtaPhiE method in the docs. But I think the SetCoordinates method should work, for example:

>>> import ROOT
>>> v = ROOT.Math.PtEtaPhiEVector()
>>> v.SetCoordinates(1,2,3,4)
<cppyy.gbl.ROOT.Math.LorentzVector<ROOT::Math::PtEtaPhiE4D<double> > object at 0x559787003480>
>>> print(v)

yes, SetCoordinates works this time. Thanks for your help!

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