TChain in PyROOT Scoping problem?

Hi,

I often have trees that I like to process with quick like python classes similar to what TTree::MakeClass creates. For example:

class ShowProAnal:
    def __init__(self, tShowPro):
        self.tShowPro = tShowPro
        self.showpro = ShowProfile()
        self.tShowPro.SetBranchAddress("showpro", self.showpro)
        
    def Loop(self):

        nentries = self.tShowPro.GetEntries()

        for i in range(0, nentries):
            self.tShowPro.GetEntry(i)
          # Things break here
def main():
    tShowPro = TChain("tShowPro")
    for i in range(1, len(sys.argv)):
        tShowPro.Add(sys.argv[1])
    showAnal = ShowProAnal(tShowPro)
    showAnal.Loop()

if __name__=="__main__":
    main()

The class ShowProfile is some class I’ve created in ROOT and have stored in ROOT Trees with the name “tShowPro”.

In this case, things will break when I call:
self.tShowPro.GetEntry(i)

Two things:
If I do this with a single file and a normal ROOT tree everything is fine. I.e.:

f = TFile(infile, "read")
t = f.Get("tShowPro")
showanal = ShowAnal(t)

Also, if I use a TChain, everything is fine in the scope of the function I declare it in, but anywhere else I have the same problem. So if I did all the analysis in the main function things would be fine. Or if I declare it in the init functions things are okay in the init function, but no where else.

Am I just not doing this in the right way? Or making some silly mistake? I think I understand the ownership of the objects I’m seeing, but I think something behind the scenes in the TChain is being cleared.

Thanks,

Elliott

Elliot,

I’m not seeing any problem … Does the crash persist if SetBranchAddress() is done just before starting the GetEntry() loop?

Also, this line:

should probably be:

Cheers,
Wim

Hi,

Thanks for the response. You’re right about that mistake with sys.argv[1], but that’s just a typo in the code I was writing for the forum.

I’m made some source that reproduces my problem. Now, I don’t think that this is a scoping problem. Things have trouble after the TChain::SetBranchAddress line with the TChain, but not with TTree. It doesn’t matter if you put everything inline. Here, take a look.

To run my example, you’ll have to first process MyClass.C, then run mktree2.py, then readChain2.py. In order,

[code]
root [0] .L MyClass.C+
root [1] .q
python mktree2.py python readChain2.py
[\code]

Thanks again,

Elliott
MyClass.C (55 Bytes)
readChain2.py (1.33 KB)
mktree2.py (647 Bytes)

Hi,

okay, got it: the problem is that TTree::SetBranchAddress() has been pythonized, so that the object can be passed “as is”, but TChain::SetBranchAddress() is not, and therefore needs the use of AddressOf():

I’ll implement TChain::SetBranchAddress.

Cheers,
Wim

Hi,

TChain::SetBranchAddress pythonization is now in trunk. Sorry for the oversight.

Cheers,
Wim

Ah,

Should of tried that. Thanks,

Elliott