TTrees and STL vectors

Hello all,
I am attempting to add a class that contains a standard STL vector to a TTree and then draw a plot of the data contained within this vector. I am able to write the class to the TTree and according to TTree::Print() I have,

*Br 0 :Myclass.Myvector : *
*Entries : 1 : Total Size= 12456 bytes File Size = 0 *
*Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 *

This vector should contain 100 elements but if I attempt to perform MyTree.Draw(“MyVector”) from the interactive Root command prompt only a single data point is produced. Is there actually a way of drawing a plot of the data contained within a vector - if so, what is the procedure? I apologise if this has been addressed before, however I have read all the manuals and can’t seem to find anything relevant.

Thanks in advance for any advice,

–Max Atkin

This should work if you use version 4.00/06 or newer


Thanks for the quick response. I think I may have been too quick to conclude that it was a problem with TTree::Draw since after posting this topic I tried reading the information out of the tree and found my vectors had zero length.

What is the proper way to write an object which contains an STL container (such as a vector or map) to a TTree so that the data in the container can be read back out at a later time? Sorry if these questions seem trivial, but I can’t find this stuff anywhere in the documentation. If there does exist such documentation I would be grateful if you could point me in the right direction.

–Max Atkin


The short answer is “any way you would like” as long as your generate the dictionary for the containing class (or the contained class if any).

The typical is just simply
class Event {
std::vector vec;
and generate the dictionary.

MyTree.Draw("MyClass.MyVector") will draw all the existing elements in the vector if it contains numerical value. If it contains object you need to specify a data member (or member function if the library is available)

This means that one of the vector had exactly one element. You can detect more accurately which one, but using the TTree::Draw special
variable Entry$