Brief ROOT Tutorial
 

     ROOT is installed on all of the machines in the lab and is also freely available for download from http://root.cern.ch.  In general it is an incredibly powerful and complicated data analysis suite; however, it can be used to perform some simple fits and analysis tasks that could be useful for the lab.  Below are three different examples of common tasks that you may want to do in the lab.


Getting Started


    You probably want to do some preliminary things before starting ROOT.  Make a shortcut to the application on the desktop and then edit the properties of this shortcut.  Tell Windows to start the program inside of a directory called “ROOT Scripts” that you can create in the “My Documents” area.  Download scripts to that directory and keep in mind that any paths to scripts you type on the ROOT command line will be relative to that directory.


    Start ROOT.  You should see a welcome screen and a command prompt.  The command prompt in ROOT is a C++ interpreter, so typing C++ directly on the command line is how one tells ROOT to do things.  If you know C++ type a few commands for fun.  I have written a few short C++ scripts that can be used to do simple tasks.


Generating a Histogram From a Set of Data


    This is a task that you did in the Poisson Statistics lab where you had a column of number that represented the number of counts observed for a sequence of trials.  Download this sample data file: data_to_histogram.txt.  You can open it up with a text editor and see that it is just a simple column of numbers.  Move the file to the same directory from which you launched ROOT.  Then also download this script: plotHistogram.C.  Open up this script and examine the commands -- try to understand what they do.  Move it to the same directory where you launched ROOT along side the sample data file.  Now type on the command line .x plotHistogram.C  This will run the script and plot the data.  Right click on one of the data points and open the Fit Panel.  Try fitting to a Gaussian.  Click on Options -> Fit Parameters in the plot window to show the parameters on the plot.  Use log likelihood and chi-square minimization -- which one gives more Poisson-like results for the mean and sigma?


Loading MCA Data into a Histogram


    The labs that use an MCA, e.g., the gamma spectroscopy lab, have on-screen displays for the MCA buffer.  As many of you have realized, it is helpful to save this to analyze later.  This is an example of how to do that using this data file, which is the Cesium-137 gamma ray spectrum: cs137_spe.txt.  Open the file and you can see it is a list of numbers, each number corresponding to a channel in the MCA.  (Understand how this list of numbers is different from the list of numbers in the previous example.)  Download this script: plotSpectrum.C.  Study the code and compare with the previous example.  Run it from the ROOT command line with the command .x plotSpectrum.C  (Again you should be sure the script and data file are in the directory that you started ROOT from.)  Right click on the canvas outside of the plot frame and select SetLogy.  Zoom in on the primary peak in the spectrum and fit to a Gaussian plus a linear background by again right clicking on a data point in the histogram and bringing up the fit panel.  You may need to use the fit panel and fix some of the parameters during the fit to get the final fit to converge.


Fitting Data to a Function


  Frequently you will just have a set of data points that you want to fit to a function.  Download this file: experiment_data.txt.  It contains four columns which are x value, x error, y value, and y error.  (I copied these out of someone’s logbook.)  Then download this script: plotGraph.C.  Study the script syntax.  See how it reads each of the columns into an array of numbers?  Run the script as you ran the above scripts and it will generate a plot.  Right click on one of the points and change the size and style of the marker.  Try to fit this plot to an exponential distribution.  You can do this by right clicking on a data point and bringing up the fit panel.  How well does it fit?  Would it fit better if it decayed away to a constant value instead of zero?  Try defining a function yourself -- use [0], [1], [2], etc. to stand for the first, second, third, and additional free parameters.


Generating Plots


    While you have a plot on the screen you may alter its appearance in many ways.  Right clicking on different regions brings up contextual menus.  Want to turn the best fit line blue?  Right click on the line and select SetLineAttributes.  Frequently the y-axis label will overlap with the numbers.  Put the cursor on the vertical y-axis line and drag to the right.  Then right click on the y-axis numbers and select SetTitleOffset and try a slightly larger value, like 1.5.  Finally, for some annoying reason, the default canvas background in ROOT (at least on Linux versions) is not white but instead light gray.  This makes plots look pretty disgusting when put into a report and printed on white paper.  You can fix this by right clicking on each region of the window and selecting SetFillAttributes.  (If you get really tired of doing this, I can help you setup a script that runs every time you start ROOT to set the backgrounds to white by default.)


Summary


    ROOT is complicated but powerful.  I’ve tried to show you some simple examples that are directly related to what you might do in the lab.  If you want to know how to do something more, e.g., multiple fits on the same graph, adding a legend, fitting to a custom function, etc. just ask me and I can help.  The documentation found on http://root.cern.ch/drupal/content/users-guide is helpful, but assumes you are primarily interacting with ROOT through the C++ command line interface.  Almost every action in ROOT can be put into a C++ script.  You can easily write scripts to fit the data instead of clicking and using the fit panel.  I’ve tried to minimize the C++ in my examples, but those familiar with C++ will probably find that writing scripts is much more efficient than going through a bunch of clicks every time you want to fit new data.  If you need help getting started, let me know and I can send you an example.