example:
# CMake versions (could be higher)
cmake_minimum_required(VERSION 3.24)
# include root package
find_package(ROOT REQUIRED COMPONENTS Hist RIO MathCore)
# project name
project(test)
# C++ standard. Options: 11, 14, 17, 20, 23
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
# Create excutable from main.cpp
add_executable(main main.cpp)
# link the external package to your main.cpp
target_link_libraries(main PUBLIC ROOT::Hist ROOT::RIO ROOT::MathCore)For other ROOT library targets, please visit this ROOT website.
To use python with ROOT in a jupyter lab, a proper python environment should be set up. Here conda is used. The following steps only need to be done once in the beginning.
The python version of the conda environment must be matched with the ROOT version. To find out which python version should be used, run following command:
root-config --python-versionIt prints out the corresponding python version for the ROOT you are currently using.
Run the following command with an environment name and the python version
conda create --name [any_name] PYTHON=[version]Activate the conda environment:
conda activate [any_name]pip install jupyterlabThe jupyter lab is usually run in our IKP servers instead of our local personal laptops. But still we can open the jupyter lab from the local web browser using so called ssh tunneling.
Inside the IKP server, run the following command to launch the jupyter server:
jupyter lab --no-browser --port [port number]Here the port number can be any larger number ( > 10000)
In your local computer, run the following command:
ssh -L [port number]:localhost:[port number] server_name -N -fHere the port number must be the same as the port number during the jupyter launching. The server_name should be the server where you launch the jupyter lab.
Once the jupyter lab is launched in the server, you can find an url from the terminal output. Copy the url, paste in the web browser and hit enter.
Import necessary ROOT libraries. The library name should be same with the class name.
from ROOT import TFile, TCanvas, TH1The usage of the ROOT classes is the same as in C++, except that in python only objects are used instead of pointers.
input_file = TFile("any_root_file.root", "read")
canvas = TCanvas("canvas", "canvas", 0, 0, 800, 600)The name of the histogram can be obtained by using a member function input_file.ls() from the TFile object. After knowing the name of the histogram, it can obtained with:
histogram = input_file.get("histogram_name")
histogram.Draw()
canvas.Draw()