Example #1
0
def getTrap():

    ds, run, chan = 1, 11085, 580  # C1P1D3
    # trigger eff vals from old study: mu = 1.448, sig = 0.305

    # 2016-9-15-P3LQG_Run60001538 C2P1D3 P42748B
    # ds, run, chan = 4, 60001538, 1110

    from ROOT import GATDataSet
    gds = GATDataSet(run)
    tt = gds.GetGatifiedChain()
    bt = gds.GetBuiltChain()
    tt.AddFriend(bt)

    tCut = "channel==%d" % chan
    n = tt.Draw("Entry$:Iteration$", tCut, "goff")
    evt, itr = tt.GetV1(), tt.GetV2()
    evtList = [[int(evt[i]), int(itr[i])] for i in range(n)]

    trapOutput = []

    pEvt = -1
    for i, (iE, iH) in enumerate(evtList):
        if iE != pEvt:
            tt.GetEntry(iE)
        pEvt = iE

        hitE = tt.trapENFCal.at(iH)
        wf = bt.event.GetWaveform(iH)
        sig = wl.processWaveform(wf)
        waveRaw = sig.GetWaveBLSub()
        waveTS = sig.GetTS()

        # mimic onboard energy trapezoid
        eTrap = wl.trapFilter(waveRaw, 400, 180, 0)
        nPad = len(waveRaw) - len(eTrap)
        eTrap = np.pad(eTrap, (nPad, 0), 'constant')
        eTrapTS = np.arange(0, len(eTrap) * 10., 10)

        # trap timestamps shouldn't change, don't recalculate them
        if i == 0:
            pTS = eTrapTS
        if not np.array_equal(eTrapTS, pTS):
            print("Warning, the timestamps changed.  Uggggg")
            return

        # print(i, iE, iH, hitE)
        trapOutput.append(eTrap)

    # np.savez("../data/trapOutput.npz",trapOutput,eTrapTS) # original ds4 run
    np.savez("../data/trapOutput-2.npz", trapOutput, eTrapTS)  # new ds1 run
Example #2
0
def pulserBL():
    """ For each bkgIdx, get the HG pulser events and all their baseline values.
    Fill a dict of channels w/ the values, then make plots'n stuff.
    """
    dsNum, modNum = 1, 1
    goodList = ds.GetGoodChanList(dsNum)

    # thing we want to make histos with
    baseDict = {ch: [] for ch in goodList}

    # for bkgIdx in range(ds.dsMap[dsNum]+1):
    for bkgIdx in range(1):

        print "DS-%d, bkgIdx %d" % (dsNum, bkgIdx)

        runList = []
        bkgList = ds.bkgRunsDS[dsNum][bkgIdx]
        for idx in range(0, len(bkgList), 2):
            [
                runList.append(run)
                for run in range(bkgList[idx], bkgList[idx + 1] + 1)
            ]

        gds = GATDataSet()
        [gds.AddRunNumber(run) for run in runList]
        gatTree = gds.GetGatifiedChain()
        bltTree = gds.GetBuiltChain()
        gatTree.AddFriend(bltTree)

        theCut = "EventDC1Bits && channel%2==0 && trapENFCal>50"  # select pulser subset
        gatTree.Draw(">>elist", theCut, "entrylist")
        elist = gDirectory.Get("elist")
        gatTree.SetEntryList(elist)

        for iList in range(elist.GetN()):

            entry = gatTree.GetEntryNumber(iList)
            gatTree.LoadTree(entry)
            gatTree.GetEntry(entry)
            nChans = gatTree.channel.size()
            numPass = gatTree.Draw("channel", theCut, "GOFF", 1, iList)
            chans = gatTree.GetV1()
            chanList = list(set(int(chans[n]) for n in xrange(numPass)))

            hitList = (iH for iH in xrange(nChans)
                       if gatTree.channel.at(iH) in goodList)
            for iH in hitList:
                if dsNum == 2:
                    wf_downsampled = bltTree.event.GetWaveform(iH)
                    wf_regular = bltTree.event.GetAuxWaveform(iH)
                    wf = MJTMSWaveform(wf_downsampled, wf_regular)
                else:
                    wf = bltTree.event.GetWaveform(iH)
                chan = int(gatTree.channel.at(iH))
                signal = wl.processWaveform(wf)
                baseline, _ = signal.GetBaseNoise()
                baseline = float("%.2f" %
                                 baseline)  # kill precision to save on memory
                baseDict[chan].append(baseline)

    # could pickle the baseDict here, but idk, let's move on

    fig = plt.figure(figsize=(8, 7), facecolor='w')
    p1 = plt.subplot(111)
    # for ch in baseDict:
    ch = 608

    arr = np.asarray(baseDict[chan])

    p1.hist(arr)  #, bins='auto') # arguments are passed to np.histogram
    p1.set_title("Channel 608")

    plt.savefig("../plots/ch608-bl.pdf")
