# run at interactive prompt by
# execfile("InvMass2.py")

def invmass(ob1,ob2):
    "calculate inv mass of 2 Objects"
    import math
    mass=(ob1.e()+ob2.e())**2
    mass-=(ob1.px()+ob2.px())**2
    mass-=(ob1.py()+ob2.py())**2
    mass-=(ob1.pz()+ob2.pz())**2
    mass=math.sqrt(mass)
    return mass/GeV


def getMuMuIM(mcon):
    "get invariant mass of Muons in Collection"
    # >= 2 elements and opposite charge
    mass = None
    if ( len(mcon)>1 and (mcon[0].charge() * mcon[1].charge()) < 0 ):
        mass =  invmass(mcon[0],mcon[1])

    return mass


# check if CollN already assigned
if not 'CollN' in dir():
    CollN = "MuonCollection"


# check if histmass already assigned
if not 'histmass' in dir():
    histmass = book ('/stat/test1', 'Zee', 100, 70., 120.)



iev=0
while ( theApp.nextEvent().isSuccess() and iev < 50):
    iev+=1
#    print "Event ", iev
#    mc=PyParticleTools.getMuons(CollN)
#    mc=PyParticleTools.getElectrons(CollN)
    mc=PyParticleTools.getParticleBases(CollN)
    if ( not mc ):
        continue
    
    mass = getMuMuIM( mc )
    if ( mass != None ):
        print "Masse = ", iev,  mass
        histmass.Fill( mass)
        

