Exemplo n.º 1
0
 def read_files(self):
     #self.source = {}
     try:
         for i, file in enumerate(self.file_list):
             infile = expandvars(file)
             self.source = event_source(infile, max_events=self.max_events)
     except:
         infile = expandvars(self.file_list)
         self.source = event_source(infile, max_events=self.max_events)
Exemplo n.º 2
0
def test_event_source_input_url_config_override():
    dataset1 = get_dataset_path("gamma_test_large.simtel.gz")
    dataset2 = get_dataset_path("gamma_test_large.simtel.gz")
    config = Config({'EventSource': {'input_url': dataset1}})
    reader = event_source(input_url=dataset2, config=config)
    assert isinstance(reader, SimTelEventSource)
    assert reader.input_url == dataset2
Exemplo n.º 3
0
 def setup(self):
     if self.events == '':
         raise ToolConfigurationError("please specify --input <events file>")
     self.log.debug("input: %s", self.events)
     self.source = event_source(self.events)
     self.calib = CameraCalibrator(parent=self)
     self.writer = HDF5TableWriter(self.outfile, "muons")
def get_pedestals(path):
    wfs = []
    maxev = 500
    for i, ev in enumerate(event_source(path, max_events=maxev)):
        wfs.append(ev.r0.tel[tel].waveform)
    wfs = np.array(wfs)  # evt, gain, pixels, samples
    return wfs.mean(axis=0)  # gain, pixels, samples
Exemplo n.º 5
0
def test_event_source_config():
    dataset1 = get_dataset_path("gamma_test_large.simtel.gz")
    dataset2 = get_dataset_path("gamma_test_large.simtel.gz")
    config = Config({"EventSource": {"input_url": dataset1}})
    reader = event_source(dataset2, config=config)
    assert isinstance(reader, SimTelEventSource)
    assert str(reader.input_url) == dataset2
Exemplo n.º 6
0
def show_pe_image(simtel_file_path, tel_num=1, channel=0, event_index=0):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = event_source(simtel_file_path,
                          allowed_tels=[tel_num],
                          max_events=event_index + 1)
    # source = hessio_event_source(simtel_file_path,
    #                              allowed_tels=[tel_num],
    #                              max_events=event_index+1)

    event_list = list(source)  # TODO
    event = event_list[event_index]  # TODO

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y)
    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('CT{:03d}, event {:010d}'.format(
        tel_num, event.dl0.event_id))

    # DISPLAY TIME-VARYING EVENT ############################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    disp.image = data[:, ii]
    #    #disp.set_limits_percent(70)   # TODO help(disp.set_limits_percent)
    #    disp.set_limits_minmax(0, 400)
    #    plt.savefig('CT{:03d}_EV{:010d}_S{:02d}.png'.format(tel_num, event.dl0.event_id, ii))

    # DISPLAY INTEGRATED EVENT ##############################################

    #disp.image = event.dl0.tel[tel_num].adc_sums[channel]
    #disp.set_limits_percent(70)        # TODO help(disp.set_limits_percent)

    # TODO: check that (taken from https://github.com/tino-michael/tino_cta/blob/e6cc6db3e64135c9ac92bce2dae6e6f81a36096a/sandbox/show_ADC_and_PE_per_event.py)
    for jj in range(len(event.mc.tel[tel_num].photo_electrons)):
        #event.dl0.tel[tel_num].adc_sums[channel][jj] = event.mc.tel[tel_num].photo_electrons[jj]
        event.r0.tel[tel_num].adc_sums[channel][jj] = event.mc.tel[
            tel_num].photo_electrons[jj]
    signals2 = event.dl0.tel[tel_num].adc_sums[channel].astype(float)
    disp.image = signals2

    disp.set_limits_minmax(0, 9000)
    plt.savefig('CT{:03d}_EV{:010d}.png'.format(tel_num, event.dl0.event_id))

    # PLOT ##################################################################

    plt.show()
Exemplo n.º 7
0
def test_factory_for_nectarcam_file():
    from ctapipe.io import event_source
    from ctapipe.io.nectarcameventsource import NectarCAMEventSource

    reader = event_source(example_file_path)
    assert isinstance(reader, NectarCAMEventSource)
    assert reader.input_url == example_file_path
Exemplo n.º 8
0
def count_simtel_events(simtel_file_path):
    """
    Count the number of events per telescope in a simtel file.

    Returns the number of events per telescope and the total number of events.
    """

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = event_source(simtel_file_path, allowed_tels=None, max_events=None)

    num_event_dict = {}  # Number of events per telescope
    total_num_events = 0  # Total number of events

    for event in source:
        total_num_events += 1

        for telescope_id in event["trig"]["tels_with_trigger"]:
            if telescope_id not in num_event_dict:
                num_event_dict[telescope_id] = 0
            num_event_dict[telescope_id] += 1

    return num_event_dict, total_num_events
