def runSubEventTest( opdata, opdisplay ):
    subevents = formSubEvents( opdata, config, pmtspe )
    print "Number of Subevents: ",len(subevents)
    products = []
    displayslot = int(opdisplay.slot.text())
    for subevent in subevents:
        for ch,subeventlist in subevent.chsubeventdict.items():
            chspe = pmtspe[ch]
            if len(subeventlist)>0:
                print "FEMCH ",ch," has subevent: spe=",chspe," gainfactor=",subeventlist[0].gainfactor
            
            # draw chsubevents
            for chsubevent in subeventlist:
                pexp,pthresh = makeChSubEventPlot( chsubevent, chspe, displayslot )
                products.append( { "plotitem":pexp,"femch":ch,"screen":"waveform"} )
                products.append( { "plotitem":pthresh,"femch":ch,"screen":"waveform"} )
        
        phitacc = pg.PlotCurveItem()
        phitacc.setData( x=range(0,len(subevent.hitacc)), y=subevent.hitacc, pen=(255,255,102,255) )
        products.append( { "plotitem":phitacc,"femch":None,"screen":"waveform"} )

        ppeacc = pg.PlotCurveItem()
        ppeacc.setData( x=range(0,len(subevent.peacc)), y=subevent.peacc, pen=(255,0,255,255) )
        products.append( { "plotitem":ppeacc,"femch":None,"screen":"waveform"} )

        print "Subevent: ",np.argmax( subevent.hitacc )

    
    return products
def run_subevent_finder():
    global opdata

    f = rt.TFile("output_test_subeventfinder.root", "RECREATE" )
    t = rt.TTree( "subevent", "Subevent info" )
    # variables
    eventid = array.array('i',[0])
    nsubevents = array.array('i',[0])
    tpeak = array.array('i',[0]*20)
    hitmax = array.array('i',[0]*20)
    pemax = array.array('f',[0]*20)
    t.Branch( "eventid", eventid, "eventid/I" )
    t.Branch( "nsubevents", nsubevents, "nsubevents/I" )
    t.Branch( "tpeak", tpeak, "tpeak[20]/I" )
    t.Branch( "hitmax", hitmax, "hitmax[20]/I" )
    t.Branch( "pemax", pemax, "pemax[20]/F" )

    ievent = 1
    ok = opdata.getEvent( ievent )
    
    while ok:

        eventid[0] = ievent
        subevents = formSubEvents( opdata, config, pmtspe )
        nsubevents[0] = len(subevents)

        if len(subevents)>0:
            print "found ",len(subevents)," subevents in event ",ievent
            for n,subevent in enumerate(subevents):
                if n>=20:
                    break
                tpeak[n] = subevent.tpeak
                hitmax[n] = int(subevent.hitmax)
                pemax[n] = subevent.pemax
            #opdisplay.run_user_analysis.setChecked(True)
            #opdisplay.gotoEvent( ievent, slot=5 )
            #opdisplay.plotData() 
            #opdisplay.run_user_analysis.setChecked(False)
            #raw_input()
        else:
            for n in range(0,20):
                tpeak[n] = 0
                hitmax[n] = 0
                pemax[n] = 0
        t.Fill()
        ievent += 1
        ok = opdata.getEvent( ievent, slot=5 )
        if ievent>=50:
            break
    t.Write()
def run_subevent_finder():
    global opdata

    f = rt.TFile("output_test_subeventfinder.root", "RECREATE")
    t = rt.TTree("subevent", "Subevent info")
    # variables
    eventid = array.array('i', [0])
    nsubevents = array.array('i', [0])
    tpeak = array.array('i', [0] * 20)
    hitmax = array.array('i', [0] * 20)
    pemax = array.array('f', [0] * 20)
    t.Branch("eventid", eventid, "eventid/I")
    t.Branch("nsubevents", nsubevents, "nsubevents/I")
    t.Branch("tpeak", tpeak, "tpeak[20]/I")
    t.Branch("hitmax", hitmax, "hitmax[20]/I")
    t.Branch("pemax", pemax, "pemax[20]/F")

    ievent = 1
    ok = opdata.getEvent(ievent)

    while ok:

        eventid[0] = ievent
        subevents = formSubEvents(opdata, config, pmtspe)
        nsubevents[0] = len(subevents)

        if len(subevents) > 0:
            print "found ", len(subevents), " subevents in event ", ievent
            for n, subevent in enumerate(subevents):
                if n >= 20:
                    break
                tpeak[n] = subevent.tpeak
                hitmax[n] = int(subevent.hitmax)
                pemax[n] = subevent.pemax
            #opdisplay.run_user_analysis.setChecked(True)
            #opdisplay.gotoEvent( ievent, slot=5 )
            #opdisplay.plotData()
            #opdisplay.run_user_analysis.setChecked(False)
            #raw_input()
        else:
            for n in range(0, 20):
                tpeak[n] = 0
                hitmax[n] = 0
                pemax[n] = 0
        t.Fill()
        ievent += 1
        ok = opdata.getEvent(ievent, slot=5)
        if ievent >= 50:
            break
    t.Write()
def runSubEventTest(opdata, opdisplay):
    subevents = formSubEvents(opdata, config, pmtspe)
    print "Number of Subevents: ", len(subevents)
    products = []
    displayslot = int(opdisplay.slot.text())
    for subevent in subevents:
        for ch, subeventlist in subevent.chsubeventdict.items():
            chspe = pmtspe[ch]
            if len(subeventlist) > 0:
                print "FEMCH ", ch, " has subevent: spe=", chspe, " gainfactor=", subeventlist[
                    0].gainfactor

            # draw chsubevents
            for chsubevent in subeventlist:
                pexp, pthresh = makeChSubEventPlot(chsubevent, chspe,
                                                   displayslot)
                products.append({
                    "plotitem": pexp,
                    "femch": ch,
                    "screen": "waveform"
                })
                products.append({
                    "plotitem": pthresh,
                    "femch": ch,
                    "screen": "waveform"
                })

        phitacc = pg.PlotCurveItem()
        phitacc.setData(x=range(0, len(subevent.hitacc)),
                        y=subevent.hitacc,
                        pen=(255, 255, 102, 255))
        products.append({
            "plotitem": phitacc,
            "femch": None,
            "screen": "waveform"
        })

        ppeacc = pg.PlotCurveItem()
        ppeacc.setData(x=range(0, len(subevent.peacc)),
                       y=subevent.peacc,
                       pen=(255, 0, 255, 255))
        products.append({
            "plotitem": ppeacc,
            "femch": None,
            "screen": "waveform"
        })

        print "Subevent: ", np.argmax(subevent.hitacc)

    return products