Reading a std::vector of TLorentzVector from a root file

rather basic question, which was surely already addressed,
i have a root file which contains a vector ofTLorentzVector*
std::vector<TLorentzVector*>
how can i in a COMPILED C++ program access it.
i tried
std::vector<TLorentzVector*> recoJets;
fChain->Branch(“recoJets”, &recoJets );
but it fails at runtime.
can someone point me to a simple example.
thanks.

Hi,

see the tree tutorials. It depends on the tree layout; a common branch definition would require you to do:

std::vector<TLorentzVector*>* pRecoJets = 0; fChain->Branch("recoJets", & pRecoJets );
You also must have a dictionary for std::vector<LorentzVector*>, see root.cern.ch/drupal/content/inte … s-rootcint

Cheers, Axel.

[Corrected to add missing ‘&’ in branch call ]

Hi,

Please note that ‘Branch’ is used to create a new branch in the TTree and not to access an existing one. For this you ought to use

Cheers,
Philippe.

oups you’re right! SetBranchAddress. this cures the error at runtime. i need now to follow the dictionnary link, i guess, because, if for instance, i read back a TLorentzVector* from my ROOT file, its values are zero. For instance:Pt() -> 6.22659e-267

TLorentzVector* recoJet = NULL;
fChain->SetBranchAddress(“recoJet”, recoJet);
for (int i=0; iGetEntries(); i++){
fChain->GetEntry(i);
if (recoJet == NULL) continue;
std::cout << recoJet->Pt() << std::endl;
}

Hi,

The branch recoJet contains a single object, then you need to use:TLorentzVector* recoJet = NULL; fChain->SetBranchAddress("recoJet", & recoJet); Note the addition &. If the branch recoJet contains a vector of pointer to object, then you need to provide the dictionary and you need to use

std::vector<TLorentzVector*>* pRecoJets = 0; fChain->SetBranchAddress("recoJets", & pRecoJets );

Cheers,
Philippe.

ok. using this time the right implementation, i keep getting 0 for the Pt of this TLorentzVector,
Do i need to add the dictionnary on top of that? on the other hand, if i open the ROOT file in a root session, i can view the distribution.

thanks.
TLorentzVector* recoJet = NULL;
fChain->SetBranchAddress(“recoJet”, &recoJet);

[quote]ok. using this time the right implementation, i keep getting 0 for the Pt of this TLorentzVector,
Do i need to add the dictionnary on top of that? on the other hand, if i open the ROOT file in a root session, i can view the distribution. [/quote]The dictionary for TLorentzVector is provided for you. If you can see the distribution via the browser, then the data is correct and you setting of the branch address is incorrect. Can you please send me your data file so that I can give your the correct information.

Cheers,
Philippe.

you’re right, i am using the ROOT shipped with the ATLAS software.
i am a bit puzzled that i cannot access my TLorentzVector here.
you can if you have some time, find the ntuple here:

/afs/cern.ch/user/g/ghodbane/public/analysisPhotonJets.root

the TTree name is PhotonJets.
As i wrote, i am using compiled C+{+ here.
many thanks for your help.

Your branches are NOT std::vector<TLorentzVector*>, but simple TlorentzVector.
To read your file, simply do something like:

void nabil() { TFile *f = TFile::Open("analysisPhotonJets.root"); TTree *T = (TTree*)f->Get("PhotonJets"); Long64_t nentries = T->GetEntries(); TLorentzVector* recoJet = 0; T->SetBranchAddress("recoJet", &recoJet); //same for all other TLorentzVector for (Long64_t i=0; i<nentries; i++){ T->GetEntry(i); if (recoJet == NULL) continue; std::cout << recoJet->Pt() << std::endl; } }

Rene

many thanks to all. it works fine. thanks for the support and sorry for the confusion about pointers!