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

**URL:** <https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423>\
**Category:** Newbie\
**Created:** [October 27, 2021, 10:36am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423 "2021-10-27T10:36:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabio\_Crespi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabio_crespi/32/27734_2.png) [@Fabio\_Crespi](https://root-forum.cern.ch/u/Fabio_Crespi)\
**Post date:** [October 27, 2021, 10:36am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/1 "2021-10-27T10:36:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 27, 2021, 10:42am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/2 "2021-10-27T10:42:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fabio\_Crespi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabio_crespi/32/27734_2.png) [@Fabio\_Crespi](https://root-forum.cern.ch/u/Fabio_Crespi)\
**Post date:** [October 28, 2021, 6:18am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/3 "2021-10-28T06:18:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Fabio\_Crespi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabio_crespi/32/27734_2.png) [@Fabio\_Crespi](https://root-forum.cern.ch/u/Fabio_Crespi)\
**Post date:** [November 4, 2021, 7:58am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/4 "2021-11-04T07:58:43Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 4, 2021, 8:14am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/5 "2021-11-04T08:14:47Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Fabio\_Crespi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabio_crespi/32/27734_2.png) [@Fabio\_Crespi](https://root-forum.cern.ch/u/Fabio_Crespi)\
**Post date:** [November 5, 2021, 5:04pm UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/6 "2021-11-05T17:04:13Z")

</div>

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);  
v1.Rotate(TMath::DegToRad()\*iii, v2);

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);  
v1.Rotate(TMath::DegToRad()\*iii, v2);

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)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 6, 2021, 10:57am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/7 "2021-11-06T10:57:47Z")

</div>

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();`

---

<div class="post-metadata">

**Author:** ![Fabio\_Crespi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabio_crespi/32/27734_2.png) [@Fabio\_Crespi](https://root-forum.cern.ch/u/Fabio_Crespi)\
**Post date:** [November 6, 2021, 2:35pm UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/8 "2021-11-06T14:35:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 6, 2021, 2:58pm UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/9 "2021-11-06T14:58:14Z")

</div>

```cpp
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

```

---

<div class="post-metadata">

**Author:** ![Fabio\_Crespi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/fabio_crespi/32/27734_2.png) [@Fabio\_Crespi](https://root-forum.cern.ch/u/Fabio_Crespi)\
**Post date:** [November 8, 2021, 7:53am UTC](https://root-forum.cern.ch/t/problem-with-tvector3-rotation-around-a-vector-v1-rotate-tmath-pi-4-v2-rotation-around-v2/47423/10 "2021-11-08T07:53:26Z")

</div>

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