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
message:

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);
tree->Fill();
}

tree->Write();
return 0;

}

The (nonworking) sample code is in pp.rhul.ac.uk/~cowan/rootTest/

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,

Glen

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 dict.cc -c TwoVector.h g++ -o rootTest rootTest.cc TwoVector.cc dict.cc `root-config --cflags --libs`

-Option 2: Use root.exe directly and do

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

Thanks Rene,

The 2nd of your solutions, namely,

root > .L TwoVector.cc+
root > .x rootTest.C

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

rootcint -f dict.cc -c TwoVector.h
g++ -o rootTest rootTest.cc TwoVector.cc dict.cc root-config --cflags --libs
./rootTest
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 pp.rhul.ac.uk/~cowan/rootTest/

Sorry to be a bother – merci d’avance,

Glen

Glen

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

Rene

Got it! Thanks, Rene. Cheers,

Glen

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

Code:
ClassDef(TwoVector,0)[/quote]

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.

Cheers,
Philippe.

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,

Glen

Glen,

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

Rene