# Problem with TVector3 Rotation around a vector v1.Rotate(TMath::Pi()/4, v2); // rotation around v2

For some reason when I use the “Rotate” member function of a TVector3
the result is always a TVector3 “v3”

with all the 3 coordinates equal to zero.
I get the warning in TRotation::Rotate(angle,axis): zero axis.

This is a piece of my code:

v1.SetXYZ(1.,0.,0.);
v3 = v2.Rotate(1., v1);|

Am I doing some simple mistake?

`v3 = v2; v3.Rotate(1., v1);`

Thanks now it works! Honestly I don’t understand why, but it definitely works.

Your suggestion helped a lot but unfortunately didn’t solve entirely my problem, sorry I missed to say that my piece of code was inside a for loop, so it is more proper like the following:

``````for(..){

vect_2.SetXYZ(vect_2_x,vect_2_y,vect_2_z);
vect_3 = vect2;
vect_3 = vect_2.Rotate(angle,vect_1);
vect_3 = vect_2;

}
``````

this for me seems to work perfectly but if you discover there is something wrong let me know

thanks again

You do not need “`vect_3`” at all (use the rotated “`vect_2`”).

``````vect_2.SetXYZ(vect_2_x, vect_2_y, vect_2_z);
vect_2.Rotate(angle, vect_1);
``````

ok thanks you are right. Sorry, again, I don’t understand why in the two pieces of codes below,
in the verision below “AAAA” v1 is exactly what I expect, in the verision below “BBBB” v1 is “changed” somehow in a strange way (I don’t understand why):

AAAA

for(.iii…){

v1.SetXYZ(v1_X,v1_Y,v1_Z);

double v1_X2 = v1.X();
double v1_Y2 = v1.Y();
double v1_Z2 = v1.Z();

v2.SetXYZ(v2_X,v2_Y,v2_Z);

}

BBBB

for(.iii…){

v1.SetXYZ(v1_X,v1_Y,v1_Z);

double v1_X2 = v1.X();
double v1_Y2 = v1.Y();
double v1_Z2 = v1.Z();

v2.SetXYZ(v2_X,v2_Y,v2_Z);

v2.Rotate(v3.Angle(v4)/2., v1);

}

(some lines are redundant, I know, but I posted as they are just to be sure not introducing confusion)

I don’t really understand what you are trying to achieve.
For “debug” purposes, after each operation on any vector “`v`”, add (and then inspect the output, if it is what you expect): `v.Print();`

Thanks for helping. Actually I am already doing this “debugging” (using cout << v1.X() …
and not v1.Print() - if it is different let me know).

My previuos question, in other words, was: could for some reason the coordinates values of v1 changed
by the operation “v2.Rotate(v3.Angle(v4)/2., v1);”?
(this is what it seems to me to happen in my program but I don’t understand why).
(“v2.Rotate(v3.Angle(v4)/2., v1);” is the only different line of code “AAAA” in respect
to code “BBBB”, pieces of codes reported in the post above).

Just to explain what I am trying to achieve:
I work for a European research project called AGATA (Advanced GAmma.ray Tracking Array),
aimed to build a high resolution gamma-ray spectrometer for nuclear physics studies.
I am trying to write a simple (hopefully) program to make some consitency checks on
some Geant4 simulations results for AGATA. The vectors v1,v2,v3 are just “directions”
of the tracks of the gamma rays inside (and outside) the AGATA germanium detectors.

``````v1.Print(); v2.Print(); v3.Print(); v4.Print(); // before
v2.Rotate(v3.Angle(v4) / 2., v1);
v1.Print(); v2.Print(); v3.Print(); v4.Print(); // after
``````

Thanks, understood, I found the bug. Actually v1.Print() is much more convenient to debug than using cout << v1.X()…