I see you are identifying extra gluons by the condition that it immediately succeed the listing of an antitop quark.  I think that works for this particular simulation, but it is a bit clumsy because you have to check neighboring indices i and i+1.  It is less awkward if you only have to check a single index, for example, I think the condition that PdgId==21 and MotherIndex==4 is sufficient to identify the same particles.  You can write it in python as

iGlu = next( (i for i in range(len(tree.genPdgId)) if tree.genPdgId[i]==21 and tree.genMotherIndex[i]==4), None)