Re: [ROOT] using STL containers in ROOT TTrees

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Mar 05 2003 - 17:59:36 MET


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