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:
- example2c.cc: contains the source code for the program
- fort_fopen.F
- example2c.sh: a shell script to run example2c.
- The GNUmakefile used for example1 can be used for compiling example2 also.
- example2croot.C: A list of ROOT commands to plot the histogram you've created.
Take a look at the differences between example1c.cc and example2c.cc. You will see that several lines having to do with histograms have been added, which call some ROOT functions. While ROOT Is a standalone program, its libraries can be compiled into C++ programs and called from there as well, as is done in this case (we’ll also call ROOT in a standalone way).
- Before the file and event loops, the line
TH1D * histo = new TH1D("histo", "Example 2 Charge", 100, 0, 100);
creates in memory a histogram called histo (the first argument), with 100 bins going from 0 to 100.
- For each hit in the loop,
histo->Fill(skq_.qisk[cablenum-1], 1.0);
“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
histo->Write();
Note that the file had already been opened near the top of the program. Compile this program in the same way as for example 1. You can then run it with (for example)
./example2c.sh $TUTEXAMPLES/rfm_run060960.000096.tqr.zbs
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 example2cout.root created. 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("./example2cout.root") .ls histo->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 histo 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 ./example2croot.C
This will execute the commands in example2croot.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 (example2c.cc), 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 ROOT histogram documentation (here, here, and here, for example), and/or some ROOT tutorials.
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