# Walkthrough: Using the Python Analyze script (10 minutes)
As it stands, the Analyze script does nothing, but let's learn how to
run it anyway. Hit SHIFT-ENTER in the cell to run the script.[^f93]
Python will pause as it reads through all the events in the Tree. Since
we haven't included any analysis code yet, you won't see anything else
happen.
Let's start making histograms. In the Set-up section, insert the
following code:
chi2Hist = TH1D("chi2","Histogram of Chi2",100,0,20)
In the Loop section, put this in:
chi2 = mychain.chi2
chi2Hist.Fill(chi2)
This goes in the Wrap-up section:
canvas = TCanvas()
chi2Hist.Draw()
canvas.Draw()
:::{note}
Don't forget about the indentation. The lines in the Loop section must
be indented to show they're part of the loop.
:::
Execute your revised script.
:::::{note}
Finally, we've made our first histogram with a Python script. In the
Set-up section, we defined a histogram; in the Loop section, we filled
the histogram with values; in the Wrap-up section, we drew the
histogram.
How did I know which bin limits to use on `chi2Hist`? Before I wrote
the code, I drew a test histogram:
:::{code-block} python
import ROOT
myFile = ROOT.TFile("experiment.root")
tree1 = ROOT.gROOT.FindObject("tree1")
acanvas = TCanvas()
tree1.Draw("chi2")
acanvas.Draw()
:::
Hmm, the histogram's axes aren't labeled. How do I put the labels in the
script? Here's how I figured it out: I went back to command-line ROOT
from {ref}`the basics` and plotted `chi2` with the Treeviewer. I labeled the
axes on my test histogram by right-clicking on them and selecting
{menuselection}`SetTitle`. I saved the canvas by selecting {menuselection}`File --> Save --> c1.C`. I looked at c1.C and saw these commands in the file:
chi2->GetXaxis()->SetTitle("chi2");
chi2->GetYaxis()->SetTitle("number of events");
I scrolled up and saw that ROOT had used the variable `chi2` for the
name of the histogram pointer. I copied the lines into my Python script,
but used the name of my histogram instead, and converted the C++ lines
into Python. This usually means replacing "->" with "." and
removing the semi-colon from the end:
chi2Hist.GetXaxis().SetTitle("chi2")
chi2Hist.GetYaxis().SetTitle("number of events")
:::::
Try this yourself: add the two lines above to the Set-up section, right
after the line that defines the histogram. Test the revised script.[^f94]
:::{figure-md} game_theory-fig
:class: align-center
by Randall Munroe.
Fortunately it's easier to analyze histograms than it is to analyze
love. At least it is for me!
:::
[^f93]: You may want to organize your scripts in files outside of
notebook cells. This lets you keep track of different versions of
your scripts, and allows you to use your favorite text editor. To
run an external script from within a notebook cell, use the `%run`
magic command; e.g.,
%run Analyze.py
You can save the contents of notebook cells by putting the
`%%writefile` cell magic command at the top of the cell and
hitting SHIFT-ENTER; e.g.,
%%writefile AnalyzeChi2.py
[^f94]: There's another way to do this in the notebook. Plot the graph
with JSROOT:
%jsroot on
acanvas.Draw()
You can then right-click on the axes, select `Title->SetTitle`,
and enter the axis label you want. However, this solution can't be
automated. If you have to generate a hundred histograms each with
different axis labels, you'll want a method you can put into a
script.