Example #3
0
def main(argv):
    """
    Matplotlib animation tutorial:
    http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
    Requires ffmpeg.  (brew install ffmpeg)
    """

    # Set input file and cuts
    # gatFile = TFile("~/project/mjddatadir/gatified/mjd_run27012.root") # generate w/ process_mjd_data_p1
    # bltFile = TFile("~/project/mjddatadir/built/OR_run27012.root")
    # gatTree = gatFile.Get("mjdTree")
    # bltTree = bltFile.Get("MGTree")
    run = 23725
    ds = GATDataSet(run)
    gatTree = ds.GetGatifiedChain()
    bltTree = ds.GetBuiltChain()
    gatTree.AddFriend(bltTree)
    print "Found",gatTree.GetEntries(),"input entries."

    # Get first timestamp
    gatTree.GetEntry(0)
    # print type(gatTree.localTime.localTime)


    theCut = "Entry$<50"
    # theCut = "channel == 674" # C1P7D3
    # theCut = "trapE < 6000 && channel==674 && timestamp/1e8 > 148359"
    # theCut = "channel==632 && timestamp/1e8 > 148359"

    outFile = "../plots/movie_run%d.mp4" % run

    # Print cut and events passing cut
    print "Using cut:\n",theCut,"\n"
    gatTree.Draw(">>elist", theCut, "entrylist")
    elist = gDirectory.Get("elist")
    gatTree.SetEntryList(elist)
    nList = elist.GetN()
    print "Found",nList,"entries passing cuts."



    # First set up the figure, the axis, and the plot element we want to animate
    fig = plt.figure(figsize=(11,5), facecolor='w')
    a1 = plt.subplot(111)
    # a2 = plt.subplot(111)
    # a1.set_xlim(0,50000)
    # a1.set_ylim(0,1000)
    a1.set_xlabel("time (ns)")
    a1.set_ylabel("ADC")
    p1, = a1.plot(np.ones(1), np.ones(1), color='blue')
    p2, = a1.plot(np.ones(1), np.ones(1), color='red')

    # initialization function: plot the background of each frame
    def init():
        p1.set_data([],[])
        p2.set_data([],[])
        return p1,p2,

    # animation function.  This is called sequentially (it's the loop over events.)
    def animate(iList):

        entry = gatTree.GetEntryNumber(iList);
        gatTree.LoadTree(entry)
        gatTree.GetEntry(entry)
        nChans = gatTree.channel.size()
        numPass = gatTree.Draw("channel",theCut,"GOFF",1,iList)
        chans = gatTree.GetV1()
        chanList = list(set(int(chans[n]) for n in xrange(numPass)))
        event = bltTree.event

        locTime = gatTree.localTime.localTime # wow, srsly?

        # Loop over hits passing cuts
        hitList = (iH for iH in xrange(nChans) if gatTree.channel.at(iH) in chanList)  # a 'generator expression'
        for iH in hitList:

            # wf = MGTWaveform()
            iEvent = 0
            wf_downsampled = event.GetWaveform(iH)
            wf_regular = event.GetAuxWaveform(iH)

            wf = MJTMSWaveform(wf_downsampled,wf_regular)


            iEvent = entry
            run = gatTree.run
            chan = gatTree.channel.at(iH)
            energy = gatTree.trapE.at(iH)
            # timestamp = gatTree.timestamp.at(iH) / 1e8
            # locTime = timestamp - firstTime


            signal = wl.processWaveform(wf)
            waveRaw = signal.GetWaveRaw()
            waveBLSub = signal.GetWaveBLSub()
            waveTS = signal.GetTS()

            baseline = np.sum(waveRaw[:50])/50

            # B, A = butter(2,1e6/(1e8/2), btype='lowpass')
            # data_lPass = lfilter(B, A, waveRaw)

            # simple trap filter (broken)
            trapTS = waveTS
            ADCThresh = 1.
            trap, trigger, triggerTS = trapFilt(waveBLSub,trapThresh=ADCThresh)
            trap = trap + baseline

            # fill the figure
            p1.set_ydata(waveRaw)
            p1.set_xdata(waveTS)
            p2.set_ydata(trap)
            p2.set_xdata(trapTS)
            plt.title("Run %d  Channel %d  Entry %d  trapE %.1f  locTime %.1f s" % (run,chan,iList,energy,locTime))

            # dynamically scale the axes
            xmin, xmax = np.amin(waveTS), np.amax(waveTS)
            a1.set_xlim([xmin,xmax])
            if energy < 1000:
                a1.set_ylim(0,1000)
            else:
                ymin, ymax = np.amin(waveRaw), np.amax(waveRaw)
                a1.set_ylim([ymin-abs(0.1*ymin),ymax+abs(0.1*ymax)])

            # print progress
            # print "%d / %d  Run %d  nCh %d  chan %d  trapE %.1f  samp %d" % (iList,nList,run,nChans,chan,energy,wf.GetLength())
            if iList%500 == 0 and iList!=0:
                print "%d / %d entries saved (%.2f %% done)." % (iList,nList,100*(float(iList)/nList))

            return p1,p2,

    # call the animator.  blit=True means only re-draw the parts that have changed.
    anim = animation.FuncAnimation(fig, animate, init_func=init, frames=elist.GetN(), interval=0, blit=True)

    # save the animation as an mp4.  This requires ffmpeg or mencoder to be
    # installed.  The extra_args ensure that the x264 codec is used, so that
    # the video can be embedded in html5.  You may need to adjust this for
    # your system: for more information, see
    # http://matplotlib.sourceforge.net/api/animation_api.html
    anim.save(outFile, fps=20)#, extra_args=['-vcodec', 'libx264'])