# Getting parameters of a tree with entries type TLorentzVector

**URL:** <https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788>\
**Category:** TMVA\
**Created:** [July 14, 2018, 7:56am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788 "2018-07-14T07:56:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KarenDmn](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@KarenDmn](https://root-forum.cern.ch/u/KarenDmn)\
**Post date:** [July 14, 2018, 7:56am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788/1 "2018-07-14T07:56:58Z")

</div>

Hi, Root Forum:

I have a tree with branches with entries that are TLorentzVector and I have to obtain pseudorapidity, transversal momentum etc. (This is in order to apply these parameters as cuts in the macro TMVAClassification.C)

This is my code:

```auto
#include "TROOT.h"
#include "TFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"
#include "TLorentzVector.h"
#include <iostream>
using namespace std;

void lorentzmeson(){
   ...
   TLorentzVector Kaon = TLorentzVector();
   ...
   vector<float> *B_kaon_px=0;
   vector<float> *B_kaon_py=0; 
   vector<float> *B_kaon_pz=0; 
  ...
  
   TFile *datafile = new TFile("Rootuple_Run2017F-v1_0000_x0.root");
   TTree *datatree = (TTree*)datafile->Get("rootuple/ntuple");
  ...
datatree->SetBranchAddress("B_kaon_px",&B_kaon_px);
datatree->SetBranchAddress("B_kaon_py",&B_kaon_py);
datatree->SetBranchAddress("B_kaon_pz",&B_kaon_pz);

Long64_t nentries = datatree->GetEntries();

for(Long64_t ientry=0; ientry<nentries; ientry++)
    {   
     datatree->GetEntry(ientry);
     Kaon.SetPxPyPzE(B_kaon_px->at(ientry),B_kaon_py->at(ientry),B_kaon_pz->at(ientry),0); 
     Float_t r;
     r=Kaon.Pt();
     cout<<"Pt: "<<r<<endl; //Only for comprobation that the macro works
    }

}

```

In the ROOT file, the tree does not have the energy, and I only get the result of the first entry, and I obtain

```auto
Pt: 0.608554
terminate called after throwing an instance of 'std::out_of_range'
  what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 1)

```

Any suggestion please? if anyone has a better idea to get these parameters as cuts in classification macro I would be very grateful.

Thanks for your attention.

Best regards,  
Karen

---

<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, 2018, 8:33am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788/2 "2018-07-14T08:33:00Z")

</div>

Try with:

```auto
Long64_t nentries = datatree->GetEntries();
for (Long64_t ientry = 0; ientry < nentries; ientry++) {
  if (datatree->GetEntry(ientry) < 1) {
    std::cout << "Event " << ientry << " : Break!" << std::endl;
    break;
  }
  // some precautions ...
  if ( (!B_kaon_px) || (!B_kaon_py) || (!B_kaon_pz) ||
       (B_kaon_px->size() != B_kaon_py->size()) ||
       (B_kaon_px->size() != B_kaon_pz->size()) ) {
    std::cout << "Event " << ientry << " : Ouch!" << std::endl;
    continue;
  }
  unsigned long n = B_kaon_px->size();
  std::cout << "Event " << ientry << " : n = " << n << std::endl;
  for (unsigned long i = 0; i < n; i++) {
    TLorentzVector Kaon;
    Kaon.SetPxPyPzE(B_kaon_px->at(i), B_kaon_py->at(i), B_kaon_pz->at(i), 0);
    Double_t r = Kaon.Pt();
    std::cout << "Pt = " << r << std::endl;
  }
}

```

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your post has been edited above).

---

<div class="post-metadata">

**Author:** ![KarenDmn](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@KarenDmn](https://root-forum.cern.ch/u/KarenDmn)\
**Post date:** [July 16, 2018, 7:16am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788/3 "2018-07-16T07:16:15Z")

</div>

Hi Wile\_E\_Coyote, thanks a lot for your advice,

And as a result, I obtain

```auto
Event 417141 : n = 3
Pt = 0.532169
Pt = 0.626101
Pt = 0.595954
Event 417142 : n = 3
Pt = 0.932717
Pt = 0.780085
Pt = 1.04427
Event 417143 : n = 5
Pt = 0.791809
Pt = 0.526647
Pt = 1.08711
Pt = 1.83948
Pt = 1.16798

```

But I think that I should obtain just one Pt, for example for the event 400, (px\_{400}, py\_{400}, pz\_{400},0) I get Pt\_{400} = x, I get more than one Pt because of variable n?

Sorry for disturbing you,

Best regards,

Karen

---

<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 16, 2018, 8:15am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788/4 "2018-07-16T08:15:31Z")

</div>

You need to talk to the one who created this “datatree”.

---

<div class="post-metadata">

**Author:** ![KarenDmn](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@KarenDmn](https://root-forum.cern.ch/u/KarenDmn)\
**Post date:** [July 22, 2018, 4:28am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788/5 "2018-07-22T04:28:34Z")

</div>

Oh well…, anyway thanks a lot for your help 😃

Have a nice day

---

<div class="post-metadata">

**Author:** ![KarenDmn](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/a9a28c/32.png) [@KarenDmn](https://root-forum.cern.ch/u/KarenDmn)\
**Post date:** [August 2, 2018, 7:41am UTC](https://root-forum.cern.ch/t/getting-parameters-of-a-tree-with-entries-type-tlorentzvector/29788/6 "2018-08-02T07:41:12Z")

</div>

Hello, again Wile\_E\_Coyote,

I have a doubt, if the variables B\_kaon\_px, etc, are vectors and not variables, is it possible that when it defines the LorentzVector, it gets three or more Pt of each event because it calculates Pt entry for entry, I mean

First Pt of any event = Result of (First entry of vector B\_kaon\_px, First entry of vector B\_kaon\_py, First entry of vector B\_kaon\_pz)

And due to of that, it gets more than one Pt ¿?

Have a nice day,  
Cheers

Karen 🙂
