Problem using ROOT in a Geant4 project


I am trying to simulate a cube of water with neutrons shooting into it and extract the information of energy deposition using Geant4. The main body of my code is adapted from Geant4 basic example B1, minus the two volumes and changing the particle from gamma to neutron. I tried to use TFile and TTree to store my data - I added the following code to, RunAction::BeginOfRunAction(const G4Run*) function.

outputFile = new TFile("test.root", "RECREATE");
Data = new TTree("Data", "Information of particles");
Data->Branch("Initial", &InitialEnergy, "fInitial/D");
Data->Branch("Final",   &FinalEnergy,   "fFinal/D");
Data->Branch("Energy Deposition", &Edep,  "fEdep/D");
Data->Branch("RMS of Energy Deposition", &rmsEdep, "rmsEdep");

and in RunAction::EndOfRunAction(const G4Run* run) I added


I think I’ve defined everything properly in the header file and initialised the variables after the RunAction::RunAction() : G4UserRunAction(). I also made sure to change my CMakeFiles.txt to include the locations of the ROOT headers by adding these:

find_package(ROOT REQUIRED)

but when I make the files after running cmake command successfully, I keep getting this error:

[ 37%] Linking CXX executable exampleB1
Undefined symbols for architecture x86_64:
  "TVersionCheck::TVersionCheck(int)", referenced from: in in in in
  "TFile::TFile(char const*, char const*, char const*, int)", referenced from:
      RunAction::RunAction() in
  "TTree::TTree(char const*, char const*, int)", referenced from:
      RunAction::RunAction() in
  "TObject::operator delete(void*)", referenced from:
      RunAction::RunAction() in
  "TStorage::ObjectAlloc(unsigned long)", referenced from:
      RunAction::RunAction() in
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [exampleB1] Error 1
make[1]: *** [CMakeFiles/exampleB1.dir/all] Error 2
make: *** [all] Error 2

I understand this error “Undefined symbols for architecture x86_64” means my definition of variables is inconsistent at some locations, but I can’t figure out where to fix it. Please can you point out where I did wrong? Sorry I’m really new to C++.

_ROOT Version: 5.34.36
_Geant4 Version: 10.5.1
_Platform: Catalina 10.15.3
_Compiler: cmake 3.16.3

It appears it is a Geant4 problem. You should post on their forum.

Thank you! I’ll try posting there.

You probably need to add something like:

add_dependencies(exampleB1 Core RIO)

in your CMakeList.txt file. I.e. you need to link against the ROOT libraries.