[ROOT] using STL containers in ROOT TTrees

From: Pietro Govoni (Pietro.Govoni@cern.ch)
Date: Wed Mar 05 2003 - 17:03:56 MET


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