ROOT (a Data analysis Framework) - Note2:关于编译和运行

Note2:关于编译运行

概述

ROOT是基于C++的,同时又嵌入了cling解释器。因此一个ROOT项目可以通过两种方式运行:解释运行和编译运行。对于小项目来说解释运行还是很方便的。编译运行适用于一些大的项目,编译后的脚本可以以更快的速度运行。此外还可以使用JupyterLab来运行ROOT脚本,实际上也是解释运行,只不过不是通过ROOT会话界面完成。因为JupyterNotebook可以将会话界面的一些命令和ROOT脚本放在一起运行,所以这个方法在调试的时候很好用。

解释运行

就像Note1中那样,你可以连续使用.L,将所有类的源文件以及main函数都加载到交互界面中,然后再运行main()函数。

编译运行

对于一些比较大的程序,可以选择编译运行,因为编译运行的速度比解释运行的速度要快得多。但是对于小项目来讲,编译时间可能更长。另外,使用编译运行的话对代码要求更高,cling给了物理系学生写更多垃圾代码的可能,它可以让代码不那么严谨就可以运行。因此解释运行成功的项目编译可能通不过。

和解释运行类似,如果你有一个脚本叫做main.C,那么你可以:

~$ root -l main.C+ # 系统对比已有的库文件的时间戳,如果库文件在脚本改动后生成,就直接运行库文件,反之,编译覆盖并运行。
~$ root -l main.C++ # 直接编译运行,而不对比库文件时间戳

你还可以进入root会话后,.L.x时在文件名后加上+++,和上面的效果一样。

还没尝试成功:

据官方的教程讲,会话窗口的元命令可以借助gROOT->ProcessLine()在宏文件里执行,因此你的宏文件里可以load其他宏文件,例如:

gROOT->ProcessLine(".L ABCClass.C")

但是我尝试了一下,报错:

error: expected function body after function declarator
gROOT->ProcessLine(".L ./a.cc+");

如果这个项目是面向对象编写的,那么上述两种运行方式都需要把所有的文件都敲一遍,这在测试的时候是很崩溃的。所以要么写一个shell脚本,要么使用Jupyter。

Jupyter系列

我目前用的比较顺手的方法是将ROOT内核加到Jupyter Notebook里,Jupyter可以帮你将交互界面的指令和C++语句结合起来。

Jupyter还是用Anaconda里的舒服。

要将ROOT内核加入到Jupyter里,需要额外两个包,下面是官网上给的指导:

The minimal version of Jupyter required in this case is 4.0.

Note that the sudo command might not be necessary on some platforms. Provided that ROOT is installed on your machine, these are the steps to follow to use a ROOT-flavoured C++ notebook:

# Install dependencies
sudo pip install jupyter metakernel zmq

# Launch ROOT notebook
root --notebook

This will open the notebook interface in your browser, where you will be able to select a ROOT C++ kernel. Note that this procedure assumes that executable python is the executable of Python2.

用anaconda的话就使用它的包管理器:

conda install metakernel
conda install zmq #最近conda好像找不到这个包了,只好用pip

这时如果像官网上,使用root --notebook命令开一个笔记本总是遇到下面的问题:

Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py", line 528, in get
    value = obj._trait_values[self.name]
KeyError: 'allow_remote_access'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 864, in _default_allow_remote
    addr = ipaddress.ip_address(self.ip)
  File "/home/user/anaconda3/lib/python3.7/ipaddress.py", line 54, in ip_address
    address)
ValueError: '' does not appear to be an IPv4 or IPv6 address

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/anaconda3/bin/jupyter-notebook", line 11, in <module>
    sys.exit(main())
  File "/home/user/anaconda3/lib/python3.7/site-packages/jupyter_core/application.py", line 266, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/home/user/anaconda3/lib/python3.7/site-packages/traitlets/config/application.py", line 657, in launch_instance
    app.initialize(argv)
  File "<decorator-gen-7>", line 2, in initialize
  File "/home/user/anaconda3/lib/python3.7/site-packages/traitlets/config/application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "/home/user/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 1628, in initialize
    self.init_webapp()
  File "/home/user/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 1378, in init_webapp
    self.jinja_environment_options,
  File "/home/user/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 159, in __init__
    default_url, settings_overrides, jinja_env_options)
  File "/home/user/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 252, in init_settings
    allow_remote_access=jupyter_app.allow_remote_access,
  File "/home/user/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py", line 556, in __get__
    return self.get(obj, cls)
  File "/home/user/anaconda3/lib/python3.7/site-packages/traitlets/traitlets.py", line 535, in get
    value = self._validate(obj, dynamic_default())
  File "/home/user/anaconda3/lib/python3.7/site-packages/notebook/notebookapp.py", line 867, in _default_allow_remote
    for info in socket.getaddrinfo(self.ip, self.port, 0, socket.SOCK_STREAM):
  File "/home/user/anaconda3/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

暂时没有心思管他……

可以将root内核植入Jupyter Notebook中:
翻了半天找到的github网站
简单地说就是从root的配置文件中找到内核文件,放到jupyter的配置目录下:

cp -r $ROOTSYS/etc/notebook/kernels/root ~/.local/share/jupyter/kernels

这个内核文件的路径有时候不太一样,例如后来我改用manjaro后,pacman一键按装了root,内核文件就放在了/etc/root/notebook/kernels下,仔(使)细(劲)找找就好。

后来发现Jupyter不仅有notebook,还有lab,就是直接在终端输入命令:

jupyter lab

PS:这个包anaconda应该是预先给你装好了,所以可以直接用。后来单独装一次jupyter,发现lab并不是随notebook默认安装的,在manjaro的库里面这样显示:

pacman -Ss jupyter     
community/haskell-ipynb 0.1-47
    Data structure for working with Jupyter notebooks (ipynb)
community/jupyter 4.1.0-7 [已安装]
    A language-agnostic web-based interactive shell/notebook server.
community/jupyter-nbconvert 5.5.0-1 [已安装]
    A language-agnostic web-based interactive shell/notebook server.
community/jupyter-nbformat 4.4.0-2 [已安装]
    The base implementation of the Jupyter Notebook format and Python APIs for working with notebooks
community/jupyter-notebook 5.7.8-1 [已安装]
    The language-agnostic HTML notebook application for Project Jupyter
community/jupyter-widgetsnbextension 1:3.4.2-1 [已安装]
    Interactive HTML widgets for Jupyter notebooks
community/jupyter_console 6.0.0-1 [已安装]
    An IPython-like terminal frontend for Jupyter kernels in any language.
community/jupyterlab 1.0.1-1 [已安装]
    JupyterLab computational environment
community/jupyterlab_server 1.0.0-2 [已安装]
    Launch an application built using JupyterLab
community/python-ipykernel 5.1.1-1 [已安装]
    The ipython kernel for Jupyter
community/python-ipywidgets 7.4.2-1 [已安装]
    IPython widgets for the Jupyter Notebook
community/python-jupyter_client 5.2.4-1 [已安装]
    Jupyter protocol implementation and client libraries
community/python-jupyter_core 4.5.0-1 [已安装]
    Jupyter core package. A base package on which Jupyter projects rely.
# ...这里省略了其他的包....

总之进去之后呢,就会得到一个更像编辑器的界面。这时你进入notebook的网址,发现notebook也是存在的。notebook和lab放在localhost的/tree/lab?两个子网站上。

截个图:

可以看到这个笔记本就是用了ROOT C++内核

发布了29 篇原创文章 · 获赞 3 · 访问量 6699

猜你喜欢

转载自blog.csdn.net/weixin_43316938/article/details/90524662