make_fits_event_list.py

Deil Christoph, 12/03/2013 07:12 PM

Download (2.31 KB)

 
1
"""Make FITS event lists."""
2
from glob import glob
3
import numpy as np
4

    
5
def make_event_list(mssg_max, offset_max):
6
    """Makes a single FITS event list out of per-run ROOT event lists.
7
    
8
    Takes a few minutes on my laptop to process 20 GB ROOT -> 2 GB FITS.
9
    
10
    TODO: Check if Model++ really used FK5 J2000 coordinates.
11
    """
12
    from root_numpy import root2array
13
    from astropy.table import Table
14
    from astropy.coordinates import FK5, Galactic
15

    
16
    DIR = '/Users/deil/work/_Data/hess/HESSEVENTLISTS/franzi/WithOwnTables/'
17
    filenames = glob(DIR + 'EventsList_SpectralHD_Custom_??????.root')[::100]
18
    selection = '(MeanScaledShowerGoodness_Model < {0})'.format(mssg_max)
19
    selection += '& (Psi2 < {0})'.format(offset_max ** 2)
20
    
21
    print('Processing {0} files.'.format(len(filenames)))
22
    array = root2array(filenames, selection=selection)
23
    print('Selected {0} events.'.format(len(array)))
24

    
25
    print('Making table...')
26
    table = Table(array)
27

    
28
    # Add header keywords to make the FITS event lists compatible with FTOOLS    
29
    table.meta['EXTNAME'] = 'EVENTS'
30
    missing_keywords = ['TSTART', 'TSTOP', 'MJDREF', 'RA_PNT', 'DEC_PNT']
31
    for missing_keyword in missing_keywords:
32
        table.meta[missing_keyword] = 0
33
    
34
    print('Computing Galactic coordinates ...')
35
    ra, dec = table['Lambda'], table['Beta']
36
    coordinates = FK5(ra, dec, unit=('deg', 'deg'))
37
    coordinates = coordinates.transform_to(Galactic)
38
    table['GLON'] = coordinates.lonangle.deg
39
    table['GLAT'] = coordinates.latangle.deg
40

    
41
    # Add and rename columns to make the FITS event lists compatible with FTOOLS    
42
    missing_columns = ['EVENT_ID', 'MULTIP', 'DIR_ERR', 'AZ', 'COREX', 'COREY',
43
                       'CORE_ERR', 'XMAX', 'XMAX_ERR', 'ENERGY_ERR']
44
    for missing_column in missing_columns:
45
        table[missing_column] = 0
46
    table['ALT'] = np.degrees(90 - np.arccos(table['CosZen']))
47
    col_names = [('MJD', 'TIME'), ('Lambda', 'RA'), ('Beta', 'DEC'),
48
                 ('NomPosX', 'DETX'), ('NomPosY', 'DETY'), ('Energy', 'ENERGY')]
49
    for old_name, new_name in col_names:
50
        table.rename_column(old_name, new_name)
51

    
52
    filename = 'test_events.fits'
53
    print('Writing file: {0}'.format(filename))
54
    table.write(filename, overwrite=True)
55

    
56

    
57
if __name__ == '__main__':
58
    make_event_list(mssg_max=-0.6, offset_max=2)