Exemplo n.º 9
0
def test_subarray():
    from ctapipe.io import event_source
    source = event_source(example_file_path)
    subarray = source.subarray
    subarray.info()
    subarray.peek()
    subarray.to_table()
Exemplo n.º 10
0
def test_factory_for_lst_file():
    from ctapipe.io import event_source
    from ctapipe.io.lsteventsource import LSTEventSource

    reader = event_source(example_file_path)
    assert isinstance(reader, LSTEventSource)
    assert reader.input_url == example_file_path
Exemplo n.º 11
0
def test_event_source_input_url_config_override():
    dataset1 = get_dataset_path("gamma_test_large.simtel.gz")
    dataset2 = get_dataset_path("gamma_test_large.simtel.gz")
    config = Config({'EventSource': {'input_url': dataset1}})
    reader = event_source(input_url=dataset2, config=config)
    assert isinstance(reader, SimTelEventSource)
    assert reader.input_url == dataset2
Exemplo n.º 12
0
 def setup(self):
     if self.events == '':
         raise ToolConfigurationError(
             "please specify --input <events file>")
     self.log.debug("input: %s", self.events)
     self.source = event_source(self.events)
     self.calib = CameraCalibrator(parent=self)
     self.writer = HDF5TableWriter(self.outfile, "muons")
Exemplo n.º 13
0
def test_read_subarray_description():
    from lstchain.io.io import read_subarray_description
    from ctapipe.io import event_source
    source = event_source(mc_gamma_testfile)
    dl1_subarray = read_subarray_description(dl1_file)
    dl1_subarray.peek()
    dl1_subarray.info()
    assert len(dl1_subarray.tels) == len(source.subarray.tels)
    assert (dl1_subarray.to_table() == source.subarray.to_table()).all()
Exemplo n.º 14
0
def load_calibrate(filename):
    # LOAD AND CALIBRATE

    # pwd = "/home/thomas/Programs/astro/CTAPIPE_DAN/"
    # filename = 'gamma_20deg_0deg_run100___cta-prod3-lapalma3-2147m-LaPalma_cone10.simtel.gz'
    # filename = 'gamma_20deg_0deg_run100___cta-prod3_desert-2150m-Paranal-merged.simtel.gz'
    # filename = 'gamma_20deg_0deg_run118___cta-prod3_desert-2150m-Paranal-merged_cone10.simtel.gz'
    # filename = 'gamma_20deg_180deg_run11___cta-prod3_desert-2150m-Paranal-merged_cone10.simtel.gz'

    # layout = np.loadtxt(pwd+'CTA.prod3Sb.3HB9-FG.lis', usecols=0, dtype=int)
    if "Paranal" in filename:
        layout = [4, 5, 6, 11]
        print("PARANAL WITH {0}".format(layout))
    elif "palma" in filename:
        layout = [5, 6, 7, 8]
        print("LAPALMA WITH {0}".format(layout))

    print("Layout telescopes IDs:".format(layout))

    # layout = [279, 280, 281, 282, 283, 284, 286, 287, 289, 297, 298, 299,
    #           300, 301, 302, 303, 304, 305, 306, 307, 308, 315, 316, 317,
    #           318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329,
    #           330, 331, 332, 333, 334, 335, 336, 337, 338, 345, 346, 347,
    #           348, 349, 350, 375, 376, 377, 378, 379, 380, 393, 400, 402,
    #           403, 404, 405, 406, 408, 410, 411, 412, 413, 414, 415, 416,
    #           417]

    layout = set(layout)

    source = event_source(filename)
    source.max_events = 50
    source.allowed_tels = layout
    events = [copy.deepcopy(event) for event in source]

    cal = CameraCalibrator(None,
                           None,
                           r1_product='HESSIOR1Calibrator',
                           extractor_product='NeighbourPeakIntegrator')
    for event in events:
        cal.calibrate(event)

    # Find "big" event (piece of code from T.V. notebook ...thanks :D )
    events_amplitude = []
    for event in events:
        event_amplitude = 0
        for tel_id in event.r0.tels_with_data:
            if event.dl1.tel[tel_id].image is not None:
                event_amplitude += event.dl1.tel[tel_id].image[0].sum()
        events_amplitude.append(event_amplitude)
    events_amplitude = np.array(events_amplitude)

    mm = events_amplitude.argmax()
    print("event: {0}".format(mm))
    event = events[mm]

    return event
Exemplo n.º 15
0
def test_factory_for_lst_file():
    from ctapipe.io import event_source

    reader = event_source(example_file_path)

    # import here to see if ctapipe detects plugin
    from ctapipe_io_lst import LSTEventSource

    assert isinstance(reader, LSTEventSource)
    assert reader.input_url == example_file_path
