Weird TVector3::SetTheta behaviour


I haven’t looked into this for two long, but I found that using the following script I can get two different vector components for a vector. I think they should be the same in both cases

ifstream file("numbers");

 double theta,phi;
 while(file >> theta >> phi)
     cout << theta << " " << phi << endl;
     TVector3 direction;

     cout << "before " << direction.x() << " " << direction.y() << " " << direction.z() << endl;
    cout << "after " << direction.x() << " " << direction.y() << " " << direction.z() << endl;


The output looks like this:

I can give you a list of numbers if you need it.


P.s.: using ROOT Version 4.02/00


Are your angles in radians? TMath::Sin and TMath::Cos simply calls std::sin and std::cos, and:


Angle in radians.

To convert deg to rad simply do

direction.SetMagThetaPhi(1., TMath::DegToRad() * theta, TMath::DegToRad() * phi);
direction.SetThetaPhi(TMath::DegToRad() * theta);

That is not the point. The thing should be the same in both cases, regardless of the units, shouldn’t it?

I set the coordinates in spherical coordinates (it doesn’t matter if in degrees or radians) write the coordinates in cartesian, set the coordinates again to the same values but using different methods (methods I believe should do the same thing) and write the cartesian coordinates again.

The point is that I’m getting different values for the cartesian coordinates, using two methods that are supposed to do the same thing, and using exactly the same values.

Am I missing something here?

I actualy think the problem is not in SetTheta but in SetPhi.

Hi Javier,
this is a problem, and I can reproduce it. I agree that the problem is in SetPhi. I’ll try to come up with a fix and let you know.


Let’s look at SetTheta definition.

Double_t ma = Mag();
Double_t ph = Phi();//here is the call to TMath::Atan2, which
//always returns value from -pi to pi in radians. How do you think, is posiible to get yout original pi == 140.168 here???

Am I missing something here?

wait, I suppose we all agree that TVector3 v(x,y,z); v.SetTheta(v.GetTheta()); should leave the vector unchanged, right? And so it has to be fixed.

What shoud be fixed??? Do you think in spherical coordinates Phi can be 140 radians and theta 54 radians??? The author need to convert angles!
And if you add SetTheta(angle * DegToRad() ) or SetPhi(angle2 * DegToRad())…

the problem stems from using theta outside its defined range. theta is only defined for [0…pi[, and by passing a value >pi, phi gets flipped: v.SetMagThetaPhi(1.,3.0,0.) v.Print() TVector3 A 3D physics vector (x,y,z)=(0.141120,0.000000,-0.989992) (rho,theta,phi)=(1.000000,171.887339,0.000000) v.SetTheta(3.) v.Print() TVector3 A 3D physics vector (x,y,z)=(0.141120,0.000000,-0.989992) (rho,theta,phi)=(1.000000,171.887339,0.000000) v.SetMagThetaPhi(1.,3.5,0.) v.Print() TVector3 A 3D physics vector (x,y,z)=(-0.350783,-0.000000,-0.936457) (rho,theta, phi)=(1.000000,159.464772,-180.000000) v.SetTheta(3.5) v.Print() TVector3 A 3D physics vector (x,y,z)=(0.350783,0.000000,-0.936457) (rho,theta,phi)=(1.000000,159.464772,0.000000)
Setting MagThetaPhi and Mag, Theta, Phi should still be consistent in my point of view, and it should probably issue a warning about an out-of-range theta. I’ll come up with a patch.