Snippet of pyRoot code works in one place in the script and then fails in another

Hi,

I have the following code:

tree0 = d.Get('tree')
for event in tree0: 
    #do some stuff

When I have this code in the beginning of my pyRoot script, the code runs successfully. However, later on in the script, if I put the exact same code, I see a segmentation fault:

Traceback (most recent call last):
File “my_compare_triggers.py”, line 131, in
for event in tree0:
File “/cvmfs/cms.cern.ch/slc7_amd64_gcc820/lcg/root/6.14.09-pafccj5/lib/ROOT.py”, line 232, in TTree__iter_
bytes_read = self.GetEntry(i)
SystemError: int TTree::GetEntry(Long64_t entry = 0, int getall = 0) =>
problem in C++; program state has been reset

I am running root version 6.14/09 with python version Python 2.7.15+ inside of a CMSSW_10_6_X release. My OS is Catalina 10.15.7.

Does anyone have any insight into this behavior?

Thanks very much.

I think I found the cause of the problem.

At the beginning of the code I had:

d = < someDir >
tree0 = d.Get(“tree”)

Later in the code I had:
d = < someOtherDir >
tree = d.Get(“tree”)

Then, when I asked it to loop over tree0 in the code AFTER I had redefined d, the program either intermittently threw the seg fault I posted above or the code would run but would loop over tree as opposed to tree0. I think the redefinition of d led to confusion in memory management and created the unstable behavior that I saw.