示例#1
0
def compute_mean_rms(file_path, plot=False):
    """Step through a charge-injection binary file and calculate the
    mean and standard deviation of the charge distribution in each pixel.

    Warning
    -------
    Note that, since the signal in ADC counts can be a relatively large
    number, we use Welford's algorithm for the running variance:
    https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
    And yes, this is not a nuisance---the first attempt at using the
    dumb running variance ended up in flames (in case you're tempted to
    change this).
    """
    assert(file_path.endswith('.mdat'))
    n = numpy.zeros((300, 352), 'd')
    mean = numpy.zeros((300, 352), 'd')
    M2 = numpy.zeros((300, 352), 'd')
    for event in xpeBinaryFileWindowed(file_path):
        indices = (slice(event.xmin, event.xmax + 1),
                   slice(event.ymin, event.ymax + 1))
        n[indices] += 1
        delta = event.adc_values - mean[indices]
        mean[indices] += delta/n[indices]
        M2[indices] += delta*(event.adc_values - mean[indices])
    rms = numpy.sqrt(M2/(n - 1))
    if plot:
        fig, axes = plt.subplots(nrows=1, ncols=2)
        img0 = axes[0].matshow(mean)
        img1 = axes[1].matshow(rms)
        plt.show()
    return mean, rms
示例#2
0
def compute_mean_rms(file_path, plot=False):
    """Step through a charge-injection binary file and calculate the
    mean and standard deviation of the charge distribution in each pixel.

    Warning
    -------
    Note that, since the signal in ADC counts can be a relatively large
    number, we use Welford's algorithm for the running variance:
    https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
    And yes, this is not a nuisance---the first attempt at using the
    dumb running variance ended up in flames (in case you're tempted to
    change this).
    """
    assert (file_path.endswith('.mdat'))
    n = numpy.zeros((300, 352), 'd')
    mean = numpy.zeros((300, 352), 'd')
    M2 = numpy.zeros((300, 352), 'd')
    for event in xpeBinaryFileWindowed(file_path):
        indices = (slice(event.xmin,
                         event.xmax + 1), slice(event.ymin, event.ymax + 1))
        n[indices] += 1
        delta = event.adc_values - mean[indices]
        mean[indices] += delta / n[indices]
        M2[indices] += delta * (event.adc_values - mean[indices])
    rms = numpy.sqrt(M2 / (n - 1))
    if plot:
        fig, axes = plt.subplots(nrows=1, ncols=2)
        img0 = axes[0].matshow(mean)
        img1 = axes[1].matshow(rms)
        plt.show()
    return mean, rms
示例#3
0
def run_xpe_recon(file_path,
                  num_events=1000000000,
                  zero_suppression=9,
                  coordinate_system='pixy',
                  output_path=None,
                  min_cluster_size=6,
                  max_cluster_size=400,
                  small_radius=1.5,
                  large_radius=3.5,
                  weight_scale=0.05):
    """Run the event reconstruction on a binary file.
    """
    assert (file_path.endswith('.mdat'))
    if output_path is None:
        output_path = file_path.replace('.mdat', '_xpe.root')
    logger.info('Opening output file %s...' % output_path)
    output_file = ROOT.TFile(output_path, 'RECREATE')
    output_tree = xpeReconTree()
    event_id = 0
    for event in xpeBinaryFileWindowed(file_path):
        cluster_list = hierarchical_clustering(event, zero_suppression,
                                               coordinate_system)
        cluster = cluster_list[0]
        recon = xpePixyRecon(cluster)
        if not recon.error_summary:
            output_tree.set_value('fRunId', -1)
            output_tree.set_value('fEventId', event_id)
            output_tree.set_value('fNClusters', len(cluster_list))
            output_tree.set_value('fTrigWindow', event.num_pixels())
            output_tree.set_value('fTimeTick', -1)
            output_tree.set_value('fTimeStamp', -1)
            output_tree.set_value('fBufferId', event.buffer_id)
            output_tree.set_value('fCluSize', cluster.num_pixels())
            output_tree.set_value('fPHeight', cluster.pulse_height)
            output_tree.set_value('fStoN', -1)
            output_tree.set_value('fTotNoise', -1)
            output_tree.set_value('fBaricenterX', cluster.baricenter.x())
            output_tree.set_value('fBaricenterY', cluster.baricenter.y())
            output_tree.set_value('fTheta0', recon.phi0)
            output_tree.set_value('fTheta1', recon.phi1)
            output_tree.set_value('fMomX', recon.ma0.mom2_long)
            output_tree.set_value('fMomY', recon.ma0.mom2_trans)
            output_tree.set_value('fMomThirdX', recon.mom3_long)
            output_tree.set_value('fImpactX', recon.conversion_baricenter.x())
            output_tree.set_value('fImpactY', recon.conversion_baricenter.y())
            output_tree.Fill()
        event_id += 1
        if event_id == num_events:
            break
    num_proc_events = output_tree.GetEntries()
    output_tree.Write()
    output_file.Close()
    logger.info('Done, %d event(s) written to the output file.' %\
                num_proc_events)
    return output_path
