# How to sum two TLorenzVectors in c++?

HI,

Sorry to bother with this simple task, but how can I sum and take the invariant mass of a pair of four vectors?. I have written the following script to read information of a tree and store it in a two four vectors:

``````void tut(){

TFile file("output.root");

//std::cout << *MET_etx << std::endl;

TLorentzVector *p1 = new TLorentzVector();
p1->SetPtEtaPhiE(TauPt,TauEta,TauPhi,TauE);

TLorentzVector *p2 = new TLorentzVector();
p2->SetPtEtaPhiE(TauPt,TauEta,TauPhi,TauE);

//std::vector<double> sum = {0,1,2,3};

std::cout << "Mass: " << (p1+p2).M() << std::endl;

}

}
``````

So, what I am doing wrong here?

Best,
Caio.

Hello, @Daumann !

I do not have access to your root files, but I assume they are correct and setting `SetPtEtaPhiE` is successful. To get your desired mass, the addition should be done as: `(*p1+*p2).M()`.

However, `TLorentzVector` is a legacy class. See: ROOT: TLorentzVector Class Reference.

A better alternative in your case is to use `ROOT::Math::PtEtaPhiEVector`. You can directly construct it with something like: `ROOT::Math::PtEtaPhiEVector v1(TauPt,TauEta,TauPhi,TauE);`
Similar for `v2`, then you need `(v1+v2).M()`

2 Likes