Of branches and templates

When i try to do the following, the code compiles, but when i run the program, at the points of usage it expects a data type “dataType” rather than the template argument type actually passed. I am using a non-template function for now, but is there any way to make this work?

Error
[size=85][color=#FF0000]Error in TTree::Bronch: Cannot find class:dataType[/color][/size]

Code:

[size=85][color=#0000FF]template
TBranch * Branch(TString name, dataType** addobj, Int_t bufsize = 32000, Int_t splitlevel = 99)
{
TBranch * branchExists = GetBranch(name);
if(branchExists!=NULL)
{
//std::cout << “Branch " << name << " already exists!!” << endl;
ERROR(“Branch " << name << " already exists!!”);
return NULL;
}
else
{
//create the branch
return TTree::Branch(name, “aaa”, addobj, bufsize, splitlevel);
}
}[/color][/size]

Hi,

[quote]return TTree::Branch(name, “aaa”, addobj, bufsize, splitlevel);[/quote]Why pass a hardcoded string as the 2nd argument?

I proposed you simply use:template<typename dataType> TBranch * Branch(TString name, dataType dataoptr, Int_t bufsize = 32000, Int_t splitlevel = 99) { TBranch * branchExists = GetBranch(name); if(branchExists!=NULL) { //std::cout << "Branch " << name << " already exists!!" << endl; ERROR("Branch " << name << " already exists!!"); return NULL; } else { //create the branch return TTree::Branch(name, dataptr, bufsize, splitlevel); } }which should propagate to the correct template instantiation.

Note: I strongly recommend not to inherit from TTree but instead to make this a free function:

template<typename dataType> TBranch * Branch(TTree &tree, TString name, dataType dataoptr, Int_t bufsize = 32000, Int_t splitlevel = 99) { TBranch * branchExists = tree.GetBranch(name); if(branchExists!=NULL) { //std::cout << "Branch " << name << " already exists!!" << endl; ERROR("Branch " << name << " already exists!!"); return NULL; } else { //create the branch return tree.Branch(name, dataptr, bufsize, splitlevel); } }

Cheers,
Philippe.

Yup that helps… and digging a bit deeper than the examples in the user guide… :slight_smile:

Thanks