[solved] Wrong content of branches when cloning a tree


I’m trying to do some skimming and slimming, so basically I clone the input tree, deactivate all unwanted branches and then get the entries event by event. For each event I’m checking if some condition is fullfiled and, in case it is, I’m filling all the active branches to the new tree.
In principal everything works fine, as long as I’m only using pointer variables for the test condition. As soon as I’m connecting any non pointer like variable with a branch (via SetBranchAddress(…)) something really strange happens: The variable is read in correctly and also all checks/prints of the conent are okay, but in the new tree that I dump, all entries of this variable are incorrect. I checked it over and over again, I doesn’t occur when it’s a pointer variable, but for every non-pointer variable I checked (independent of the type). And this happens as soon as I’m setting the branch address and connect this variable with a branch. Doesn’t matter whether I’m using the variable at all or not, but the dumped value is wrong (and has all the time the same value).
So, e.g.

  vector<int>   *el_loose;
  TBranch        *b_el_loose;
  fChain->SetBranchAddress("el_loose", &el_loose, &b_el_loose);

works fine, while this will give wrong results in the output tree:

  Int_t         el_n;
  TBranch        *b_el_n;
  fChain->SetBranchAddress("el_n", &el_n, &b_el_n);

Does anybody have a clue, what’s going wrong here?

Thanks a lot!

[quote]Does anybody have a clue, what’s going wrong here?[/quote]At the very least you need to initialize the pointer to zero.


Why does one need to initialize the branch pointer “b_el_n” to zero (that’s the branch which fails according to the original post)?
That’s never done in the code generated by “MakeClass” nor “MakeSelector”.
BTW. How about my proposal here: [url]MakeClass and MakeSelector SetBranchStatus("*", 0) in Init


The pointer that must be initialized is the one for the user data:vector<int> *el_loose;Indeed the branch pointer does not have to be set since SetBranchAddress will unconditionally set it to either 0 or the address of the branch.

As far as your other proposal I am still mulling it over (I don’t like the SetBranchStatus interface as a general rule but you do have a point :slight_smile:).


I’ve been thinking that maybe, because Chris plays with branches (switching them on and off), he suffers from the same problem as reported in my old post (i.e. there are branches which have “status” 1 by default but their “branch address” was not set).
What concerns the additional ‘SetBranchStatus("*", 0)’ in the “MakeClass” and “MakeSelector” generated code - putting this call in the “Init” method seems to me to be the easiest thing that heals this problem.

Hi and thanks a lot!

Finally found the solution: I also had to set the branch address for the cloned tree as well.