ROOT class undefined reference to MyEvent::Class() and MyParticle::Class()

I'm using ROOT 6.15/01 Built for linuxx8664gcc From heads/master@v6-13-04-218-g3ec307bbe3

Hi, All

I am trying to use the ROOT class in one example main112.cc (PYTHIA8230)

To download and Install in the Linux Ubuntu, the command line:

$ wget http://home.thep.lu.se/~torbjorn/pythia8/pythia8230.tgz
$ tar -zxf pythia8230.tgz
$ cd pythia8230
$ ./configure --enable-shared --with-root=$ROOTSYS
$ sudo make -j4 && sudo make install

After install:

Put in the bashrc the following lines:

open the bashrc: gedit ~/.bashrc

#Pythia/root Env. Variables
export PYTHIA8=/home/user/pythia8230
export PYTHIA8DATA=$PYTHIA8/xmldoc
export ROOTSYS=/opt/root6
export PATH=$ROOTSYS/bin:$PATH
export LD_LIBRARY_PATH=$ROOTSYS/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$ROOTSYS/lib:$PYTHONPATH 
#end of My variables

Update the bashrc . ~/.bashrc

$ cd examples$ ls
main112.cc main112Dct.cc  main112Dct_rdict.pcm  main112.h  main112LinkDef.h  Makefile

main112.h (447 Bytes)
main112Dct.cc (209.1 KB)
main112LinkDef.h (2.2 KB)
makefile.txt (6.5 KB) (change this file to stay without extension i.e. Makefile)

the change the example main112.cc to put my ROOT classes:

I’m using this example to get the output : px, py, pz

My new main112.cc (3.9 KB):

// main112.cc is a part of the PYTHIA event generator.
// Copyright (C) 2017 Torbjorn Sjostrand.

// Simple program Heavy Ion Collisions. 
// Modified by Andre Vieira da Silva in 2018-05-30

// This file contains the main class for event generation.
// Pythia: provide the main user interface to everything else.
#include "Pythia8/Pythia.h"

// You need to include this to get access to the HIInfo object for HeavyIons.
#include "Pythia8/HeavyIons.h"

// ROOT, for saving Pythia events as trees in a file.
#include "TTree.h"
#include "TFile.h"

//Library Math
#include "TMath.h"

#include "sstream"

//load Library
#include "MyParticle.cxx"
#include "MyEvent.cxx"

using namespace Pythia8;

    int main() {
        
        Pythia pythia;
       
        //define variables:
        Double_t px, py, pz, pt, E, eta, phi;
        int id;
        
        // Set up the ROOT TFile and TTree.
        TFile *file = TFile::Open("pythia_heavy_ion.root","recreate");
        
        //Define TTRee "Tree" with the variable of the event
        TTree *Tree = new TTree("Tree","Tree");
        MyEvent *event = new MyEvent;
        
        Tree->Branch("event", &event);
        
        // Setting the beams.
        pythia.readString("Beams:idA = 2212");        // proton
        pythia.readString("Beams:idB = 1000822080");  // Lead Ion
        pythia.readString("Beams:eA = 4000");         // Energy beam A
        pythia.readString("Beams:eB = 1570");         // Energy beam B
        pythia.readString("Beams:frameType = 2");     // LAB Frame
        
        // Initialize the Angantyr model to fit the total and semi-includive
        // cross sections in Pythia within some tolerance.
        pythia.readString("HeavyIon:SigFitErr = "
        "0.02,0.02,0.1,0.05,0.05,0.0,0.1,0.0");
        
        // These parameters are typicall suitable for sqrt(S_NN)= 5.02 TeV
        pythia.readString("HeavyIon:SigFitDefPar = "
        "17.24,2.15,0.33,0.0,0.0,0.0,0.0,0.0");
        
        // A simple genetic algorithm is run for 20 generations 
        // to fit the parameters.
        pythia.readString("HeavyIon:SigFitNGen = 20");
        
        
        // Initialise Pythia.
        pythia.init();
       
        // Number of events!
        int nEvents = 10;  
    
        // Loop over events.
        for ( int iEvent = 0; iEvent < nEvents; ++iEvent ) {
            
            // make sure the event object is empty
            event->Clear();
          
            if ( !pythia.next() ) continue;
            
                //Loop over the particles of the event
                for (int i = 0; i < pythia.event.size(); ++i) {
                                  
                    if ( pythia.event[i].isFinal()) {
                    
                        // Create a particle and set its properties
                        MyParticle *part = new MyParticle();
                        
                        px = pythia.event[i].px();
                        py = pythia.event[i].py();
                        pz = pythia.event[i].pz();
                        
                        part->SetPx(px);
                        part->SetPy(py);
                        part->SetPz(pz);
                        part->PrintInfo();
                        
                        // add this particle to the current event 
                        event->AddParticle(part);
                          
                    }
                    
                }
                //Fill the TTRee "Tree" with this event
                Tree->Fill();
        }
        
        // Print the screen (terminal) the information about the TTRee particle
        // Tree->Print(); 
        
        // Record Tree Object in the file
        Tree->Write();
        
        // The run is over, so we write out some statistics.
        // I moved the delete statement here.
        file->Close();
        delete file;
        
        cout<< " And we're done!" << endl;
        
        return 0;
    }

