Extract type from a branch

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?

Thanks!

Hi,

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).

Cheers,
Philippe.

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.

Cheers,
Philippe.

I got it to work but it was not easy, is ther anything better than :

root [0] TFile *f = new TFile(“t.root”)
root [1] f->cd(“ATLFAST”)
(Bool_t)1
root [2] TTree * t= new TTree;
root [3] t= (TTree )gDirectory->Get(“h51”);
root [4] TBranch * b = new TBranch;
root [5] b= t->GetBranch(“Jetc”)
(class TBranch
)0x6420b00
root [6] 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 [7] TObjArray *leaves = t->GetListOfLeaves();
root [11] TLeafObject *leafobj;
root [12] TLeaf leaf = (TLeaf)leaves->At(1);
root [13] TLeafObject leafobj;
root [14] leafobj = (TLeafObject
)leaf;

root [16] 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

leaf->GetTypeName();

directly.
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.

Cheers,
Philippe.

I saw that after i wrote the code, but thanks for all the help!!!