Save stl vector to a tree

Hello!

I know my question came up a few times in the past, but I couldn’t find an example. I want to save stl vectors to a branch of a tree in a c++ program, which uses root-classes. Something like the following:

[code]
#include <vector.h>
#include <TFile.h>
#include <TTree.h>

//…

TFile * file = new TFile (“test.root”, “RECREATE”);
TTree * tree = new TTree(“tree”,"");

vector vectortest;
for(int i = 0; i < 40; i++) vectortest.push_back(i);

tree->Branch(“vectortest”, “vector”, &vectortest);
tree->Fill();
file->Write();
file->Close();[/code]

The program breaks with a segmentation violation. I’m using Root 4.04/02 and Scientific Linux SL Release 3.0.4.

Could you please help me?

Philip

Hi,

As for any other type the call to Branch expects the address of a pointer to the object:vector<int> * vectortest = new vector<int>; ... tree->Branch("vectortest", "vector<int>", &vectortest); The following simplier call should also work:vector<int> * vectortest = new vector<int>; ... tree->Branch("vectortest",&vectortest);

Cheers,
Philippe.

Thank you!