Re: [ROOT] using STL containers in ROOT TTrees

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Mar 05 2003 - 22:22:41 MET


Hi Pietro,

In attachement, a slightly modified version of your script 
temperature2.cpp.
You had an error when creating the branches.
See more examples in $ROOTSYS/tutorials/tree*.C

Rene Brun

On Wed, 5 Mar 2003, Pietro 
Govoni wrote:

> Dear Mr. Brun,
> thak you for your immediate answer!
> 
> As far as I understand if I want to be sure to save something in a TTree,
> I must create a class that inherits from TObject and contains my personal
> stuff.
> 
> Now I am trying to implement a solution following this path; however, I
> also tried to save my data, instead in a stl::vector, in a simple C-like
> array, following the structure of tree2.C.
> 
> Again, I got a strange behaviour; I don't have any precise clue on where 
> the problem could be, but the fact that I am using arrays, since if I try 
> to save a (simple) class in a TTree I can read it afterward, without any 
> problem, provided that it doesn't contain any array.
> In attachment you can find the code (temperature2.cpp) with the array and 
> the one that seems to work (prova.cpp).
> 
> Thanks again
> 
> pietro
> 
> 
> 
> On Wed, 5 Mar 2003, Rene Brun wrote:
> 
> >Hi Pietro,
> >
> >STL collections are not accepted as top level branches. They must be included
> >inside a class. Currently top level branches can only be
> >  - basic types
> >  - TObject*
> >  - TCollection
> >  - folders
> >
> >We are now in the process of including native support for STL collections
> >in the ROOT dictionary. Like we do now with TClonesArray, STL vector and
> >list vector<myclass>, vector <myclass*>, etc will be supported by
> >the Tree split mechanism. We are testing this code now and hope to include it in
> >a few weeks in the standard version. The coming release 3.05/03 will not include
> >it.
> >
> >Rene Brun
> >
> >Pietro Govoni wrote:
> >> 
> >> Dear Roottalk,
> >> is there anbody who created a TTree containing an STL container?
> >> I am trying to store a simple vector<double>, following an example that I
> >> found among the tutorials.
> >> When I try to put in a TTree that vector I get a lot of different errors,
> >> depending on strange parameters, e.g. whether I put or not a line like:
> >> 
> >> cout << "---------------------------------------" << endl;
> >> 
> >> in the code.
> >> There should be something fundamental that I'm forgetting, does anybody
> >> know what should I do?
> >> 
> >> Thanks in advance
> >> 
> >> pietro
> >> 
> >> PS follows my little code:
> >> 
> >> #include "TFile.h"
> >> #include "TTree.h"
> >> #include <vector>
> >> 
> >> //
> >> // to run the example, do:
> >> // .x temperature.cpp   to execute with the CINT interpreter
> >> //
> >> 
> >> void temperature_w()
> >> {
> >>    TFile f("treeTemperature.root","recreate");     // create the file
> >>    TTree tempo("tempo","tentativo");               // create a new tree
> >>    vector <double> Temperature;                    // create a vector
> >>    for (int i=0; i< 40; ++i) Temperature.push_back(0.);    // initialize
> >> it
> >> 
> >>    cout << Temperature.size() << endl;
> >> 
> >>    tempo.Branch("Temperature",&Temperature,"temperatura 40 sonde (V)");
> >> 
> >>    for (int i=0; i<3; ++i)
> >>     {
> >>       int j = 0;
> >>       for (vector<double>::iterator it = Temperature.begin();
> >>           it != Temperature.end();
> >>           ++it)
> >>         {
> >>           *it = 10 * i + (++j);
> >>           cout << *it << '\t';
> >>         }
> >>       cout << Temperature.size() << endl;
> >>       tempo.Fill();
> >>     }
> >>    f.Write();
> >> }
> >> 
> >> void temperature_r()
> >> {
> >>    TFile f("treeTemperature.root");
> >>    TTree *tempo = (TTree*)f->Get("tempo");  // si puo' fare in altro
> >> modo??
> >>    tempo->Print();
> >> 
> >>    vector <double> Temperature;
> >>    for (int i=0; i< 40; ++i) Temperature.push_back(0.);  // (1)
> >>    cout << "ole " << Temperature.size() << endl;         // (2)
> >>    TBranch *b_Temperature = tempo->GetBranch("Temperature");
> >>    b_Temperature->SetAddress(&Temperature);
> >> 
> >>    Int_t nentries = (Int_t)tempo->GetEntries();
> >>    cout << nentries << endl;
> >> 
> >>    for (Int_t i=0; i<nentries; ++i)
> >>      {
> >>        b_Temperature->GetEntry(i);
> >>        cout << Temperature.size() << endl;
> >> /*
> >>        for (vector<double>::iterator it = Temperature.begin();
> >>            it != Temperature.end();
> >>            ++it)
> >>          {
> >>            cout << *it << '\t';
> >>          }
> >> */
> >>        cout << endl;
> >>      }
> >> 
> >>    delete tempo;
> >> }
> >> 
> >> void temperature()
> >> {
> >>    temperature_w();
> >>     temperature_r();
> >> }
> >
> 
> 
> 





This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:09 MET