Re: [ROOT] problem storing a pointed class in a TTree

From: Rene Brun (Rene.Brun@cern.ch)
Date: Sat Aug 10 2002 - 12:11:36 MEST


Hi Christophe,

I have modified your main program to inhibit split mode.
I also added a gDebug statement to show that your referenced object is
written.
Note that you should initiliaze your pointer LP in QEXT constructor,
otherwise you will have problems when reading.

output of test program
======================
starting
start filling
WriteBuffer, class:QEXT, name=TObject, fType[0]=66, TStreamerBase,
bufpos=74, pointer=82b9380, offset=0
WriteBuffer, class:QEXT, name=LP, fType[1]=64, TStreamerObjectPointer,
bufpos=84, pointer=82b9380, offset=12
WriteBuffer, class:RLEP, name=TObject, fType[0]=66, TStreamerBase,
bufpos=103, pointer=82b9398, offset=0
WriteBuffer, class:RLEP, name=LE, fType[1]=23, TStreamerBasicType,
bufpos=113, pointer=82b9398, offset=12
WriteBuffer, class:QEXT, name=bb, fType[2]=3, TStreamerBasicType,
bufpos=121, pointer=82b9380, offset=16
done. Writing
printing
******************************************************************************
*Tree    :altree    : a test ALEPH tree
*
*Entries :        1 : Total =           12849 bytes  File  Size =
458 *
*        :          : Tree compression factor =   1.00
*
******************************************************************************
*Br    0 :external_bank :
*
*Entries :        1 : Total  Size=      12511 bytes  File Size  =
0 *
*Baskets :        0 : Basket Size=      16000 bytes  Compression=   1.00
*
*............................................................................*
closing
test: 45
test: 206
deleting object


main program
===========
#include <iostream>
#include "TFile.h"
#include "TTree.h"
#include "TSystem.h"
#include "BankClasses.h"

int main()
{
        cout << "starting" << endl;
        TFile f("alephtree.root","RECREATE");
        TTree t("altree","a test ALEPH tree");
        QEXT* mqext = new QEXT;
        RLEP* mrlep = new RLEP;
        mqext->bb = 45;
        mrlep->LE = 206;
        mrlep->LF = 123;
        mqext->LP = mrlep;
        t.Branch("external_bank","QEXT", &mqext,16000,0);
        cout << "start filling" << endl;
        gDebug=2;
        t.Fill();
        gDebug=0;
        
        cout << "done. Writing" << endl;
        f.Write();
        cout << "printing" << endl;
        t.Print();
        cout << "closing" << endl;
        f.Close();
        cout << "test: " << mqext->bb << endl;
        cout << "test: " << mqext->LP->LE << endl;
        cout << "deleting object" << endl;
        delete mqext;
        delete mrlep;
        return 0;
}


Rene Brun

On Fri, 9 Aug 2002, Christophe Delaere wrote:

> Hi Rooters,
> 
> I plan to store a set of classes with pointers between them. As a first
> exercise I've just taken 2 classes, one having a pointer to the other.
> I just want to store everything in a TTree, with the second class being
> a sub-branch of the first one.
> 
> The problem I have is that the pointer seems not to be followed by the
> streamer.
> I can't figure out where is the problem. If I replace the pointer by a
> simple member, everything woks then.
> 
> I've included the code of my test program, as well as the linkdef file
> and the sequence I performed to compile it.
> 
> Can anybody tell me what I'm doing wrong ?
> 
> 
> Thanks in advance,
> Christophe.
> 
> 
> 
> /********************************************************
>  * BankClasses.h
>  *******************************************************/
> 
> #ifndef _ALPHACLASSES_H_
> #define _ALPHACLASSES_H_
> 
> #include <TObject.h>
> 
> class RLEP : public TObject
> {
> public:
>      virtual ~RLEP();
>      int LE;
>      int LF;
> ClassDef(RLEP,1)  //LEP information
> };
> 
> class QEXT : public TObject
> {
> public:
>      virtual ~QEXT();
>      RLEP* LP;
>      int bb;
> ClassDef(QEXT,1)  //external information access point
> };
> 
> #endif
> 
> /********************************************************
>  * BankClasses.cpp
>  *******************************************************/
> 
> #include "BankClasses.h"
> 
> ClassImp(RLEP)
> 
> RLEP::~RLEP(){}
> 
> 
> ClassImp(QEXT)
> 
> QEXT::~QEXT(){}
> 
> /************************************************************
>  * Test main file
>  ***********************************************************/
> 
> #include <iostream>
> #include "TFile.h"
> #include "TTree.h"
> #include "BankClasses.h"
> 
> int main()
> {
>         cout << "starting" << endl;
>         TFile f("alephtree.root","RECREATE");
>         TTree t("altree","a test ALEPH tree");
>         QEXT* mqext = new QEXT;
>         RLEP* mrlep = new RLEP;
>         mqext->bb = 45;
>         mrlep->LE = 206;
>         mrlep->LF = 123;
>         mqext->LP = mrlep;
>         t.Branch("external_bank","QEXT", &mqext);
>         cout << "start filling" << endl;
>         t.Fill();
>         cout << "done. Writing" << endl;
>         f.Write();
>         cout << "printing" << endl;
>         t.Print();
>         cout << "closing" << endl;
>         f.Close();
>         cout << "test: " << mqext->bb << endl;
>         cout << "test: " << mqext->LP->LE << endl;
>         cout << "deleting object" << endl;
>         delete mqext;
>         delete mrlep;
>         return 0;
> }
> 
> /****************************************************************
>  * BanksLinkDef.h
>  ***************************************************************/
> 
> #ifdef __CINT__
> 
> #pragma link off all globals;
> #pragma link off all classes;
> #pragma link off all functions;
> 
> #pragma link C++ class RLEP+;
> #pragma link C++ class QEXT+;
> 
> #endif
> 
> /****************************************************************
>  * "makefile"
>  ***************************************************************/
> 
> #!/bin/sh
> 
> rootcint -f BankClassesDict.cpp -c BankClasses.h BanksLinkDef.h
> g++ -O -Wall -fPIC -I/usr/include/root -c BankClasses.cpp
> g++ -O -Wall -fPIC -I/usr/include/root -c BankClassesDict.cpp
> g++ -O -Wall -fPIC -I/usr/include/root -c main.cpp
> g++ -shared -O BankClasses.o  BankClassesDict.o -o libBankClasses.so
> g++ -O main.o libBankClasses.so -L/usr/lib/root -lCore -lCint -lTree
> -lRint -lg++ -lm -ldl -rdynamic -o test
> 
> 
> 
> -- 
> +-----------------------------------------------------------\|/---+
> | Christophe DELAERE            office: e253               !o o!  |
> | UCL - FYNU                    phone : 32-(0)10-473234    ! i !  |
> | chemin du cyclotron, 2        fax   : 32-(0)10-452183     `-'   |
> | 1348 Louvain-la-Neuve BELGIUM e-mail: delaere@fynu.ucl.ac.be    |
> +-----------------------------------------------------------------+
> 



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:04 MET