Re: [ROOT] STL vectors in trees

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Aug 21 2002 - 10:10:32 MEST


Hi Nicola,

Simply replace the line in LinkDef.h

#pragma link C++ class EventSTL;
by
#pragma link C++ class EventSTL+;

Rene Brun

On Wed, 21 Aug 2002, Nicola Amapane wrote:

> 
> Hi,
> 
> I am trying to write some STL vectors in a Tree. Apparently this should be
> easy, however it does not work for me. I tried to  find some examples but
> I could only find quite an old one:
> http://root.cern.ch/cgi-bin/print_hit_bold.pl/root/roottalk/roottalk99/2746.html
> 
> I tried to remplement it as enclosed here; it seems to write the tree
> correctly, but when I read it back the vector size is always 0...
> 
> I tried to play with split level, Bronch instead that Branch, etc., with
> no luck.
> 
> I am using 3.03.07.
> 
> What I do wrong? Is there any simple example to do something
> similar?
> 
> Thanks!
> 	Nicola
> 
> //------------------file EventSTL.h
> #include "TObject.h"
> #include <vector>
> 
> class EventSTL : public TObject
> {
>  public:
>  EventSTL();
>  virtual ~EventSTL();
>  void Electron(Double_t x);
>  void VI();
>  void Clear();
>  vector<Double_t> A;
> 
>  ClassDef(EventSTL,1)
> };
> 
> 
> //------------------file EventSTL.cxx
> #include "EventSTL.h"
> 
> ClassImp(EventSTL)
> 
> EventSTL::EventSTL() {}
> EventSTL::~EventSTL() {}
> void EventSTL::Electron(Double_t x)
> {
>  A.push_back(x); 
> }
> 
> void EventSTL::Clear()
> {
>  A.clear();      
> }
> 
> void EventSTL::VI() 
> {
>  cout << "Size: " << A.size() << " - Contents : " ;
>  for (Int_t i=0;i<A.size();i++)
>   {
>    cout << " " << A[i];
>   }
>  cout << endl;
> }
> 
> 
> //------------------file myLinkDef.h
> #ifdef __CINT__
> 
> #pragma link off all globals;
> #pragma link off all classes;
> #pragma link off all functions;
> 
> #pragma link C++ class EventSTL;
> #endif
> 
> 
> //------------------file STLW.C
> //
> {
>   gSystem->Load("EventSTL.so");
>   TFile myfile("STL.root","RECREATE","TTree Dynamic ROOT file");
> 
>   EventSTL *CC = new EventSTL;
>   TTree *mytree = new TTree("T","My tree");
>   mytree->Branch("abc","EventSTL",&CC,16000,0);
> 
>   for (Int_t i=0;i<20;++i) {
>     for (Int_t j=0;j<i;++j) {
>       CC->Electron(i);
>     }
>     mytree->Fill();
>     CC->VI();
>     CC->Clear();
>   }
>   
>   mytree->Write();
>   mytree->Print();
> }
> 
> 
> //------------------file STLr.C
> //
> {
>   gSystem->Load("EventSTL.so");
>   TFile myfile("STL.root");
> 
>   EventSTL *CC = 0;
>   TTree *T = (TTree*)myfile.Get("T");
>   T->SetBranchAddress("abc",&CC);
> 
>   T->Print();
> 
>   T->GetEntry(15);
>   CC->VI();
> }
> 
> 
> -----------------------------------
> Nicola Amapane                                   []=-
> INFN +39-011-6707374                             //    _-=__-=
> CERN +41-22-7671519                            _/|]    ) ___ \
> Mobile/SMS +39-(0)3471411410                  (_) \___/_(___)_|
> mailto:nicola.amapane@cern.ch                  @          @
> 



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