This is running with the following files:

Load library:
MyParticle.cxx (893 Bytes) - Class implementation of MyParticle
MyParticle.h (2.2 KB) - Class definition of MyParticle
MyEvent.cxx (1.9 KB) - Class implementation of MyEvent
MyEvent.h (1.2 KB) - Class definition of MyEvent

My problem is: `undefined reference to MyEvent::Class() and
undefined reference to MyParticle::Class()

Error:

andre@andre-QBEX-H61H2-M17:~/pythia8230/examples$ make main112
g++ main112.cc main112.so -o main112 -w -I/opt/root6/include -I../include -O2  -pedantic -W -Wall -Wshadow -fPIC -L../lib -Wl,-rpath,../lib -lpythia8 -ldl \
 `root-config --cflags` -Wl,-rpath,./\
 -Wl,-rpath,/opt/root6/lib `/opt/root6/bin/root-config --glibs`
/tmp/ccmBzrIp.o: In function `MyEvent::IsA() const':
main112.cc:(.text._ZNK7MyEvent3IsAEv[_ZNK7MyEvent3IsAEv]+0x1): undefined reference to `MyEvent::Class()'
/tmp/ccmBzrIp.o: In function `MyEvent::ShowMembers(TMemberInspector&) const':
main112.cc:(.text._ZNK7MyEvent11ShowMembersER16TMemberInspector[_ZNK7MyEvent11ShowMembersER16TMemberInspector]+0xd): undefined reference to `MyEvent::Class()'
/tmp/ccmBzrIp.o: In function `MyParticle::ShowMembers(TMemberInspector&) const':
main112.cc:(.text._ZNK10MyParticle11ShowMembersER16TMemberInspector[_ZNK10MyParticle11ShowMembersER16TMemberInspector]+0xd): undefined reference to `MyParticle::Class()'
/tmp/ccmBzrIp.o: In function `MyParticle::IsA() const':
main112.cc:(.text._ZNK10MyParticle3IsAEv[_ZNK10MyParticle3IsAEv]+0x1): undefined reference to `MyParticle::Class()'
/tmp/ccmBzrIp.o: In function `MyEvent::CheckTObjectHashConsistency() const':
main112.cc:(.text._ZNK7MyEvent27CheckTObjectHashConsistencyEv[_ZNK7MyEvent27CheckTObjectHashConsistencyEv]+0x77): undefined reference to `MyEvent::Class()'
/tmp/ccmBzrIp.o: In function `MyParticle::CheckTObjectHashConsistency() const':
main112.cc:(.text._ZNK10MyParticle27CheckTObjectHashConsistencyEv[_ZNK10MyParticle27CheckTObjectHashConsistencyEv]+0x7a): undefined reference to `MyParticle::Class()'
/tmp/ccmBzrIp.o: In function `_GLOBAL__sub_I_main112.cc':
main112.cc:(.text.startup+0x5de): undefined reference to `ROOT::GenerateInitInstance(MyParticle const*)'
main112.cc:(.text.startup+0x5f9): undefined reference to `ROOT::GenerateInitInstance(MyEvent const*)'
/tmp/ccmBzrIp.o:(.data.rel.ro._ZTV10MyParticle[_ZTV10MyParticle]+0x1e0): undefined reference to `MyParticle::Streamer(TBuffer&)'
/tmp/ccmBzrIp.o:(.data.rel.ro._ZTV7MyEvent[_ZTV7MyEvent]+0x48): undefined reference to `MyEvent::Streamer(TBuffer&)'
collect2: error: ld returned 1 exit status
Makefile:165: recipe for target 'main112' failed
make: *** [main112] Error 1

Sorry for a long description,
Someone can help me with some tip?

Thanks,
Cheers, Andre

I had a progress on this issue…

MyEvent.cxx:10:1:

