Standalone application to create TTree

Does there exist a simple example of a standalone C++ program that
creates a TTree and then stores it in a file? I would like the TTree to
contain one of my own objects, and I can’t seem to figure out how to
tell it about my class (in my example called TwoVector). I get the error

Error in : Cannot find class:TwoVector

The relevant lines of what I’m trying to do are :

#include "TwoVector.h"
int main(int argc, char **argv) {

TFile* outfile = new TFile(“test.root”, “RECREATE”);
TTree* tree = new TTree(“tree”, “test tree”);
TTree::SetBranchStyle(1); // new style (Bronch)
int bufsize = 32000;
int split = 1;
TwoVector* twoVec = new TwoVector(1,1);
TBranch* b = tree->Branch(“twoVec”, “TwoVector”, &twoVec, bufsize, split);
for (int i = 0; i<100; ++i){
float r1 = static_cast(rand()) / static_cast(RAND_MAX);
float r2 = static_cast(rand()) / static_cast(RAND_MAX);
twoVec->set(r1, r2);

return 0;


The (nonworking) sample code is in

I am sure that I just haven’t understood some step needed to tell it
about TwoVector. Sorry to ask what is probably such a dumb question
but I cannot seem to find any examples that I can understand.

Any help appreciated – cheers,


Probably in the TwoVector class you have to add the ClassDef macro:

this code must be placed before the ‘};’ the end the declaration statement of the class

Hi Glen,

You have two options:

-Option 1 : Implement your own main program

You must generate a dictionary for your class (see below)
You must add a ClassDef in your include (see attachment)
run the following script

rootcint -f -c TwoVector.h g++ -o rootTest `root-config --cflags --libs`

-Option 2: Use root.exe directly and do

root > .L root > .x rootTest.C (see rootTest.C in attachment)
TwoVector.h (1.17 KB)
rootTest.C (576 Bytes)

Thanks Rene,

The 2nd of your solutions, namely,

root > .L
root > .x rootTest.C

works fine but the 1st one (which is what I really need) still has a problem, namely

rootcint -f -c TwoVector.h
g++ -o rootTest root-config --cflags --libs
Error in : can not find any ShowMembers function for TwoVector!

I thought maybe the problem is related to what volpig mentioned above,
namely, that I need a ClassDef(TwoVector,0) in TwoVector.h, but I also
could not get this to work (that’s a longer story). Please let me know
if that’s where I’m going wrong. But in any case I hope can do this
with existing classes without modifying them.

As before I put the relevant code on

Sorry to be a bother – merci d’avance,



It looks like you did not take the file TwoVector.h that I posted.
Have a look at the differences with your original version.


Got it! Thanks, Rene. Cheers,


[quote]Probably in the TwoVector class you have to add the ClassDef macro:


Just to mention that this comment is unfortunately wrong! Using this statement explicitly says that the objects of that class should NOT be saved!

Instead, all that was needed was to generate the dictionary.


Dear All,

Just a brief follow up question on the previous (5 months ago)
discussion on how to store an object in a TTree. The example provided above by Rene works only if I add some lines of code to the header
file for the class that defines my object, namely, ClassDef (TwoVector,1)
and ClassImp(TwoVector).

My question now is: is there a way to store objects in a TTree in
cases where I don’t have the ability/permission to modify the
header files as required above? That is, suppose it’s someone
else’s class I want to use – I don’t own the .h file. Can I still
store such objects in a TTRee? Thanks,



You can remove the ClassDef/ClassImp statements and still do:
root > .L
root > .x rootTest.C