def test_factory_for_nectarcam_file():
    from ctapipe.io import event_source

    reader = event_source(example_file_path)

    # explicit import after event_source, to test if this
    # package is detected by ctapipe
    from ctapipe_io_nectarcam import NectarCAMEventSource
    assert isinstance(reader, NectarCAMEventSource)
    assert str(reader.input_url) == example_file_path
Exemplo n.º 17
0
def test_display_dl1_event():
    from ctapipe.io import event_source, EventSeeker
    from lstchain.tests.test_lstchain import mc_gamma_testfile
    from ctapipe.calib import CameraCalibrator
    source = event_source(mc_gamma_testfile, back_seekable=True)
    seeker = EventSeeker(source)
    event = seeker[11]  # event 11 has telescopes 1 and 4 with data
    CameraCalibrator()(event)
    display_dl1_event(event, tel_id=1)
    display_dl1_event(event, tel_id=4)
Exemplo n.º 18
0
def test_reconstruction():
    """
    a test of the complete fit procedure on one event including:
    • tailcut cleaning
    • hillas parametrisation
    • HillasPlane creation
    • direction fit
    • position fit

    in the end, proper units in the output are asserted """

    filename = get_dataset_path("gamma_test_large.simtel.gz")

    fit = HillasReconstructor()

    tel_azimuth = {}
    tel_altitude = {}

    source = event_source(filename, max_events=10)

    for event in source:

        hillas_dict = {}
        for tel_id in event.dl0.tels_with_data:

            geom = event.inst.subarray.tel[tel_id].camera
            tel_azimuth[tel_id] = event.mc.tel[tel_id].azimuth_raw * u.rad
            tel_altitude[tel_id] = event.mc.tel[tel_id].altitude_raw * u.rad

            pmt_signal = event.r0.tel[tel_id].waveform[0].sum(axis=1)

            mask = tailcuts_clean(geom,
                                  pmt_signal,
                                  picture_thresh=10.,
                                  boundary_thresh=5.)
            pmt_signal[mask == 0] = 0

            try:
                moments = hillas_parameters(geom, pmt_signal)
                hillas_dict[tel_id] = moments
            except HillasParameterizationError as e:
                print(e)
                continue

        if len(hillas_dict) < 2:
            continue

        fit_result = fit.predict(hillas_dict, event.inst, tel_azimuth,
                                 tel_altitude)

        print(fit_result)
        fit_result.alt.to(u.deg)
        fit_result.az.to(u.deg)
        fit_result.core_x.to(u.m)
        assert fit_result.is_valid
Exemplo n.º 19
0
 def init(self):
     self.log.debug('%self.filename' % "--- SimTelArrayReader init {}---")
     try:
         in_file = get_dataset_path(self.filename)
         self.source = event_source(in_file, max_events=3)
         self.log.debug('%s successfully opened %s', self.filename,
                        self.source)
     except Exception:
         self.log.error('could not open ' + in_file)
         return False
     return True
Exemplo n.º 20
0
def getNbTel(inputFileName):
    """
	Get the number of telescope in the simulation file
	Parameters:
		inputFileName : name of the input file to be used

	Return:
		number of telescopes in the simulation file
	"""
    with event_source(inputFileName) as source:
        nbTel = source.subarray.num_tels
        return nbTel
Exemplo n.º 21
0
def test_reconstruction():
    """
    a test of the complete fit procedure on one event including:
    • tailcut cleaning
    • hillas parametrisation
    • HillasPlane creation
    • direction fit
    • position fit

    in the end, proper units in the output are asserted """

    filename = get_dataset_path("gamma_test_large.simtel.gz")

    fit = HillasReconstructor()

    tel_azimuth = {}
    tel_altitude = {}

    source = event_source(filename, max_events=10)

    for event in source:

        hillas_dict = {}
        for tel_id in event.dl0.tels_with_data:

            geom = event.inst.subarray.tel[tel_id].camera
            tel_azimuth[tel_id] = event.mc.tel[tel_id].azimuth_raw * u.rad
            tel_altitude[tel_id] = event.mc.tel[tel_id].altitude_raw * u.rad

            pmt_signal = event.r0.tel[tel_id].image[0]

            mask = tailcuts_clean(geom, pmt_signal,
                                  picture_thresh=10., boundary_thresh=5.)
            pmt_signal[mask == 0] = 0

            try:
                moments = hillas_parameters(geom, pmt_signal)
                hillas_dict[tel_id] = moments
            except HillasParameterizationError as e:
                print(e)
                continue

        if len(hillas_dict) < 2:
            continue

        fit_result = fit.predict(hillas_dict, event.inst, tel_azimuth, tel_altitude)

        print(fit_result)
        fit_result.alt.to(u.deg)
        fit_result.az.to(u.deg)
        fit_result.core_x.to(u.m)
        assert fit_result.is_valid
