Listing all variables available in an event


Is it possible to retrieve a list of all the variables that are available for a given event in a TTree? Functions like dir(event) don’t seem to show, say, ph_n etc.



the variables are created on the fly and never cached (in CppyyROOT they’re all created on first access through a different mechanism that does allow caching, and hence you’ll find them in dict). You can use things like TTree.Print() or if you need the actual names, something like:names = [b.GetName() for b in tree.GetListOfBranches()]might do. And if you want, you can define your own dir that way (for p2.6 and later) and attach it to the TTree class.


Ah, gotcha. Yeah, the names are all I wanted. Worked like a charm, thanks!

I find tree.Show(0)ing an artbirary event is more compact than tree.Print()ing, since I don’t care about basket sizes and stuff. For some types of branches kept in trees this might be less compact.

Incidentally for getting the list of items in a TFile, I use a little function:

def hists_from_file(filepath): """Function used by loadhists to load the histograms from a single file located at filepath. The return value is a tuple with the rootfil e object and the dict of histograms from that file.""" histos = {} rootfile = r.TFile(filepath) listofkeys = rootfile.GetListOfKeys() tit = r.TIter(listofkeys) tit.Reset() key = tit.Next() while key: histoname = gethistoname(key) histos[histoname] = rootfile.Get(histoname) key = tit.Next() return rootfile,histos
The function calls them all histograms, but it will also return any TTrees or other objects with keys in the file.

I also had to write a helper function to return the actual .Name() of an object with a given TKey, so I could later use the name in TTree->Draw(“some_name”) commands…

def gethistoname(key): """From a ROOT TKey, this returns the actual name of the histogram associated with the key. """ keystr = str(key) try: first = keystr.find('"',0,-1) second = keystr.find('"',first+1,-1) if first==-1 or second==-1: raise ValueError("Could not find double quotes in key name.") else: return keystr[first+1:second] except AttributeError: print 'Call gethistoname(key) with a key, or something which can be str()ed'