Printing TTree Branches

I use ROOT, you use ROOT, all of high energy physics uses ROOT.  How many times have you found yourself scrolling through that graphical TBrowser trying to find that one particular TBranch in the TTree?  Like someone sends you the Release17 version of the blah-di-blah centrally produced NTuple and you just want to know if it has “photon_crazyRecoMethod_px” in it.  This crap happens to me all the time.  So I decided to write a little Unix-inspired bash script to solve all my life’s woes; or well..just this one.

The trick here is to get ROOT to echo all the TBranch names to stdout (through TTree::Print()) so that we can use all our normal grep tricks to find that one name we are looking for.  Here is the script in its totality:

#!/bin/bash
#./thisScript treeName fileName
{ echo "TFile f(\"$2\", \"READ\")" ; echo "$1->Print()" ; echo ".q" ; } |\
 root -l -b |\
 grep -v "^[*]Tree" |\
 sed -n -r "s/^[^:]*:([^ ]+)\s+:.*/\1/p"
#EOF

Bash curly braces always freak me out, because they can do several different jobs. In this particular case, we are moving the output of several commands to stdin of another process. Remember in the above script, the $1 and $2 get expanded to the first and second arguments passed to the shell script. Here is, effectively, how the curly braces and ROOT call are interacting…

$> root -l -b
##ROOT ASCII ART###
>>>TFile f("$2", "READ")
>>>$1->Print()
>>>.q
$>

Which causes ROOT to vomit lots of text like this…

...
*Entries : 172375 : Total Size= 23966439 bytes File Size = 13664685 *
*Baskets : 917 : Basket Size= 32000 bytes Compression= 1.75 *
*............................................................................*
*Br 180 :mooreseg_phi : vector<double> *
*Entries : 172375 : Total Size= 23968281 bytes File Size = 8502181 *
*Baskets : 917 : Basket Size= 32000 bytes Compression= 2.82 *
*............................................................................*
*Br 181 :mooreseg_theta : vector<double> *
*Entries : 172375 : Total Size= 23970123 bytes File Size = 14002768 *
*Baskets : 917 : Basket Size= 32000 bytes Compression= 1.71 *
*............................................................................*
...

Since all that goes to stdout we can just grep/sed our way through it until we get a pretty list of branch names.  Now we just have a plain text stream.

$>./treeprint.sh CollectionTree data12_jte.root
...
mooreseg_z
mooreseg_phi
mooreseg_theta
...
$>

Pretty simple huh? How about that for “worse is better”?

Posted in Computing Tagged with: , ,
2 comments on “Printing TTree Branches
  1. Jet says:

    Slick. I always just used MakeCode or MakeClass to spit out the structure, but this is cleverer.

    • Josh says:

      Thats a good idea too, but I thought the MakeClass hammer was a bit too big; also it leaves filesystem side effects. However I do use/grep MakeClass code for an application I have that auto-generates PROOF code. Hopefully I’ll get a page up about that at some point =)

      Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>