I am working on a project where I need to read data from an input tree and then make a decision on whether or not to copy each event over to an output tree so I wrote a short program in C++ to tackle this issue. This has caused problems when I am reading and copying data from branches that contain array values in each event. The size of the array varies between events, but there is an additional branch detailing how big this array is for each event. So I am using vectors to store the data in each event of the array branch temporarily and choosing the size of the vector after I read the branch that gives me the size of the array. I will show a quick example of what I have been doing so far:
However, when I look at the branch for “Array” in the output file, the values of the branch will be nonsense. What am I doing wrong and is there a better way to do what I am trying to accomplish? Thanks for the advice in advance.
As per what goes wrong: all other branches are fine, just the “Array” branch contains bogus values in the output file? If yes, my bet would be that SetBranchAddress(..., nullptr) interferes with the CloneTree. Maybe you can call otree->SetBranchAddress to force the right address for the input branch (not sure, @pcanal can comment with more authority).
Yes the SetBranchAddress to nullptr is disastrous as the address is propagated also to the output tree … thus it has nothing valid to write down. Just removing that SetBranchAddress should solve the problem.
I would consider using the following ‘improvements’ on the code
Thank you for the response, I tried this and it seems to solve my issue. However, I want to understand how this solution works. I am a bit unclear on a few things:
What is the purpose of including a TBranch in the code and using itree->LoadTree(i) rather than just using the integer ArraySize and calling itree->GetEntry(i)?
Since we don’t set the address of “Array” to a nullptr and after the loop the branch address is still set to Array.data(), how are we making sure that a seg fault doesn’t occur from trying to get the new value of ArraySize but in the process of doing so dumping the data from the branch ArraySize into the vector ArraySize and having the vector be smaller than it’s supposed to since we haven’t resize it yet.
I think I have an idea of what is going on in the code, but want to make sure I fully understand what you did. Thank you for your help.