Exemplo n.º 22
0
def show_image(simtel_file_path, tel_num=1, channel=0, event_index=0):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = event_source(simtel_file_path,
                          allowed_tels=[tel_num],
                          max_events=event_index + 1)
    # source = hessio_event_source(simtel_file_path,
    #                              allowed_tels=[tel_num],
    #                              max_events=event_index+1)

    event_list = list(source)  # TODO
    event = event_list[event_index]  # TODO

    # INIT PLOT #############################################################

    x, y = event.meta.pixel_pos[tel_num]
    geom = ctapipe.io.CameraGeometry.guess(x, y)
    disp = ctapipe.visualization.CameraDisplay(geom, title='CT%d' % tel_num)
    disp.enable_pixel_picker()
    disp.add_colorbar()

    disp.axes.set_title('CT{:03d}, event {:010d}'.format(
        tel_num, event.dl0.event_id))

    # DISPLAY TIME-VARYING EVENT ############################################

    #data = event.dl0.tel[tel_num].adc_samples[channel]
    #for ii in range(data.shape[1]):
    #    disp.image = data[:, ii]
    #    #disp.set_limits_percent(70)   # TODO help(disp.set_limits_percent)
    #    disp.set_limits_minmax(0, 400)
    #    plt.savefig('CT{:03d}_EV{:010d}_S{:02d}.png'.format(tel_num, event.dl0.event_id, ii))

    # DISPLAY INTEGRATED EVENT ##############################################

    #disp.image = event.dl0.tel[tel_num].adc_sums[channel]  # ctapipe 0.3.0
    disp.image = event.r0.tel[tel_num].adc_sums[channel]  # ctapipe 0.4.0
    #disp.set_limits_percent(70)        # TODO help(disp.set_limits_percent)
    disp.set_limits_minmax(0, 9000)
    plt.savefig('CT{:03d}_EV{:010d}.png'.format(tel_num, event.dl0.event_id))

    # PLOT ##################################################################

    plt.show()
Exemplo n.º 23
0
    def setup(self):
        kwargs = dict(config=self.config, tool=self)

        self.eventsource = event_source(
            get_dataset_path("gamma_test.simtel.gz"),
            **kwargs
        )

        self.calibrator = CameraCalibrator(
            eventsource=self.eventsource, **kwargs
        )

        self.plotter = ImagePlotter(**kwargs)
Exemplo n.º 24
0
def test_check_and_apply_volume_reduction():
    source = event_source(get_dataset_path('gamma_test.simtel.gz'))
    ev = next(iter(source))
    cal = CameraCalibrator()
    config = get_standard_config()
    config['volume_reducer']['algorithm'] = 'zero_suppression_tailcut_dilation'

    cal(ev)
    check_and_apply_volume_reduction(ev, config)

    for tel_id in ev.r0.tels_with_data:
        assert 0 in ev.dl1.tel[tel_id].image
        assert 0 in ev.dl1.tel[tel_id].pulse_time
        assert 0 in ev.dl0.tel[tel_id].waveform
Exemplo n.º 25
0
def test_dl1writer(tmpdir: Path):
    """
    Check that we can write DL1 files

    Parameters
    ----------
    tmpdir :
        temp directory fixture
    """

    output_path = Path(tmpdir / "events.dl1.h5")
    source = event_source(
        get_dataset_path(
            "gamma_LaPalma_baseline_20Zd_180Az_prod3b_test.simtel.gz"),
        max_events=20,
        allowed_tels=[1, 2, 3, 4],
    )
    calibrate = CameraCalibrator(subarray=source.subarray)

    with DL1Writer(
            event_source=source,
            output_path=output_path,
            write_parameters=False,
            write_images=True,
    ) as write_dl1:
        write_dl1.log.level = logging.DEBUG
        for event in source:
            calibrate(event)
            write_dl1(event)
        write_dl1.write_simulation_histograms(source)

    assert output_path.exists()

    # check we can get the subarray description:
    sub = SubarrayDescription.from_hdf(output_path)
    assert sub.num_tels > 0

    # check a few things in the output just to make sure there is output. For a
    # full test of the data model, a verify tool should be created.
    with tables.open_file(output_path) as h5file:
        images = h5file.get_node("/dl1/event/telescope/images/tel_001")
        assert images.col("image").max() > 0.0
        assert (h5file.root._v_attrs["CTA PRODUCT DATA MODEL VERSION"]  # pylint: disable=protected-access
                == DL1_DATA_MODEL_VERSION)
        shower = h5file.get_node("/simulation/event/subarray/shower")
        assert len(shower) > 0
        assert shower.col("true_alt").mean() > 0.0
        assert (shower._v_attrs["true_alt_UNIT"] == "deg")  # pylint: disable=protected-access