示例#4
0
def test(filePath, num_events, zero_suppression=9, coordinate_system='xpedaq'):
    """
    """
    from pyxpe.recon.binio import xpeBinaryFileWindowed
    input_file = xpeBinaryFileWindowed(filePath)
    for i in xrange(num_events):
        event = input_file.next()
        print event
        cluster = hierarchical_clustering(event, zero_suppression,
                                          coordinate_system)[0]
        print cluster
        cluster.draw(coordinate_system)
示例#5
0
def run_xpe_recon(file_path, num_events=1000000000, zero_suppression=9,
                  coordinate_system='pixy', output_path=None,
                  min_cluster_size=6, max_cluster_size=400,
                  small_radius=1.5, large_radius=3.5, weight_scale=0.05):
    """Run the event reconstruction on a binary file.
    """
    assert(file_path.endswith('.mdat'))
    if output_path is None:
        output_path = file_path.replace('.mdat', '_xpe.root')
    logger.info('Opening output file %s...' % output_path)
    output_file = ROOT.TFile(output_path, 'RECREATE')
    output_tree = xpeReconTree()
    event_id = 0
    for event in xpeBinaryFileWindowed(file_path):
        cluster_list = hierarchical_clustering(event, zero_suppression,
                                               coordinate_system)
        cluster = cluster_list[0]
        recon = xpePixyRecon(cluster)
        if not recon.error_summary:
            output_tree.set_value('fRunId', -1)
            output_tree.set_value('fEventId', event_id)
            output_tree.set_value('fNClusters', len(cluster_list))
            output_tree.set_value('fTrigWindow', event.num_pixels())
            output_tree.set_value('fTimeTick', -1)
            output_tree.set_value('fTimeStamp', -1)
            output_tree.set_value('fBufferId', event.buffer_id)
            output_tree.set_value('fCluSize', cluster.num_pixels())
            output_tree.set_value('fPHeight', cluster.pulse_height)
            output_tree.set_value('fStoN', -1)
            output_tree.set_value('fTotNoise', -1)
            output_tree.set_value('fBaricenterX', cluster.baricenter.x())
            output_tree.set_value('fBaricenterY', cluster.baricenter.y())
            output_tree.set_value('fTheta0', recon.phi0)
            output_tree.set_value('fTheta1', recon.phi1)
            output_tree.set_value('fMomX', recon.ma0.mom2_long)
            output_tree.set_value('fMomY', recon.ma0.mom2_trans)
            output_tree.set_value('fMomThirdX', recon.mom3_long)
            output_tree.set_value('fImpactX', recon.conversion_baricenter.x())
            output_tree.set_value('fImpactY', recon.conversion_baricenter.y())
            output_tree.Fill()
        event_id += 1
        if event_id == num_events:
            break
    num_proc_events = output_tree.GetEntries()
    output_tree.Write()
    output_file.Close()
    logger.info('Done, %d event(s) written to the output file.' %\
                num_proc_events)
    return output_path
示例#6
0
def get_ci_run_noise_stat(run_id, verbose=False):
    """ Noisy channel analysis of charge injection runs
    """
    print("Processing run Id %d" % run_id)
    run_info = load_run_info('/data1/xpe/xpedata/033_%07d' % run_id)
    thr_dac = run_info['config']['threshold_dacs'][0]
    if verbose:
        print(run_info)
    nMinWin = 0
    nLargerWin = 0
    RoiDict = {}
    PixelDict = {}
    file_path = run_info['data_file_path']
    if not os.path.isfile(file_path):
        # No event found, upper limits not implemented yet
        return (thr_dac, 0, 0, -1, {}, [])
    for event in xpeBinaryFileWindowed(file_path):
        # check event has minimum window of 396
        if event.num_pixels() in SINGLE_PXL_WINDOW:
            nMinWin += 1
            # Eval ROI and check if already found, if not add it
            roi = event.roi()
            if RoiDict.has_key(roi):
                RoiDict[roi] += 1
            else:
                RoiDict[roi] = 1
        else:
            nLargerWin += 1
            if verbose:
                print("******************* ROI different than Minimum:")
                print(event.roi(), event.num_pixels())
        # get the highest pixel per event:
        # I'm sure there is a better way to do it...
        (highest_x, highest_y) = event.highest_pixel()
        h_pxl = (highest_x + event.xmin, highest_y + event.ymin)
        if PixelDict.has_key(h_pxl):
            PixelDict[h_pxl] += 1
        else:
            PixelDict[h_pxl] = 1

    PixelList = []
    for ((x, y), v) in PixelDict.items():
        PixelList.append((v, x, y))
    PixelList.sort()
    PixelList.reverse()
    timeLastEvt = event.microseconds
    # dac, nGoodRoi, nBadRoi, timeLastEvt, RoiDict
    return (thr_dac, nMinWin, nLargerWin, timeLastEvt, RoiDict, PixelList)
