Compiling macro with TGeoTrack

Hi all!

I have the following piece of code:

#include <iostream>
#include <vector>
#include <string>
#include "Math/Vector3D.h"
#include "TGraphErrors.h"
#include "TF1.h"
#include "TString.h"
#include "TROOT.h"
#include "TH1F.h"
#include "TCanvas.h"
#include "TGeoManager.h"
#include "TVirtualGeoTrack.h"
#include <TParticle.h>
#include "TPolyLine3D.h"
#include <TGeoTrack.h>
#include "TGeoTube.h"
#include "TGeoHelix.h"
#include "TView.h"
#include "TSystem.h"
#include "TString.h"

using namespace std;

int test(){

    TCanvas* canvas = new TCanvas();
    new TGeoManager("test", "test");

    TGeoMaterial* material = new TGeoMaterial("Vaccum", 0,0,0);
    TGeoMedium* medium = new TGeoMedium("Vaccum",1,material);
    TGeoVolume* top = gGeoManager->MakeBox("TOP", medium, 4000, 4000, 4000);

    TGeoVolume* vol = gGeoManager->MakeTube("TUBE",medium, 1790.,1800.,2350.);

    TGeoTrack* track = new TGeoTrack();

    int nHits = 220;
    double scale = M_PI/nHits;
    for(int i=0; i < nHits; ++i){
        track->AddPoint(5.*std::cos(i*scale), 5.*std::sin(i*scale), i*scale, i);
    // top->AddNode(track, 2);

    TView *view = gPad->GetView();

    return 0;

Running it as root test.cpp works fine.

However I want to use this logic inside bigger program which I compile using cmake…
And I cannot understand how to make this work…

My CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
cmake_policy(VERSION 3.12)

project(SETAnalysis VERSION 1.0)

# -03 compiler optimization for speed
# -O0 disable for valgrind debugging

add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCE_DIR}/src/SETAnalysis.cpp)


find_package(ROOT REQUIRED COMPONENTS Geom Physics)

find_package(Marlin 1.15 REQUIRED)
find_package(MarlinUtil 1.4 REQUIRED)
find_package(MarlinTrk REQUIRED)

FOREACH(pkg ROOT Marlin MarlinUtil MarlinTrk )
    target_link_libraries(${PROJECT_NAME} ${${pkg}_LIBRARIES})


And it returns undefined reference error during compilation

/cvmfs/ CMakeFiles/SETAnalysis.dir/src/SETAnalysis.cpp.o: in function `SETAnalysis::drawTrack(EVENT::Track*)':
/cvmfs/ undefined reference to `TGeoTrack::TGeoTrack()'
collect2: error: ld returned 1 exit status
make[2]: *** [lib/] Error 1
make[1]: *** [CMakeFiles/SETAnalysis.dir/all] Error 2
make: *** [all] Error 2

I have tried to do simpier task and compiler without cmake but raw g++ with:
g++ test.cpp root-config --cflags --glibs -lGeom

but it outputs me something similar but different:

/cvmfs/ /lib/../lib64/crt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
/cvmfs/ /tmp/ccwsvOlX.o: in function `test()':
test.cpp:(.text+0x19d): undefined reference to `TGeoTrack::TGeoTrack()'
collect2: error: ld returned 1 exit status

Could anyone help how one should use these? Thanks!

ROOT Version: 6.18.04, but g++ failed with 6.24.00 as well
Platform: Centos7
Compiler: g++ 8.2 / 10.1


I managed to make it work using as an usage example this thread from the very old root forum…

So I assume I needed to

  1. remove constructor and create track via
    int trackIdx = gGeoManager->AddTrack(0, 211)
  2. Access it with
    TVirtualGeoTrack *track = gGeoManager->GetTrack(trackIdx);

somehow I feel extreme lack of examples and documentation for these classes…

May be @agheata can say more.

Hi, TGeoTrack is one of the first attempts to make an event display in the pad early millennium, meanwhile people managed to implement similar functionality in much better ways. Although the class is still working, its usage was not encouraged, so not documented properly, sorry about this. It wasn’t removed though just because some people may be still using it, but it may worth throwing some deprecation warnings indeed.

I will add the deprecated flag in the reference guide.

PR here: [skip-ci] Mark TGeoTrack as deprecated by couet · Pull Request #8943 · root-project/root · GitHub

Just to complete the topic
I mention that one probably needs to use TEve for the event display and TEveTracks or TEvePointSet to display tracks specificaly

To be honest, actually, now I am having trouble with TEve…

Here is the code snippet:

#include "TEveManager.h"
#include "TEveGeoShape.h"
#include "TGeoTube.h"

int main(){

    TEveGeoShape* geoSET = new TEveGeoShape();
    geoSET->SetShape( new TGeoTube("SET", 1773.,1776.,2300.) );

    TEveGeoShape* geoSIT = new TEveGeoShape();
    geoSIT->SetShape( new TGeoTube("SIT", 300.,303.,644.) );

    return 0;

Trying to compile with root 6.18 as
g++ test.cpp `root-config --cflags --glibs` -lGeom -lPhysics -lEve

returns seg. fault at TEveManager::Create();… What is wrong now…?

==21716== Invalid read of size 8
==21716==    at 0x570A631: TApplication::InitializeGraphics() (TApplication.cxx:267)
==21716==    by 0x9D41375: TEveManager::Create(bool, char const*) (TEveManager.cxx:907)
==21716==    by 0x4010C7: main (in /afs/
==21716==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

 *** Break *** segmentation violation

May be @matevz can help you.

You need to create a TApplication (or TRint) object and later call Run() on it.

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