STL member in a TREE to file

I would like to add an STL member (in the example below a simple vector, but in general a more complex map of vectors) to a Tree and make this persistent in a file. The code below works for the write case but crashes when I read back. I obviously miss some conceptual point… actually the code below is a snippet from a much more complex program (simplicity helps). I considered embedding the STL in a class and make that class persistent in a tree (with dictionary created by rootcint), but that would be an overkill for such a simple case (or would that be the only possibility?)

[code]#include
#include
#include
#include <TApplication.h>
#include <TTree.h>
#include <TBranch.h>

using namespace std;

int main(int argc, char **argv)
{
TApplication app(“App”,&argc, argv);
string option = argv[1] ;
vector * v = NULL ;
TTree * tree = NULL ;

if( option == “w” )
{
v = new vector ;
TFile * file = new TFile(“test.root”,“RECREATE”) ;
tree = new TTree(“vector”,“vector”) ;
v->push_back(0) ;
v->push_back(35) ;
for (int i=0; isize(); i++) {cout << i << " " << (v)[i] << endl ;}
tree->Branch(“v”,&v,“vector”) ;
tree->Fill() ;
tree->Write() ;
file->Close() ;
} else {
TFile * file = new TFile(“test.root”,“OLD”) ;
tree = (TTree
)file->Get(“vector”) ;
TBranch * branch = tree->GetBranch(“v”) ;
branch->SetAddress(&v) ;
int entries = (int)tree->GetEntries() ;
cout << "Entries " << entries << endl ;
tree->GetEntry(0) ;
cout << "v->size() " << v->size() << endl ;
for (int i=0; isize(); i++) {cout << i << " " << (*v)[i] << endl ;}
file->Close() ;
}

cout << “Done” << endl ;
app.Run ();
return 0;
}
[/code]
Thanks for any help/suggestion
Dario

Dario,

Use a TRint instead of a TApplication and change your call to TTree::Branch as shown in thecode below

Rene

[code]#include
#include
#include
#include <TRint.h>
#include <TTree.h>
#include <TBranch.h>
#include <TFile.h>
#include <TClass.h>

using namespace std;

int main(int argc, char **argv)
{
//TApplication app(“App”,&argc, argv);
TRint app(“App”,&argc, argv);
string option = argv[1] ;
vector * v = NULL ;
TTree * tree = NULL ;

if( option == “w” )
{
v = new vector ;
TFile * file = new TFile(“test.root”,“RECREATE”) ;
tree = new TTree(“vector”,“vector”) ;
v->push_back(0) ;
v->push_back(35) ;
for (int i=0; isize(); i++) {cout << i << " " << (v)[i] << endl ;}
tree->Branch(“v”,&v) ;
tree->Fill() ;
tree->Write() ;
tree->Print();
file->Close() ;
} else {
TFile * file = new TFile(“test.root”) ;
tree = (TTree
)file->Get(“vector”) ;
TBranch * branch = tree->GetBranch(“v”) ;
branch->SetAddress(&v) ;
int entries = (int)tree->GetEntries() ;
cout << "Entries " << entries << endl ;
tree->GetEntry(0) ;
cout << "v->size() " << v->size() << endl ;
for (int i=0; isize(); i++) {cout << i << " " << (*v)[i] << endl ;}
file->Close() ;
}

cout << “Done” << endl ;
app.Run ();
return 0;
}
[/code]

Hi René,
just for understanding: The reason it works with TRint instead of TApplication is some kind of streaming ability that comes with the interpreter? Am i guessing right? (My guess comes from the docu where it’s stated that TRint = TApplication + I/F to Interpreter.)

Assuming I am right, and i build an application that needs to add STL stuff into a branch, how would i suppress the interpreter window if I don’t want it?
Because when I use TRint it always pops up, doesn’t it?
Cheers,
Michael

It is not correct. The “console window” is generated by “Visual C++” just because you create the so-called “Win32 Console Application”. it has nothing to do with the ROOT.

TRint gives you the “banner” that can be suppressed by starting ROOT with “-l” flag.

The prompt is generated by root.cern.ch/root/html/src/TRint … #TRint:Run

More precisely by line if (needGetlinemInit) Getlinem(kInit, GetPrompt());
You can mimic what TRint::Run method does yopurself to exclude calling Getlinem(kInit to
(see: root.bnl.gov/QtRoot/htmldoc/TQtW … t:InitRint for example)

To exclude the console window you shoudl create “Win32” application rather “console” one. At this point you may not have worried about TRint and Getlinem because with no “console window” they do the dummy job

Actually the only you should need is the loading of the dictionary for the vector. You can either provide it yourself (by including in your own linkdef.h a #pragma link C++ class vector) or load the pre-generated vector dictionary by usinggROOT->ProcessLine("#include <vector>");

Cheers,
Philippe