TLorentzVector.Eta() speed and caching


I am wondering why classes such as TLorentzVector, and its underlying TVector3, don’t cache the Eta() value. Especially if I call SetPtEtaPhiE(pt,eta,phi,e); and SetPtEtaPhiM(pt,eta,phi,m) it is a shame the internal coordinates first have to get calculated, and then when I next want Eta(), it has to calculate a log again. I’m not interested in the polar angles at all, but creating a 4-vector they get calculated (time lost) and getting Eta() again the original value has to be recalculated!

As a result, a program looping over 20000 events with truth jets, smearing them an applying several deltaR cuts spends ~7% of its time just calculating log() - whereas this value is supposed to be known. Is this by design?

Of course, this applies to Phi() and atan2() as well. Why does it have to be recalculated?

It can of course be avoided by deriving TLorentzVector, caching the values of eta, etc. But it’s a shame the TLorentzVector() class itself does not remember it state has not changed (which can be achieved by a simple bool) and caches the result of Eta() internally until one of the 4-vector components is reset.


P.S. I see now that it went into the wrong subforum. My apologies for that, I would be grateful if someone could move the topic.

There are multiple vector classes in ROOT, with TLorentzVector being the “easy” option which also works very nicely with CINT. If you care about performance you should at least try what is in GenVector.

ROOT::TMath::LorentzVector<ROOT::Math::PxPyPzE4D<double> > p1;
ROOT::TMath::LorentzVector<ROOT::Math::PtEtaPhiE4D<double> > p2;

This gives you some freedom how to compose your types (especially in compiled code) and might compile to more efficient code.