示例#7
0
def plot_pixel_distribution(file_path, x, y):
    assert (file_path.endswith('.mdat'))
    heightList = []
    for event in xpeBinaryFileWindowed(file_path):
        try:
            heightList.append(event.adc_value(x - event.xmin, y - event.ymin))
        except IndexError:
            pass
    #print heightList
    heights = numpy.array(heightList)
    print len(heights), numpy.mean(heights), numpy.std(heights)
    xmin = -1
    xmax = numpy.amax(heights)
    nbins = xmax - xmin
    histRange = (xmin, xmax)
    plt.hist(heights, nbins, histRange, facecolor='g')
    plt.show()
示例#8
0
def plot_pixel_distribution(file_path, x, y):
    assert(file_path.endswith('.mdat'))
    heightList = []
    for event in xpeBinaryFileWindowed(file_path):
        try:
            heightList.append(event.adc_value(x - event.xmin, y- event.ymin))
        except IndexError:
            pass
    #print heightList
    heights = numpy.array(heightList)
    print len(heights), numpy.mean(heights), numpy.std(heights)
    xmin = -1
    xmax = numpy.amax(heights)
    nbins = xmax - xmin
    histRange = (xmin, xmax)
    plt.hist(heights, nbins, histRange, facecolor='g')
    plt.show()
示例#9
0
file_path = '/data/xpedata/001/001_0000669/001_0000669_data.mdat'

#runId = 670 # ci(100,100)
#runId = 679 # ci(9,101)
#runId = 680 # ci(8,101)
#runId = 681 # ci(7,101)
#runId = 682 # ci(2,101)
#runId = 683 # ci(3,101)
#runId = 684 # ci(0,101)
#runId = 685 # ci(298,10)
#runId = 686 # ci(298,3)
#runId = 687 # ci(298,350)
#runId = 688 # ci(0,0)
#file_path = '/data/xpedata/001/001_%07d/001_%07d_data.mdat'% (runId,runId)
input_file = xpeBinaryFileWindowed(file_path)

# matrix for pixel occupancy
# DON"T FORGET: np.zeros([row,col])
pxl_occupancy = np.zeros([XPOL_NUM_ROWS, XPOL_NUM_COLUMNS])

for i in range(N_MAX):
    event = input_file.next()
    #event.draw_ascii(ZERO_SUPP)
    #print (event)
    #print  guess_trg_pxl(event)
    #col, row = pixels_overthr(event, ZERO_SUPP)
    #for (c,r) in zip(col, row):
    #    pxl_occupancy[r][c] +=event.adc_value(c-event.xmin, r-event.ymin)
    #    pxl_occupancy[r][c] += 1
    col, row = guess_trg_pxl(event)
示例#10
0
                        '--full-frame',
                        action='store_true',
                        dest='full_frame',
                        default=False,
                        help='read events in full frame mode')
    parser.add_argument('-o',
                        '--offset',
                        type=int,
                        default=0,
                        help='offset (in bytes) of the desired event header')
    parser.add_argument('-n',
                        '--num-events',
                        type=int,
                        default=10,
                        help='number of events to be read')
    parser.add_argument('-z',
                        '--zero-suppression',
                        type=int,
                        default=10,
                        help='zero-suppression threshold')
    args = parser.parse_args()

    if args.full_frame:
        input_file = xpeBinaryFileFullFrame(args.binfile)
    else:
        input_file = xpeBinaryFileWindowed(args.binfile)
    input_file.seek(args.offset)
    for i in xrange(args.num_events):
        event = input_file.next()
        print('%d - %s' % (i, event))
示例#11
0
if __name__ == '__main__':
    import argparse
    formatter = argparse.ArgumentDefaultsHelpFormatter
    parser = argparse.ArgumentParser(formatter_class=formatter)
    parser.add_argument('binfile', type=str,
                        help='the input binary file')
    parser.add_argument('-f', '--full-frame', action='store_true',
                        dest = 'full_frame', default=False,
                        help='read events in full frame mode')
    parser.add_argument('-o', '--offset', type=int, default=0,
                        help='offset (in bytes) of the desired event header')
    parser.add_argument('-n', '--num-events', type=int, default=10,
                        help='number of events to be read')
    parser.add_argument('-z', '--zero-suppression', type=int, default=10,
                        help='zero-suppression threshold')
    args = parser.parse_args()

    if args.full_frame:
      input_file = xpeBinaryFileFullFrame(args.binfile)
    else:
      input_file = xpeBinaryFileWindowed(args.binfile)
    input_file.seek(args.offset)
    for i in xrange(args.num_events):
        event = input_file.next()
        if args.full_frame:
            event.draw()
        else:
            event.draw_ascii(args.zero_suppression)
            event.draw(args.zero_suppression)