andre@andre-QBEX-H61H2-M17:~/pythia8230/examples$ make main112
g++ main112.cc main112.so -o main112 -w -I/opt/root6/include -I../include -O2  -pedantic -W -Wall -Wshadow -fPIC -L../lib -Wl,-rpath,../lib -lpythia8 -ldl \
 `root-config --cflags` -Wl,-rpath,./\
 -Wl,-rpath,/opt/root6/lib `/opt/root6/bin/root-config --glibs`
In file included from /opt/root6/include/TObject.h:17:0,
                 from /opt/root6/include/TNamed.h:25,
                 from /opt/root6/include/TKey.h:15,
                 from /opt/root6/include/TBasket.h:28,
                 from /opt/root6/include/ROOT/TIOFeatures.hxx:14,
                 from /opt/root6/include/TTree.h:29,
                 from main112.cc:16:
/opt/root6/include/Rtypes.h:354:81: error: redefinition of ‘int ROOT::{anonymous}::R__dummyintdefault10’
          static int _R__UNIQUE_(_NAME2_(R__dummyint,key)) __attribute__((unused)) = \
                                                                                 ^
/opt/root6/include/Rtypes.h:359:24: note: in expansion of macro ‘ClassImpUnique’
 #define ClassImp(name) ClassImpUnique(name,default)
                        ^~~~~~~~~~~~~~
MyEvent.cxx:10:1: note: in expansion of macro ‘ClassImp’
 ClassImp(MyEvent)
 ^~~~~~~~

MyParticle.cxx:10:1:

/opt/root6/include/Rtypes.h:356:42: error: redefinition of ‘class ROOT::{anonymous}::R__dummyintdefault10_c’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
                                          ^
/opt/root6/include/ROOT/RConfig.h:434:33: note: in definition of macro ‘_NAME2_’
 #   define _NAME2_(name1,name2) name1##name2
                                 ^~~~~
/opt/root6/include/Rtypes.h:356:10: note: in expansion of macro ‘R__UseDummy’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
          ^~~~~~~~~~~
/opt/root6/include/ROOT/RConfig.h:452:27: note: in expansion of macro ‘_NAME2_’
 #   define _R__JOIN_(X,Y) _NAME2_(X,Y)
                           ^~~~~~~
/opt/root6/include/ROOT/RConfig.h:455:27: note: in expansion of macro ‘_R__JOIN_’
 #   define _R__UNIQUE_(X) _R__JOIN_(X,__LINE__)
                           ^~~~~~~~~
/opt/root6/include/Rtypes.h:356:22: note: in expansion of macro ‘_R__UNIQUE_’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
                      ^~~~~~~~~~~
/opt/root6/include/Rtypes.h:356:34: note: in expansion of macro ‘_NAME2_’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
                                  ^~~~~~~
/opt/root6/include/Rtypes.h:359:24: note: in expansion of macro ‘ClassImpUnique’
 #define ClassImp(name) ClassImpUnique(name,default)
                        ^~~~~~~~~~~~~~
MyEvent.cxx:10:1: note: in expansion of macro ‘ClassImp’
 ClassImp(MyEvent)
 ^~~~~~~~
/opt/root6/include/Rtypes.h:356:42: note: previous definition of ‘class ROOT::{anonymous}::R__dummyintdefault10_c’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
                                          ^
/opt/root6/include/ROOT/RConfig.h:434:33: note: in definition of macro ‘_NAME2_’
 #   define _NAME2_(name1,name2) name1##name2
                                 ^~~~~
/opt/root6/include/Rtypes.h:356:10: note: in expansion of macro ‘R__UseDummy’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
          ^~~~~~~~~~~
/opt/root6/include/ROOT/RConfig.h:452:27: note: in expansion of macro ‘_NAME2_’
 #   define _R__JOIN_(X,Y) _NAME2_(X,Y)
                           ^~~~~~~
/opt/root6/include/ROOT/RConfig.h:455:27: note: in expansion of macro ‘_R__JOIN_’
 #   define _R__UNIQUE_(X) _R__JOIN_(X,__LINE__)
                           ^~~~~~~~~
/opt/root6/include/Rtypes.h:356:22: note: in expansion of macro ‘_R__UNIQUE_’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
                      ^~~~~~~~~~~
/opt/root6/include/Rtypes.h:356:34: note: in expansion of macro ‘_NAME2_’
          R__UseDummy(_R__UNIQUE_(_NAME2_(R__dummyint,key))); \
                                  ^~~~~~~
/opt/root6/include/Rtypes.h:359:24: note: in expansion of macro ‘ClassImpUnique’
 #define ClassImp(name) ClassImpUnique(name,default)
                        ^~~~~~~~~~~~~~
