Learning 4 Programming languages at the same time: C++ ,C , Python and Javascript¶

try commands from https://root.cern.ch/notebooks/HowTos/HowTo_ROOT-Notebooks.html but I set default to c++, so you need to use %%python when you do python code.

in order to execute code press shift+enter

Now with the new ROOT Python kernel you should first do

import ROOT

Then you can get cpp interpreter inside the notebook with:

cxx
cout <<"hello"

The main function in c++ or c is not needed but we will show them from time to time.

%jsroot on switches on the interactive root plots

In [2]:
import ROOT
In [3]:
%jsroot on
In [4]:
h = ROOT.TH1F("gauss","Example histogram",100,-4,4)
h.FillRandom("gaus")
In [5]:
c = ROOT.TCanvas("myCanvasName","The Canvas Title",800,600)
h.Draw()
In [6]:
c.Draw()

c++ and python work together

In [7]:
%%cpp
    gauss->Fit("gaus", "S");
    myCanvasName->Draw();
****************************************
Minimizer is Minuit2 / Migrad
Chi2                      =      65.0502
NDf                       =           82
Edm                       =  4.60684e-07
NCalls                    =           55
Constant                  =      157.425   +/-   2.76819     
Mean                      =    0.0138321   +/-   0.0143799   
Sigma                     =      1.00178   +/-   0.0104857    	 (limited)

Installing fortran magics¶

In [8]:
%pip install fortran-magic==1.0.0a2
Requirement already satisfied: fortran-magic==1.0.0a2 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (1.0.0a2)
Requirement already satisfied: ipython in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from fortran-magic==1.0.0a2) (9.12.0)
Requirement already satisfied: meson in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from fortran-magic==1.0.0a2) (1.11.1)
Requirement already satisfied: ninja in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from fortran-magic==1.0.0a2) (1.13.0)
Requirement already satisfied: numpy in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from fortran-magic==1.0.0a2) (2.4.3)
Requirement already satisfied: decorator>=5.1.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (5.2.1)
Requirement already satisfied: ipython-pygments-lexers>=1.0.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (1.1.1)
Requirement already satisfied: jedi>=0.18.2 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (0.19.2)
Requirement already satisfied: matplotlib-inline>=0.1.6 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (0.2.1)
Requirement already satisfied: pexpect>4.6 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (4.9.0)
Requirement already satisfied: prompt_toolkit<3.1.0,>=3.0.41 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (3.0.52)
Requirement already satisfied: pygments>=2.14.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (2.20.0)
Requirement already satisfied: stack_data>=0.6.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (0.6.3)
Requirement already satisfied: traitlets>=5.13.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from ipython->fortran-magic==1.0.0a2) (5.14.3)
Requirement already satisfied: wcwidth in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from prompt_toolkit<3.1.0,>=3.0.41->ipython->fortran-magic==1.0.0a2) (0.6.0)
Requirement already satisfied: parso<0.9.0,>=0.8.4 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from jedi>=0.18.2->ipython->fortran-magic==1.0.0a2) (0.8.6)
Requirement already satisfied: ptyprocess>=0.5 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from pexpect>4.6->ipython->fortran-magic==1.0.0a2) (0.7.0)
Requirement already satisfied: executing>=1.2.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython->fortran-magic==1.0.0a2) (2.2.1)
Requirement already satisfied: asttokens>=2.1.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython->fortran-magic==1.0.0a2) (3.0.1)
Requirement already satisfied: pure_eval in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from stack_data>=0.6.0->ipython->fortran-magic==1.0.0a2) (0.2.3)
Note: you may need to restart the kernel to use updated packages.
In [9]:
import fortranmagic
In [10]:
fortranmagic.__version__
Out[10]:
'1.0.0a2'
In [11]:
%load_ext fortranmagic
In [12]:
import os
import sys

import numpy as np
f_config=""
if sys.platform.startswith("win"):
        # Depends of system, python builds, and compilers compatibility.
        # See below.
    f_config = "--fcompiler=gnu95 --compiler=mingw32"
else:
        # For Unix, compilers are usually more compatible.
    f_config = ""

    # Disable only deprecated NumPy API warning without disable any APIs.
#f_config += " --extra '-DNPY_NO_DEPRECATED_API=0'"

%fortran_config {f_config}
Current defaults arguments for %fortran:
	 --extra '-DNPY_NO_DEPRECATED_API=0'
In [13]:
%%fortran

subroutine f1(x, y, z)
    real, intent(in) :: x,y
    real, intent(out) :: z

    z = sin(x+y)

end subroutine f1
In [14]:
f1(1.0, 2.1415)
Out[14]:
9.26574066397734e-05

R¶

In [15]:
%pip install rpy2 pandas
Requirement already satisfied: rpy2 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (3.6.7)
Requirement already satisfied: pandas in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (3.0.2)
Requirement already satisfied: rpy2-rinterface>=3.6.6 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from rpy2) (3.6.6)
Requirement already satisfied: rpy2-robjects>=3.6.5 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from rpy2) (3.6.5)
Requirement already satisfied: numpy>=2.3.3 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from pandas) (2.4.3)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)
Requirement already satisfied: cffi>=1.15.1 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from rpy2-rinterface>=3.6.6->rpy2) (2.0.0)
Requirement already satisfied: pycparser in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from cffi>=1.15.1->rpy2-rinterface>=3.6.6->rpy2) (2.22)
Requirement already satisfied: jinja2 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from rpy2-robjects>=3.6.5->rpy2) (3.1.6)
Requirement already satisfied: tzlocal in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from rpy2-robjects>=3.6.5->rpy2) (5.3.1)
Requirement already satisfied: MarkupSafe>=2.0 in /home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages (from jinja2->rpy2-robjects>=3.6.5->rpy2) (3.0.3)
Note: you may need to restart the kernel to use updated packages.
In [16]:
%load_ext rpy2.ipython
Error importing in API mode: ImportError('/home/k/miniforge3/envs/ROOT/lib/python3.14/site-packages/_rinterface_cffi_api.abi3.so: undefined symbol: R_ClosureEnv')
Trying to import in ABI mode.
In [17]:
%%R
1+2
[1] 3