Using array of branch name to set branch address

Hi Rooters,

For few branches in a tree, I do this to set the branch address:

int x, y, z;             // three brances
TBranch *b_x, *b_y, *b_z;    // pointers to the branches
TChain *chain = new TChain("sometree");
chain->SetBranchAddress("x", &x, &b_x);
chain->SetBranchAddress("y", &y, &b_y);
chain->SetBranchAddress("y", &y, &b_y);

However, I wounder if this can be done for more branches using an array of the branches and loop.

I don’t think you can do that strictly in C++. There are two ways I know to make the variable/branch/address thing less repetitive:

a) define a macro like:

#define defbranch(tree,varname,vartype) vartype varname; TBranch *b_varname;tree->SetBranchAddress(#varname,&varname,&b_varname);


note that I don’t know if that macro is actually going to work correctly, I don’t use them myself. You could write a shorter macro if you hardcoded the chain name and the types.

b) use a POD struct and create a branch for that with “splitting”:

struct {int x,y,z;} outvars;

I use b) myself, and it works for mixed types and array types. Reading the output tree becomes difficult however as you have to re-connect a new “invars” variable of the same type as the original.