Wrong calculation results using vectors

Hi Rooters, I’m using Root to calculate some particles physics observable by looping over events in a ttree some of the output values are wrong and always given this value: -2.78275e-08
this is what im trying to calculate :
aco_init= ( 1. - acos(cos(Photon_phi->at(0)-Photon_phi->at(1)))/TMath::Pi());
when looking to this the numbers are correct
cout<<"Photon_phi->at(0) = "<<Photon_phi->at(0)<<endl;
cout<<"Photon_phi->at(1) = "<<Photon_phi->at(1)<<endl;

but the result is not
cout<<"aco = "<< aco_init<<endl;

If you have any hint in how to overcome this I will be grateful.

Exactly how much are these “correct numbers”?
And how much is “Photon_phi->at(0)-Photon_phi->at(1)”?

Thanks for the answer
the Photon_phi is between -pi and pi
here is an example of a good result
Photon_phi->at(0) = 0.475022
Photon_phi->at(1) = -2.76514
aco = 0.0313744
and a wrong one :
Photon_phi->at(0) = 1.16033
Photon_phi->at(1) = -1.98119
aco = -2.78275e-08

So, for the “wrong one” result, “Photon_phi->at(0)-Photon_phi->at(1)” is around “pi” and then “aco” should be around zero (and that’s what you get).

I think I can reproduce your “wong” result (I guess you are switching between “float” and “double” floating point values):

{
  double double_pi = TMath::Pi();
  std::cout << double_pi - TMath::Pi() << std::endl;
  std::cout << 1. - std::acos(std::cos(double_pi)) / TMath::Pi() << std::endl;
  std::cout << 1. - double_pi / TMath::Pi() << std::endl;
  float float_pi = TMath::Pi();
  std::cout << float_pi - TMath::Pi() << std::endl;
  std::cout << 1. - std::acos(std::cos(float_pi)) / TMath::Pi() << std::endl;
  std::cout << 1. - float_pi / TMath::Pi() << std::endl;
}

Thank you
indeed its a double/float problem

Best

Assuming that “Photon_phi->at(i)” are “float” values, try to calculate everything as “float”:

float(1.) - std::acos(std::cos(Photon_phi->at(0) - Photon_phi->at(1))) / float(TMath::Pi())

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