A couple of weeks ago, I was asked to prepare a lecture for bachelor students on ROOT for the advanced laboratory in Physics at the University of Freiburg. I knew the students had prior knowledge in Python, so I decided to show them PyROOT. One of the obvious benefits is that data files can be read really easily. While developing the talks, I wanted to add a CI pipeline to test all the code example on my slides. (I think there is nothing worse than a talk with buggy code examples, see doxec, a tool to test code examples). I quickly realized that there is no Docker image for ROOT using Python 3.

Well, there is now: You can pull the image for my Docker Hub repository with

docker pull sauerburger/pyroot3

In this article, I will show an example CI configuration to test a PyROOT 3 code example. We have a simple text file with measured decay energies. Each line corresponds to one decay. The task is to read the data and visualize them in a histogram. The following code snippet should do the trick.

# histogram.py  -- This should be Python 3.
import ROOT

canvas = ROOT.TCanvas()
histo = ROOT.TH1F("", "Decay measurement;Channel", 32, 0, 32)

with open("decay_data.txt") as data:
  for line in data:
    histo.Fill(float(line))

histo.Draw()
canvas.SaveAs("decay.eps")

We also want to have a CI job to check that the code runs and that a histogram is created. The definition of the CI job is rather short. We execute the program. If the program fails or if the file decay.eps does not exist, the pipeline should fail. This can be achieved with the following .gitlab-ci.yml.

decay_data:
  image: sauerburger/pyroot3
  script:
    - python3 histogram.py
    - test -f decay.eps || exit 1

To see all the files in action, check out the pyroot_tests repository.