Exemplo n.º 26
0
    def start(self):

        output_parameters = {'MuonEff': [], 'ImpactP': [], 'RingWidth': []}

        numev = 0
        num_muons_found = 0

        for event in event_source(self.infile):
            self.log.info("Event Number: %d, found %d muons", numev,
                          num_muons_found)
            self.calib.calibrate(event)
            muon_evt = analyze_muon_event(event)

            numev += 1

            if not muon_evt[
                    'MuonIntensityParams']:  # No telescopes contained a good muon
                continue
            else:
                if self.display:
                    plot_muon_event(event, muon_evt)

                for tid in muon_evt['TelIds']:
                    idx = muon_evt['TelIds'].index(tid)
                    if muon_evt['MuonIntensityParams'][idx] is not None:
                        self.log.info("** Muon params: %s",
                                      muon_evt['MuonIntensityParams'][idx])

                        output_parameters['MuonEff'].append(
                            muon_evt['MuonIntensityParams']
                            [idx].optical_efficiency_muon)
                        output_parameters['ImpactP'].append(
                            muon_evt['MuonIntensityParams']
                            [idx].impact_parameter.value)
                        output_parameters['RingWidth'].append(
                            muon_evt['MuonIntensityParams']
                            [idx].ring_width.value)
                        print_muon(muon_evt, printer=self.log.info)
                        num_muons_found += 1

        t = Table(output_parameters)
        t['ImpactP'].unit = 'm'
        t['RingWidth'].unit = 'deg'
        if self.outfile:
            t.write(self.outfile)
Exemplo n.º 27
0
def test_zero_suppression_tailcut_dilation():
    source = event_source(get_dataset_path('gamma_test.simtel.gz'))
    for i, event in enumerate(source):
        for tel_id in list(event.r0.tels_with_data):
            if tel_id <= 4:
                break
            else:
                continue
        break

    cal = CameraCalibrator()
    cal(event)
    camera = event.inst.subarray.tel[tel_id].camera
    imag = event.dl1.tel[tel_id].image

    pixels_zero_supp = zero_suppression_tailcut_dilation(camera, imag)
    pixels_tailcut = tailcuts_clean(camera,
                                    imag,
                                    picture_thresh=8,
                                    boundary_thresh=4,
                                    keep_isolated_pixels=True,
                                    min_number_picture_neighbors=0)

    reduced_imag = np.copy(imag)
    cleaned_imag = np.copy(imag)

    reduced_imag[~pixels_zero_supp] = 0
    cleaned_imag[~pixels_tailcut] = 0

    pixels_cleaned_after_reduction = tailcuts_clean(
        camera,
        reduced_imag,
        picture_thresh=8,
        boundary_thresh=4,
        keep_isolated_pixels=True,
        min_number_picture_neighbors=0)

    reduced_imag[~pixels_cleaned_after_reduction] = 0

    # Check that a (reduced & cleaned) and a cleaned image give the same result
    assert (reduced_imag == cleaned_imag).all()
    # Check that the anti mask of a volume reduced imaged is just filled by zeros.
    assert (reduced_imag[~pixels_zero_supp] == 0).all()
Exemplo n.º 28
0
def main():
    print("--> Input file: {}".format(args.input_file))
    print("--> Number of events: {}".format(args.max_events))
    reader = event_source(input_url=args.input_file, max_events=args.max_events)
    print("--> Number of files", reader.multi_file.num_inputs())

    seeker = EventSeeker(reader)
    ev = seeker[0]
    tel_id = ev.r0.tels_with_data[0]
    n_modules = ev.lst.tel[tel_id].svc.num_modules

    config = Config({
        "LSTR0Corrections": {
            "tel_id": tel_id
        }
    })
    lst_r0 = LSTR0Corrections(config=config)

    pedestal = DragonPedestal(tel_id=tel_id, n_module=n_modules, r0_sample_start=args.start_r0_waveform)

    if args.deltaT:
        print("DeltaT correction active")
        for i, event in enumerate(reader):
            for tel_id in event.r0.tels_with_data:
                lst_r0.time_lapse_corr(event, tel_id)
                pedestal.fill_pedestal_event(event)
                if i%500 == 0:
                    print("i = {}, ev id = {}".format(i, event.r0.event_id))
    else:
        print("DeltaT correction no active")
        for i, event in enumerate(reader):
            pedestal.fill_pedestal_event(event)
            if i%500 == 0:
                print("i = {}, ev id = {}".format(i, event.r0.event_id))

    # Finalize pedestal and write to fits file
    pedestal.finalize_pedestal()

    primaryhdu = fits.PrimaryHDU(ev.lst.tel[tel_id].svc.pixel_ids)
    secondhdu = fits.ImageHDU(np.int16(pedestal.meanped))

    hdulist = fits.HDUList([primaryhdu, secondhdu])
    hdulist.writeto(args.output_file)
