Tutorials Step 3: Histograms! (Fortran)

Super-K Code Example 2: Making Histograms

This example is similar to the first, except that you will now create, fill, and output histograms, and use a program called ROOT to view them. You may want to have a look at a ROOT tutorial (such as this one) first.  Be sure to understand how to start (and close) ROOT, what histograms are, and how to do things with them.  Also, remember to make your plots look nice!

Files for this example

Copy the following files from

$TUTEXAMPLES/tutorials

into your working directory or whatever subdirectory of it you like:

  • example2.F: contains the source code for the program
  • example2.sh: a shell script for running the program
  • The GNUmakefile used for example1 can be used for compiling example2 also.
  • example2root.C: A list of ROOT commands to plot the histogram you've created.

Take a look at the differences between example1.F and example2.F. You will see that several lines having to do with histograms have been added, which call HBOOK routines. HBOOK is a CERN library histogramming package, and the histograms it creates can be read by PAW.  In the Duke neutrino group, we prefer ROOT to PAW (ROOT was made to replace PAW, it is C++ based, with many more features than PAW).  While the fortran code produces a hbook file, we can convert it into root format immediately, and then look at the produced histogram in ROOT.

  • Before the file and event loops, the line
    call hbook1(100,'Example 2 charge',100,0., 100., 0.)

    creates in memory histogram no. 100 (the first argument), with 50 bins going from 0 to 100.  That last parameter should be 0., don’t worry too much about why.

  • For each hit in the loop,
     call hf1(100, qisk(ihcab(ihit)), 1.)

    “fills” the histogram with that hit’s charge value. In other words, it increments the bin of the histogram corresponding to qisk for tube ihcab(ihit) by 1.

  • At the end of the event and file loops, it outputs the histogram to a file using
     call hrput(0,'example2.hbk','N')

Compile this program in the same way as for example 1. Now, this time we will call it from a shell script.  Take a look at example2.sh.  The commands here are similar to what you’d type in on the command line (though there are some added comments).  You can see that this script will run the example2 executable with one argument, and then convert the outputted hbk file to a root file.  You will likely be using a lot of shell scripts in the future, but shell scripts are mostly for executing code in an organized way or file manipulation, so we won’t get into any fancy syntax there.  Note that your .tcshrc file is technically a shell script too!

You can use the same data file you used last time.  You should get less output to the screen (because many of the print statements have been removed), but you should get a file called example2.root created (it is produced through the intermediate example2.hbk). This contains the histogram created and filled by the program.

Viewing the histogram

  • Run the command “root”.  After a brief visit from some kind of tree woman thing on the splash screen (run root -l to escape her time consuming gaze), ROOT will be running!  We are now in the interpreter, where we can enter commands, and ROOT will execute them, one-by-one.  The commands used here are based on C++.
  • Enter the following lines:
    TFile * fin = TFile::Open("./example2.root")
    .ls
    h100->Draw()

    The first line opens the file.  We’ve given the file pointer the name “fin” in case we want to do something with it later, though we won’t at this time.  The next line, “.ls”, lists all objects now loaded in memory.  We see that, having opened that file, the histogram h100 is now available for use!  Finally, the last line draws the histogram.

    If you want to exit out of ROOT at any time, just type .q into the interpreter.

    We could accomplish the same by running a script which has these commands in it.  You can try this as well.  Start a new ROOT session, and type:

    .x ./example2root.C

    This will execute the commands in example2root.C, and plot the histogram again.  Quit ROOT in the same way as before.

Stuff to try

After you’ve gotten this working and viewed the charge histogram, try some of the following. In the source code (example2.F), change the histogram creation calls to:

  • change the number of bins.
  • change the x range plotted.
  • create an additional hit time histogram with appropriate range.
  • create a 2D histogram of charge vs time.

For the above, you may need to consult the HBOOK docs (herehere, and here, for example).

Also, inside ROOT, try:

  • getting a log scale (can be done graphically or by command line).
  • putting more than one plot on a page (from the ROOT command line).
  • superimposing more than one histogram (from the ROOT command line).
  • changing other display options (graphically or from the command line).

Next: ntuples

Back to the tutorials top page