def hessio_get_list_event_ids(url, max_events=None): """ Faster method to get a list of all the event ids in the hessio file. This list can also be used to find out the number of events that exist in the file. Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read Returns ------- event_id_list : list[num_events] A list with all the event ids that are in the file. """ logger.warning("This method is slow. Need to find faster method.") try: with open_hessio(url) as pyhessio: counter = 0 event_id_list = [] eventstream = pyhessio.move_to_next_event() for event_id in eventstream: event_id_list.append(event_id) counter += 1 if max_events is not None and counter >= max_events: pyhessio.close_file() break return event_id_list except HessioError: raise RuntimeError( "hessio_event_source failed to open '{}'".format(url))
def hessio_get_list_event_ids(url, max_events=None): """ Faster method to get a list of all the event ids in the hessio file. This list can also be used to find out the number of events that exist in the file. Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read Returns ------- event_id_list : list[num_events] A list with all the event ids that are in the file. """ logger.warning("This method is slow. Need to find faster method.") try: with open_hessio(url) as pyhessio: counter = 0 event_id_list = [] eventstream = pyhessio.move_to_next_event() for event_id in eventstream: event_id_list.append(event_id) counter += 1 if max_events is not None and counter >= max_events: pyhessio.close_file() break return event_id_list except HessioError: raise RuntimeError("hessio_event_source failed to open '{}'" .format(url))
def get_spectral_w_pars(filename): N = 0 Emin = -1 Emax = -1 index = 0. Omega = 0. A = 0. Core_max = 0. particle = utils.guess_type(filename) N = pyhessio.count_mc_generated_events(filename) with pyhessio.open_hessio(filename) as f: f.fill_next_event() Emin = f.get_mc_E_range_Min() Emax = f.get_mc_E_range_Max() index = f.get_spectral_index() Cone = f.get_mc_viewcone_Max() Core_max = f.get_mc_core_range_X() K = N * (1 + index) / (Emax**(1 + index) - Emin**(1 + index)) A = np.pi * Core_max**2 Omega = 2 * np.pi * (1 - np.cos(Cone)) MeVtoTeV = 1e-6 if particle == "gamma": K_w = 5.7e-16 * MeVtoTeV index_w = -2.48 E0 = 0.3e6 * MeVtoTeV if particle == "proton": K_w = 9.6e-2 index_w = -2.7 E0 = 1 Simu_E0 = K * E0**index N_ = Simu_E0 * (Emax**(index_w + 1) - Emin**(index_w + 1)) / (E0**index_w) / (index_w + 1) R = K_w * A * Omega * (Emax**(index_w + 1) - Emin**(index_w + 1)) / (E0**index_w) / (index_w + 1) w_pars = np.array([E0, index, index_w, R, N_]) return w_pars
def read_simtel_mc_information(simtel_file): with pyhessio.open_hessio(simtel_file) as f: # do some weird hessio fuckup eventstream = f.move_to_next_event() _ = next(eventstream) d = { 'mc_spectral_index': f.get_spectral_index(), 'mc_num_reuse': f.get_mc_num_use(), 'mc_num_showers': f.get_mc_num_showers(), 'mc_max_energy': f.get_mc_E_range_Max(), 'mc_min_energy': f.get_mc_E_range_Min(), 'mc_max_scatter_range': f.get_mc_core_range_Y(), # range_X is always 0 in simtel files 'mc_max_viewcone_radius': f.get_mc_viewcone_Max(), 'mc_min_viewcone_radius': f.get_mc_viewcone_Min(), 'run_id': f.get_run_number(), 'mc_max_altitude': f.get_mc_alt_range_Max(), 'mc_max_azimuth': f.get_mc_az_range_Max(), 'mc_min_altitude': f.get_mc_alt_range_Min(), 'mc_min_azimuth': f.get_mc_az_range_Min(), } return d
def hessio_event_source(url, max_events=None, allowed_tels=None, requested_event=None, use_event_id=False): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read allowed_tels : list[int] select only a subset of telescope, if None, all are read. This can be used for example emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) requested_event : int Seek to a paricular event index use_event_id : bool If True ,'requested_event' now seeks for a particular event id instead of index """ with open_hessio(url) as pyhessio: # the container is initialized once, and data is replaced within # it after each yield Provenance().add_input_file(url, role='dl0.sub.evt') counter = 0 eventstream = pyhessio.move_to_next_event() if allowed_tels is not None: allowed_tels = set(allowed_tels) data = DataContainer() data.meta['origin'] = "hessio" # some hessio_event_source specific parameters data.meta['input'] = url data.meta['max_events'] = max_events for event_id in eventstream: # Seek to requested event if requested_event is not None: current = counter if use_event_id: current = event_id if not current == requested_event: counter += 1 continue data.r0.run_id = pyhessio.get_run_number() data.r0.event_id = event_id data.r0.tels_with_data = set(pyhessio.get_teldata_list()) data.r1.run_id = pyhessio.get_run_number() data.r1.event_id = event_id data.r1.tels_with_data = set(pyhessio.get_teldata_list()) data.dl0.run_id = pyhessio.get_run_number() data.dl0.event_id = event_id data.dl0.tels_with_data = set(pyhessio.get_teldata_list()) # handle telescope filtering by taking the intersection of # tels_with_data and allowed_tels if allowed_tels is not None: selected = data.r0.tels_with_data & allowed_tels if len(selected) == 0: continue # skip event data.r0.tels_with_data = selected data.r1.tels_with_data = selected data.dl0.tels_with_data = selected data.trig.tels_with_trigger \ = pyhessio.get_central_event_teltrg_list() time_s, time_ns = pyhessio.get_central_event_gps_time() data.trig.gps_time = Time(time_s * u.s, time_ns * u.ns, format='unix', scale='utc') data.mc.energy = pyhessio.get_mc_shower_energy() * u.TeV data.mc.alt = Angle(pyhessio.get_mc_shower_altitude(), u.rad) data.mc.az = Angle(pyhessio.get_mc_shower_azimuth(), u.rad) data.mc.core_x = pyhessio.get_mc_event_xcore() * u.m data.mc.core_y = pyhessio.get_mc_event_ycore() * u.m first_int = pyhessio.get_mc_shower_h_first_int() * u.m data.mc.h_first_int = first_int # mc run header data data.mcheader.run_array_direction = \ pyhessio.get_mc_run_array_direction() data.count = counter # this should be done in a nicer way to not re-allocate the # data each time (right now it's just deleted and garbage # collected) data.r0.tel.clear() data.r1.tel.clear() data.dl0.tel.clear() data.dl1.tel.clear() data.mc.tel.clear() # clear the previous telescopes _fill_instrument_info(data, pyhessio) for tel_id in data.r0.tels_with_data: # event.mc.tel[tel_id] = MCCameraContainer() data.mc.tel[tel_id].dc_to_pe \ = pyhessio.get_calibration(tel_id) data.mc.tel[tel_id].pedestal \ = pyhessio.get_pedestal(tel_id) data.r0.tel[tel_id].adc_samples = \ pyhessio.get_adc_sample(tel_id) if data.r0.tel[tel_id].adc_samples.size == 0: # To handle ASTRI and dst files data.r0.tel[tel_id].adc_samples = \ pyhessio.get_adc_sum(tel_id)[..., None] data.r0.tel[tel_id].adc_sums = \ pyhessio.get_adc_sum(tel_id) data.mc.tel[tel_id].reference_pulse_shape = \ pyhessio.get_ref_shapes(tel_id) nsamples = pyhessio.get_event_num_samples(tel_id) if nsamples <= 0: nsamples = 1 data.r0.tel[tel_id].num_samples = nsamples # load the data per telescope/pixel hessio_mc_npe = pyhessio.get_mc_number_photon_electron data.mc.tel[tel_id].photo_electron_image \ = hessio_mc_npe(telescope_id=tel_id) data.mc.tel[tel_id].meta['refstep'] = \ pyhessio.get_ref_step(tel_id) data.mc.tel[tel_id].time_slice = \ pyhessio.get_time_slice(tel_id) data.mc.tel[tel_id].azimuth_raw = \ pyhessio.get_azimuth_raw(tel_id) data.mc.tel[tel_id].altitude_raw = \ pyhessio.get_altitude_raw(tel_id) data.mc.tel[tel_id].azimuth_cor = \ pyhessio.get_azimuth_cor(tel_id) data.mc.tel[tel_id].altitude_cor = \ pyhessio.get_altitude_cor(tel_id) yield data counter += 1 if max_events and counter >= max_events: pyhessio.close_file() return
def hessio_event_source(url, max_events=None, allowed_tels=None, requested_event=None, use_event_id=False): """A generator that streams data from an EventIO/HESSIO MC data file (e.g. a standard CTA data file.) Parameters ---------- url : str path to file to open max_events : int, optional maximum number of events to read allowed_tels : list[int] select only a subset of telescope, if None, all are read. This can be used for example emulate the final CTA data format, where there would be 1 telescope per file (whereas in current monte-carlo, they are all interleaved into one file) requested_event : int Seek to a paricular event index use_event_id : bool If True ,'requested_event' now seeks for a particular event id instead of index """ try: with open_hessio(url) as pyhessio: # the container is initialized once, and data is replaced within # it after each yield counter = 0 eventstream = pyhessio.move_to_next_event() if allowed_tels is not None: allowed_tels = set(allowed_tels) data = DataContainer() data.meta['source'] = "hessio" # some hessio_event_source specific parameters data.meta['hessio__input'] = url data.meta['hessio__max_events'] = max_events for event_id in eventstream: # Seek to requested event if requested_event is not None: current = counter if use_event_id: current = event_id if not current == requested_event: counter += 1 continue data.dl0.run_id = pyhessio.get_run_number() data.dl0.event_id = event_id data.dl0.tels_with_data = set(pyhessio.get_teldata_list()) # handle telescope filtering by taking the intersection of # tels_with_data and allowed_tels if allowed_tels is not None: selected = data.dl0.tels_with_data & allowed_tels if len(selected) == 0: continue # skip event data.dl0.tels_with_data = selected data.trig.tels_with_trigger \ = pyhessio.get_central_event_teltrg_list() time_s, time_ns = pyhessio.get_central_event_gps_time() data.trig.gps_time = Time(time_s * u.s, time_ns * u.ns, format='gps', scale='utc') data.mc.energy = pyhessio.get_mc_shower_energy() * u.TeV data.mc.alt = Angle(pyhessio.get_mc_shower_altitude(), u.rad) data.mc.az = Angle(pyhessio.get_mc_shower_azimuth(), u.rad) data.mc.core_x = pyhessio.get_mc_event_xcore() * u.m data.mc.core_y = pyhessio.get_mc_event_ycore() * u.m data.mc.h_first_int = pyhessio.get_mc_shower_h_first_int() * u.m # mc run header data data.mcheader.run_array_direction = \ pyhessio.get_mc_run_array_direction() data.count = counter # this should be done in a nicer way to not re-allocate the # data each time (right now it's just deleted and garbage # collected) data.dl0.tel.clear() data.mc.tel.clear() # clear the previous telescopes _fill_instrument_info(data,pyhessio) for tel_id in data.dl0.tels_with_data: # event.mc.tel[tel_id] = MCCameraContainer() data.mc.tel[tel_id].dc_to_pe \ = pyhessio.get_calibration(tel_id) data.mc.tel[tel_id].pedestal \ = pyhessio.get_pedestal(tel_id) data.dl0.tel[tel_id].adc_samples = \ pyhessio.get_adc_sample(tel_id) data.dl0.tel[tel_id].adc_sums = \ pyhessio.get_adc_sum(tel_id) data.mc.tel[tel_id].reference_pulse_shape = \ pyhessio.get_ref_shapes(tel_id) # load the data per telescope/pixel data.mc.tel[tel_id].photo_electron_image \ = pyhessio.get_mc_number_photon_electron(telescope_id=tel_id) data.mc.tel[tel_id].meta['refstep'] = pyhessio.get_ref_step(tel_id) data.mc.tel[tel_id].time_slice = \ pyhessio.get_time_slice(tel_id) data.mc.tel[tel_id].azimuth_raw = \ pyhessio.get_azimuth_raw(tel_id) data.mc.tel[tel_id].altitude_raw = \ pyhessio.get_altitude_raw(tel_id) data.mc.tel[tel_id].azimuth_cor = \ pyhessio.get_azimuth_cor(tel_id) data.mc.tel[tel_id].altitude_cor = \ pyhessio.get_altitude_cor(tel_id) yield data counter += 1 if max_events is not None and counter >= max_events: pyhessio.close_file() return except HessioError: raise RuntimeError("hessio_event_source failed to open '{}'" .format(url))
def load_hessio(filename): """ Function to open and load a hessio file Parameters ---------- filename: string name of the file """ print("Hessio file will be opened.") with open_hessio(filename) as h: h.fill_next_event() #version = h.get... version = 'Feb2016' #Creating 3 dictionaries where the instrument configuration will be stored #The dictionaries themselves contain astropy.table.Table objects telescope = {} camera = {} optics = {} #-------------------------------------------------------------------------- #Telescope configuration tel_table_prime = Table() tel_table_prime.meta = {'VERSION': version} try: tel_id = h.get_telescope_ids() tel_table_prime['TelID'] = tel_id except: pass try: tel_posX = [h.get_telescope_position(i)[0] for i in tel_id] tel_table_prime['TelX'] = tel_posX tel_table_prime['TelX'].unit = u.m tel_table_prime.meta['TelX_description'] =\ 'x-position of the telescope measured by...' except: pass try: tel_posY = [h.get_telescope_position(i)[1] for i in tel_id] tel_table_prime['TelY'] = tel_posY tel_table_prime['TelY'].unit = u.m except: pass try: tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id] tel_table_prime['TelZ'] = tel_posZ tel_table_prime['TelZ'].unit = u.m except: pass try: tel_table_prime['CameraClass'] = [ h.get_camera_class(i) for i in tel_id ] except: pass try: tel_table_prime['MirA'] = [h.get_mirror_area(i) for i in tel_id] tel_table_prime['MirA'].unit = u.m**2 except: pass try: tel_table_prime['MirN'] = [h.get_mirror_number(i) for i in tel_id] except: pass try: tel_table_prime['FL'] = [h.get_optical_foclen(i) for i in tel_id] tel_table_prime['FL'].unit = u.m except: pass try: tel_table_prime.meta['TelNum'] = len(tel_posX) except: pass #Beside other tables containimng telescope configuration data, the main #telescope table is written into the telescope dictionary. telescope['TelescopeTable_Version%s' % version] = tel_table_prime #-------------------------------------------------------------------------- #Camera and Optics configuration try: for t in range(len(tel_id)): cam_table_prime = Table() cam_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version} opt_table_prime = Table() opt_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version} try: pix_posX = h.get_pixel_position(tel_id[t])[0] pix_id = np.arange(len(pix_posX)) cam_table_prime['PixID'] = pix_id cam_table_prime['PixX'] = pix_posX cam_table_prime['PixX'].unit = u.m cam_table_prime.meta['PixXDescription'] =\ 'x-position of the pixel measured by...' except: pass try: pix_posY = h.get_pixel_position(tel_id[t])[1] cam_table_prime['PixY'] = pix_posY cam_table_prime['PixY'].unit = u.m except: pass try: camera_class = CD.guess_camera_geometry( pix_posX * u.m, pix_posY * u.m) pix_area_prime = camera_class.pix_area pix_type_prime = camera_class.pix_type pix_neighbors_prime = camera_class.pix_neighbors except: pass try: pix_area = h.get_pixel_area(tel_id[t]) cam_table_prime['PixA'] = pix_area cam_table_prime['PixA'].unit = u.mm**2 except: try: cam_table_prime['PixA'] = pix_area_prime cam_table_prime['PixA'].unit = u.mm**2 except: pass try: pix_type = h.get_pixel_type(tel_id[t]) except: try: pix_type = pix_type_prime except: pix_type = 'unknown' cam_table_prime.meta['PixType'] = pix_type try: pix_neighbors = h.get_pixel_neighbor(tel_id[t]) cam_table_prime['PixNeig'] = pix_neighbors except: try: cam_table_prime['PixNeig'] = pix_neighbors_prime except: pass #as long as no mirror IDs are given, use the following: opt_table_prime['MirrID'] = [1, 2] try: opt_table_prime.meta['MirNum'] = h.get_mirror_number( tel_id[t]) except: pass try: opt_table_prime['MirArea'] = h.get_mirror_area(tel_id[t]) opt_table_prime['MirArea'].unit = u.m**2 opt_table_prime.meta['MirAreaDescription'] =\ 'Area of all mirrors' except: pass try: opt_table_prime['OptFocLen'] = h.get_optical_foclen( tel_id[t]) opt_table_prime['OptFocLen'].unit = u.m except: pass #Beside other tables containing camera and optics configuration #data, the main tables are written into the camera and optics #dictionary. camera['CameraTable_Version%s_TelID%i' % (version,tel_id[t])] \ = cam_table_prime optics['OpticsTable_Version%s_TelID%i' % (version,tel_id[t])] \ = opt_table_prime except: pass print('Astropy tables have been created.') #h.close_file() print("Hessio file has been closed.") return (telescope, camera, optics)
""" from pyhessio import open_hessio from pyhessio import HessioTelescopeIndexError import sys from astropy.table import Table from astropy import units as u from ctapipe.instrument import CameraDescription # TODO: use io.fits instead and make the table be variable length # TODO: make this a tool (ctapipe-eventio2tels) if __name__ == '__main__': filename = sys.argv.pop(1) with open_hessio(filename) as h: event = h.move_to_next_event() next(event) for telid in range(1, h.get_num_telescope()): try: px, py = h.get_pixel_position(telid) camtab = Table(names=['PIX_POS_X', 'PIX_POS_Y'], data=[px * u.m, py * u.m]) camtab.meta['N_PIX'] = h.get_num_pixels(telid) camtab.meta['N_SAMPS'] = h.get_num_samples(telid) camtab.meta['N_TIMES'] = h.get_pixel_timing_num_times_types(telid) camtab.meta['MIR_AREA'] = h.get_mirror_area(telid) geom = CameraDescription.guess_camera_geometry(px * u.m, py * u.m) #camtab.meta['TELCLASS'] = geom.cam_id camtab.meta['PIXTYPE'] = geom.pix_type
def load_hessio(filename): """ Function to open and load a hessio file Parameters ---------- filename: string name of the file """ print("Hessio file will be opened.") with open_hessio(filename) as h: h.fill_next_event() #version = h.get... version = 'Feb2016' #Creating 3 dictionaries where the instrument configuration will be stored #The dictionaries themselves contain astropy.table.Table objects telescope = {} camera = {} optics = {} #-------------------------------------------------------------------------- #Telescope configuration tel_table_prime = Table() tel_table_prime.meta = {'VERSION': version} try: tel_id = h.get_telescope_ids() tel_table_prime['TelID']= tel_id except: pass try: tel_posX = [h.get_telescope_position(i)[0] for i in tel_id] tel_table_prime['TelX'] = tel_posX tel_table_prime['TelX'].unit = u.m tel_table_prime.meta['TelX_description'] =\ 'x-position of the telescope measured by...' except: pass try: tel_posY = [h.get_telescope_position(i)[1] for i in tel_id] tel_table_prime['TelY'] = tel_posY tel_table_prime['TelY'].unit = u.m except: pass try: tel_posZ = [h.get_telescope_position(i)[2] for i in tel_id] tel_table_prime['TelZ'] = tel_posZ tel_table_prime['TelZ'].unit = u.m except: pass try: tel_table_prime['CameraClass'] = [h.get_camera_class(i) for i in tel_id] except: pass try: tel_table_prime['MirA'] = [h.get_mirror_area(i) for i in tel_id] tel_table_prime['MirA'].unit = u.m**2 except: pass try: tel_table_prime['MirN'] = [h.get_mirror_number(i) for i in tel_id] except: pass try: tel_table_prime['FL'] = [h.get_optical_foclen(i) for i in tel_id] tel_table_prime['FL'].unit = u.m except: pass try: tel_table_prime.meta['TelNum'] = len(tel_posX) except: pass #Beside other tables containimng telescope configuration data, the main #telescope table is written into the telescope dictionary. telescope['TelescopeTable_Version%s' % version] = tel_table_prime #-------------------------------------------------------------------------- #Camera and Optics configuration try: for t in range(len(tel_id)): cam_table_prime = Table() cam_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version} opt_table_prime = Table() opt_table_prime.meta = {'TELID': tel_id[t], 'VERSION': version} try: pix_posX = h.get_pixel_position(tel_id[t])[0] pix_id = np.arange(len(pix_posX)) cam_table_prime['PixID'] = pix_id cam_table_prime['PixX'] = pix_posX cam_table_prime['PixX'].unit = u.m cam_table_prime.meta['PixXDescription'] =\ 'x-position of the pixel measured by...' except: pass try: pix_posY = h.get_pixel_position(tel_id[t])[1] cam_table_prime['PixY'] = pix_posY cam_table_prime['PixY'].unit = u.m except: pass try: camera_class = CD.guess_camera_geometry(pix_posX*u.m,pix_posY*u.m) pix_area_prime = camera_class.pix_area pix_type_prime = camera_class.pix_type pix_neighbors_prime = camera_class.pix_neighbors except: pass try: pix_area = h.get_pixel_area(tel_id[t]) cam_table_prime['PixA'] = pix_area cam_table_prime['PixA'].unit = u.mm**2 except: try: cam_table_prime['PixA'] = pix_area_prime cam_table_prime['PixA'].unit = u.mm**2 except: pass try: pix_type = h.get_pixel_type(tel_id[t]) except: try: pix_type = pix_type_prime except: pix_type = 'unknown' cam_table_prime.meta['PixType'] = pix_type try: pix_neighbors = h.get_pixel_neighbor(tel_id[t]) cam_table_prime['PixNeig'] = pix_neighbors except: try: cam_table_prime['PixNeig'] = pix_neighbors_prime except: pass #as long as no mirror IDs are given, use the following: opt_table_prime['MirrID'] = [1,2] try: opt_table_prime.meta['MirNum'] = h.get_mirror_number(tel_id[t]) except: pass try: opt_table_prime['MirArea'] = h.get_mirror_area(tel_id[t]) opt_table_prime['MirArea'].unit = u.m**2 opt_table_prime.meta['MirAreaDescription'] =\ 'Area of all mirrors' except: pass try: opt_table_prime['OptFocLen'] = h.get_optical_foclen(tel_id[t]) opt_table_prime['OptFocLen'].unit = u.m except: pass #Beside other tables containing camera and optics configuration #data, the main tables are written into the camera and optics #dictionary. camera['CameraTable_Version%s_TelID%i' % (version,tel_id[t])] \ = cam_table_prime optics['OpticsTable_Version%s_TelID%i' % (version,tel_id[t])] \ = opt_table_prime except: pass print('Astropy tables have been created.') #h.close_file() print("Hessio file has been closed.") return(telescope,camera,optics)