def main():
    print("--> Input file: {}".format(args.input_file))
    print("--> Number of events: {}".format(args.max_events))
    reader = event_source(input_url=args.input_file,
                          max_events=args.max_events)
    print("--> Number of files", reader.multi_file.num_inputs())

    config_dic = {}
    if args.config_file is not None:
        try:
            config_dic = read_configuration_file(args.config_file)

        except ("Custom configuration could not be loaded !!!"):
            pass
    # read the configuration file
    config = Config(config_dic)

    # declare the pedestal calibrator
    lst_r0 = LSTR0Corrections(pedestal_path=args.pedestal_file, config=config)

    # declare the time corrector
    timeCorr = TimeCorrectionCalculate(calib_file_path=args.output_file,
                                       config=config)

    tel_id = timeCorr.tel_id

    for i, event in enumerate(reader):
        if event.r0.event_id % 5000 == 0:
            print(event.r0.event_id)

        lst_r0.calibrate(event)

        # Cut in signal to avoid cosmic events
        if event.r1.tel[tel_id].trigger_type == 4 or (np.median(
                np.sum(event.r1.tel[tel_id].waveform[0], axis=1)) > 300):

            timeCorr.calibrate_pulse_time(event)
    # write output
    timeCorr.finalize()
def main():
    quality_param_list = []
    hillas_containers = {}

    data = []

    resource_path = '/home/matteo/ctasoft/resources'
    filename = 'gamma_20deg_0deg_run101___cta-prod3-lapalma3-2147m-LaPalma.simtel.gz'

    input_url = find_in_path(filename, resource_path)
    events = event_source(input_url, max_events=1)

    calibrator = CameraCalibrator()

    for number_event, event in enumerate(events):
        hillas_containers.clear()
        calibrator(event)
        data.append((number_event, event.mc.energy, event.inst.subarray.tel_coords))

        for telescope_id, dl1 in event.dl1.tel.items():
            quality_param_list.clear()

            image = dl1.image
            camera = event.inst.subarray.tels[telescope_id].camera

            optimized_cleaning_level = iterate_param(possible_cleaning_levels, event, camera, image,
                                                     telescope_id, quality_param_list, hillas_containers)

            data.append((camera.cam_id, telescope_id,
                         optimized_cleaning_level))

    np.savez_compressed('parameters_gamma_20deg_0deg_run101___cta-prod3-lapalma3-2147m-LaPalma.npz', data=data)

    loaded = np.load('parameters_gamma_20deg_0deg_run101___cta-prod3-lapalma3-2147m-LaPalma.npz'
                     , allow_pickle=True)

    data_loaded = loaded['data']

    print(data_loaded)
Exemplo n.º 31
0
def test_eventplotter():
    dataset = get_dataset_path("gamma_test_large.simtel.gz")
    with event_source(dataset, max_events=1) as source:
        event = next(iter(source))

        telid = list(event.r0.tels_with_data)[0]

        data = event.r0.tel[telid].waveform[0]
        plotter = CameraPlotter(event)

        camera = plotter.draw_camera(telid, data[:, 0])
        assert camera is not None
        np.testing.assert_array_equal(camera.image, data[:, 0])

        plotter.draw_camera_pixel_ids(telid, [0, 1, 2])

        waveform = plotter.draw_waveform(data[0, :])
        assert waveform is not None
        np.testing.assert_array_equal(waveform.get_ydata(), data[0, :])

        line = plotter.draw_waveform_positionline(0)
        assert line is not None
        np.testing.assert_array_equal(line.get_xdata(), [0, 0])
Exemplo n.º 32
0
def find_nice_event():
    filename = datasets.get_dataset_path("gamma_test_large.simtel.gz")
    source = event_source(filename)
    calib = CameraCalibrator()
    
    for i, event in tqdm(enumerate(source)):
        # from IPython import embed; embed()
        # if i in [0, 1, 2, 3, 4, 39]:  # skip ugly events
        #     print(i, event.mc.energy)
        #     continue 
        subarray = event.inst.subarray
        calib(event)
        for tel_id in event.dl0.tels_with_data:
            # Camera Geometry required for hillas parametrization
            camgeom = subarray.tel[tel_id].camera
            # note the [0] is for channel 0 which is high-gain channel
            image = event.dl1.tel[tel_id].image
            # Cleaning  of the image
            cleaned_image = image.copy()
            # create a clean mask of pixels above the threshold
            cleanmask = tailcuts_clean(
                camgeom, image, picture_thresh=10, boundary_thresh=5, min_number_picture_neighbors=3
            )
            # set all rejected pixels to zero
            cleaned_image[~cleanmask] = 0

            # Calculate hillas parameters
            try:
                d = hillas_parameters(camgeom, cleaned_image)
            except HillasParameterizationError:
                pass  # skip failed parameterization (normally no signal)
            # from IPython import embed; embed()
            tel_name = event.inst.subarray.tel[tel_id].name
            if tel_name == 'LST' and d.r < 1 * u.m and d.intensity > 400:
                print(i, d.intensity, event.mc.energy)
                return tel_id, d, event, cleanmask
