Ejemplo n.º 1
0
def lst_calibration(event, telescope_id):
    """
    Custom lst calibration.
    Update event.dl1.tel[telescope_id] with calibrated image and peakpos

    Parameters
    ----------
    event: ctapipe event container
    telescope_id: int
    """

    data = event.r0.tel[telescope_id].waveform

    ped = event.mc.tel[telescope_id].pedestal  # the pedestal is the
    # average (for pedestal events) of the *sum* of all samples,
    # from sim_telarray

    nsamples = data.shape[2]  # total number of samples

    # Subtract pedestal baseline. atleast_3d converts 2D to 3D matrix

    pedcorrectedsamples = data - np.atleast_3d(ped) / nsamples

    integrator = LocalPeakIntegrator(None, None)
    integration, peakpos, window = integrator.extract_charge(
        pedcorrectedsamples)  # these are 2D matrices num_gains * num_pixels

    signals = integration.astype(float)

    dc2pe = event.mc.tel[telescope_id].dc_to_pe  # numgains * numpixels
    signals *= dc2pe

    event.dl1.tel[telescope_id].image = signals
    event.dl1.tel[telescope_id].peakpos = peakpos
Ejemplo n.º 2
0
def test_local_peak_integration(camera_waveforms):
    waveforms, _ = camera_waveforms
    integrator = LocalPeakIntegrator()
    charge, _, _ = integrator.extract_charge(waveforms)

    assert_allclose(charge[0][0], 240.3, rtol=1e-3)
    assert_allclose(charge[1][0], 427.158, rtol=1e-3)
Ejemplo n.º 3
0
def test_local_peak_integration(example_event):
    telid = list(example_event.r0.tel)[0]
    data = example_event.r0.tel[telid].waveform
    nsamples = data.shape[2]
    ped = example_event.mc.tel[telid].pedestal
    data_ped = data - np.atleast_3d(ped / nsamples)
    data_ped = np.array([data_ped[0], data_ped[0]])  # Test LG functionality

    integrator = LocalPeakIntegrator()
    integration, peakpos, window = integrator.extract_charge(data_ped)
Ejemplo n.º 4
0
 def setup(self):
     kwargs = dict(config=self.config, tool=self)
     self.dl0 = CameraDL0Reducer(**kwargs)
     self.dl1 = CameraDL1Calibrator(**kwargs)
     self.cal = CameraCalibrator(r1_product=self.calibrator)
     self.cross = CrossCorrelation()
     self.glob_peak = GlobalPeakIntegrator()
     self.local_peak = LocalPeakIntegrator()
     self.neighbour = NeighbourPeakIntegrator()
     self.aver = AverageWfPeakIntegrator()
Ejemplo n.º 5
0
def test_local_peak_integration(example_event):
    telid = 11
    data = example_event.r0.tel[telid].waveform
    nsamples = data.shape[2]
    ped = example_event.mc.tel[telid].pedestal
    data_ped = data - np.atleast_3d(ped / nsamples)
    data_ped = np.array([data_ped[0], data_ped[0]])  # Test LG functionality

    integrator = LocalPeakIntegrator()
    integration, peakpos, window = integrator.extract_charge(data_ped)

    assert_almost_equal(integration[0][0], 76, 0)
    assert_almost_equal(integration[1][0], 76, 0)
    assert peakpos[0][0] == 13
    assert peakpos[1][0] == 13
Ejemplo n.º 6
0
def test_local_peak_integration():
    telid = 11
    event = get_test_event()
    data = event.r0.tel[telid].adc_samples
    nsamples = data.shape[2]
    ped = event.mc.tel[telid].pedestal
    data_ped = data - np.atleast_3d(ped/nsamples)
    data_ped = np.array([data_ped[0], data_ped[0]])  # Test LG functionality

    integrator = LocalPeakIntegrator(None, None)
    integration, peakpos, window = integrator.extract_charge(data_ped)

    assert_almost_equal(integration[0][0], 76, 0)
    assert_almost_equal(integration[1][0], 76, 0)
    assert peakpos[0][0] == 13
    assert peakpos[1][0] == 13
Ejemplo n.º 7
0
 def get_extractor(self):
     return LocalPeakIntegrator(None, self.parent,
                                window_width=self.window_width,
                                window_shift=self.window_shift)
from argparse import ArgumentParser
from ctapipe_fact import fact_event_generator

