Jupyter Notebook with python3 (Python3.4) and ROOT

Hi all,

I’m trying to use Jupyter Notebook with python3 (Python3.4) and ROOT in Linux environment. Firstly, I compile the last release from ROOT (ROOT 6.06/00) with my python3 version, before that I check that the directories contain Python.h and libpython.3.4.so files:

./configure --enable-python --with-python-incdir=/usr/include/python3.4/ --with-python-libdir=/usr/lib/python3.4/ and
make

When I executed my codes written with python 2.6 and ROOT 5.27/06b everything worked. Now, when the codes are executed with python3.4 and ROOT6.06/00 fail, giving us an error message. But, changing the ROOT version doesn’t fix the problems. Here there is a brief summary of error:

My code reads a tree from a rootfile (written in ROOT 5.27/06b). It fails (in Python3.4) reading the tree branch information after the statement SetBranchAddress, so I can 't charge my variable associated.

[code]
import ROOT
from ROOT import gROOT , TCanvas, TH1F
import numpy as n
import array

class Analyzer(object):
""“
Analyzer
”""
def init(self):
’’‘
Analyzer Constructor. Only read the tree from the root file.
’’’
# Get tree from mytree.root file
#self.file = ROOT.gROOT.GetListOfFiles().FindObject(“mytree.root”)
#if not self.file or not file.IsOpen():
self.file = ROOT.TFile(“mytree.root”, “read”)
self.tree = self.file.Get(“muons”)
#Call Init function to initialize the tree and set branches
self.init()

    def init(self):
        '''
        Function to declare the variables for each branch and the histograms for all
        and selected muons
        '''
        ....
        # Define and init the variables for each branch as ROOT vectors
        self.Muon_pt = ROOT.std.vector('float')()
    self.process()
   
        
	#####################################################################
	###                        PROCESS                                ###
#####################################################################

    def process(self):
        '''
        Initialization of the reading process. This function reads the NTuples.root, 
        select the good muons and fill the histograms.
        '''
       self.tree.SetBranchAddress("Muon_pt", self.Muon_pt)
        ...
        
        #Loop over events
        #--------------------------------------------------------------------
        # Get the number of entries(events) of the TTree (file.root)
        numEntries= self.tree.GetEntries()
        # For each event or entry,the following loop populates the tree branches, creates every muon and add it to all_muons list
                    
        
        for event in range(0, self.Muon_pt.size()):

       # Address the data of each physical variable registed in this event or entry number to its branch associated listed above.    
            self.tree.GetEntry(event)
            print (event)
            print (self.tree.Scan("Muon_pt")) # This line works fine
            print (self.Muon_pt.size()) ## this line give us number with at least 10 digits and that has non sense. The size should be, more or less, 3 or 4.  
            print (self.Muon_pt[0]) ## this line breaks the running
            ....
            [/code] 

The last line give us the next error:
IndexError: index out of range

Later, we run the same code compiling ROOT version 5.34 with python3 and the error is:

 self.tree.SetBranchAddress("Muon_pt", self.Muon_pt)

TypeError: none of the 3 overloaded methods succeeded. Full details:
Int_t TTree::SetBranchAddress(const char* bname, void** add,
TBranch** ptr = 0) =>
could not convert argument 1 (must be bytes, not str)
Int_t TTree::SetBranchAddress(const char* bname, void* add, TClass*
realClass, EDataType datatype, Bool_t isptr) =>
takes at least 5 arguments (2 given)
Int_t TTree::SetBranchAddress(const char* bname, void* add, TBranch**
ptr, TClass* realClass, EDataType datatype, Bool_t isptr) =>
takes at least 6 arguments (2 given)

To end, I probe other ROOT versions with the Python3.4 including 5.29, 5.27/06b/a, 5.28.00b, 5.30, but they didn’t compile because bin/rmkdepend: Command not found.

Please help!

Thanks and cheers!

Hi,

I don’t see where the notebooks are involved: could you clarify?
We are investigating the pyROOT issue you reported with the ROOT6 series.

Danilo

Thanks for your quick reply.

Because Jupyter needs to execute the code in Python3, not in Python2.7 where my code runs very well.

Hi,

you can have Jupyter based on Python3 but a Python2 kernel which works flawlessly with ROOT.
Danilo

Thanks!

That’s OK, but that’s the problem too. I need Jupyter based on Python3 for my purpose.

[quote=“dpiparo”]Hi,

you can have Jupyter based on Python3 but a Python2 kernel which works flawlessly with ROOT.
Danilo[/quote]

Hi,

I don’t understand the issue. The language in which Jupyter is written is independent of the languages supported by kernels by construction.
For example, have a look here
mybinder.org/repo/cernphsft/rootbinder
you can start both a Python2 or Python3 kernel.

Cheers,
Danilo

Thanks =D> !

I missunderstand my problem and i thinked that Jupyter requires python 3.3 or greater. Otherwise, that page is amazing!! I’m going to compile ROOT with python2.7 and probe my code. But i’m still having issues with python3.

Thanks again!

Cheers,
Aida

[quote=“dpiparo”]Hi,

I don’t understand the issue. The language in which Jupyter is written is independent of the languages supported by kernels by construction.
For example, have a look here
mybinder.org/repo/cernphsft/rootbinder
you can start both a Python2 or Python3 kernel.

Cheers,
Danilo[/quote]

Hi Aida,

glad that you are not stuck anymore.
I am aware that the Python3 issue needs an action from our side: I am investigating.

Cheers,
Danilo

Hi,
I tried your link and python 3 is not working but indeed python2 works fine

Hi Aishen,

indeed not all Python3 issues have been solved yet.

Cheers,
Danilo