Exemplo n.º 33
0
def list_simtel_content(simtel_file_path):

    # GET EVENT #############################################################

    # hessio_event_source returns a Python generator that streams data from an
    # EventIO/HESSIO MC data file (e.g. a standard CTA data file).
    # This generator contains ctapipe.core.Container instances ("event").
    #
    # Parameters:
    # - max_events: maximum number of events to read
    # - allowed_tels: select only a subset of telescope, if None, all are read.
    source = event_source(simtel_file_path, allowed_tels=None, max_events=None)

    for event in source:
        print("Count:", event["count"])
        print("Trigger data:", event["trig"])
        print("Monte-Carlo shower data:", event["mc"])
        print("Raw data:", event["dl0"])

        #print("Simtel file path:", event.meta["hessio__input"])
        print("Pixel pos:", event.meta["pixel_pos"])
        #print("Max events:", event.meta["hessio__max_events"])

        print()
Exemplo n.º 34
0
    def setup(self):
        with event_source(self.infile) as source:
            data = next(iter(source))  # get one event, so the instrument table is there

        self.inst = data.inst  # keep a reference to the instrument stuff
Exemplo n.º 35
0
"""
Example of showing some information about the instrumental description
"""

from ctapipe.io import event_source
from ctapipe.utils import get_dataset_path

if __name__ == '__main__':

    # load up one event so that we get the instrument info
    infile = get_dataset_path("gamma_test.simtel.gz")
    with event_source(infile) as source:
        gsource = (x for x in source)
        event = next(gsource)

    print("------ Input: ", infile)

    subarray = event.inst.subarray

    print("\n---------- Subarray Info: -----------")
    subarray.info()
    print("\n---------- Subarray Table:-----------")
    print(subarray.to_table())
    print("\n---------- Subarray Optics:----------")
    print(subarray.to_table(kind='optics'))
    print("\n---------- Mirror Area: -------------")
    print(subarray.tel[1].optics.mirror_area)
Exemplo n.º 36
0
def test_factory_nonexistant_file():
    with pytest.raises(FileNotFoundError):
        dataset = "/fake_path/fake_file.fake_extension"
        event_source(input_url=dataset)
Exemplo n.º 37
0
 def start(self):
     """ main event loop """
     with event_source(self.infile) as source:
         for event in source:
             self.add_event_to_table(event)
Exemplo n.º 38
0
import numpy as np

from ctapipe.coordinates import HorizonFrame, CameraFrame, NominalFrame


cleaning_level = {
    'LSTCam': (3.5, 7.5, 2),  # ?? (3, 6) for Abelardo...
    'FlashCam': (4, 8, 2),  # there is some scaling missing?
    'ASTRICam': (5, 7, 2),
}


input_url = get_dataset_path('gamma_test_large.simtel.gz')


with event_source(input_url=input_url) as source:
    calibrator = CameraCalibrator(
        eventsource=source,
    )

    for event in source:

        calibrator.calibrate(event)

        nominal_frame = NominalFrame(
            origin=SkyCoord(alt=70 * u.deg, az=0 * u.deg, frame=HorizonFrame)
        )

        nom_delta_az = []
        nom_delta_alt = []
        photons = []
Exemplo n.º 39
0
def test_factory():
    dataset = get_dataset_path("gamma_test_large.simtel.gz")
    reader = event_source(input_url=dataset)
    assert isinstance(reader, SimTelEventSource)
    assert reader.input_url == dataset
Exemplo n.º 40
0
import matplotlib.pylab as plt
import numpy as np
from astropy import units as u

from ctapipe.io import event_source
from ctapipe.utils import datasets
from ctapipe.visualization import ArrayDisplay

if __name__ == '__main__':

    plt.figure(figsize=(9.5, 8.5))

    # load up a single event, so we can get the subarray info:
    source = event_source(
        datasets.get_dataset_path("gamma_test_large.simtel.gz"),
        max_events=1,
    )

    event = next(iter(source))

    # display the array
    subarray = event.inst.subarray
    ad = ArrayDisplay(subarray, tel_scale=3.0)

    print("Now setting vectors")
    plt.pause(1.0)
    plt.tight_layout()

    for phi in np.linspace(0, 360, 30) * u.deg:
        r = np.cos(phi / 2)
        ad.set_vector_rho_phi(r, phi)
