Step 4: Trees! (C++ Version)

Super-K Code Example 3: Trees

This example is similar to the first two, except that you will now create an TTree (which we sometimes refer to as an ntuple, based on the PAW term), and use ROOT to view it. ROOT tutorials (here and here) may be useful.  Also, remember to make your plots look nice!

Trees are quite a powerful feature of ROOT: they allow you to look at the variables associated with a bunch of events, and plot any variable versus any other; you can also make selections using other variables. For instance, it is very easy to ask ROOT to do something like: “plot a vs b for all events for which c is greater than 2″.

This example produces the tree from inside the same code which reads the zbs file.  For a “quick and dirty” way to generate ntuples, see the Fortran version of this example.

Files for this example

Copy the following files from


into your working directory:

  • contains the source code for the program
  • fort_fopen.F
  • a shell script for running the program
  • The same GNUmakefile as before can be used.

Take a look at the differences between and

  • We set up an output root file, example3cout.root.
  • We define variables to go in our tree, and create a tree called tqtree.
  • For each event in the hit loop, we set the tree variables, and then fill the tree.
  • At the end of the event and file loops, it closes the root output file.

Compile and run this program in the same way as for examples 1 and 2. You should get less output to the screen (because the output goes to the file instead of the screen), but you should get a file called example3cout.root created. This root file contains the tree (or ntuple, whatever you like to call it).

Working with the ntuple

  • We’ll run root again, but, this time, let’s load the script immediately!
  • Enter the following line:
    root -l example3cout.root

    This opens root (without the splash screen) and immediately loads the contents of that root file.

    Now that we have a tree, let’s plot charge vs. time.  It is very easy to plot variables from a tree in ROOT.  The format is:

    tree_name->Draw("var1:var2", "any_cuts", "formatting");

    So, let’s do….


    Now we have our plot!

Stuff to try

After you’ve gotten this working try some of the following:

  • Note that tqtree->Print() shows all the variables in the tree.
  • Try plotting other variables versus each other.
  • Try putting “cuts” on the variables. For example,
  • tqtree->Draw("tisk:qisk", "tisk> 1000 & tisk < 2000", "colz");
  • That will draw time vs. charge for times between 1000 and 2000.  The format there on the end (colz) will draw it using colors rather than density of dots, and add a scale.
  • Change the C++ source code to add new variables.  For instance, add skhead_.idtgsk to the variable list: this is the trigger type of the event.
  • Try plotting distributions of single variables: for instance tqtree->Draw("qisk"); will plot just a histogram of charge. Try putting cuts on these distributions, too (for example, see how the charge varies depending on the trigger).
  • Another thing to note: the root file containing the ntuple is much smaller than a text file containing exactly the same information.  You’ll only really notice this if you do the “quick and dirty” method for making the ntuple.  Can you guess (or find out) why?

Back to the tutorials top page