Re: RE : TTree with user defined class

From: John Idarraga <idarraga_at_cern.ch>
Date: Mon, 27 Sep 2010 17:38:13 +0200


Hello Bob,

Here the makefile you need

# John Idarraga <idarraga_at_lal.in2p3.fr>
# Minimal example to compile dynamically loadable code for CINT with its
#  proper dictionary generated with rootcint.

ROOTCFLAGS := $(shell $(ROOTSYS)/bin/root-config --prefix=$(ROOTSYS) --cflags) -fPIC
ROOTLIBS := $(shell $(ROOTSYS)/bin/root-config --prefix=$(ROOTSYS) --libs)

mycode: MyCode.cpp Dict_MyCode.cpp

    g++ -W -Wall -g3 -shared ${ROOTCFLAGS} ${ROOTLIBS} \     MyCode.cpp Dict_MyCode.cpp -o MyCode.so

Dict_MyCode.cpp: MyCode.h LinkDef_MyCode.h

    rootcint -v -f Dict_MyCode.cpp -c MyCode.h LinkDef_MyCode.h

clean:

    rm -f MyCode.so *.o Dict_MyCode.cpp Dict_MyCode.h

(mind the tabs in each target !). I am supposing your code is found in MyCode.cpp and MyCode.h. Now, you also need a file called LinkDef_MyCode.h. That file should contain the following

#ifdef __CINT__

#pragma link off all global;
#pragma link off all classes;
#pragma link off all functions;
#pragma link off all extern;

#pragma link C++ class MyClass;

#endif  

And finally you need two macros in your source. In the class definition (found in .h files in general), at the very end of the class (before the ending '}'), you write

ClassDef(MyClass, 1)

And in the implementation part (found in .cpp files normally) ... at the top for example

ClassImp(MyClass)

Replace "MyClass", "MyCode" as needed. Let me know how it goes ! If you have a problem understanding what's going on ... let me know too.

cheers !

John

