Home | News | Documentation | Download

Read .root files with Python on Windows

Dear ROOTers and Python3rs,

I am interested in ways to browse and manipulate .root files with Python in a Windows environment.

In my research group, we use ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on win32) on VS to process big data. The subsequent manipulation of single final plots become very demanding using c++ and such an obsolete (aka buggy) version of ROOT. Recently, some colleagues started to export our resulting plots into ASCII format to process them with Python 3.7. My goal would be to avoid this last step, browsing into a .root file exporting TH* plots to manipulate them with Py libraries.

Do you have any suggestion? I guess it should be possible without the need of PyROOT, am I right? In this thread there were already some suggestions, but I’d like to go deeper in the subject.

Best regards,

GN


_ROOT Version: 5.34/36
_Platform: Windows 10
_Compiler: VScode / VS15


You can try with the latest version of ROOT 6 on Windows, PyROOT should come with it anyway, even if the new PyROOT will only be in the next version. And @etejedor might correct me, but I think you need PyROOT to read ROOT files with Python

I think https://github.com/scikit-hep/uproot is a good choice. this python package doesn’t need c++ root.

Thank you, I can give it a try! Is there a specific procedure to integrate it with Python or the standard

import ROOT as root

should be enough?

Yes, it should be enough

Thank you, I am digging into the tutorial since a bit and it seems a good compromise!

My main concern is that a root file produced with v5.34 would not be fully readable from v6.* . Could this be possible?

I would be surprised. If it would be the case, please open a Jira ticket or at least report it on this forum.

I have installed ROOT v6.22.02 and set the right ENV variables (ROOTSYS, PATH and PYTHONPATH) up. I have pyhton 3.7.6 with anaconda 3 acivated.

I want to use PyROOT on Visual Studio Code. From a Jupiter Notebook I call:

import ROOT as root

but I get

**ModuleNotFoundError** Traceback (most recent call last) in **----> 1 ****import****** ROOT **as** root **ModuleNotFoundError** : No module named 'ROOT'

Should I install pip install or conda install the root package?

I just downloaded root_v6.22.02.win32.vc16.zip, unzipped it in the Downloads folder, and then simply:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Users\bellenot>cd Downloads

C:\Users\bellenot\Downloads>root\bin\thisroot.bat

C:\Users\bellenot\Downloads>python
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Warning in <TClassTable::Add>: class ROOT::Detail::TTypedIter<class TEnumConstant> already in TClassTable
>>>

So as you can see, it works just fine. Can you try it the same way?

>>> import ROOT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\root\6.22.02vs19_aclic\bin\ROOT.py", line 24, in <module>
    import cppyy
  File "C:\root\6.22.02vs19_aclic\bin\cppyy.py", line 61, in <module>
    import libPyROOT as _backend
ImportError: DLL load failed: %1 is not a valid Win32 application.
>>>

that’s what I get in the terminal! Should I import some other modules?
Thank you for the help!

Did you follow the exact same procedure that what I showed? Is it a 32 bit Python?

Yes I did.
It seems to me a 64 bit python, I am wrong?

Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Yes, please try with 32 bit Python. ROOT is 32 bit only

There is something that I am doing wrong with the evirnomental variables. If I call root from the terminal I get

C:\Users\Giammarco>root
'root' is not recognized as an internal or external command,
operable program or batch file.

But if I double click on the root.exe in each /bin folder of each version they run perfectly.

As a consequence python 32 bit (with an acrtive environment) throws the following:

(anaconda32) C:\Users\Giammarco>python
Python 3.8.3 (default, Jul  2 2020, 17:28:51) [MSC v.1916 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ROOT'
>>>

My user environemental variables are
ROOTSYS = C:\root\6.22.02vs19_aclic;C:\root\5.34.36vs13_aclic;C:\root\5.34.38vs13_aclic;
PATH = %ROOTSYS%\bin

What do you think?

This cannot work. Either you change %ROOTSYS% to point to the only one version of ROOT you want to use, or you don’t set those variables at all and you call C:\root\6.22.02vs19_aclic\bin\thisroot.bat, or C:\root\5.34.36vs13_aclic\bin\thisroot.bat, or C:\root\5.34.38vs13_aclic\bin\thisroot.bat in a command prompt to properly set-up the environment variables before starting python

Ok, like this is working!

Python 3.8.3 (default, Jul  2 2020, 17:28:51) [MSC v.1916 32 bit (Intel)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ROOT
Warning in <TClassTable::Add>: class ROOT::Detail::TTypedIter<class TEnumConstant> already in TClassTable
>>>

Nevertheless it is not a sustainable solution because I will use the v6.22 for python and the other 5.34.x for my current analysis! What’s your way of hopping among multiple versions of root on the same machine?

As I said, I use the thisroot.bat file to use different versions at the same time (you can have as many different versions running in their own command prompt session). And I know many people doing the same on different platforms

ok, but this implies I need to change the ROOTSYS environmental variable too everytime! Am I getting it right?

No, the thisroot.bat script does it for you, it is the purpose of this script