MyParticle.cxx:10:1: note: in expansion of macro ‘ClassImp’
 ClassImp(MyParticle)
 ^~~~~~~~

Cheers, Andre

Just remove the two ClassImp() “invocations”; they are not needed.

Are you saying that this should get fixed within Pythia itself? (I’m not clear how closely your example relates to the examples in Pythia.)

Hi, @Axel Thanks for the reply.

I remove two ClassImp() in MyParticle.cxx and MyEvent.cxx

Error:

/tmp/ccMVYLCt.o: In function `MyEvent::IsA() const':
main112.cc:(.text._ZNK7MyEvent3IsAEv[_ZNK7MyEvent3IsAEv]+0x1): undefined reference to `MyEvent::Class()'
/tmp/ccMVYLCt.o: In function `MyEvent::ShowMembers(TMemberInspector&) const':
main112.cc:(.text._ZNK7MyEvent11ShowMembersER16TMemberInspector[_ZNK7MyEvent11ShowMembersER16TMemberInspector]+0xd): undefined reference to `MyEvent::Class()'
/tmp/ccMVYLCt.o: In function `MyParticle::ShowMembers(TMemberInspector&) const':
main112.cc:(.text._ZNK10MyParticle11ShowMembersER16TMemberInspector[_ZNK10MyParticle11ShowMembersER16TMemberInspector]+0xd): undefined reference to `MyParticle::Class()'
/tmp/ccMVYLCt.o: In function `MyParticle::IsA() const':
main112.cc:(.text._ZNK10MyParticle3IsAEv[_ZNK10MyParticle3IsAEv]+0x1): undefined reference to `MyParticle::Class()'
/tmp/ccMVYLCt.o: In function `MyEvent::CheckTObjectHashConsistency() const':
main112.cc:(.text._ZNK7MyEvent27CheckTObjectHashConsistencyEv[_ZNK7MyEvent27CheckTObjectHashConsistencyEv]+0x77): undefined reference to `MyEvent::Class()'
/tmp/ccMVYLCt.o: In function `MyParticle::CheckTObjectHashConsistency() const':
main112.cc:(.text._ZNK10MyParticle27CheckTObjectHashConsistencyEv[_ZNK10MyParticle27CheckTObjectHashConsistencyEv]+0x7a): undefined reference to `MyParticle::Class()'
/tmp/ccMVYLCt.o:(.data.rel.ro._ZTV10MyParticle[_ZTV10MyParticle]+0x1e0): undefined reference to `MyParticle::Streamer(TBuffer&)'
/tmp/ccMVYLCt.o:(.data.rel.ro._ZTV7MyEvent[_ZTV7MyEvent]+0x48): undefined reference to `MyEvent::Streamer(TBuffer&)'
collect2: error: ld returned 1 exit status
Makefile:165: recipe for target 'main112' failed
make: *** [main112] Error 1

Yup, you need to create a dictionary for MyEvent.h and MyParticle.h and link this in. Let me know if you need the steps spelled out!

Could you explain to me how to do it?

Thanks,

Add

#pragma link C++ class MyEvent+;
#pragma link C++ class MyParticle+;

in the file called main92LinkDef.h.

In your Makefile, replace

main92Dct.cc: main92.h main92LinkDef.h
	export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(ROOT_LIB);\
	 $(ROOT_BIN)rootcint -f $@ -c -I$(PREFIX_INCLUDE) $^

with

main92Dct.cc: main92.h MyParticle.h MyEvent.h main92LinkDef.h
	export LD_LIBRARY_PATH=$$LD_LIBRARY_PATH:$(ROOT_LIB);\
	 $(ROOT_BIN)rootcint -f $@ -c -I$(PREFIX_INCLUDE) $^

Done!

Thank you very much, Axel.

it worked!

--------  End PYTHIA Particle Data Table  ---------------------------

 And we're done!
andre@andre-QBEX-H61H2-M17:~/pythia8230/examples$ rootls -t pythia_heavy_ion.root 
TTree  Jun 01 10:28 2018 Tree  "Tree"
  event  "event"  0
    fNparticles  "fNparticles"  118
    fParticles   "fParticles_"  117
      fParticles.fUniqueID  "fUniqueID[fParticles_]"  9447
      fParticles.fBits      "fBits[fParticles_]"      9443
      fParticles.fPx        "fPx[fParticles_]"        18753
      fParticles.fPy        "fPy[fParticles_]"        18753
      fParticles.fPz        "fPz[fParticles_]"        18753

Cheers, Andre

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