How to sum two TLorenzVectors in c++?


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");

    TTreeReader reader("cutted", &file);

    TTreeReaderValue<float> MET_etx(reader, "MET_etx");

    TTreeReaderArray<double> TauE(reader, "TauE");

    TTreeReaderArray<double> TauPt(reader, "TauPt");

    TTreeReaderArray<double> TauEta(reader, "TauEta");

    TTreeReaderArray<double> TauPhi(reader, "TauPhi");


    while( reader.Next()) {

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

        TLorentzVector *p1 = new TLorentzVector();    
        TLorentzVector *p2 = new TLorentzVector();    

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

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



So, what I am doing wrong here?


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[0],TauEta[0],TauPhi[0],TauE[0]);
Similar for v2, then you need (v1+v2).M()


Hi @ikabadzhov,

It worked wonderfully, thank you for the tips!

1 Like