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();
}
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.
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.ccTwoVector.ccdict.ccroot-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.
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,