# Problem initializing a 4-vector

**URL:** <https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789>\
**Category:** Newbie\
**Created:** [July 13, 2022, 7:25pm UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789 "2022-07-13T19:25:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![abunka](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@abunka](https://root-forum.cern.ch/u/abunka)\
**Post date:** [July 13, 2022, 7:25pm UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789/1 "2022-07-13T19:25:09Z")

</div>

Hello,  
I am working with root 6.24/06 on lxplus in eventloop.  
I declare a 4 vector and initialize it:

```auto
ROOT::Math::LorentzVector<ROOT::Math::PtEtaPhiM4D<double>> myVector;
...
myVector = cluster->p4(xAOD::CaloCluster_v1::State::UNCALIBRATED);

```

This returns a confusing error to me:

```auto
/cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBaseExternals/21.2.186/InstallArea/x86_64-centos7-gcc8-opt/include/Math/GenVector/LorentzVector.h: In instantiation of 'ROOT::Math::LorentzVector<CoordSystem>& ROOT::Math::LorentzVector<CoordSystem>::operator=(const ForeignLorentzVector&) [with ForeignLorentzVector = TLorentzVector; CoordSystem = ROOT::Math::PtEtaPhiM4D<double>]':
/afs/cern.ch/work/a/abunka/private/dijetStudies/source/dijetVariables/Root/variableAnalysis.cxx:1089:94: required from here
/cvmfs/atlas.cern.ch/repo/sw/software/21.2/AnalysisBaseExternals/21.2.186/InstallArea/x86_64-centos7-gcc8-opt/include/Math/GenVector/LorentzVector.h:137:22: error: 'const class TLorentzVector' has no member named 'x'
           SetXYZT( v.x(), v.y(), v.z(), v.t() );
                    ~~^

```

The “no member named” part is repeated for the y, z, t…  
As far as I can tell, my vector is being treated as a TLorentzVector, which then does not have the ability to be initialized to the cluster p4() I am using? But I am really not sure. I do know that when I tried working with TLorentzVectors I had many issues, so i switched to the newer ROOT::Math::LorentzVector. So I am surprised to see TLorentzVector appear in the error.

Thanks!

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [July 13, 2022, 9:09pm UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789/2 "2022-07-13T21:09:33Z")

</div>

Hi @abunka,

you see the TLorentzVector, because `cluster->p4` it’s the type that `cluster->p4()` returns, as you can see in this xAOD documentation for example:  
[https://ucatlas.github.io/RootCoreDocumentation/2.4.28/dd/d91/classxAOD\_1\_1CaloCluster\_\_v1.html#a4ee752abfcacde56b2a0ac06667a8e9e](https://ucatlas.github.io/RootCoreDocumentation/2.4.28/dd/d91/classxAOD_1_1CaloCluster__v1.html#a4ee752abfcacde56b2a0ac06667a8e9e)

The new `ROOT::Math::LorentzVector` doesn’t support direct assignment from TLorenzVector, but you can do it in two steps:

```c++
ROOT::Math::LorentzVector<ROOT::Math::PtEtaPhiM4D<double>> myVector;
{
   TLorentzVector const& v = cluster->p4(xAOD::CaloCluster_v1::State::UNCALIBRATED);
   myVector.SetXYZT( v.X(), v.Y(), v.Z(), v.T() );
}

```

Maybe if this is a pattern that will happen more often in your code, you should write a little helper function to do this.

Hope that helps and thanks for asking the question!

---

<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:** [July 14, 2022, 7:09am UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789/3 "2022-07-14T07:09:18Z")

</div>

> [@jonas](#):
>
> pattern that will happen more often

@Axel I really expect that it is the ROOT Team responsibility to provide the corresponding assignment operators (between a [TLorentzVector](https://root.cern/doc/master/classTLorentzVector.html) and a [ROOT::Math::LorentzVector](https://root.cern/doc/master/classROOT_1_1Math_1_1LorentzVector.html)). Plenty of the existing source code uses the “old” class and it’s a pain to switch to the new one in new macros (which use “old” libraries).

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [July 26, 2022, 2:05pm UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789/4 "2022-07-26T14:05:08Z")

</div>

Agreed, see [[genvector] Add construction from C-style array: by Axel-Naumann · Pull Request #11053 · root-project/root · GitHub](https://github.com/root-project/root/pull/11053)

(I prefer not to ease the transition in the “wrong” direction.)

---

<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:** [July 26, 2022, 2:33pm UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789/5 "2022-07-26T14:33:18Z")

</div>

Not sure what you mean by _the “wrong” direction_.

I think it should work both ways.  
Assume you have an “old” library (which knows the “old” classes only), and you need to pass your “new” object to it and then retrieve some “old” result (which you then want to process in the “new” way).

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [September 20, 2022, 12:20pm UTC](https://root-forum.cern.ch/t/problem-initializing-a-4-vector/50789/6 "2022-09-20T12:20:07Z")

</div>

It’s always possible, and I prefer a slightly adiabatic ease of conversion 🙂