示例#12
0
            output_tree.set_value('fBaricenterY', cluster.baricenter.y())
            output_tree.set_value('fTheta0', recon.phi0)
            output_tree.set_value('fTheta1', recon.phi1)
            output_tree.set_value('fMomX', recon.ma0.mom2_long)
            output_tree.set_value('fMomY', recon.ma0.mom2_trans)
            output_tree.set_value('fMomThirdX', recon.mom3_long)
            output_tree.set_value('fImpactX', recon.conversion_baricenter.x())
            output_tree.set_value('fImpactY', recon.conversion_baricenter.y())
            output_tree.Fill()
        event_id += 1
        if event_id == num_events:
            break
    num_proc_events = output_tree.GetEntries()
    output_tree.Write()
    output_file.Close()
    logger.info('Done, %d event(s) written to the output file.' %\
                num_proc_events)
    return output_path


if __name__ == '__main__':
    file_path = '/data/work/xpe/xpedaq/data/test_fe_500evts.mdat'
    for event in xpeBinaryFileWindowed(file_path):
        cluster_list = hierarchical_clustering(event, 9, 'xpe')
        cluster = cluster_list[0]
        recon = xpePixyRecon(cluster)
        print(cluster.pulse_height, cluster.num_pixels(),\
            cluster.baricenter.x(), cluster.baricenter.y(), recon.phi0,\
            recon.ma0.mom2_long, recon.ma0.mom2_trans)
        raw_input()
示例#13
0
            output_tree.set_value('fBaricenterY', cluster.baricenter.y())
            output_tree.set_value('fTheta0', recon.phi0)
            output_tree.set_value('fTheta1', recon.phi1)
            output_tree.set_value('fMomX', recon.ma0.mom2_long)
            output_tree.set_value('fMomY', recon.ma0.mom2_trans)
            output_tree.set_value('fMomThirdX', recon.mom3_long)
            output_tree.set_value('fImpactX', recon.conversion_baricenter.x())
            output_tree.set_value('fImpactY', recon.conversion_baricenter.y())
            output_tree.Fill()
        event_id += 1
        if event_id == num_events:
            break
    num_proc_events = output_tree.GetEntries()
    output_tree.Write()
    output_file.Close()
    logger.info('Done, %d event(s) written to the output file.' %\
                num_proc_events)
    return output_path


if __name__ == '__main__':
    file_path = '/data/work/xpe/xpedaq/data/test_fe_500evts.mdat'
    for event in xpeBinaryFileWindowed(file_path):
        cluster_list = hierarchical_clustering(event, 9, 'xpe')
        cluster = cluster_list[0]
        recon = xpePixyRecon(cluster)
        print(cluster.pulse_height, cluster.num_pixels(),\
            cluster.baricenter.x(), cluster.baricenter.y(), recon.phi0,\
            recon.ma0.mom2_long, recon.ma0.mom2_trans)
        raw_input()
示例#14
0
    _phi1 = numpy.degrees(ma0.phi) + 90
    _phi2 = _phi1 - 180.
    w2 = Wedge(cluster.baricenter, r2, _phi1, _phi2, facecolor='none',
               edgecolor=_color, lw=_lw, hatch='///')
    plt.gca().add_artist(w2)
    recon.conversion_point.draw(color='green')
    annotate('Absorption point', recon.conversion_point, (0.16, 0.9))
    plt.savefig('sample_evt_conv_point.pdf')


    dir2_fig = cluster.draw(coordinate_system, hexcol_padding=0.1, show=False)
    cluster.baricenter.draw(color='blue')
    annotate('Baricenter', cluster.baricenter, (0.13, 0.65))
    ma0.axis.draw(color='blue', lw=_lw, ls='dashed')
    p = ma0.axis.at(0.4)
    annotate('Principal axis', p, (0.45, 0.95))    
    ma1.draw(color='green', ellipse=False, semiaxes=False)
    annotate('Absorption point', ma1.pivot, (0.16, 0.9))
    p = ma1.axis.at(0.5)
    annotate('Final direction', p, (0.86, 0.35))
    plt.savefig('sample_evt_phi2.pdf')
    plt.show()
    

if __name__ == '__main__':
    file_path = '/data/work/xpe/xpedaq/data/test_fe_500evts.mdat'
    input_file = xpeBinaryFileWindowed(file_path) 
    for i in range(108):
        event = input_file.next()
    display_recon(event)