Bob wrote:
> Thanks a lot ! After some minor modifications everything run fine
> "inside" root.
>
> Right now I'll continue to work like this that but my final goal is to
> be able to run my program as a standalone binary, compiled with
> standard tools, and linked with the Root libraries.
>
> Any idea to make this happen ?
>
> Bob
>
> 2010/9/27 John Idarraga <idarraga_at_cern.ch>:
>
>> Well, if he is compiling with ACLIC
>>
>> .L Sample.h++
>>
>> he doesn't need to load the lib ... it is already loaded.
>>
>> John
>>
>> Ahmed Ali Abdelalim wrote:
>>
>>> Hi Bob,
>>> you compiled and linked now you need to tell ROOT to loaded your library
>>> .To do this you have two ways:
>>> 1- login to root then do
>>> gSystem->Load("libMyLib.so");
>>> before execute your code.
>>> 2- make a root map file ( for ex.: myLib.rootmap) to tell root to load
>>> your library when it starts. the format is trivial like,
>>> Library.TmyLib: libMyLib.so
>>> hth
>>> Cheers,
>>> Ahmed
>>> ##################################################
>>> #Ahmed Ali Abdelalim Phone(Uni):+41223796213
>>> #University of Geneva mobil: +41 78 611 7329 #Physics Section, DPNC
>>> #
>>> #CERN PH/UAT (ATLAS), Phone:+412276 74279 #Office: 40-1-D32
>>> #CH-1211 Geneve 23 Switzerland.
>>> ##################################################
>>>
>>> ------------------------------------------------------------------------
>>> *De:* owner-roottalk_at_root.cern.ch de la part de Bob
>>> *Date:* lun. 9/27/2010 12:41
>>> *À:* roottalk_at_lxroot01.cern.ch
>>> *Objet :* Re: [ROOT] TTree with user defined class
>>>
>>> Thanks John,
>>>
>>> First I moved the class definition to Sample.h then I choose the ACLiC
>>> option to generate the dictionary (.L Sample.h+). I got two new file
>>> Sample_h.so and Sample_h.d
>>>
>>> When I try to run the program I have exactly the same error :
>>> Error in <TTree::Branch>: The pointer specified for debug is not of a
>>> class or type known to ROOT.
>>>
>>> I also tried to link ttree_write.o against the library Sample_h.so but
>>> without success, should I keep going in this way ? I'm a bit lost !
>>>
>>> Thanks,
>>>
>>> Bob
>>>
>>> 2010/9/27 John Idarraga <idarraga_at_cern.ch>:
>>>
>>>> Hello Bob,
>>>>
>>>> Yeah, you need a dictionary, a couple of macros in your source file and
>>>> that's it. Check this out
>>>>
>>>> http://root.cern.ch/drupal/content/interacting-shared-libraries-rootcint
>>>>
>>>> if you have any trouble let me know,
>>>>
>>>> John Idarraga
>>>>
>>>> Bob wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I'm a Root beginner, I'm trying to save a class to a TTree. Until now
>>>>> I managed to store and read "simple" type like int and STL objects,
>>>>> but not homemade objects.
>>>>>
>>>>> I'm made a short example :
>>>>>
>>>>> Code snippet :
>>>>>
>>>>> #include <TFile.h>
>>>>> #include <TTree.h>
>>>>> #include <vector>
>>>>> #include <iostream>
>>>>>
>>>>> class Sample {
>>>>> public:
>>>>> int i;
>>>>> std::vector<int> hits;
>>>>> };
>>>>>
>>>>> int main() {
>>>>> Sample *s = new Sample();
>>>>> TFile *f1 = new TFile("debug.root","RECREATE");
>>>>> if (f1->IsZombie()) {
>>>>> std::cerr << "! Unable to create debug.root" <<
>>>>> std::endl;
>>>>> return -1;
>>>>> }
>>>>> TTree *tree = new TTree("T","Debug");
>>>>> //Case C
>>>>> // MyClass object;
>>>>> // TBranch *branch = tree->Branch(branchname, &object,
>>>>> bufsize, splitlevel)
>>>>> //
>>>>> tree->Branch("debug",s);
>>>>> for(int j=0;j<10;j++) {
>>>>> s->hits.empty();
>>>>> for(int i=0;i<100;i++) {
>>>>> s->hits.push_back(42+i);
>>>>> }
>>>>> tree->Fill();
>>>>> }
>>>>> f1->Write();
>>>>> delete s;
>>>>> delete tree;
>>>>> delete f1;
>>>>> return 0;
>>>>> }
>>>>>
>>>>> I compile with
>>>>>
>>>>> g++ -Wall -pedantic -pthread -m32
>>>>> -I/home/bob/root/root-5.26.00d/include -c ttree_write.cxx -o
>>>>> ttree_write.o
>>>>>
>>>>> and I link with
>>>>>
>>>>> g++ -L/home/bob/root/root-5.26.00d/lib -lNew -lCore -lCint -lRIO
>>>>> -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript
>>>>> -lMatrix -lPhysics -lMathCore -lThread -pthread -lm -ldl -rdynamic
>>>>> -L/home/bob/root/root-5.26.00d/lib -lNew -lCore -lCint -lRIO -lNet
>>>>> -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix
>>>>> -lPhysics -lMathCore -lThread -lGui -pthread -lm -ldl -rdynamic
>>>>> ttree_write.o -o ttree_write
>>>>>
>>>>> When I run the program I got
>>>>> Error in <TTree::Branch>: The pointer specified for debug is not of a
>>>>> class known to ROOT
>>>>>
>>>>> I'm sure that I miss an important step but I'm out of ideas ! Any tips
>>>>> ?
>>>>>
>>>>>
>>>>>
>>>>
>>
>
>
Received on Mon Sep 27 2010 - 17:38:25 CEST

This archive was generated by hypermail 2.2.0 : Mon Sep 27 2010 - 17:50:02 CEST