TTree->GetBranch("branch") segmentation violation after removing a branch

Dear root expert,
In my code I need to change entries of a particular TBranch. I figured out that the easiest way was to first remove the branch, then adding a new empty branch with the same name and then fill it.
To remove a TBranch I followed the following topic:


using a code more or less like this:

auto *f = new TFile(fileName, "UPDATE");
auto *t = (TTree*)f->Get(treeName);
if(t->GetBranch(branchName)) 
{
    t->GetListOfBranches()->Remove(t->GetBranch(branchName));
    t->Write();
}
f->Close();

Sadly this procedure half-broken my file. Now

auto *f = new TFile(fileName, "UPDATE");
auto *t = (TTree*)f->Get(treeName);
t->GetBranch(branchName);  //<--- segmentation violation 

gives a segmentation violation if branchName (any branchName) is not present in the TTree, while should give a null pointer.

I also tried to re-add the branch

auto *f = new TFile(fileName, "UPDATE");
auto *t = (TTree*)f->Get(treeName);
TBranch *b;
float value = -1.;
b = t->Branch(name, &value, name + "/F");
for(int i=0; i<t->GetEntries(); ++i){
    t->GetEntry(i);
    bMva->Fill();
}
t->Write();
f->Close();

But I get another segmentation violation if I try to Draw this branch:

auto *f = new TFile(fileName,);
auto *t = (TTree*)f->Get(treeName);
t->Draw(branchName); //<--- segmentation violation

What I did wrong?
What can I avoid this?

Alberto


ROOT Version: ROOT 6.12/07
Platform: Not Provided
Compiler: Not Provided


You would also need to remove the corresponding leaves from GetListOfLeaves.

How can I do it?
I find it strange that this is never mentioned in any of the topic I read about the argument.

Alberto

For the ‘simple’ cases, an example is at the last post of the topic you linked.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.