Getting parameters of a tree with entries type TLorentzVector

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:

#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

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

Try with:

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).

Hi Wile_E_Coyote, thanks a lot for your advice,

And as a result, I obtain

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

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

Oh well…, anyway thanks a lot for your help :smiley:

Have a nice day

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 :slight_smile: