[ROOT] IO Problem with a TClonesArray of non-TObjects

From: David Chamont (David.Chamont@poly.in2p3.fr)
Date: Thu Mar 28 2002 - 10:14:14 MET


Hi rooters,

I would like to understand why the program below does not work.
So to be able to collect in a TClonesArray some non-TObject objects
(TString in the example below), I use a template which transform the
class T into a TObject via double inheritance.  When I write and reread
the TClonesArray, it seems to lose the data. Any idea ?

David C.

===================================================
the file with the template
===================================================

#include <TObject.h>

template <class T>
class RtObj : public TObject, public T
 {
  public:
    RtObj() {}
    RtObj( const T & val ) : T(val) {}
    ClassDefT(RtObj,1)
 } ;

ClassDefT2(RtObj,T)

ClassImpT(RtObj,T)

===================================================
the linkdef file
===================================================

#ifdef __CINT__

#pragma link off all globals;
#pragma link off all classes;
#pragma link off all functions;

#pragma link C++ class RtObj<TString>+;

#endif

===================================================
the main file
===================================================

int main()
 {
  // begin
  TROOT rdummy("dummy","dummy") ;

  // write
  TFile * file =  new TFile("RtObj.root","recreate") ;
  TClonesArray array("RtObj<TString>",2) ;
  new (array[0]) RtObj<TString>("hello") ;
  new (array[1]) RtObj<TString>(" world") ;
  cout
    <<"write: "
    <<*((RtObj<TString> *)(array[0]))
    <<*((RtObj<TString> *)(array[1]))
    <<endl ;
  array.Write("clones",TObject::kSingleKey) ;
  file->Close() ;
  delete file ;

  // read
  file =  new TFile("RtObj.root","read") ;
  TClonesArray * parray = (TClonesArray *)(file->Get("clones")) ;
  cout
    <<"read: "
    <<*((RtObj<TString> *)((*parray)[0]))
    <<*((RtObj<TString> *)((*parray)[1]))
    <<endl ;
  file->Close() ;
  delete file ;

  // end
  return 0 ;
 }



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