Step 4: Trees! (Fortran 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 uses a “quick and dirty” way to generate ntuples. The really proper way to do it is to create the ntuple file and write to it directly from HBOOK calls inside the Fortran source code (this applies only if you need to use Fortran). This is most efficient for processing a lot of data. However the method I find more convenient when you’re just playing around with data, and developing analysis methods, is the following: I create a text file containing a list of events and the variables you are interested in. Next, I use a ROOT macro to create the tree and fill it from the text file, from inside ROOT. This works fine for small amounts of data, and it’s easy to make modifications.

Files for this example

Copy the following files from

$TUTEXAMPLES/tutorials

into your working directory:

  • example3.F: contains the source code for the program
  • example3.sh: a shell script for running the program
  • The same GNUmakefile as before can be used.
  • example3froot.C: a root macro to read in the text file and make a tree, which you can then use for plotting.

Take a look at the differences between example1.F and example3.F.

  • Before the file loop, a text file, example3.out is opened using a Fortran open statement.
  • For each event in the hit loop, the output is written to the text file (“logical unit number” 22) rather than dumped to the screen (“logical unit number” 6).
  • At the end of the event and file loops, it closes the text 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 example3.out created. This contains the list of events and their variables.

Creating and working with the ntuple

  • Take a look at example3froot.C.  This script will read in that output text file and make a tree (sometimes called an ntuple, I use the terms mostly interchangeably).  See if you can understand (roughly) how it is doing this.
  • We’ll run root again, but, this time, let’s load the script immediately!
  • Enter the following line:
    root -l example3froot.C

    This opens root (without the splash screen) and immediately runs the script.  Because the script isn’t named inside of the file (it just starts with and opening “{“), the tree it creates will be available to us after the script ends.

    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….

    tqtree->Draw("tisk:qisk");

    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 will draw it using colors rather than density of dots.
  • Change the Fortran source code to add new variables; modify example3froot.C accordingly, to add the new variables to the ntuple. For instance, add 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).
  • Also, note that the macro we ran also created a file, example3out.root.  This file can be loaded in a future root session (you can use root -l example3out.root) to use this ntuple in the future without needing to keep the text file.
  • Another thing to note: the root file containing the ntuple and the text file containing the information have exactly the same information.  However the file size for the root file is much smaller.  Can you guess (or find out) why?