I’m a beginner when it comes to root, and I have a problem.
I’m trying to get the the type of a branch and don’t know how to. I can use the TBrach::Print() function to see it but not get it and i don’t want to parse the string.I have seen that there is a function(GetTypeName) in the TBranchElement class but don’t know how to use it.
So is there a easy way to get the type from a branch?
The means to retrieve the type a branch slightly depends on the type of TTree you have.
More importantly, what are you trying to accomplish (they are many tools that may already provide the functionality you are looking for).
What i’m trying to do is a wrapper to root, so you can search a root file like a database. What i need to do is to have function that takes a tree and return all variable names and types.
Is there a way to do that?
[quote]so you can search a root file like a database. [/quote]Did you look at TTree::Query?
[quote]What i need to do is to have function that takes a tree and return all variable names and types. [/quote]To do it in a generic way is not trivial. See the method TTreePlayer::MakeClass and the class ROOT::TTreeProxyGenerator for a couple of example.
I got it to work but it was not easy, is ther anything better than :
root  TFile *f = new TFile(“t.root”)
root  f->cd(“ATLFAST”)
root  TTree * t= new TTree;
root  t= (TTree )gDirectory->Get(“h51”);
root  TBranch * b = new TBranch;
root  b= t->GetBranch(“Jetc”)
root  b->Print()
*Br 0 :Jetc : Jetc/I *
*Entries : 25000 : Total Size= 100584 bytes File Size = 9275 *
*Baskets : 1 : Basket Size= 64000 bytes Compression= 6.90 *
root  TObjArray *leaves = t->GetListOfLeaves();
root  TLeafObject *leafobj;
root  TLeaf leaf = (TLeaf)leaves->At(1);
root  TLeafObject leafobj;
root  leafobj = (TLeafObject)leaf;
root  leafobj->GetTypeName()
(const char* 0x1c6c678)“Int_t”
Not quite … In your example the leaf is not a TLeafObject and doing
leafobj = (TLeafObject*)leaf;is technically wrong.
On the positive side, you can actually do
Also you can ask for the list of leaves
t->GetListOfLeaves();You can then iterate through each leaves of the TTree (See User’s Guide collection for details on how to iterate through a collection) and call GetTypeName on each.
I saw that after i wrote the code, but thanks for all the help!!!