Where can I find a C++ ROOT 6 example?

Quite generally can anyone point me to any ROOT 6 example projects that just work? No writing CMake files or googling Visual Studio build settings, just an actual example project.

Thereafter, can anyone point me to a project that uses Cling (ideally with CMake on Ubuntu) so I can define my own SIGNAL/SLOT functionality (I just want a button or TTimer that can toggle a boolean!)

The following is what I have tried:

I have installed ROOT 6 and I am able to run demo2.cxx from https://root.cern/manual/creating_a_user_application/

My CMake file looks like this:

cmake_minimum_required(VERSION 3.16)
project(TestRoot VERSION 0.1.0 LANGUAGES C CXX)
set (CMAKE_CXX_STANDARD 17)
set(SOURCE main.cpp)
add_executable(TestRoot ${SOURCE})
target_include_directories(TestRoot PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
$ENV{ROOTSYS}/include)
target_link_libraries(TestRoot
$ENV{ROOTSYS}/lib/libProofPlayer.so
$ENV{ROOTSYS}/lib/libmapDict.so
$ENV{ROOTSYS}/lib/libXrdHttpUtils.so
...
$ENV{ROOTSYS}/lib/libRooFitZMQ.so
$ENV{ROOTSYS}/lib/libXrdCksCalczcrc32-5.so
$ENV{ROOTSYS}/lib/libImt.so)

find_package(ROOT) seems to have no impact on the CMake file.

Attempting to add a button (or do anything that would require Cint in ROOT 5) gives me a runtime error:

Fatal in <operator new>: with std::align_val_t is not implemented yet
aborting
#0  0x00007f3a960ea45a in __GI___wait4 (pid=28528, stat_loc=stat_loc
entry=0x7ffed8f92f88, options=options
...
#16 0x00007f3a975b7ff6 in dl_open_worker (a=0x7ffed8f960c0) at ./elf/dl-open.c:808
...
#20 0x00007f3a960906bc in dlopen_doit (a=a
entry=0x7ffed8f96330) at ./dlfcn/dlopen.c:56
...
#29 0x000055a3ffade823 in main (argc=1, argv=0x7ffed8f966b8) at ...
Aborted (core dumped)

I have manually run ${ROOTSYS}/bin/rootcling -v -f mainframecling.cpp -c mainframe.h mainframe_Linkdef.h which generates the files mainframecling_rdict.pcm and mainframecling.cpp.

I have attempted to add them to my CMake file but Iā€™m unclear how and nothing I tried resolved anything.

_ROOT Version: 6 (any)
_Platform: Ubuntu
_Compiler: g++
_IDE: VS Code

Hi @fwoodruff ,

and welcome to the ROOT forum!

I have a couple of cmake projects that depend on ROOT, e.g. GitHub - eguiraud/rdf-benchmarks: A collection of RDataFrame benchmarks and GitHub - eguiraud/root-readspeed: A tool to measure what throughput can be expected from ROOT for a given application. . I hope they provide a useful example.

For generic ROOT+CMake instructions see Integrating ROOT into CMake projects - ROOT .

Cheers,
Enrico

EDIT:
as long as root is available in your environment when you run the cmake configuration, find_package(ROOT) should work. It should also produce output that tells you what it found.

Make sure to use the same C++ standard for your project as the one ROOT was built with, changing C++ standards can introduce problems.

Itā€™s best practice to use targets such as ROOT::ROOTDataFrame instead of explicitly passing $ENV{ROOTSYS}/lib/libImt.so because targets bring with them their own usage requirements (include directories, libraries to link, etc.).

EDIT 2:
for handling ROOT dictionaries in cmake projects I have a little tutorial at GitHub - eguiraud/root_dictionaries_tutorial: A tutorial on creating ROOT dictionaries to perform I/O of custom C++ classes, in case that helps. The official docs are at I/O of custom classes - ROOT .

Hi Enrico! Thank you so much for the response especially since I can see you are a ROOT author/contributor and my query is extremely basic!

So from a (near) fresh Ubuntu install, I have opened a terminal and performed:

cd
sudo apt-get install git
sudo snap install root-framework
git clone https://github.com/eguiraud/rdf-benchmarks.git
cd rdf-benchmarks
cmake .

This gives me the error

-- Looking for ROOT
CMake Error at src/CMakeLists.txt:2 (find_package):
  By not providing "FindROOT.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "ROOT", but
  CMake did not find one.

  Could not find a package configuration file provided by "ROOT" with any of
  the following names:

    ROOTConfig.cmake
    root-config.cmake

  Add the installation prefix of "ROOT" to CMAKE_PREFIX_PATH or set
  "ROOT_DIR" to a directory containing one of the above files.  If "ROOT"
  provides a separate development package or SDK, be sure it has been
  installed.

-- Configuring incomplete, errors occurred!
See also "/home/***/rdf-benchmarks/CMakeFiles/CMakeOutput.log".

The error is comprehensive but I am not sure how to execute the fix? Looking at your EDIT 2, I think I have a good chance of getting ROOTCling to work in CMake once I can get this first example to work! For the ā€˜integrating ROOT into CMake projectsā€™ link, it looks like the newer ā€˜targetā€™ CMake features arenā€™t being used, and me blindly following the tutorial there didnā€™t resolve my issues.

Googling relevant ROOT forum posts about the issue sends me to the following dead link: h t t p s : / / r o o t . c e r n / h o w / i n t e g r a t e - r o o t - m y - p r o j e c t - c m a k e /
(new users canā€™t post links)

Thanks!
Freddie

Hi @fwoodruff ,

