RDataFrame in external program

Dear Experts,

I would like to use the concept of RDataFrame in my C++ standalone program, you see here a simplified version of the program.cxx and CMakeLists.txt

#include "ROOT/RDataFrame.hxx"
#include "ROOT/RVec.hxx"
#include "ROOT/RDF/RInterface.hxx"

#include "TFile.h"

int main() {
	TFile *f = new TFile("file.root");
	ROOT::RDataFrame df("tree", "file.root");
	//f->ls();
}

and

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(program)

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
find_package(ROOT REQUIRED)

include(${ROOT_USE_FILE})
#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

add_executable(program program.cxx)
target_link_libraries(program "${ROOT_LIBRARIES}")
set_property(TARGET program PROPERTY CXX_STANDARD 11)

and I always get this error:

/usr/bin/ld: CMakeFiles/program.dir/program.cxx.o: in function `main':
program.cxx:(.text+0x134): undefined reference to `ROOT::RDataFrame::RDataFrame(std::experimental::__ROOT::basic_string_view<char, std::char_traits<char> >, std::experimental::__ROOT::basic_string_view<char, std::char_traits<char> >, std::vector<std::string, std::allocator<std::string> > const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/program.dir/build.make:105: program] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/program.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

What is going on?

Thank you very much in advance,


ROOT Version: 6.18
Platform: Ubuntu
Compiler: 8.33


Hi,
It looks like libROOTDataFrame is not being liked to your program. Does ${ROOT_LIBRARIES} contain ROOTDataFrame? Alternatively, can you try adding ROOTDataFrame or ROOT::ROOTDataFrame explicitly to the target_link_libraries?

Cheers,
Enrico

No, that’s not it, I think that there is a mismatch in C++ standards being used. Just remove the last line from your CMakeLists.txt in which you set the CXX_STANDARD property. The ROOT libraries already export this property. You have to ensure that your code is compiled with the same stardard as ROOT or it will not work. Cheers,

Hi,

Thank you for your reply. It seems that the problem I had is related to the other library (PyTorch) that I want to compile it with root. That if I have this small program

#include "ROOT/RDataFrame.hxx"
#include "ROOT/RVec.hxx"
#include "ROOT/RDF/RInterface.hxx"
#include "TFile.h"

#include <torch/torch.h>
int main() {
	TFile *f = new TFile("file.root");
	ROOT::RDataFrame df("tree", "file.root");
	//f->ls();
        torch::Tensor tensor = torch::rand({2, 3});
        std::cout << std::endl << tensor << std::endl;
        return 0;
}

and CMakeLists.txt:

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(program)

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS} "path/to/libtorch")
find_package(ROOT REQUIRED)
find_package(Torch REQUIRED)

include(${ROOT_USE_FILE})

add_executable(program program.cxx)
target_link_libraries(program "${ROOT_LIBRARIES}" "${Torch_LIBRARIES}")
set_property(TARGET program PROPERTY CXX_STANDARD 11)

That NEVER compiled, there is always a problem in the linker.

libtorch can be download from here

Everything linked with ROOT must use the same C++ standard as ROOT, if ROOT is compiled with C++11, then it’s ok to have what you have above, otherwise, you have to recompile either ROOT or PyTorch to make it work if they are using different versions of the C++ standard. Cheers,