Particle Hunter

ROOT on Colab


Update [July 2023]: Google Colab moved the default python version to python3.10 and dropped python3.6 support so the original method will not work. I have built ROOT and updated the code to work with this version.

Several months ago, I needed to use ROOT on Google Colab. I found a few suggesetions on the web, but none of them worked for me. So, I decided explore and try to make it work myself tutorial. In this article, I will show you how to install ROOT on Google Colab and how to use it in colab Jupyter notebook session.

First I did the build from source part that is completely described in the ROOT installation guide. and uploaded the ROOT folder to a github repository. Then I created a new colab notebook and added the following code to the first cell:

!wget https://github.com/MohamedElashri/ROOT/releases/download/ubuntu/root_v6.28.04_Ubuntu_20.04.zip
!unzip /content/root_v6.28.04_Ubuntu_20.04.zip
!apt-get install git dpkg-dev cmake g++ gcc binutils libx11-dev libxpm-dev libxft-dev libxext-dev tar gfortran subversion

The first line of this code download a tar file containting the ROOT folder from my github repository. The second line unzip the tar file. The third line extract the ROOT folder from the tar file. The last line install the required packages to build ROOT.

Then I added the following code to the second cell:

import sys
sys.path.append("/content/root_build/")
sys.path.append("/content/root_build/bin/")
sys.path.append("/content/root_build/include/")
sys.path.append("/content/root_build/lib/")
import ctypes
ctypes.cdll.LoadLibrary('/content/root_build/lib//libCore.so')
ctypes.cdll.LoadLibrary('/content/root_build/lib//libThread.so')
ctypes.cdll.LoadLibrary('/content/root_build/lib//libTreePlayer.so')

The first line of this code add the ROOT folder to the python path. The second line add the bin folder to the python path. The third line add the include folder to the python path. The last line add the lib folder to the python path. Then it load the ROOT libraries.

Now we are ready to use ROOT in colab. I added the following code to the third cell:

import ROOT

h = ROOT.TH1F("gauss","Example histogram",100,-4,4)
h.FillRandom("gaus")
c = ROOT.TCanvas("myCanvasName","The Canvas Title",800,600)
h.Draw()
c.Draw()

You can see the output of this code in the following image:

ROOT example output

Also you can find example notebook on Colab here that you can use directly.

#ROOT #Colab #Jupytrer #LHC #CERN #Notebook