Issue with TLorentzVector and if condition behaviour

Hi all,

I hava a very simple issue that I am unable to understand. Please help me to understand.

TLorentzVector  NU0;
NU0.SetPtEtaPhiE(pt, eta, phi, E);
if (NU0.Beta() > 1.0) cout<<"NU0 beta = "<< NU0.Beta() << endl;

So, if NU.Beta() is equal to 1 then the last line should not print. But the issue is that last line prints:

NU0 beta = 1

I also tried to typecast last line like:

if (NU0.Beta() > 1.0) cout<<"NU0 beta = "<< (double)NU0.Beta() << endl;

But I got the same result. Do you have idea whats happening here?

with regards,
Ram

Try:

if (NU0.Beta() > 1.0) std::cout << "NU0 beta = 1 + " << (NU0.Beta() - 1.0) << std::endl;

Dear @Wile_E_Coyote,

Thanks for quick response.

Now, I got this. When I tried yours statement It printed

NU0 beta = 1 + 3.54407e-09

So, this is the reason. As I understand when a double values is very low then it might be treated as its non-decimal number!!!

Thanks & regards,
Ram

Try:

if (NU0.Beta() > 1.0) printf("NU0 beta = %.17g\n", NU0.Beta());

Now, it gives me: NU0 beta = 1.0000000229065995.

So, whenever we expect the result will be very precise then we should do something like this

if (NU0.Beta() > 1.0) std::cout << "NU0 beta = 1 + " << (NU0.Beta() - 1.0) << std::endl;

This cannot be. You now get “1 + 2.29066e-08”. It seems to me that something in your code misbehaves.

Dear @Wile_E_Coyote,

Sorry, I sent those numbers for different events. Here are the two outputs for same events:

------------- NU0 beta = 1.0000000017788548
***********   NU0 beta = 1 + 1.77885e-09

Both statement prints exactly same.

Can it be that some of your pt, eta, phi, E variables originate in “float” and not “double” numbers / variables?
Moving between “float” and “double” will create such “glitches” (same if you read these values from some previously saved character strings).

Dear @Wile_E_Coyote ,

Yes, pt, eta, phi and E all are of float type. So, this is the main culprit. If they are decleared as double then they should behave as normal.

So, If I typecast them as double then it should print the value?? I tried

NU0.SetPtEtaPhiE((double)pt, (double)eta, (double)phi, (double) E);

if (NU0.Beta() > 1.0) cout<<"NU0 beta = "<< NU0.Beta() << endl;

but it again print out 1. Is this also expected?

with regards,
Ram

The “typecast” is done automatically by the interpreter / compiler.
The problem is that “float” values are precise up to 6 to 9 decimal digits, while “double” values give 15 to 17 decimal digits.
So, when you “move” between them, the least significant 6 to 11 decimal digits are usually “noise”.

You need to “re-calculate” your variables in full “double” precision (e.g. E) before you pass them to “SetPtEtaPhiE” (or you could “correct” them after you detect that “beta > 1.0”).

Dear @Wile_E_Coyote,

Thank you very much for helping me with my issue.

Thanks & regards,
Ram

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