Read tree with STL branches

I recently have to move to from pyroot to root because of performance issue in python and now I stuck in every direction that I move. I have some Tree with this structure:

TauJet = 0 TauJet_E = (vector<float>*)0x4250f40 TauJet_Px = (vector<float>*)0x4250e90 TauJet_Py = (vector<float>*)0x42510f0 TauJet_Pz = (vector<float>*)0x4253e60 TauJet_Eta = (vector<float>*)0x42541d0 TauJet_Phi = (vector<float>*)0x4254540 TauJet_PT = (vector<float>*)0x42548b0 TauJet_Charge = (vector<float>*)0x4254c20 TauJet_NTracks = (vector<int>*)0x425c860 TauJet_EHoverEE = (vector<float>*)0x425cb10 TauJet_size = 0 Jet = 6 Jet_E = (vector<float>*)0x425cc50 Jet_Px = (vector<float>*)0x43931b0 Jet_Py = (vector<float>*)0x439c040 Jet_Pz = (vector<float>*)0x43a4ed0 Jet_Eta = (vector<float>*)0x43add60 Jet_Phi = (vector<float>*)0x43b6bf0 Jet_PT = (vector<float>*)0x43bfa80 Jet_Btag = (vector<int>*)0x43c8910 Jet_NTracks = (vector<int>*)0x43d17a0 Jet_EHoverEE = (vector<float>*)0x43da630 Jet_size = 6 Electron = 0 Electron_E = (vector<float>*)0x43f3240 Electron_Px = (vector<float>*)0x43fc270 Electron_Py = (vector<float>*)0x44052a0 Electron_Pz = (vector<float>*)0x440e2d0 Electron_Eta = (vector<float>*)0x4417300 Electron_Phi = (vector<float>*)0x4420330 Electron_PT = (vector<float>*)0x4429360 Electron_Charge = (vector<float>*)0x4432390 Electron_IsolFlag = (vector<int>*)0x443b3c0 Electron_SumPt = (vector<float>*)0x443b760 Electron_EtaCalo = (vector<float>*)0x443bad0 Electron_PhiCalo = (vector<float>*)0x443be70 Electron_EHoverEE = (vector<float>*)0x443c210 Electron_FlagLoose = (vector<int>*)0x4468590 Electron_FlagMedium = (vector<int>*)0x44715f0 Electron_FlagTight = (vector<int>*)0x447a650 Electron_size = 0 Muon = 0 Muon_E = (vector<float>*)0x4493430 Muon_Px = (vector<float>*)0x449c460 Muon_Py = (vector<float>*)0x44a5490 Muon_Pz = (vector<float>*)0x44ae4c0 Muon_Eta = (vector<float>*)0x44b74f0 Muon_Phi = (vector<float>*)0x44c0520 Muon_PT = (vector<float>*)0x44c9550 Muon_Charge = (vector<float>*)0x44d2580 Muon_IsolFlag = (vector<int>*)0x44db5b0 Muon_SumPt = (vector<float>*)0x44e45e0 Muon_EtaCalo = (vector<float>*)0x44ed610 Muon_PhiCalo = (vector<float>*)0x44f6640 Muon_EHoverEE = (vector<float>*)0x44ff670 Muon_EtRatio = (vector<float>*)0x45086a0 Muon_ID = (vector<int>*)0x45116d0 Muon_size = 0 Photon = 7 Photon_E = (vector<float>*)0x452a480 Photon_Px = (vector<float>*)0x4533150 Photon_Py = (vector<float>*)0x453be20 Photon_Pz = (vector<float>*)0x4544af0 Photon_Eta = (vector<float>*)0x454d7c0 Photon_Phi = (vector<float>*)0x4556490 Photon_PT = (vector<float>*)0x455f160 Photon_EHoverEE = (vector<float>*)0x4567e30 Photon_size = 7 Tracks = 102 Tracks_Eta = (vector<float>*)0x45808a0 Tracks_Phi = (vector<float>*)0x4588cf0 Tracks_EtaOuter = (vector<float>*)0x4591140 Tracks_PhiOuter = (vector<float>*)0x4599590 Tracks_PT = (vector<float>*)0x45a1a20 Tracks_E = (vector<float>*)0x45a9e70 Tracks_Px = (vector<float>*)0x45b22c0 Tracks_Py = (vector<float>*)0x45ba710 Tracks_Pz = (vector<float>*)0x45c2b60 Tracks_Charge = (vector<float>*)0x45cafb0 Tracks_Vx = (vector<float>*)0x45d3400 Tracks_Vy = (vector<float>*)0x45db850 Tracks_Vz = (vector<float>*)0x45e3ca0 Tracks_size = 102 ETmis = 1 ETmis_ET = (vector<float>*)0x45fbe70 ETmis_Phi = (vector<float>*)0x4604ed0 ETmis_Px = (vector<float>*)0x460df30 ETmis_Py = (vector<float>*)0x4616f90 ETmis_size = 1 CaloTower = 333 CaloTower_Eta = (vector<float>*)0x462fd70 CaloTower_Phi = (vector<float>*)0x46386c0 CaloTower_E = (vector<float>*)0x4641010 CaloTower_E_em = (vector<float>*)0x4649960 CaloTower_E_had = (vector<float>*)0x46522b0 CaloTower_ET = (vector<float>*)0x465a3a0 CaloTower_size = 333

what I need to do is for each event, read the branches and put them in a nested vector. for example
for Event_Vec = [[TauJet], [Jet], [Electro], [Muon], [Photon], [Tracks], [ETmis], [CaloTower]] where each element again is a vector:

[TauJet] = [TauJet_E,TauJet_Px,TauJet_Py,TauJet_Pz,TauJet_Eta,TauJet_Phi,TauJet_PT,TauJet_Charge,TauJet_NTracks,TauJet_EHoverEE,TauJet_size]
and each element here is a vector of float.

Now my first question is there any built-in in root to do this? if not, how should I access the vector in each branch? right now I can read one branch using this code:

gROOT->ProcessLine("#include <vector>"); vector<float> *Jet_E = new vector<float>(); tree->SetBranchAddress("Jet_E", &Jet_E);

is this the correct way? do I have to do these steps for every branch?
Thanks in advance

At some point some code will have to exist that has those lines for each branch that you want to use. Fortunately you don’t have to type it by hand, as some TTree methods are available to generate that code for you.

See TTree::MakeClass and MakeSelector:

I’ve heard people say that you should use MakeSelector over MakeClass, but the difference is still not clear to me. I use MakeClass since it seems to generate slightly simpler code. The generated code has a preferred structure unfortunately, so you’ll be constrained to use/derive from the “Analysis Class” that is generated and its methods unless you want to modify it heavily.


Thanks it looks something helpful. I still have a question after using myClass.GetEntry(). How should I build my nested vector? Should I make my own nested array and fill it in by loopping through each branch?


The TTree will create the object as needed when you call GetEntry.