Exemplo n.º 41
0
"""
The most basic pipeline, using no special features of the framework other 
than a for-loop. This is useful for debugging and profiling of speed.
"""

import sys

from ctapipe.calib import CameraCalibrator
from ctapipe.io import event_source

if __name__ == '__main__':

    filename = sys.argv[1]

    source = event_source(filename, max_events=None)

    cal = CameraCalibrator()

    for ii, event in enumerate(source):

        print(
            "{} EVENT_ID: {}, ENERGY: {:.2f}, NTELS:{}".format(
                ii,
                event.r0.event_id, event.mc.energy, len(event.dl0.tels_with_data)
            )
        )

        cal.calibrate(event)

        # now the calibrated images are in data.dl1.tel[x].image
Exemplo n.º 42
0
def test_factory_allowed_tels():
    dataset = get_dataset_path("gamma_test_large.simtel.gz")
    reader = event_source(input_url=dataset)
    assert len(reader.allowed_tels) == 0
    reader = event_source(input_url=dataset, allowed_tels={1, 3})
    assert len(reader.allowed_tels) == 2
Exemplo n.º 43
0
def test_event_source_helper():
    with event_source(get_dataset_path("gamma_test_large.simtel.gz")) as source:
        for _ in source:
            pass
Exemplo n.º 44
0
from ctapipe.image import hillas_parameters
from ctapipe.image import tailcuts_clean
from ctapipe.io import event_source
from ctapipe.reco import HillasReconstructor
from ctapipe.utils import datasets


if len(sys.argv) >= 2:
    filename = sys.argv[1]
else:
    # importing data from avaiable datasets in ctapipe
    filename = datasets.get_dataset_path("gamma_test_large.simtel.gz")


# reading the Monte Carlo file for LST
source = event_source(filename, allowed_tels={1, 2, 3, 4})

reco = HillasReconstructor()
calib = CameraCalibrator()
off_angles = []

for event in source:

    # calibrating the event
    calib.calibrate(event)
    hillas_params = {}
    subarray = event.inst.subarray

    # pointing direction of the telescopes
    point_azimuth = {}
    point_altitude = {}
Exemplo n.º 45
0
    print("q               - Quit")
    return input("Choice: ")


if __name__ == '__main__':

    if len(sys.argv) > 1:
        filename = sys.argv.pop(1)
    else:
        filename = get_dataset_path("gamma_test_large.simtel.gz")

    plt.style.use("ggplot")
    plt.show(block=False)

    # loop over events and display menu at each event:
    source = event_source(filename)

    for event in source:

        print(
            "EVENT_ID: ", event.r0.event_id, "TELS: ", event.r0.tels_with_data,
            "MC Energy:", event.mc.energy
        )

        while True:
            response = get_input()
            if response.startswith("d"):
                disps = display_event(event)
                plt.pause(0.1)
            elif response.startswith("p"):
                print("--event-------------------")
Exemplo n.º 46
0
"""
very simple example that loads a single event into memory, for exploration
purposes
"""
import sys

from ctapipe.calib import CameraCalibrator
from ctapipe.io import event_source
from ctapipe.utils import get_dataset_path

if __name__ == '__main__':

    calib = CameraCalibrator(r1_product="HESSIOR1Calibrator")

    if len(sys.argv) >= 2:
        filename = sys.argv[1]
    else:
        filename = get_dataset_path("gamma_test_large.simtel.gz")

    with event_source(filename, max_events=1) as source:
        for event in source:
            calib.calibrate(event)

    print(event)
Exemplo n.º 47
0
def test_factory_max_events():
    max_events = 10
    dataset = get_dataset_path("gamma_test_large.simtel.gz")
    reader = event_source(input_url=dataset, max_events=max_events)
    assert reader.max_events == max_events
Exemplo n.º 48
0
def test_factory_incompatible_file():
    with pytest.raises(ValueError):
        dataset = get_dataset_path("optics.ecsv.txt")
        event_source(input_url=dataset)
Exemplo n.º 49
0
import matplotlib.pyplot as plt
from ctapipe.visualization import ArrayDisplay


# unoptimized cleaning levels, copied from
# https://github.com/tudo-astroparticlephysics/cta_preprocessing
cleaning_level = {
    'LSTCam': (3.5, 7.5, 2),  # ?? (3, 6) for Abelardo...
    'FlashCam': (4, 8, 2),  # there is some scaling missing?
    'ASTRICam': (5, 7, 2),
}


input_url = get_dataset_path('gamma_test_large.simtel.gz')
event_source = event_source(input_url)

calibrator = CameraCalibrator()

reco = HillasReconstructor()

for event in event_source:
    print('Event', event.count)
    calibrator(event)

    # mapping of telescope_id to parameters for stereo reconstruction
    hillas_containers = {}
    pointing_azimuth = {}
    pointing_altitude = {}
    time_gradients = {}