Activating/deactivating branches

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

Hi. I think I have a problem with TTrees. I have a file with a tree. I read it. Then I must copy a subset of this tree to another tree and I deactivate the branches I don’t want to copy. And all is fine .

But then I must reactivate all branches of the first tree because I need them in the macro. With the following code I get a segmentation violation, but if I don’t try “reactivation” and I read the tree again from the file to have all branches active (i.e. the first “Get” to have the tree to clone it, the second to have the same tree to use it), I have no problem. So…is there a solution?
Thanks a lot

TFile *hfile=TFile::Open("simulazione.root", "READ");
TTree *treelec1 = (TTree*)hfile->Get("tree");
treelec1->SetBranchStatus("*", 0);

for (auto activeBranchName : {"vertext*", "accept"}) 
  treelec1->SetBranchStatus(activeBranchName, 1); 

 TFile  *hfilerec = TFile::Open("ricostruzione.root","RECREATE");
auto treerec =treelec1->CloneTree();

treelec1->SetBranchStatus("vertext*", 0);//even without this, same problem
treelec1->SetBranchStatus("accept", 0);//even without this, same problem
treelec1->SetBranchStatus("*", 1);


So after cloning treelec1, if you set the status of all branches to active and then do treelect1->GetEntry(0), that causes a segfault?

If that’s the case I think your approach of creating another tree object to avoid the issue might be the right one, unless @pcanal has another suggestion.

Thanks @etejedor

Yes. As if it really doesn’t set them to active. If there is no solution I will work with two copies of the tree, but any suggestion is welcome

This is a bit odd. Can you run the failing case under valgrind --suppresions=$ROOTSYS/etc/valgrind-root.supp ... to see where the problem is?

Thanks @pcanal. Is there documentation about Valgrind? I never used it before and I don’t know how to use it🙈

Yes at :wink:
But you can simply try:

valgrind --suppressions=$ROOTSYS/etc/valgrind-root.supp --log-file=val.log your_command your_arguments

Ok thanks. I will try

Should I get this? (ok there is an error, but I don’t know if you need this) (779 Bytes)

And the minimal code that I used to produce this
error.cpp (947 Bytes)

Hi. Sorry, someone has an idea?I still can’t understand why my code crashes

The log seems to be saying that the process did not crash when using valgrind, is that right?

When I use valgrind I don’t get something like “segmentation violation”, when I use ROOT I get a crash when I run a simulation.
Maybe I have some ROOT problem. Compiler reports a warning even if it doesn’t crash (maybe I shouldn’t underestimate it)

No need to worry, I think. The gcc compiler on Ubuntu 20.04 has been upgraded (from “gcc 9.3.0” to “gcc 9.4.0” ) several days ago.