Hi,
I am trying to take an original TTree and then make multiple subsets of this TTree saved to different files. i.e. pseudocode of what I am trying to do:
Open file with original TTree
Extract original TTree
Make many empty clones of the original TTree
Loop over the original TTree{
Copy entry to each clone that passes some selection criteria.
}
Save out each cloned TTree which is now a subset of the original TTree, all to different files.
It works fine with up to 3 TTrees, but after that it gives a seg fault. e.g.
TFile inputfile("input.root");
TTree *inputTree;
inputfile.GetObject("treeName",inputTree);
const auto nentries = inputTree->GetEntries();
TFile file1("file1.root","recreate")
auto tree1 = inputTree->CloneTree(0);
TFile file2("file2.root","recreate")
auto tree2 = inputTree->CloneTree(0);
TFile file3("file3.root","recreate")
auto tree3 = inputTree->CloneTree(0);
std::vector<int>* someVectorInOriginalTTree;
inputtree->SetBranchAddress("someVectorInOriginalTTree",&someVectorInOriginalTTree);
for (auto i : ROOT::TSeqI(nentries) ){
inputTree->GetEntry(i);
if ( someVectorInOriginalTTree.at(0)==1){
tree1->Fill();
}
if ( someVectorInOriginalTTree.at(1)==1){
tree2->Fill();
}
if (someVectorInOriginalTTree.at(2)==1){
tree3->Fill();
}
}
Manages to run fine, but if I add the line “auto tree4 = inputTree->CloneTree(0);” (even without changing anything else so it shouldn’t actually even be doing anything), I get a segmentation fault, on the line “inputTree->GetEntry(i);”, even though nothing about inputTree should have changed.
Using root 6.14.04-x86_64-slc6-gcc62-opt has the problem above.
Using root 6.20.06-x86_64-centos7-gcc8-opt has the same problem, but for some reason it manages to do 6 clones before failing instead of 3 as in the 6.14.04 case.
If I remove the lines
TFile file1("file1.root","recreate")
TFile file2("file2.root","recreate")
TFile file3("file3.root","recreate")
then I am able to do auto tree4 = inputTree->CloneTree(0); without it seg faulting… However if I then add these lines after the loop, i.e.
TFile inputfile("input.root");
TTree *inputTree;
inputfile.GetObject("treeName",inputTree);
const auto nentries = inputTree->GetEntries();
auto tree1 = inputTree->CloneTree(0);
auto tree2 = inputTree->CloneTree(0);
auto tree3 = inputTree->CloneTree(0);
for (auto i : ROOT::TSeqI(nentries) ){
inputTree->GetEntry(i);
if ( [...some selection...]){
tree1->Fill();
}
if ( [...some other selection...]){
tree2->Fill();
}
if ( [...some other selection...]){
tree3->Fill();
}
}
TFile file1("file1.root","recreate")
TFile file2("file2.root","recreate")
TFile file3("file3.root","recreate")
It then seg faults again… And again it is seg faulting on the line
inputTree->GetEntry(i);
How can initiating some TFiles after a loop cause something within the loop to seg fault?
What is wrong with what I am doing (in particular how can initiating TFiles after a loop cause something completely unrelated during a loop to seg fault?), and how do I do what I want to do?
I’ve also noticed that when removing the selection if statement the issue is still there, but removing the line “inputtree->SetBranchAddress(“someVectorInOriginalTTree”,&someVectorInOriginalTTree);” gets rid of the issue (but I need this line to make selections).