This workshop has moved! New address:

https://hsf-training.github.io/hsf-training-cmake-webpage/10-root/index.html

ROOT

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • How do I use ROOT?

Objectives
  • Use ROOT a couple of different ways

Let’s try a couple of ROOT examples; one with the classic variable/global configure and one with the newer target method. You will need a ROOT install or a ROOT docker container to run these examples. You can use rootproject/root:latest to test this, which is an official Ubuntu based build. Conda-Forge ROOT + CMake would work too, if you like Conda. (ROOT has tags for lots of other base images, too).

For these examples, you should be using a recent version of ROOT - especially for targets, which is still being worked on. The CONFIG files were added in 6.10, and targets received a lot of work in 6.14+. 6.16 has pretty decent targets.

Example 1: UseROOT

Change to the code/05a-root directory. Run:

cmake -S . -B build
cd build
make
root -b -q -x ../CheckLoad.C
cmake_minimum_required(VERSION 3.14...3.18)

project(RootUseFileExample LANGUAGES CXX)

# 6.16 fixes a bug in ROOT_EXE_LINKER_FLAGS, especially on macOS
find_package(ROOT 6.16 CONFIG REQUIRED)

include("${ROOT_USE_FILE}")

include_directories("${CMAKE_CURRENT_SOURCE_DIR}")

add_library(DictExample SHARED DictExample.cxx DictExample.h G__DictExample.cxx)

root_generate_dictionary(G__DictExample DictExample.h LINKDEF DictLinkDef.h)

target_link_libraries(DictExample PUBLIC ${ROOT_LIBRARIES})

Example 2: Targets

Change to the code/05b-root directory. Run the same command above.

cmake_minimum_required(VERSION 3.14...3.18)

project(RootTargetExample LANGUAGES CXX)

find_package(ROOT 6.20 CONFIG REQUIRED)

# Get the generate dictionary command from ROOT
include("${ROOT_DIR}/RootMacros.cmake")

# Make the dictionary, produces G__DictExample.cxx
root_generate_dictionary(G__DictExample DictExample.h LINKDEF DictLinkDef.h)

add_library(DictExample SHARED DictExample.cxx DictExample.h G__DictExample.cxx)
target_include_directories(DictExample PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

# Normally you need to link to lots of ROOT:: targets, but we aren't using much
# here.
target_link_libraries(DictExample PUBLIC ROOT::Core)

Key Points

  • ROOT has a CONFIG package