Skip to content

Instantly share code, notes, and snippets.

@raggleton
Created November 13, 2014 18:04
Show Gist options
  • Save raggleton/433326f052a841ca7b98 to your computer and use it in GitHub Desktop.
Save raggleton/433326f052a841ca7b98 to your computer and use it in GitHub Desktop.
How to loop over TFile with BRanches etc
#include <iostream>
#include <TChain.h>
#include "TFile.h"
#include "TDirectoryFile.h"
#include "TTree.h"
#include "DeltaR_Matcher.h"
int main() {
std::cout << "Looping test" << std::endl;
// Do conversions better!
TFile * inFile = TFile::Open("test/L1Tree.root");
TDirectoryFile * tdf = (TDirectoryFile*) inFile->Get("l1ExtraTreeProducer");
TTree * tree = (TTree*) tdf->Get("L1ExtraTree");
tree->SetMakeClass(1); // MAGIC FUNCTION MAKES THIS ALL WORK SOMEHOW
tree->Print();
// tree->MakeCode("treeCode.C");
// tree->MakeClass("treeCode");
TLorentzVector * l = new TLorentzVector();
TBranch * b_L1Extra_cenJetEt = tree->GetBranch("cenJetEt");
TBranch * b_L1Extra_cenJetEta = tree->GetBranch("cenJetEta");
TBranch * b_L1Extra_cenJetPhi = tree->GetBranch("cenJetPhi");
std::vector<double> cenJetEt;
std::vector<double> cenJetEta;
std::vector<double> cenJetPhi;
tree->SetBranchAddress("cenJetEt", &cenJetEt, &b_L1Extra_cenJetEt);
tree->SetBranchAddress("cenJetEta", &cenJetEta, &b_L1Extra_cenJetEta);
tree->SetBranchAddress("cenJetPhi", &cenJetPhi, &b_L1Extra_cenJetPhi);
std::vector<TLorentzVector*> vecs;
Long64_t nentries = tree->GetEntriesFast();
std::cout << nentries << std::endl;
for (Long64_t jentry = 0; jentry < nentries; jentry++) {
vecs.clear(); // dodgy as hell cos it doens't delete the pointed to memeory only the pointer itself
tree->GetEntry(jentry);
std::cout << cenJetEt.size() << std::endl;
for (uint i = 0; i < cenJetEt.size(); ++i){
l = new TLorentzVector();
l->SetPtEtaPhiM(cenJetEt[i], cenJetEta[i], cenJetPhi[i], 0);
vecs.push_back(l);
vecs[i]->Print();
}
// std::cout << vecs.size() << std::endl;
// std::cout << vecs[0]->Pt() << std::endl;
// std::cout << cenJetEt[0] << std::endl;
}
// DeltaR_Matcher * m = new DeltaR_Matcher(0.5);
// m->printName();
// m->setRefJets(refJets);
// m->setL1Jets(l1Jets);
// m->setMinRefJetPt(10);
// m->setMinL1JetPt(0);
// m->setMaxJetEta(5.0);
// std::vector<std::pair<TLorentzVector, TLorentzVector>> results = m->produceMatchingPairs();
// for (auto &it: results)
// {
// std::cout << "refjet: " ;
// it.first.Print() ;
// std::cout << " l1jet: " ;
// it.second.Print();
// std::cout << std::endl;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment