#include "TFile.h" #include "TTree.h" #include "TBranch.h" #include "TClonesArray.h" #include "TLorentzVector.h" #include int Analyze_LHE() { TFile *lhe = new TFile("lhe.root", "READ"); if (!(lhe && lhe->IsOpen())) { delete lhe; return -1; } TTree *tree; lhe->GetObject("tree", tree); if (!tree) { delete lhe; return -2; } tree->SetMakeClass(1); // all branches in decomposed object mode tree->SetBranchStatus("*", 0); // disable all branches TClonesArray *Llep0_ = new TClonesArray("TLorentzVector"); tree->SetBranchStatus("Llep0", 1); // activate "Llep0" tree->GetBranch("Llep0")->SetAutoDelete(kFALSE); tree->SetBranchAddress("Llep0", &Llep0_); Long64_t nevent = tree->GetEntries(); std::cout << "####### tree->GetEntries() = " << nevent << std::endl; for (Long64_t i = 0; i < nevent; i++) { if (Llep0_) Llep0_->Clear(); tree->GetEntry(i); std::cout << "i = " << i << std::endl; if (!Llep0_) continue; // just a precaution Int_t nvectors = Llep0_->GetEntriesFast(); std::cout << "Llep0_->GetEntriesFast() = " << nvectors << std::endl; for (Int_t j = 0; j < nvectors; j++) { TLorentzVector *v = ((TLorentzVector *)(Llep0_->At(j))); if (!v) continue; // skip an "empty slot" std::cout << "Pt[" << j << "] = " << v->Pt() << std::endl; } } tree->ResetBranchAddresses(); // "detach" from local variables delete lhe; // it will automatically delete "tree", too delete Llep0_; // cleanup return 0; }