I think itā€™s Snap thatā€™s making things more difficult for you ā€“ as far as I know Snap tries to sandbox each package so that might be the reason find_package(ROOT) fails to find ROOT.

In the annoucement post the author of the Snap package mentions, under ā€œCreating Independent Executablesā€, that it might not be possible (but Iā€™m not 100% sure if thatā€™s your case, letā€™s ping @James-Carroll :smiley: ).

it looks like the newer ā€˜targetā€™ CMake features arenā€™t being used

At Integrating ROOT into CMake projects - ROOT, the line

target_link_libraries(Event PUBLIC ROOT::RIO ROOT::Net)

is using the ROOT::RIO and ROOT::Net cmake targets as link libraries for the Event target. A good intro to ā€œmodern cmakeā€ concepts is https://www.youtube.com/watch?v=bsXLMQ6WgIk .

To unblock you in the meanwhile I would suggest that you download and use a pre-compiled binary as described at Installing ROOT - ROOT instead of the Snap package.

Cheers,
Enrico

Hey,

Thereā€™s some limited facility to use CMake with the ROOT snap; but it involves using the CMake bundled with the snap rather than the one on the system. Itā€™ll only have access to libraries bundled with the Snap but for simple cases works fairly well. (It even manages to build Geant4 for example, not that Iā€™d recommend it for proper use).

You can invoke it with root-framework.cmake, it takes all the parameters CMake would usually take; and then will be running CMake in the snaps container against the snaps libraries.

With the binary output, youā€™d be able to use root-framework.run ./myapplication.a for example, which again, runs in the Snap container against the snaps libs.

Itā€™s not something I tell people about often because while itā€™s useful in certain contexts, itā€™s admittedly very non-functional in a lot of other contexts. E.G, if you use sudo apt install mylibrary-dev for any arbitrary package on the host system, the snap canā€™t see it.

Thereā€™s also a downside that the ABI stability of ROOT isnā€™t guaranteed and the Snap automatically updates, when it does, youā€™d have to rebuild your projects and it might not be clear whatā€™s broken when trying. But you can stop the snap updating with sudo snap refresh --hold root-framework.

For educational use or use in certain contexts this might be all you need, but if you were planning on more advanced projects, Iā€™d agree that the pre-compiled binaries (or Docker/Conda) could suit your use case better.

Hopefully if you give it a try with the wrappers, you might find some more success.

1 Like

Hi Enrico,

Iā€™m getting close!

I typed the following:

wget https://root.cern/download/root_v6.28.04.Linux-centos8-x86_64-gcc8.5.tar.gz
tar -xzvf root_v6.28.04.Linux-centos8-x86_64-gcc8.5.tar.gz
source root/bin/thisroot.sh # also available: thisroot.{csh,fish,bat}
https://github.com/eguiraud/root_dictionaries_tutorial.git
cd root_dictionaries_tutorial/with_cmake
cmake . && cmake --build .

Then after getting the error:

warning: #warning "The C++ standard in this build does not match ROOT configuration (201402L); this might cause unexpected issues" [-Wcpp]
   30 | #  warning "The C++ standard in this build does not match ROOT configuration (201402L); this might cause unexpected issues"
      |    ^~~~~~~

I went nano CMakeLists.txt and included
the line

set(CMAKE_CXX_STANDARD 14)

after project(readwrite_twoints CXX)

cmake . && cmake --build . now compiles the program but I get the following runtime error when I run ./readwrite_twoints

ERROR in cling::CIFactory::createCI(): cannot extract standard library include paths!
Invoking:
  LC_ALL=C ccache   -xc++ -E -v /dev/null 2>&1 | sed -n -e '/^.include/,${' -e '/^ \/.*++/p' -e '}'
Results was:
With exit code 0
Written TwoInts(1, 2)
Read TwoInts(1, 2)

What should I try next?

Hi Freddie,

alright, thatā€™s quite some progress.

I see you downloaded the centos8 package but in the first post you mention you are on Ubuntu.
That might be the reason behind that last error message (the first error message is about the need to match the C++ standard of the application with the C++ standard of ROOT, as you figured out).

Does that help?

Hi Enrico!

Weā€™re in!

The steps I took deviate slightly from what people will gather from the above so in summary and for posterity:

I opened a terminal in Ubuntu 22,

cd
sudo apt-get update
sudo apt update
sudo apt install build-essential
sudo apt-get install git
wget https://root.cern/download/root_v6.28.04.Linux-ubuntu22-x86_64-gcc11.3.tar.gz
tar -xzvf root_v6.28.04.Linux-ubuntu22-x86_64-gcc11.3.tar.gz
source root/bin/thisroot.sh
git clone https://github.com/eguiraud/root_dictionaries_tutorial.git
cd root_dictionaries_tutorial/with_cmake
cmake . && cmake --build .
./readwrite_twoints

Outputs:

Written TwoInts(1, 2)
Read TwoInts(1, 2)

Modifying the CMakeLists.txt file wasnā€™t necessary in the end. Iā€™m on Ubuntu 22 but for any other OS, modify the above by navigating to the latest stable release from Releases - ROOT. It was also not necessary to go into .profile or .bashrc and update any environment variables to get this far (although there may be benefits to doing so).

1 Like

Great!

So in the end the only ā€œrealā€ issue was the Snap package sandboxing IIUC.

About .profile and .bashrc: the only thing people usually put there is the source thisroot.sh command so they donā€™t have to type it every time they start a new terminal.

Cheers,
Enrico

1 Like

I would say the links to your Github projects are also super valuable! Theyā€™re simple but theyā€™re not trivial to write if you donā€™t know what to Google :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.