Problem storing dynamical vectors in tree

I’m currently using root 5.14/00f

Basically, I need to make an ntuple from some simulation data with many events (~10,000 to 1,000,000)

I created a class with many TClonesArray pointers (of Lorentz vectors) and std::vector pointers, for instance

//class members are
std::vector<int>* data;
TFile* file;
TTree* tree;

a script runs through this code, initializing this object,

Analyzer(){
data=new std::vector<int>(400); //for example
file=new TFile("something");
file->cd();
tree= new TTree("tree","tree");
tree->Branch("data", "vector<int>", &data);
}

The object is initialized only once. For every event in the simulation, the script iterates over a member function. the function would look something like this (pseudocode)

run(){
for (something)
(*data)[i]=output;
tree->Fill(); //this line gives segfaults
data->clear();
}

This code runs fine for many events, however, it gives segfaults whenever the size of data changes to be greater than the initial size. For instance, if I initialize the size of data to be 1, the code immediate gives segfault the first event. When I change the initial size to 600, it runs fine for many events but eventually gives segfault. If I make the function so that the # of entries that go into data is always fixed, the script will not give a segfault.

Perhaps I need some kind of command to refresh the branch? i tried UpdateAddress(), SetAddress(), Refresh() for the branch but none seem to work.
How can I fix this problem?

Any insight is appreciated.

Regards,
Tim.

Ah! I think I realized what was wrong with my code.

The [] operator does not check for out of bound problems and cannot be used for assigning new elements to the vector. Such a stupid error. :blush:

It must have been luck that the program didn’t crash until Fill is called.