# Rotate z axis to point into myvector direction

**URL:** <https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429>\
**Category:** Newbie\
**Created:** [November 18, 2022, 2:59pm UTC](https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429 "2022-11-18T14:59:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_H](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Alan\_H](https://root-forum.cern.ch/u/Alan_H)\
**Post date:** [November 18, 2022, 2:59pm UTC](https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429/1 "2022-11-18T14:59:55Z")

</div>

Hi guys,

I have several vectors in xyz space, vector1, vector2, vector3, etc.  
I need to rotate the whole system so that the z-axis of my new system points in the direction of vector1. Is there a good way to do this? I have found the following from RotateAxes, but not sure how to define the newX and newY. I’d assume my newZ is my vector1.

```auto
          vector1.SetPxPyPzE( px1, py1,pz1, ee1 );
          vector2.SetPxPyPzE( px2, py2,pz2, ee2 );
          vector3.SetPxPyPzE( px3, py3,pz3, ee2 );
         TVector3 newX(0,1,0); //not sure what should come here
         TVector3 newY(0,0,1); //nor here.
         TVector3 newZ(vector1);
         a.RotateAxes(newX,newY,newZ);

```

What is a in this case? It’s not specified in the documentation, and when passing a vector it returns

**error:** ‘ **class TVector3** ’ has no member named ‘ **RotateAxes** ’;

Thanks.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 18, 2022, 8:51pm UTC](https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429/2 "2022-11-18T20:51:14Z")

</div>

See the documentation of [TVector3](https://root.cern.ch/doc/master/classTVector3.html)

---

<div class="post-metadata">

**Author:** ![Alan\_H](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/3d9bf3/32.png) [@Alan\_H](https://root-forum.cern.ch/u/Alan_H)\
**Post date:** [November 22, 2022, 12:07am UTC](https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429/3 "2022-11-22T00:07:09Z")

</div>

I’m sorry, I am new to this. From this documentation:  
“_[TVector3](https://root.cern.ch/doc/master/classTVector3.html) objects can be rotated by objects of the [TRotation](https://root.cern.ch/doc/master/classTRotation.html) class using the [Transform()](https://root.cern.ch/doc/master/classTVector3.html#ac42a48102d1a92a981e0ba0d8848c61e)member functions,_”

And when checking the TRotation class I again find the function I have tried to use:

### _Rotation of local axes_

_Member function [RotateAxes()](https://root.cern.ch/doc/master/classTRotation.html#acf7e3da35816052c3b6e73f7fe71239d) adds a rotation of local axes to the current rotation and returns the result:_

_[TVector3](https://root.cern.ch/doc/master/classTVector3.html) newX(0,1,0);_

_[TVector3](https://root.cern.ch/doc/master/classTVector3.html) newY(0,0,1);_

_[TVector3](https://root.cern.ch/doc/master/classTVector3.html) newZ(1,0,0);_

_[a](https://root.cern.ch/doc/master/textangle_8C.html#a217ecf07abce72f5f96efd1a01c98c77).RotateAxes(newX,newY,newZ);_

My question again is: What is _a_ in the last line of the example?

Thanks.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [November 22, 2022, 4:15am UTC](https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429/4 "2022-11-22T04:15:41Z")

</div>

Hover the mouse over it and you’ll see it’s a TArc, with a link. With the pop-up tips it’s not always easy to click on those links, but eventually you should get to [this example](https://root.cern.ch/doc/master/textangle_8C_source.html#l00012) and then the [TArc documentation](https://root.cern.ch/doc/master/classTArc.html).

---

<div class="post-metadata">

**Author:** ![Emil\_R](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emil_r/32/31255_2.png) [@Emil\_R](https://root-forum.cern.ch/u/Emil_R)\
**Post date:** [November 23, 2022, 7:31am UTC](https://root-forum.cern.ch/t/rotate-z-axis-to-point-into-myvector-direction/52429/5 "2022-11-23T07:31:21Z")

</div>

I am also quite a new to Root and therefor my help might not be as good as others, but I think you have a simpler problem. You have to create a TRotation “a” and can afterwards apply this “a” to a vector. I think it should be something like:

> TVector3 newX(0,1,0);  
> TVector3 newY(0,0,1);  
> TVector3 newZ(1,0,0);
> 
> ```
> TRotation a;
> a.RotateAxes(newX,newY,newZ);
> 
> TVector3 ThisGetsRotated(2,1,3);
> std::cout<<ThisGetsRotated.X()<<" "<<ThisGetsRotated.Y()<<" "<<ThisGetsRotated.Z()<<std::endl;
> ThisGetsRotated.Transform(a);
> std::cout<<ThisGetsRotated.X()<<" "<<ThisGetsRotated.Y()<<" "<<ThisGetsRotated.Z()<<std::endl;
> 
> ```

This code should just permutate the coordinates. “a” is a matrix (a Rotation Object) and has to be defined with RotateAxes. Afterwards it can be applied to a Vector with Transform or simply by multiplying.  
Hope that helps.