from ctapipe.visualization import CameraDisplay
from ctapipe.instrument import CameraGeometry
from ctapipe.calib.camera import CameraDL1Calibrator
from ctapipe.image.charge_extractors import LocalPeakIntegrator
from ctapipe.image.hillas import hillas_parameters_5 as hillas_parameters
from ctapipe.image.cleaning import tailcuts_clean

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

integrator = LocalPeakIntegrator(config=None, tool=None)
integrator.window_shift = 5
integrator.window_width = 30

dl1_calibrator = CameraDL1Calibrator(
    config=None,
    tool=None,
    extractor=integrator,
)

parser = ArgumentParser()
parser.add_argument('inputfile')
parser.add_argument('drsfile')


def main():
    args = parser.parse_args()
    event_generator = fact_event_generator(
Ejemplo n.º 9
0
def get_events(filename,
               storedata=False,
               concatenate=False,
               storeimg=False,
               outdir='./results/'):
    """
    Read a Simtelarray file, extract pixels charge, calculate image parameters and timing
    parameters and store the result in an hdf5 file. 
    
    Parameters:
    filename: str
    Name of the simtelarray file.

    storedata: boolean
    True: store extracted data in a hdf5 file

    concatenate: boolean
    True: store the extracted data at the end of an existing file

    storeimg: boolean
    True: store also pixel data
    
    outdir: srt
    Output directory
    
    
    
    """
    #Particle type:

    particle_type = guess_type(filename)

    #Create data frame where DL2 data will be stored:

    features = [
        'ObsID', 'EvID', 'mcEnergy', 'mcAlt', 'mcAz', 'mcCore_x', 'mcCore_y',
        'mcHfirst', 'mcType', 'GPStime', 'width', 'length', 'w/l', 'phi',
        'psi', 'r', 'x', 'y', 'intensity', 'skewness', 'kurtosis', 'mcAlttel',
        'mcAztel', 'impact', 'mcXmax', 'time_gradient', 'intercept', 'SrcX',
        'SrcY', 'disp', 'hadroness'
    ]
    output = pd.DataFrame(columns=features)

    #Read LST1 events:
    source = EventSourceFactory.produce(
        input_url=filename, allowed_tels={1})  #Open Simtelarray file

    #Cleaning levels:

    level1 = {'LSTCam': 6.}
    level2 = level1.copy()
    # We use as second cleaning level just half of the first cleaning level
    for key in level2:
        level2[key] *= 0.5

    log10pixelHGsignal = {}
    survived = {}

    imagedata = np.array([])

    for key in level1:

        log10pixelHGsignal[key] = []
        survived[key] = []
    i = 0
    for event in source:
        if i % 100 == 0:
            print("EVENT_ID: ", event.r0.event_id, "TELS: ",
                  event.r0.tels_with_data, "MC Energy:", event.mc.energy)
        i = i + 1
        ntels = len(event.r0.tels_with_data)
        '''
        if i > 100:   # for quick tests
            break
        '''
        for ii, tel_id in enumerate(event.r0.tels_with_data):

            geom = event.inst.subarray.tel[tel_id].camera  #Camera geometry
            tel_coords = event.inst.subarray.tel_coords[
                event.inst.subarray.tel_indices[tel_id]]
            data = event.r0.tel[tel_id].waveform
            ped = event.mc.tel[tel_id].pedestal
            # the pedestal is the average (for pedestal events) of the *sum* of all samples, from sim_telarray

            nsamples = data.shape[2]  # total number of samples
            pedcorrectedsamples = data - np.atleast_3d(
                ped
            ) / nsamples  # Subtract pedestal baseline. atleast_3d converts 2D to 3D matrix

            integrator = LocalPeakIntegrator(None, None)
            integration, peakpos, window = integrator.extract_charge(
                pedcorrectedsamples
            )  # these are 2D matrices num_gains * num_pixels

            chan = 0  # high gain used for now...
            signals = integration[chan].astype(float)

            dc2pe = event.mc.tel[tel_id].dc_to_pe  # numgains * numpixels
            signals *= dc2pe[chan]

            # Add all individual pixel signals to the numpy array of the corresponding camera inside the log10pixelsignal dictionary
            log10pixelHGsignal[str(geom)].extend(
                np.log10(signals)
            )  # This seems to be faster like this, with normal python lists

            # Apply image cleaning
            cleanmask = tailcuts_clean(geom,
                                       signals,
                                       picture_thresh=level1[str(geom)],
                                       boundary_thresh=level2[str(geom)],
                                       keep_isolated_pixels=False,
                                       min_number_picture_neighbors=1)
            survived[str(geom)].extend(
                cleanmask
            )  # This seems to be faster like this, with normal python lists

            clean = signals.copy()
            clean[
                ~cleanmask] = 0.0  # set to 0 pixels which did not survive cleaning
            if np.max(clean) < 1.e-6:  # skip images with no pixels
                continue

            # Calculate image parameters
            hillas = hillas_parameters(
                geom,
                clean)  # this one gives some warnings invalid value in sqrt
            foclen = event.inst.subarray.tel[
                tel_id].optics.equivalent_focal_length

            w = np.rad2deg(np.arctan2(hillas.width, foclen))
            l = np.rad2deg(np.arctan2(hillas.length, foclen))

            #Calculate Timing parameters

            peak_time = units.Quantity(peakpos[chan]) * units.Unit("ns")
            timepars = time.timing_parameters(geom.pix_x, geom.pix_y, clean,
                                              peak_time, hillas.psi)

            if w >= 0:

                if storeimg == True:
                    if imagedata.size == 0:
                        imagedata = clean
                    else:
                        imagedata = np.vstack([imagedata,
                                               clean])  #Pixel content

                width = w.value
                length = l.value
                phi = hillas.phi.value
                psi = hillas.psi.value
                r = hillas.r.value
                x = hillas.x.value
                y = hillas.y.value
                intensity = np.log10(hillas.intensity)
                skewness = hillas.skewness
                kurtosis = hillas.kurtosis

                #Store parameters from event and MC:
                ObsID = event.r0.obs_id
                EvID = event.r0.event_id

                mcEnergy = np.log10(event.mc.energy.value *
                                    1e3)  #Log10(Energy) in GeV
                mcAlt = event.mc.alt.value
                mcAz = event.mc.az.value
                mcCore_x = event.mc.core_x.value
                mcCore_y = event.mc.core_y.value
                mcHfirst = event.mc.h_first_int.value
                mcType = event.mc.shower_primary_id
                mcAztel = event.mcheader.run_array_direction[0].value
                mcAlttel = event.mcheader.run_array_direction[1].value
                mcXmax = event.mc.x_max.value
                GPStime = event.trig.gps_time.value

                impact = np.sqrt(
                    (tel_coords.x.value - event.mc.core_x.value)**2 +
                    (tel_coords.y.value - event.mc.core_y.value)**2)

                time_gradient = timepars[0].value
                intercept = timepars[1].value

                #Calculate Disp and Source position in camera coordinates
                tel = OpticsDescription.from_name(
                    'LST')  #Telescope description
                focal_length = tel.equivalent_focal_length.value
                sourcepos = transformations.calc_CamSourcePos(
                    mcAlt, mcAz, mcAlttel, mcAztel, focal_length)
                SrcX = sourcepos[0]
                SrcY = sourcepos[1]
                disp = transformations.calc_DISP(sourcepos[0], sourcepos[1], x,
                                                 y)

                hadroness = 0
                if particle_type == 'proton':
                    hadroness = 1

                eventdf = pd.DataFrame([[
                    ObsID, EvID, mcEnergy, mcAlt, mcAz, mcCore_x, mcCore_y,
                    mcHfirst, mcType, GPStime, width, length, width / length,
                    phi, psi, r, x, y, intensity, skewness, kurtosis, mcAlttel,
                    mcAztel, impact, mcXmax, time_gradient, intercept, SrcX,
                    SrcY, disp, hadroness
                ]],
                                       columns=features)

                output = output.append(eventdf, ignore_index=True)

    outfile = outdir + particle_type + '_events.hdf5'

    if storedata == True:

        if concatenate == False or (concatenate == True and
                                    np.DataSource().exists(outfile) == False):
            output.to_hdf(outfile, key=particle_type + "_events", mode="w")
            if storeimg == True:
                f = h5py.File(outfile, 'r+')
                f.create_dataset('images', data=imagedata)
                f.close()
        else:
            if storeimg == True:
                f = h5py.File(outfile, 'r')
                images = f['images']
                del f['images']
                images = np.vstack([images, imagedata])
                f.close()
                saved = pd.read_hdf(outfile, key=particle_type + '_events')
                output = saved.append(output, ignore_index=True)
                output.to_hdf(outfile, key=particle_type + "_events", mode="w")
                f = h5py.File(outfile, 'r+')
                f.create_dataset('images', data=images)
                f.close()
            else:
                saved = pd.read_hdf(outfile, key=particle_type + '_events')
                output = saved.append(output, ignore_index=True)
                output.to_hdf(outfile, key=particle_type + "_events", mode="w")
    del source
    return output
Ejemplo n.º 10
0
class SPEGenerator(Tool):
    name = "SPEGenerator"
    description = "Generate the a pickle file of single photo electron spectrum for " \
                  "either MC or data files."

    telescopes = Int(1,help='Telescopes to include from the event file. '
                           'Default = 1').tag(config=True)
    output_name = Unicode('single_photoelectron_spectum',
                          help='Name of the output SPE hdf5 file '
                               'file').tag(config=True)
    input_path = Unicode(help='Path to input file containing data').tag(config=True)

    max_events = Int(1000, help='Maximum number of events to use').tag(config=True)

    pixel = Int(1000, allow_none=True, help='Pixel to generate SPE data for').tag(config=True)

    aliases = Dict(dict(input_path='SPEGenerator.input_path',
                        output_name='SPEGenerator.output_name',
                        max_events='SPEGenerator.max_events',
                        clip_amplitude='CameraDL1Calibrator.clip_amplitude',
                        radius='CameraDL1Calibrator.radius',
                        T='SPEGenerator.telescopes',
                        p='SPEGenerator.pixel'
                        ))
    classes = List([EventSourceFactory,
                    CameraDL1Calibrator,
                    CameraCalibrator
                    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.eventsource = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.cal = None
        self.recope12 = np.array([])
        self.recope22 = np.array([])
        self.recope32 = np.array([])
        self.recope42 = np.array([])
        self.recope52 = np.array([])

    def setup(self):
        kwargs = dict(config=self.config, tool=self)
        self.r1 = HESSIOR1Calibrator(**kwargs)

        self.dl0 = CameraDL0Reducer(**kwargs)

        self.dl1 = CameraDL1Calibrator(**kwargs)

        self.cal = CameraCalibrator()

        self.cross = CrossCorrelation()

        self.glob_peak = GlobalPeakIntegrator()

        self.local_peak = LocalPeakIntegrator()

        self.neighbour = NeighbourPeakIntegrator()

        self.aver = AverageWfPeakIntegrator()

    def start(self):

        file_name = "%s" % (self.input_path)
        print('opening ', file_name)

        try:
            print('trying to open file')
            source = EventSourceFactory.produce(input_url=file_name, max_events=self.max_events)
            for event in tqdm(source):
                self.r1.calibrate(event)
                self.dl0.reduce(event)
                self.dl1.calibrate(event)

                # measured_charge = event.dl1.tel[self.telescopes].image[0][self.pixel]
                # print(self.glob_peak.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0])
                # print(self.glob_peak.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0].shape)
                # exit()
                # self.recope12 = np.append(self.recope12, max(self.cross._apply_cc(event.r1.tel[self.telescopes].waveform[0][self.pixel])))
                # self.recope22 = np.append(self.recope22, self.glob_peak.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0][self.pixel])
                self.recope32 = np.append(self.recope32, self.local_peak.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0][self.pixel])
                # self.recope42 = np.append(self.recope32, self.aver.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0][self.pixel])
                # self.recope52 = np.append(self.recope52, self.neighbour.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0][self.pixel])


        except FileNotFoundError:
            print('file_not_found')
            exit()


    def finish(self):
        bins2 = np.arange(-0.5, 4, 0.055)
        bins = np.arange(-0.5, 4, 0.055)
        center = (bins[:-1] + bins[1:]) / 2
        fig0 = plt.figure(0)
        # ax1 = fig0.add_subplot(141)
        # ax2 = fig0.add_subplot(142)
        ax3 = fig0.add_subplot(111)
        # ax4 = fig0.add_subplot(144)
        # ax1.hist(self.recope12, bins=bins2, color='C0', alpha =0.6, label='cross correlation')
        # ax1.set_title('cross correlation')
        # ax2.hist(self.recope22, bins=bins, color='C1', alpha =0.6, label='global peak')
        # ax2.set_title('global peak')
        histout = ax3.hist(self.recope32, bins=bins, color='C2', alpha =0.6, label='local peak')
        ax3.set_title('local peak')
        out_file = open(self.output_name, 'w')
        for i in range(len(center)):
            out_file.write('%s\t%s\n' % (center[i], histout[0][i]))
        out_file.close()
        # ax4.hist(self.recope42, bins=bins, color='C3', alpha =0.6, label='average waveform')
        # ax4.set_title('average waveform')
        plt.show()
        # out_file = open(self.output_name, 'w')
        # for n,i in enumerate(self.trig_eff_array):
        #     out_file.write('%s\t%s\n' % (self.disc_array[n], i))
        # out_file.close()
        print('done')
Ejemplo n.º 11
0
    def setup(self):
        self.log_format = "%(levelname)s: %(message)s [%(name)s.%(funcName)s]"
        kwargs = dict(config=self.config, tool=self)

        filepath = '/Volumes/gct-jason/data/170322/led/Run04345_r0.tio'
        self.reader_led = TargetioFileReader(input_path=filepath, **kwargs)
        filepath = '/Volumes/gct-jason/data/170320/linearity/Run04167_r0.tio'
        self.reader_laser = TargetioFileReader(input_path=filepath, **kwargs)

        extractor = LocalPeakIntegrator(**kwargs)

        self.r1_led = TargetioR1Calibrator(
            pedestal_path=
            '/Volumes/gct-jason/data/170322/pedestal/Run04240_ped.tcal',
            tf_path='/Volumes/gct-jason/data/170322/tf/Run04277-04327_tf.tcal',
            pe_path=
            '/Users/Jason/Software/CHECAnalysis/targetpipe/adc2pe/adc2pe_800gm_c1.tcal',
            **kwargs,
        )
        self.r1_laser = TargetioR1Calibrator(
            pedestal_path=
            '/Volumes/gct-jason/data/170320/pedestal/Run04109_ped.tcal',
            tf_path='/Volumes/gct-jason/data/170320/tf/Run04110-04159_tf.tcal',
            pe_path=
            '/Users/Jason/Software/CHECAnalysis/targetpipe/adc2pe/adc2pe_1100.tcal',
            **kwargs,
        )
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(extractor=extractor, **kwargs)

        self.dead = Dead()

        self.n_events_led = self.reader_led.num_events
        first_event = self.reader_led.get_event(0)
        telid = list(first_event.r0.tels_with_data)[0]
        r1 = first_event.r1.tel[telid].pe_samples[0]
        self.n_pixels, self.n_samples = r1.shape

        p_kwargs = kwargs
        p_kwargs['script'] = "checm_paper_timing"
        p_kwargs['figure_name'] = "led_eid_vs_fci"
        self.p_led_eidvsfci = Scatter(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_time_vs_tack"
        self.p_led_timevstack = Scatter(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_bp_vs_tack"
        self.p_led_bpvstack = Hist2D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_eid_vs_t"
        self.p_led_eidvst = Hist2D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_eid_vs_tgrad"
        self.p_led_eidvstgrad = Hist2D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_t_vs_tgrad"
        self.p_led_tvstgrad = Hist2D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_t_vs_charge"
        self.p_led_tvscharge = Hist2D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_tgrad_vs_charge"
        self.p_led_tgradvscharge = Hist2D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_1D_comparison_eid{}_wavg".format(
            self.eoi)
        self.p_led_1deoicomp_wavg = WaveformHist1D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_1D_comparison_allevents_wavg".format(
            self.eoi)
        self.p_led_1dcomp_wavg = WaveformHist1D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_1D_comparison_eid{}".format(self.eoi)
        self.p_led_1deoicomp = WaveformHist1DInt(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_1D_comparison_allevents".format(
            self.eoi)
        self.p_led_1dcomp = WaveformHist1D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "led_image_tgrad_eid{}".format(self.eoi)
        self.p_led_imageeoitgrad = ImagePlotter(**p_kwargs)

        p_kwargs['figure_name'] = "laser_1D_comparison_eid{}_wavg".format(
            self.eoi)
        self.p_laser_1deoicomp_wavg = WaveformHist1D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "laser_1D_comparison_allevents_wavg".format(
            self.eoi)
        self.p_laser_1dcomp_wavg = WaveformHist1D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "laser_1D_comparison_eid{}".format(self.eoi)
        self.p_laser_1deoicomp = WaveformHist1DInt(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "laser_1D_comparison_allevents".format(
            self.eoi)
        self.p_laser_1dcomp = WaveformHist1D(**p_kwargs, shape='wide')
        p_kwargs['figure_name'] = "laser_1D_finalmethod"
        self.p_laser_1d_final = WaveformHist1D(**p_kwargs, shape='square')
        p_kwargs['figure_name'] = "laser_1D_finalmethod_pix"
        self.p_laser_1d_final_pix = WaveformHist1D(**p_kwargs, shape='square')
        p_kwargs['figure_name'] = "laser_image_tgrad_eid{}".format(self.eoi)
        self.p_laser_imageeoitgrad = ImagePlotter(**p_kwargs)

        p_kwargs['figure_name'] = "laser_fwhm_allevents"
        self.p_laser_fwhm = WaveformHist1DInt(**p_kwargs, shape='wide')
def corr_time(input_file_1, input_file_2, gain, nr, pix):
    n_combine = 8
    n_harm = 16
    n_cap = 1024
    timeCorr = TimeCalCorr(n_combine, n_harm, n_cap)

    # compute curve to calib time
    reader = event_source(input_url=input_file_1, max_events=4000)
    for ev in reader:
        expected_pixel_id = ev.lst.tel[0].svc.pixel_ids
        fc = get_first_capacitor(ev, nr)
        pixel = expected_pixel_id[nr * 7 + pix]
        integrator = LocalPeakIntegrator(None, None)
        integration, peakpos, window = integrator.extract_charge(
            ev.r0.tel[0].waveform[:, :, :])
        print("pekpos = {}, charge = {}".format(peakpos[gain, pixel],
                                                integration[gain, pixel]))
        if integration[0, pixel] > 4000:
            timeCorr.fill(fc[gain, pix] % 1024, peakpos[gain, pixel])

    timeCorr.finalize()

    fc = np.arange(0, 1024, 8)
    an = timeCorr.fan
    bn = timeCorr.fbn
    y = np.zeros(128)

    for i in range(0, len(y)):
        temp_cos = 0
        temp_sin = 0
        for j in range(0, len(an)):
            temp_cos += an[j] * np.cos(2 * j * np.pi * (fc[i] / 1024.))
            temp_sin += bn[j] * np.sin(2 * j * np.pi * (fc[i] / 1024.))
        y[i] = (temp_cos + temp_sin)

    fig, ax = plt.subplots(1, 2, figsize=(16, 9))
    ax[0].plot(np.arange(0, 1024, 8), timeCorr.fMeanVal, 'bo', markersize=4)
    offset = y[0] - timeCorr.fMeanVal[0]
    ax[0].plot(fc, y - offset, 'r--')
    ax[0].set_ylabel("Mean arrival time")
    ax[0].set_xlabel("Position in the DRS ring")
    ax[0].set_title("n_harm = {}, pixel = {}".format(n_harm, pixel))

    # Apply time corr on second file
    reader = event_source(input_url=input_file_2, max_events=4000)
    arrival_time_list = []
    arrival_time_corr_list = []

    for ev in reader:
        expected_pixel_id = ev.lst.tel[0].svc.pixel_ids
        pixel = expected_pixel_id[nr * 7 + pix]
        fc = get_first_capacitor(ev, nr)
        integrator = LocalPeakIntegrator(None, None)
        integration, peakpos, window = integrator.extract_charge(
            ev.r0.tel[0].waveform[:, :, :])
        print("pekpos = {}, charge = {}".format(peakpos[gain, pixel],
                                                integration[gain, pixel]))
        if integration[0, pixel] > 4000:
            arrival_time_list.append(peakpos[gain, pixel])
            arrival_time_corr_list.append(
                timeCorr.get_corr_time(fc[gain, pix] % 1024))

    binwidth = 1
    ax[1].hist(arrival_time_list,
               histtype='step',
               lw=3,
               linestyle='--',
               range=(20, 30),
               bins=np.arange(min(arrival_time_list),
                              max(arrival_time_list) + binwidth, binwidth),
               label="before time corr")

    ax[1].hist(arrival_time_corr_list,
               histtype='step',
               lw=3,
               linestyle='--',
               color='red',
               range=(20, 30),
               bins=np.arange(min(arrival_time_corr_list),
                              max(arrival_time_corr_list) + binwidth,
                              binwidth),
               label="after time corr")
    ax[1].set_ylabel("Number of events")
    ax[1].set_xlabel("Arrival time [time samples 1 ns]")
    ax[1].legend()
    plt.tight_layout()
    plt.show()


# corr_time("/media/pawel1/ADATA HD330/20190312/LST-1.1.Run00250.0000.fits.fz", "/media/pawel1/ADATA HD330/20190312/LST-1.2.Run00250.0000.fits.fz", 1, 25, 1)
Ejemplo n.º 13
0
            break
        '''
        for ii, tel_id in enumerate(event.r0.tels_with_data):

            geom = event.inst.subarray.tel[tel_id].camera  #Camera geometry

            data = event.r0.tel[tel_id].waveform
            ped = event.mc.tel[tel_id].pedestal
            # the pedestal is the average (for pedestal events) of the *sum* of all samples, from sim_telarray

            nsamples = data.shape[2]  # total number of samples
            pedcorrectedsamples = data - np.atleast_3d(
                ped
            ) / nsamples  # Subtract pedestal baseline. atleast_3d converts 2D to 3D matrix

            integrator = LocalPeakIntegrator(None, None)
            integration, peakpos, window = integrator.extract_charge(
                pedcorrectedsamples
            )  # these are 2D matrices num_gains * num_pixels

            chan = 0  # high gain used for now...
            signals = integration[chan].astype(float)

            dc2pe = event.mc.tel[tel_id].dc_to_pe  # numgains * numpixels
            signals *= dc2pe[chan]

            # Add all individual pixel signals to the numpy array of the corresponding camera inside the log10pixelsignal dictionary
            log10pixelHGsignal[str(geom)].extend(
                np.log10(signals)
            )  # This seems to be faster like this, with normal python lists
Ejemplo n.º 14
0
class FlatFieldGenerator(Tool):
    name = "FlatFieldGenerator"
    description = "Generate the a pickle file of FlatField for " \
                  "either MC or data files."

    telescopes = Int(1,
                     help='Telescopes to include from the event file. '
                     'Default = 1').tag(config=True)
    output_name = Unicode('extracted_flatfield',
                          help='Name of the output extracted flat field hdf5 '
                          'file').tag(config=True)
    infile = Unicode(help='Path to file containing data').tag(config=True)

    max_events = Int(1,
                     help='Maximum number of events to use').tag(config=True)

    plot_cam = Bool(False,
                    "enable plotting of individual camera").tag(config=True)

    use_true_pe = Bool(False, "Use true mc p.e.").tag(config=True)

    calibrator = Unicode(
        'HESSIOR1Calibrator',
        help='which calibrator to use, default = HESSIOR1Calibrator').tag(
            config=True)
    debug = Bool(False, "plot resulting histograms").tag(config=True)

    aliases = Dict(
        dict(infile='FlatFieldGenerator.infile',
             calibrator='FlatFieldGenerator.calibrator',
             max_events='FlatFieldGenerator.max_events',
             extractor='ChargeExtractorFactory.product',
             window_width='ChargeExtractorFactory.window_width',
             t0='ChargeExtractorFactory.t0',
             window_shift='ChargeExtractorFactory.window_shift',
             sig_amp_cut_HG='ChargeExtractorFactory.sig_amp_cut_HG',
             sig_amp_cut_LG='ChargeExtractorFactory.sig_amp_cut_LG',
             lwt='ChargeExtractorFactory.lwt',
             clip_amplitude='CameraDL1Calibrator.clip_amplitude',
             radius='CameraDL1Calibrator.radius',
             T='FlatFieldGenerator.telescopes',
             o='FlatFieldGenerator.output_name',
             plot_cam='FlatFieldGenerator.plot_cam',
             use_true_pe='FlatFieldGenerator.use_true_pe',
             dd='FlatFieldGenerator.debug'))
    classes = List([
        EventSourceFactory, HESSIOEventSource, TargetIOEventSource,
        ChargeExtractorFactory, CameraDL1Calibrator, CameraCalibrator
    ])

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.eventsource = None
        self.r1 = None
        self.dl0 = None
        self.dl1 = None
        self.calculator = None
        self.cal = None
        self.aver = None
        self.cross = None
        self.glob_peak = None
        self.local_peak = None
        self.neighbour = None
        self.reconstructed_image_array = []
        self.mean_reconstructed_image_array = None
        self.mean_true_image_array = None
        self.event_count = 0
        self.geom = None
        self.disp = None

    def setup(self):
        kwargs = dict(config=self.config, tool=self)
        self.dl0 = CameraDL0Reducer(**kwargs)
        self.dl1 = CameraDL1Calibrator(**kwargs)
        self.cal = CameraCalibrator(r1_product=self.calibrator)
        self.cross = CrossCorrelation()
        self.glob_peak = GlobalPeakIntegrator()
        self.local_peak = LocalPeakIntegrator()
        self.neighbour = NeighbourPeakIntegrator()
        self.aver = AverageWfPeakIntegrator()

    def start(self):
        fig1 = plt.figure(3)
        ax1 = fig1.add_subplot(111, aspect='equal')
        try:
            source = EventSourceFactory.produce(input_url=self.infile,
                                                max_events=self.max_events)

            for event in tqdm(source):
                self.cal.calibrate(event)
                self.dl0.reduce(event)
                self.dl1.calibrate(event)

                if self.disp is None:
                    self.geom = event.inst.subarray.tel[self.telescopes].camera
                    self.mean_reconstructed_image_array = np.zeros(
                        len(self.geom.pix_id))
                    self.mean_true_image_array = np.zeros(len(
                        self.geom.pix_id))
                    self.disp = 1
                    if self.debug:
                        self.disp = CameraDisplay(self.geom)
                        self.disp.add_colorbar()

                # reco_array = self.cross.get_charge(event.r1.tel[self.telescopes].waveform[0])['charge']
                # reco_array = self.glob_peak.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0]
                reco_array = self.local_peak.extract_charge(
                    event.r1.tel[self.telescopes].waveform
                )[0][
                    0]  #/ np.mean(self.local_peak.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0])
                true_array = event.mc.tel[
                    self.
                    telescopes].photo_electron_image  #/ np.mean(event.mc.tel[self.telescopes].photo_electron_image)
                # print(true_array)
                # print(reco_array)
                # exit()
                # plt.scatter(event.mc.tel[self.telescopes].photo_electron_image,reco_array)
                # plt.show()
                #
                # exit()
                # reco_array = self.aver.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0]
                # reco_array = self.neighbour.extract_charge(event.r1.tel[self.telescopes].waveform)[0][0]
                # print(reco_array)
                # exit()
                # print(event.mc.tel[self.telescopes].photo_electron_image)

                self.mean_true_image_array = np.add(self.mean_true_image_array,
                                                    true_array)
                self.mean_reconstructed_image_array = np.add(
                    self.mean_reconstructed_image_array, reco_array)
                # print(self.mean_true_image_array)
                # print(self.mean_reconstructed_image_array)
                # dist = np.sqrt(self.geom.pix_x.value**2 + self.geom.pix_y.value**2)
                # plt.scatter( self.mean_true_image_array/np.mean(self.mean_true_image_array), self.mean_reconstructed_image_array/np.mean(self.mean_reconstructed_image_array), c = dist, alpha=0.4)
                # ax1.set_ylim(0,2)
                # ax1.set_xlim(0,2)
                # plt.draw()
                # plt.pause(0.4)
                # plt.cla()

                # self.reconstructed_image_array.append(event.dl1.tel[self.telescopes].image[0])
                self.event_count += 1
        except FileNotFoundError:
            print('file_not_found')

    def finish(self):
        # out_file = open(self.output_name)
        # out_file.write('#PixID meanIllum\n')
        # for i in range(len(self.reconstructed_image_array)):
        #     out_file.write('%s\t%s\n' % (self.geom.pix_id, self.reconstructed_image_array))
        # out_file.close()
        self.mean_reconstructed_image_array = self.mean_reconstructed_image_array / self.event_count
        self.mean_true_image_array = self.mean_true_image_array / self.event_count
        if self.debug:
            # mean_image = np.mean(self.reconstructed_image_array,axis=0)/np.mean(np.mean(self.reconstructed_image_array))
            mean_image = self.mean_reconstructed_image_array
            self.disp.image = self.mean_true_image_array

            # fig = plt.figure(3)
            # plt.hist(self.mean_true_image_array/self.mean_true_image_array.mean(), alpha = 0.5, label='true')
            # plt.hist( self.mean_reconstructed_image_array/self.mean_reconstructed_image_array.mean(), alpha = 0.5, label='reco')
            # plt.legend()
            # from IPython import embed
            # embed()
            # self.disp.norm = 'log'
            # self.disp.set_limits_minmax(40.5,44.5)
            plt.show()