PyTorch with ROOT

username: WaleedEsmail
cooked: <p>Dear Experts,</p>
<p>I am actually new to PyTorch. I would like to use the concept of RDataFrame from ROOT software together with PyTorch C++ frontend, but I faced problems in compiling my program, you see here a simplified version of the Program.cxx and CMakeLists.txt</p>
<pre><code class="lang-auto">#include &lt;iostream&gt;
#include &lt;torch/torch.h&gt;
#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-&gt;ls();
       torch::Tensor tensor = torch::rand({2, 3});
      std::cout &lt;&lt; std::endl &lt;&lt; tensor &lt;&lt; std::endl;
       return 0;
}
</code></pre>
<p>and CMakeLists.txt</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(Program)

list(APPEND CMAKE_PREFIX_PATH $ENV{ROOTSYS})
find_package(Torch REQUIRED)
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}")
target_link_libraries(Program "${TORCH_LIBRARIES}")
set_property(TARGET Program PROPERTY CXX_STANDARD 11)
</code></pre>
<p>and I always get this error:</p>
<pre><code class="lang-auto">/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&lt;char, std::char_traits&lt;char&gt; &gt;, std::experimental::__ROOT::basic_string_view&lt;char, std::char_traits&lt;char&gt; &gt;, std::vector&lt;std::string, std::allocator&lt;std::string&gt; &gt; const&amp;)'
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
</code></pre>
<p>NOTE: It compiles and run correctly when I comment the line <code>ROOT::RDataFrame df("tree", "file.root");</code>. Also when I disable Torch (see example below) and compile without it it compiles and run correctly. So it seems to me that there is a conflict between Torch and ROOTDataframe (specifically) from ROOT.</p>
<p><em><strong>CMakeLists.txt/Program without Torch</strong></em>:</p>
<pre><code class="lang-auto">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)
</code></pre>
<pre><code class="lang-auto">#include &lt;iostream&gt;
#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-&gt;ls();
       return 0;
}
</code></pre>
<p>Any suggestions how to solve this problem?!</p>
<p>Thank you all</p>

username: dnnagy
cooked: <aside class="quote no-group quote-modified" data-username="WaleedEsmail" data-post="1" data-topic="52185">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/waleedesmail/48/14631_2.png" class="avatar"> WaleedEsmail:</div>
<blockquote>
<p>undefined reference to `ROOT::RDataFrame::RDataFrame(std::experimental::__ROOT::basic_string_view&lt;char, std::char_traits &gt;, std::experimental::__ROOT::basic_string_view&lt;char, std::char_traits &gt;, std::vector&lt;std::string, std::allocator<a>std::string</a> &gt; const&amp;)’ collect2: error: ld returned 1 exit status</p>
</blockquote>
</aside>
<p>This says that the linker does not find the reference for RDataFrame this is probably an issue with the ROOT libraries and has nothing to do with libtorch. Try rebuilding ROOT from sources.</p>